Skip to content

Commit

Permalink
Merge pull request #75 from pierresouchay/fix_issue_74
Browse files Browse the repository at this point in the history
Added new utility methods to fix #74
  • Loading branch information
kamaradclimber authored Aug 23, 2021
2 parents fa13ad2 + 9a39004 commit 6da7148
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 29 deletions.
9 changes: 9 additions & 0 deletions TemplateAPI.md
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,15 @@ Full example: [samples/consul_template.txt.erb](samples/consul_template.txt.erb)
[List all the services of a given Node](https://www.consul.io/api/catalog.html#list-services-for-node) using its
name or its ID. If DC is specified, will lookup for given node in another datacenter.

The value returned might be nil (because the node does not exists).
Thus, it might be safer to use the helper methods on the result:

* node('mynode').exists? -> true if node exists and result is not nil
* node('mynode').services() -> to get the hash of services defined for this node (return empty map if nodes does not exists)
* node('mynode').node() -> to get node information (empty map if node does not exists)

See also: https://github.com/criteo/consul-templaterb/issues/74

## checks_for_node(name, dc: nil, passing: false, tag: nil, [agent: consul_agent_address])

[Find all the checks](https://www.consul.io/api/health.html#list-checks-for-node) of a given node name.
Expand Down
32 changes: 31 additions & 1 deletion lib/consul/async/consul_template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ def node(name_or_id, dc: nil, agent: nil)
path = "/v1/catalog/node/#{name_or_id}"
query_params = {}
query_params[:dc] = dc if dc
create_if_missing(path, query_params, agent: agent) { ConsulTemplateNodes.new(ConsulEndpoint.new(consul_conf, path, true, query_params, '{}', agent)) }
create_if_missing(path, query_params, agent: agent) { ConsulTemplateNode.new(ConsulEndpoint.new(consul_conf, path, true, query_params, '{}', agent)) }
end

# https://www.consul.io/api/agent.html#read-configuration
Expand Down Expand Up @@ -628,6 +628,36 @@ def initialize(consul_endpoint)
end
end

# Get information about a single node
class ConsulTemplateNode < ConsulTemplateAbstractMap
def initialize(consul_endpoint)
super(consul_endpoint)
end

def exists?
!result_delegate.nil?
end

def safe_get
if exists?
result_delegate
else
{
'Node': {},
'Services': {}
}
end
end

def node
safe_get['Node'] || {}
end

def services
safe_get['Services'] || {}
end
end

# List of nodes of the whole cluster
class ConsulTemplateNodes < ConsulTemplateAbstractArray
def initialize(consul_endpoint)
Expand Down
1 change: 1 addition & 0 deletions spec/consul/async/async_mock.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ def mock_consul
v1/catalog/datacenters
v1/catalog/nodes
v1/catalog/node/consul02.prod
v1/catalog/node/does_not_exists
v1/catalog/node/7a985997-3925-4f18-8613-637c81bd750b
v1/catalog/services
v1/coordinate/datacenters
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
{
"ID": "7a985997-3925-4f18-8613-637c81bd750b",
"Node": "consul02.prod",
"Address": "10.251.1.96",
"Datacenter": "par",
"TaggedAddresses": {
"lan": "10.251.1.96",
"wan": "10.251.1.96"
"Node":{
"ID": "7a985997-3925-4f18-8613-637c81bd750b",
"Node": "consul02.prod",
"Address": "10.251.1.96",
"Datacenter": "par",
"TaggedAddresses": {
"lan": "10.251.1.96",
"wan": "10.251.1.96"
},
"Meta": {
"consul-network-segment": "",
"rack_name": "2207"
},
"CreateIndex": 228090242,
"ModifyIndex": 228091668
},
"Meta": {
"consul-network-segment": "",
"rack_name": "2207"
},
"CreateIndex": 228090242,
"ModifyIndex": 228091668
"Services": {}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
{
"ID": "7a985997-3925-4f18-8613-637c81bd750b",
"Node": "consul02.prod",
"Address": "10.251.1.96",
"Datacenter": "par",
"TaggedAddresses": {
"lan": "10.251.1.96",
"wan": "10.251.1.96"
"Node": {
"ID": "7a985997-3925-4f18-8613-637c81bd750b",
"Node": "consul02.prod",
"Address": "10.251.1.96",
"Datacenter": "par",
"TaggedAddresses": {
"lan": "10.251.1.96",
"wan": "10.251.1.96"
},
"Meta": {
"consul-network-segment": "",
"rack_name": "2207"
},
"CreateIndex": 228090242,
"ModifyIndex": 228091668
},
"Meta": {
"consul-network-segment": "",
"rack_name": "2207"
},
"CreateIndex": 228090242,
"ModifyIndex": 228091668
"Services": {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
null
11 changes: 9 additions & 2 deletions spec/consul/async/resources/templates/unit_nodes.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,12 @@ Nodes
<%
end
%>
Node: <%= node('consul02.prod')['Node'] %>
ID: <%= node('7a985997-3925-4f18-8613-637c81bd750b')['ID'] %>
Helper when exists:
Node: <%= node('consul02.prod').node['Node'] %>
ID: <%= node('7a985997-3925-4f18-8613-637c81bd750b').node['ID'] %>
exists? <%= node('consul02.prod').exists? %>

Helpes when does not exists:
* exists? = <%= node("does_not_exists").exists? %>
* node empty = <%= node("does_not_exists").node == {} %>
* services empty <%= node("does_not_exists").services == {} %>
7 changes: 7 additions & 0 deletions spec/consul/async/resources/templates/unit_nodes.txt.expected
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,12 @@ consul02.prod
consul03.prod
consul04.prod

Helper when exists:
Node: consul02.prod
ID: 7a985997-3925-4f18-8613-637c81bd750b
exists? true

Helpes when does not exists:
* exists? = false
* node empty = true
* services empty true

0 comments on commit 6da7148

Please sign in to comment.