Class: Diplomat::Service

Inherits:
RestClient show all
Defined in:
lib/diplomat/service.rb

Overview

Methods for interacting with the Consul service API endpoint.

Instance Method Summary collapse

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

Parameters:

  • service_name (String)

    Service name to de-register

  • options (Hash) (defaults to: {})

    options parameter hash

Returns:

  • (Boolean)


61
62
63
64
# File 'lib/diplomat/service.rb', line 61

def deregister(service_name, options = {})
  deregister = send_put_request(@conn, ["/v1/agent/service/deregister/#{service_name}"], options, nil)
  deregister.status == 200
end

#deregister_external(definition, options = {}) ⇒ Boolean

Deregister an external service

Parameters:

  • definition (Hash)

    Hash containing definition of service

  • options (Hash) (defaults to: {})

    options parameter hash

Returns:

  • (Boolean)


79
80
81
82
# File 'lib/diplomat/service.rb', line 79

def deregister_external(definition, options = {})
  deregister = send_put_request(@conn, ['/v1/catalog/deregister'], options, definition)
  deregister.status == 200
end

#get(key, scope = :first, options = {}, meta = nil) ⇒ OpenStruct

Get a service by it’s key rubocop:disable Metrics/PerceivedComplexity

Parameters:

  • key (String)

    the key

  • scope (Symbol) (defaults to: :first)

    :first or :all results

  • options (Hash) (defaults to: {})

    options parameter hash

  • meta (Hash) (defaults to: nil)

    output structure containing header information about the request (index)

Returns:

  • (OpenStruct)

    all data associated with the service



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/diplomat/service.rb', line 15

def get(key, scope = :first, options = {}, meta = nil)
  custom_params = []
  custom_params << use_named_parameter('wait', options[:wait]) if options[:wait]
  custom_params << use_named_parameter('index', options[:index]) if options[:index]
  custom_params << use_named_parameter('dc', options[:dc]) if options[:dc]
  custom_params << use_named_parameter('filter', options[:filter]) if options[:filter]
  custom_params += [*options[:tag]].map { |value| use_named_parameter('tag', value) } if options[:tag]

  ret = send_get_request(@conn, ["/v1/catalog/service/#{key}"], options, custom_params)
  if meta && ret.headers
    meta[:index] = ret.headers['x-consul-index'] if ret.headers['x-consul-index']
    meta[:knownleader] = ret.headers['x-consul-knownleader'] if ret.headers['x-consul-knownleader']
    meta[: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

Parameters:

  • options (Hash) (defaults to: {})

    :dc Consul datacenter to query

Returns:

  • (OpenStruct)

    the list of all services



41
42
43
44
45
# File 'lib/diplomat/service.rb', line 41

def get_all(options = {})
  custom_params = options[:dc] ? use_named_parameter('dc', options[:dc]) : nil
  ret = send_get_request(@conn, ['/v1/catalog/services'], options, custom_params)
  OpenStruct.new JSON.parse(ret.body)
end

#maintenance(service_id, options = { enable: true }) ⇒ Boolean

Enable or disable maintenance for a service

Parameters:

  • service_id (String)

    id of the service

  • options (Hash) (defaults to: { enable: true })

    opts the options for enabling or disabling maintenance for a service

Returns:

  • (Boolean)

    if the request was successful or not

Raises:



91
92
93
94
95
96
97
98
# File 'lib/diplomat/service.rb', line 91

def maintenance(service_id, options = { enable: true })
  custom_params = []
  custom_params << ["enable=#{options[:enable]}"]
  custom_params << ["reason=#{options[:reason].split(' ').join('+')}"] if options[:reason]
  maintenance = send_put_request(@conn, ["/v1/agent/service/maintenance/#{service_id}"],
                                 options, nil, custom_params)
  maintenance.status == 200
end

#register(definition, options = {}) ⇒ Boolean

Register a service

Parameters:

  • definition (Hash)

    Hash containing definition of service

  • options (Hash) (defaults to: {})

    options parameter hash

Returns:

  • (Boolean)


51
52
53
54
55
# File 'lib/diplomat/service.rb', line 51

def register(definition, options = {})
  url = options[:path] || ['/v1/agent/service/register']
  register = send_put_request(@conn, url, options, definition)
  register.status == 200
end

#register_external(definition, options = {}) ⇒ Boolean

Register an external service

Parameters:

  • definition (Hash)

    Hash containing definition of service

  • options (Hash) (defaults to: {})

    options parameter hash

Returns:

  • (Boolean)


70
71
72
73
# File 'lib/diplomat/service.rb', line 70

def register_external(definition, options = {})
  register = send_put_request(@conn, ['/v1/catalog/register'], options, definition)
  register.status == 200
end