Class: Diplomat::Service
- Inherits:
-
RestClient
- Object
- RestClient
- Diplomat::Service
- Includes:
- ApiOptions
- Defined in:
- lib/diplomat/service.rb
Overview
Methods for interacting with the Consul serivce API endpoint.
Instance Method Summary collapse
-
#deregister(service_name) ⇒ Boolean
De-register a service.
-
#deregister_external(definition) ⇒ Boolean
Deregister an external service.
-
#get(key, scope = :first, options = nil, meta = nil) ⇒ OpenStruct
Get a service by it’s key rubocop:disable PerceivedComplexity, MethodLength, CyclomaticComplexity, AbcSize.
-
#get_all(options = nil) ⇒ OpenStruct
Get all the services.
-
#maintenance(service_id, options = { enable: true }) ⇒ Boolean
Enable or disable maintenance for a service.
-
#register(definition, path = '/v1/agent/service/register') ⇒ Boolean
Register a service.
-
#register_external(definition) ⇒ Boolean
Register an external service.
Methods included from ApiOptions
#check_acl_token, #use_cas, #use_consistency, #valid_transaction_verbs, #valid_value_transactions
Methods inherited from RestClient
access_method?, #concat_url, #initialize, method_missing, respond_to?, respond_to_missing?, #use_named_parameter
Constructor Details
This class inherits a constructor from Diplomat::RestClient
Instance Method Details
#deregister(service_name) ⇒ Boolean
De-register a service
77 78 79 80 |
# File 'lib/diplomat/service.rb', line 77 def deregister(service_name) deregister = @conn.put "/v1/agent/service/deregister/#{service_name}" deregister.status == 200 end |
#deregister_external(definition) ⇒ Boolean
Deregister an external service
92 93 94 95 96 |
# File 'lib/diplomat/service.rb', line 92 def deregister_external(definition) json_definition = JSON.dump(definition) deregister = @conn.put '/v1/catalog/deregister', json_definition deregister.status == 200 end |
#get(key, scope = :first, options = nil, meta = nil) ⇒ OpenStruct
Get a service by it’s key rubocop:disable PerceivedComplexity, MethodLength, CyclomaticComplexity, AbcSize
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/diplomat/service.rb', line 19 def get(key, scope = :first, = nil, = nil) url = ["/v1/catalog/service/#{key}"] url += check_acl_token url << use_named_parameter('wait', [:wait]) if && [:wait] url << use_named_parameter('index', [:index]) if && [:index] url << use_named_parameter('dc', [:dc]) if && [:dc] url << use_named_parameter('tag', [:tag]) if && [:tag] # If the request fails, it's probably due to a bad path # so return a PathNotFound error. begin ret = @conn.get concat_url url rescue Faraday::ClientError => e raise Diplomat::PathNotFound, e end if && ret.headers [:index] = ret.headers['x-consul-index'] [:knownleader] = ret.headers['x-consul-knownleader'] [:lastcontact] = ret.headers['x-consul-lastcontact'] end if scope == :all JSON.parse(ret.body).map { |service| OpenStruct.new service } else OpenStruct.new JSON.parse(ret.body).first end end |
#get_all(options = nil) ⇒ OpenStruct
Get all the services
52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/diplomat/service.rb', line 52 def get_all( = nil) url = ['/v1/catalog/services'] url += check_acl_token url << use_named_parameter('dc', [:dc]) if && [:dc] begin ret = @conn.get concat_url url rescue Faraday::ClientError raise Diplomat::PathNotFound end OpenStruct.new JSON.parse(ret.body) end |
#maintenance(service_id, options = { enable: true }) ⇒ Boolean
Enable or disable maintenance for a service
104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/diplomat/service.rb', line 104 def maintenance(service_id, = { enable: true }) url = ["/v1/agent/service/maintenance/#{service_id}"] url += check_acl_token url << ["enable=#{[:enable]}"] url << ["reason=#{[:reason].split(' ').join('+')}"] if && [:reason] begin maintenance = @conn.put concat_url(url) rescue Faraday::ClientError raise Diplomat::PathNotFound end maintenance.status == 200 end |
#register(definition, path = '/v1/agent/service/register') ⇒ Boolean
Register a service
68 69 70 71 72 |
# File 'lib/diplomat/service.rb', line 68 def register(definition, path = '/v1/agent/service/register') json_definition = JSON.dump(definition) register = @conn.put path, json_definition register.status == 200 end |
#register_external(definition) ⇒ Boolean
Register an external service
85 86 87 |
# File 'lib/diplomat/service.rb', line 85 def register_external(definition) register(definition, '/v1/catalog/register') end |