Class: Diplomat::Service
- Inherits:
-
RestClient
- Object
- RestClient
- Diplomat::Service
- Defined in:
- lib/diplomat/service.rb
Overview
Methods for interacting with the Consul service API endpoint.
Instance Method Summary collapse
-
#deregister(service_name, options = {}) ⇒ Boolean
De-register a service.
-
#deregister_external(definition, options = {}) ⇒ Boolean
Deregister an external service.
-
#get(key, scope = :first, options = {}, meta = nil) ⇒ OpenStruct
Get a service by it’s key rubocop:disable PerceivedComplexity.
-
#get_all(options = {}) ⇒ OpenStruct
Get all the services.
-
#maintenance(service_id, options = { enable: true }) ⇒ Boolean
Enable or disable maintenance for a service.
-
#register(definition, options = {}) ⇒ Boolean
Register a service.
-
#register_external(definition, options = {}) ⇒ Boolean
Register an external service.
Methods inherited from RestClient
access_method?, #concat_url, #configuration, #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, options = {}) ⇒ Boolean
De-register a service
72 73 74 75 |
# File 'lib/diplomat/service.rb', line 72 def deregister(service_name, = {}) deregister = send_put_request(@conn, ["/v1/agent/service/deregister/#{service_name}"], , nil) deregister.status == 200 end |
#deregister_external(definition, options = {}) ⇒ Boolean
Deregister an external service
90 91 92 93 |
# File 'lib/diplomat/service.rb', line 90 def deregister_external(definition, = {}) deregister = send_put_request(@conn, ['/v1/catalog/deregister'], , definition) deregister.status == 200 end |
#get(key, scope = :first, options = {}, meta = nil) ⇒ OpenStruct
Get a service by it’s key rubocop:disable PerceivedComplexity
13 14 15 16 17 18 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 13 def get(key, scope = :first, = {}, = nil) custom_params = [] custom_params << use_named_parameter('wait', [:wait]) if [:wait] custom_params << use_named_parameter('index', [:index]) if [:index] custom_params << use_named_parameter('dc', [:dc]) if [:dc] if [:tag] # tag can be either a String, or an array of strings # by splatting it is guaranteed to be an array of strings [*[:tag]].each do |value| custom_params << use_named_parameter('tag', value) end end # We have to provide a custom params encoder here because Faraday - by default - assumes that # list keys have [] as part of their name. This is however not the case for consul tags, which # just use repeated occurences of the same key. # # So faraday reduces this: http://localhost:8500?a=1&a=2 to http://localhost:8500?a=2 unless you # explicitly tell it not to. [:params_encoder] = Faraday::FlatParamsEncoder ret = send_get_request(@conn, ["/v1/catalog/service/#{key}"], , custom_params) if && ret.headers [:index] = ret.headers['x-consul-index'] if ret.headers['x-consul-index'] [:knownleader] = ret.headers['x-consul-knownleader'] if ret.headers['x-consul-knownleader'] [:lastcontact] = ret.headers['x-consul-lastcontact'] if 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 = {}) ⇒ OpenStruct
Get all the services
52 53 54 55 56 |
# File 'lib/diplomat/service.rb', line 52 def get_all( = {}) custom_params = [:dc] ? use_named_parameter('dc', [:dc]) : nil ret = send_get_request(@conn, ['/v1/catalog/services'], , custom_params) OpenStruct.new JSON.parse(ret.body) end |
#maintenance(service_id, options = { enable: true }) ⇒ Boolean
Enable or disable maintenance for a service
102 103 104 105 106 107 108 109 |
# File 'lib/diplomat/service.rb', line 102 def maintenance(service_id, = { enable: true }) custom_params = [] custom_params << ["enable=#{[:enable]}"] custom_params << ["reason=#{[:reason].split(' ').join('+')}"] if [:reason] maintenance = send_put_request(@conn, ["/v1/agent/service/maintenance/#{service_id}"], , nil, custom_params) maintenance.status == 200 end |
#register(definition, options = {}) ⇒ Boolean
Register a service
62 63 64 65 66 |
# File 'lib/diplomat/service.rb', line 62 def register(definition, = {}) url = [:path] || ['/v1/agent/service/register'] register = send_put_request(@conn, url, , definition) register.status == 200 end |
#register_external(definition, options = {}) ⇒ Boolean
Register an external service
81 82 83 84 |
# File 'lib/diplomat/service.rb', line 81 def register_external(definition, = {}) register = send_put_request(@conn, ['/v1/catalog/register'], , definition) register.status == 200 end |