Class: Consul::Client::Catalog

Inherits:
Base
  • Object
show all
Defined in:
lib/consul/client/catalog.rb

Instance Method Summary collapse

Methods inherited from Base

#initialize, #is_reachable

Constructor Details

This class inherits a constructor from Consul::Client::Base

Instance Method Details

#data_centersObject



81
82
83
# File 'lib/consul/client/catalog.rb', line 81

def data_centers
  _get build_url('datacenters'), params = nil, json_only = false
end

#node(name, dc = nil) ⇒ Object

Public: Returns all the nodes within a data center that have the service specified.

dc - Data center, default: agent current data center tag - Tag, filter for tags

Example:

ConsulCatalog.new('dc1').node('my_node') =>
  ConsulNode<@service_id=my_service_id ...>

Returns: Returns the node by the argument name.



70
71
72
73
74
75
76
77
78
79
# File 'lib/consul/client/catalog.rb', line 70

def node(name, dc = nil)
  params = {}
  params[:dc] = dc unless dc.nil?
  resp = JSON.parse(_get build_url("node/#{name}"), params)
  n = Consul::Model::Node.new.extend(Consul::Model::Node::Representer).from_hash(resp['Node'])
  unless resp[:Services].nil?
    n.services = resp['Services'].keys.map{|k| Consul::Model::Service.new.extend(Consul::Model::Service::Representer).from_hash(resp[:Services][k])}
  end
  n
end

#nodes(dc = nil) ⇒ Object

Public: Returns a list of all the nodes on this client

dc - Data Center to look for services in, defaults to the agents data center



13
14
15
16
17
# File 'lib/consul/client/catalog.rb', line 13

def nodes(dc = nil)
  params = {}
  params[:dc] = dc unless dc.nil?
  JSON.parse(_get build_url('nodes'), params).map {|n| Consul::Model::Node.new.extend(Consul::Model::Node::Representer).from_hash(n)}
end

#service(id, dc = nil, tag = nil) ⇒ Object

Public: Returns all the nodes within a data center that have the service specified.

dc - Data center, default: agent current data center tag - Tag, filter for tags

Example:

ConsulCatalog.new('dc1').service('my_service_id') =>
  [ConsulNode<@service_id=my_service_id ...>,
   ConsulNode<@service_id=my_service_id ...>,
  ...]

Returns: List of nodes that have this service.



53
54
55
56
57
58
# File 'lib/consul/client/catalog.rb', line 53

def service(id, dc = nil, tag = nil)
  params = {}
  params[:dc] = dc unless dc.nil?
  params[:tag] = tag unless tag.nil?
  JSON.parse(_get build_url("service/#{id}"), params).map {|n| Consul::Model::Node.new.extend(Consul::Model::Node::Representer).from_hash(n)}
end

#services(dc = nil) ⇒ Object

Public: Returns a list of services that are within the supplied or agent data center

dc - Data Center to look for services in, defaults to the agents data center

Example:

Consul::Client::Catalog.new('dc1').services =>
{
  "consul": [],
  "redis": [],
  "postgresql": [
    "master",
    "slave"
  ]
}

Returns: List of services ids.



35
36
37
38
39
# File 'lib/consul/client/catalog.rb', line 35

def services(dc = nil)
  params = {}
  params[:dc] = dc unless dc.nil?
  JSON.parse(_get build_url('services'), params)
end