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, #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 = nil) ⇒ Object

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

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: nil)

    :dc string for dc specific query

Returns:

  • true if call is successful

Raises:



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

def enable(enable = true, reason = nil, options = nil)
  raw = @conn.put do |req|
    url = ['/v1/agent/maintenance']
    url << use_named_parameter('enable', enable.to_s)
    url << use_named_parameter('reason', reason) if reason
    url << use_named_parameter('dc', options[:dc]) if options && options[:dc]
    req.url concat_url url
  end

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

#enabled(n, options = nil) ⇒ Hash

Get the maintenance state of a host

Parameters:

  • n (String)

    the node

  • options (Hash) (defaults to: nil)

    :dc string for dc specific query

Returns:

  • (Hash)

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



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

def enabled(n, options = nil)
  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