Class: Diplomat::Service

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

Overview

Methods for interacting with the Consul serivce API endpoint.

Instance Method Summary collapse

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

Parameters:

  • service_name (String)

    Service name to de-register

Returns:

  • (Boolean)


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

Parameters:

  • definition (Hash)

    Hash containing definition of service

Returns:

  • (Boolean)


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

Parameters:

  • key (String)

    the key

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

    :first or :all results

  • options (Hash) (defaults to: nil)

    options parameter hash

  • meta (Hash) (defaults to: nil)

    output structure containing header information about the request (index)

  • wait (Hash)

    a customizable set of options

  • index (Hash)

    a customizable set of options

  • dc (Hash)

    a customizable set of options

  • tag (Hash)

    a customizable set of options

Returns:

  • (OpenStruct)

    all data associated with the service



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, options = nil, meta = nil)
  url = ["/v1/catalog/service/#{key}"]
  url += check_acl_token
  url << use_named_parameter('wait', options[:wait]) if options && options[:wait]
  url << use_named_parameter('index', options[:index]) if options && options[:index]
  url << use_named_parameter('dc', options[:dc]) if options && options[:dc]
  url << use_named_parameter('tag', options[:tag]) if options && options[: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 meta && ret.headers
    meta[:index] = ret.headers['x-consul-index']
    meta[:knownleader] = ret.headers['x-consul-knownleader']
    meta[: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

Parameters:

  • options (Hash) (defaults to: nil)

    :dc Consul datacenter to query

Returns:

  • (OpenStruct)

    the list of all services



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/diplomat/service.rb', line 52

def get_all(options = nil)
  url = ['/v1/catalog/services']
  url += check_acl_token
  url << use_named_parameter('dc', options[:dc]) if options && options[: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

Parameters:

  • opts (Hash)

    the options for enabling or disabling maintenance for a service

Returns:

  • (Boolean)

    if the request was successful or not

Raises:



104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/diplomat/service.rb', line 104

def maintenance(service_id, options = { enable: true })
  url = ["/v1/agent/service/maintenance/#{service_id}"]
  url += check_acl_token
  url << ["enable=#{options[:enable]}"]
  url << ["reason=#{options[:reason].split(' ').join('+')}"] if options && options[: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

Parameters:

  • definition (Hash)

    Hash containing definition of service

Returns:

  • (Boolean)


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

Parameters:

  • definition (Hash)

    Hash containing definition of service

Returns:

  • (Boolean)


85
86
87
# File 'lib/diplomat/service.rb', line 85

def register_external(definition)
  register(definition, '/v1/catalog/register')
end