Class: Diplomat::Health

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

Overview

Methods for interacting with the Consul health 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

#any(options = {}) ⇒ OpenStruct

Convenience method to get services in any state

Parameters:

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

    :dc, :near, :filter string for specific query

Returns:

  • (OpenStruct)

    all data associated with the node



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

def any(options = {})
  state('any', options)
end

#checks(s, options = {}) ⇒ OpenStruct

Get service checks

Parameters:

  • s (String)

    the service

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

    :dc, :filter string for specific query

Returns:

  • (OpenStruct)

    all data associated with the node



26
27
28
29
30
31
32
33
# File 'lib/diplomat/health.rb', line 26

def checks(s, options = {})
  custom_params = []
  custom_params << use_named_parameter('dc', options[:dc]) if options[:dc]
  custom_params << use_named_parameter('filter', options[:filter]) if options[:filter]

  ret = send_get_request(@conn, ["/v1/health/checks/#{s}"], options, custom_params)
  JSON.parse(ret.body).map { |check| OpenStruct.new check }
end

#critical(options = {}) ⇒ OpenStruct

Convenience method to get services in critical state

Parameters:

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

    :dc, :near, :filter string for specific query

Returns:

  • (OpenStruct)

    all data associated with the node



101
102
103
# File 'lib/diplomat/health.rb', line 101

def critical(options = {})
  state('critical', options)
end

#node(n, options = {}) ⇒ OpenStruct

Get node health

Parameters:

  • n (String)

    the node

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

    :dc, :filter string for specific query

Returns:

  • (OpenStruct)

    all data associated with the node



13
14
15
16
17
18
19
20
# File 'lib/diplomat/health.rb', line 13

def node(n, options = {})
  custom_params = []
  custom_params << use_named_parameter('dc', options[:dc]) if options[:dc]
  custom_params << use_named_parameter('filter', options[:filter]) if options[:filter]

  ret = send_get_request(@conn, ["/v1/health/node/#{n}"], options, custom_params)
  JSON.parse(ret.body).map { |node| OpenStruct.new node }
end

#passing(options = {}) ⇒ OpenStruct

Convenience method to get services in passing state

Parameters:

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

    :dc, :near, :filter string for specific query

Returns:

  • (OpenStruct)

    all data associated with the node



87
88
89
# File 'lib/diplomat/health.rb', line 87

def passing(options = {})
  state('passing', options)
end

#service(s, options = {}, meta = nil) ⇒ OpenStruct

Get service health rubocop:disable Metrics/PerceivedComplexity

Parameters:

  • s (String)

    the service

  • 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 node



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/diplomat/health.rb', line 41

def service(s, options = {}, meta = nil)
  custom_params = []
  custom_params << use_named_parameter('dc', options[:dc]) if options[:dc]
  custom_params << ['passing'] if options[:passing]
  custom_params += [*options[:tag]].map { |value| use_named_parameter('tag', value) } if options[:tag]
  custom_params << use_named_parameter('near', options[:near]) if options[:near]
  custom_params << use_named_parameter('node-meta', options[:node_meta]) if options[:node_meta]
  custom_params << use_named_parameter('index', options[:index]) if options[:index]
  custom_params << use_named_parameter('filter', options[:filter]) if options[:filter]

  ret = send_get_request(@conn, ["/v1/health/service/#{s}"], 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

  JSON.parse(ret.body).map { |service| OpenStruct.new service }
end

#state(s, options = {}) ⇒ OpenStruct

Get service health

Parameters:

  • s (String)

    the state (“any”, “passing”, “warning”, or “critical”)

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

    :dc, :near, :filter string for specific query

Returns:

  • (OpenStruct)

    all data associated with the node



67
68
69
70
71
72
73
74
75
# File 'lib/diplomat/health.rb', line 67

def state(s, options = {})
  custom_params = []
  custom_params << use_named_parameter('dc', options[:dc]) if options[:dc]
  custom_params << use_named_parameter('near', options[:near]) if options[:near]
  custom_params << use_named_parameter('filter', options[:filter]) if options[:filter]

  ret = send_get_request(@conn, ["/v1/health/state/#{s}"], options, custom_params)
  JSON.parse(ret.body).map { |status| OpenStruct.new status }
end

#warning(options = {}) ⇒ OpenStruct

Convenience method to get services in warning state

Parameters:

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

    :dc, :near, :filter string for specific query

Returns:

  • (OpenStruct)

    all data associated with the node



94
95
96
# File 'lib/diplomat/health.rb', line 94

def warning(options = {})
  state('warning', options)
end