Class: Diplomat::Maintenance

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

Overview

Methods to interact with the Consul maintenance 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

#enable(enable = true, reason = nil, options = {}) ⇒ Object

Enable or disable maintenance mode. This endpoint only works on the local agent.

Parameters:

  • enable (defaults to: true)

    enable or disable maintenance mode

  • reason (String) (defaults to: nil)

    the reason for enabling maintenance mode

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

    :dc string for dc specific query

Returns:

  • true if call is successful

Raises:



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/diplomat/maintenance.rb', line 30

def enable(enable = true, reason = nil, options = {})
  custom_params = []
  custom_params << use_named_parameter('enable', enable.to_s)
  custom_params << use_named_parameter('reason', reason) if reason
  custom_params << use_named_parameter('dc', options[:dc]) if options[:dc]
  raw = send_put_request(@conn, ['/v1/agent/maintenance'], options, nil, custom_params)

  return_status = raw.status == 200
  raise Diplomat::UnknownStatus, "status #{raw.status}: #{raw.body}" unless return_status

  return_status
end

#enabled(n, options = {}) ⇒ Hash

Get the maintenance state of a host

Parameters:

  • n (String)

    the node

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

    :dc string for dc specific query

Returns:

  • (Hash)

    { :enabled => true, :reason => ‘foo’ }



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

def enabled(n, options = {})
  health = Diplomat::Health.new(@conn)
  result = health.node(n, options)
                 .select { |check| check['CheckID'] == '_node_maintenance' }

  if result.empty?
    { enabled: false, reason: nil }
  else
    { enabled: true, reason: result.first['Notes'] }
  end
end