Class: Diplomat::Node

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

Overview

Methods for interacting with the Consul node 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(definition, path = '/v1/catalog/deregister') ⇒ Boolean

De-register a node (and all associated services and checks)

Parameters:

  • definition (Hash)

    Hash containing definition of a node to de-register

Returns:

  • (Boolean)


50
51
52
53
54
# File 'lib/diplomat/node.rb', line 50

def deregister(definition, path = '/v1/catalog/deregister')
  deregister = @conn.put path, JSON.dump(definition)

  deregister.status == 200
end

#get(key, options = nil) ⇒ OpenStruct

Get a node by it’s key

Parameters:

  • key (String)

    the key

  • options (Hash) (defaults to: nil)

    :dc string for dc specific query

Returns:

  • (OpenStruct)

    all data associated with the node



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

def get(key, options = nil)
  url = ["/v1/catalog/node/#{key}"]
  url += check_acl_token
  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
  OpenStruct.new JSON.parse(ret.body)
rescue Faraday::ClientError
  raise Diplomat::PathNotFound
end

#get_all(options = nil) ⇒ OpenStruct

Get all the nodes

Parameters:

  • options (Hash) (defaults to: nil)

    :dc string for dc specific query

Returns:

  • (OpenStruct)

    the list of all nodes



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

def get_all(options = nil)
  url = ['/v1/catalog/nodes']
  url << use_named_parameter('dc', options[:dc]) if options && options[:dc]

  ret = @conn.get concat_url url
  JSON.parse(ret.body).map { |service| OpenStruct.new service }
rescue Faraday::ClientError
  raise Diplomat::PathNotFound
end

#register(definition, path = '/v1/catalog/register') ⇒ Boolean

Register a node

Parameters:

  • definition (Hash)

    Hash containing definition of a node to register

Returns:

  • (Boolean)


41
42
43
44
45
# File 'lib/diplomat/node.rb', line 41

def register(definition, path = '/v1/catalog/register')
  register = @conn.put path, JSON.dump(definition)

  register.status == 200
end