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, #initialize, method_missing, respond_to?, respond_to_missing?, #use_named_parameter

Constructor Details

This class inherits a constructor from Diplomat::RestClient

Instance Method Details

#anyObject

Convenience method to get services in any state



82
83
84
# File 'lib/diplomat/health.rb', line 82

def any
  state('any')
end

#checks(s, options = nil) ⇒ OpenStruct

Get service checks

Parameters:

  • s (String)

    the service

  • options (Hash) (defaults to: nil)

    :dc string for dc specific query

Returns:

  • (OpenStruct)

    all data associated with the node



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/diplomat/health.rb', line 27

def checks(s, options = nil)
  url = ["/v1/health/checks/#{s}"]
  url << use_named_parameter('dc', options[:dc]) if options && options[:dc]

  # If the request fails, it's probably due to a bad path
  # so return a PathNotFound error.
  ret = @conn.get concat_url url
  JSON.parse(ret.body).map { |check| OpenStruct.new check }
rescue Faraday::ClientError
  raise Diplomat::PathNotFound
end

#criticalObject

Convenience method to get services in critical state



97
98
99
# File 'lib/diplomat/health.rb', line 97

def critical
  state('critical')
end

#node(n, options = nil) ⇒ OpenStruct

Get node health

Parameters:

  • n (String)

    the node

  • options (Hash) (defaults to: nil)

    :dc string for dc specific query

Returns:

  • (OpenStruct)

    all data associated with the node



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

def node(n, options = nil)
  url = ["/v1/health/node/#{n}"]
  url << use_named_parameter('dc', options[:dc]) if options && options[:dc]

  # If the request fails, it's probably due to a bad path
  # so return a PathNotFound error.
  ret = @conn.get concat_url url
  JSON.parse(ret.body).map { |node| OpenStruct.new node }
rescue Faraday::ClientError
  raise Diplomat::PathNotFound
end

#passingObject

Convenience method to get services in passing state



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

def passing
  state('passing')
end

#service(s, options = nil) ⇒ OpenStruct

Get service health rubocop:disable PerceivedComplexity, CyclomaticComplexity, AbcSize

Parameters:

  • s (String)

    the service

  • options (Hash) (defaults to: nil)

    :dc string for dc specific query

  • options (Hash) (defaults to: nil)

    :passing boolean to return only checks in passing state

  • options (Hash) (defaults to: nil)

    :tag string for specific tag

Returns:

  • (OpenStruct)

    all data associated with the node



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

def service(s, options = nil)
  url = ["/v1/health/service/#{s}"]
  url << use_named_parameter('dc', options[:dc]) if options && options[:dc]
  url << 'passing' if options && options[:passing]
  url << use_named_parameter('tag', options[:tag]) if options && options[:tag]
  url << use_named_parameter('near', options[:near]) if options && options[:near]

  # If the request fails, it's probably due to a bad path
  # so return a PathNotFound error.
  ret = @conn.get concat_url url
  JSON.parse(ret.body).map { |service| OpenStruct.new service }
rescue Faraday::ClientError
  raise Diplomat::PathNotFound
end

#state(s, options = nil) ⇒ OpenStruct

Get service health rubocop:disable AbcSize

Parameters:

  • s (String)

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

  • options (Hash) (defaults to: nil)

    :dc string for dc specific query

Returns:

  • (OpenStruct)

    all data associated with the node



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

def state(s, options = nil)
  url = ["/v1/health/state/#{s}"]
  url << use_named_parameter('dc', options[:dc]) if options && options[:dc]
  url << use_named_parameter('near', options[:near]) if options && options[:near]

  # If the request fails, it's probably due to a bad path
  # so return a PathNotFound error.
  ret = @conn.get concat_url url
  JSON.parse(ret.body).map { |status| OpenStruct.new status }
rescue Faraday::ClientError
  raise Diplomat::PathNotFound
end

#warningObject

Convenience method to get services in warning state



92
93
94
# File 'lib/diplomat/health.rb', line 92

def warning
  state('warning')
end