lib/bolt/inventory/group.rb in bolt-0.16.3 vs lib/bolt/inventory/group.rb in bolt-0.16.4
- old
+ new
@@ -4,23 +4,25 @@
# structured data.
class Group
attr_accessor :name, :nodes, :groups, :config, :rest
def initialize(data)
+ @logger = Logging.logger[self]
@name = data['name']
- @nodes = if data['nodes']
- data['nodes'].map do |n|
- if n.is_a? String
- { 'name' => n }
- else
- n
- end
- end
- else
- []
- end
+ @nodes = {}
+ if data['nodes']
+ data['nodes'].each do |n|
+ n = { 'name' => n } if n.is_a? String
+ if @nodes.include? n['name']
+ @logger.warn("Ignoring duplicate node in #{@name}: #{n}")
+ else
+ @nodes[n['name']] = n
+ end
+ end
+ end
+
@config = data['config'] || {}
@groups = if data['groups']
data['groups'].map { |g| Group.new(g) }
else
[]
@@ -42,15 +44,16 @@
end
raise ValidationError.new("Group #{@name} is too deeply nested", @name) if depth > 1
used_names << @name
- @nodes.each do |n|
- # Require nodes to be referenced only by their host name
- host = Addressable::URI.parse('//' + n['name']).host
- ipv6host = Addressable::URI.parse('//[' + n['name'] + ']').host
- if n['name'] != host && n['name'] != ipv6host
+ @nodes.each_value do |n|
+ # Require nodes to be parseable as a Target.
+ begin
+ Target.new(n['name'])
+ rescue Addressable::URI::InvalidURIError => e
+ @logger.debug(e)
raise ValidationError.new("Invalid node name #{n['name']}", n['name'])
end
raise ValidationError.new("Node #{n['name']} does not have a name", n['name']) unless n['name']
if used_names.include?(n['name'])
@@ -78,11 +81,11 @@
def data_for(node_name)
data_merge(group_collect(node_name), node_collect(node_name))
end
def node_data(node_name)
- if (data = @nodes.find { |n| n['name'] == node_name })
+ if (data = @nodes[node_name])
{ 'config' => data['config'] || {},
# groups come from group_data
'groups' => [] }
end
end
@@ -121,11 +124,11 @@
acc.merge(g.collect_groups)
end
end
def local_node_names
- @_node_names ||= Set.new(nodes.map { |n| n['name'] })
+ Set.new(@nodes.keys)
end
private :local_node_names
def node_collect(node_name)
data = @groups.inject(nil) do |acc, g|
@@ -147,10 +150,10 @@
end
end
if data
data_merge(group_data, data)
- elsif local_node_names.include?(node_name)
+ elsif @nodes.include?(node_name)
group_data
end
end
end
end