Skip to content

Commit

Permalink
(puppetlabsGH-1379) Show all aliases for a target in inventory show -…
Browse files Browse the repository at this point in the history
…-detail

This commit fixes a bug where `inventory show --detail` only showed the
rightmost alias for a target. Now, all aliases for a given target will
be displayed in the output.
  • Loading branch information
beechtom committed Nov 12, 2019
1 parent 6bb9436 commit 4da48a9
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 19 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Expand Up @@ -15,6 +15,12 @@

The CLI now provides the subcommand `project migrate` which migrates Bolt projects to the latest version. When migrating a project, the [inventory file](https://puppet.com/docs/bolt/latest/inventory_file.html) will be changed from `v1` to `v2`. Changes are made in place and will not preserve comments or formatting.

## Bug fixes

* **Display all target aliases in `bolt inventory show --detail`** ([#1379](https://github.com/puppetlabs/bolt/issues/1379))

Bolt will now display aliases from all groups that a target is a member of in the output for `bolt inventory show --detail`. Previously, only the rightmost alias was shown in the output.

## Bolt 1.37.0

## New features
Expand Down
19 changes: 10 additions & 9 deletions lib/bolt/inventory/group.rb
Expand Up @@ -52,13 +52,7 @@ def initialize(data)
raise ValidationError.new("Node entry must be a String or Hash, not #{node.class}", @name)
end

if @nodes.include?(node['name'])
@logger.warn("Ignoring duplicate node in #{@name}: #{node}")
next
end

raise ValidationError.new("Node #{node} does not have a name", @name) unless node['name']
@nodes[node['name']] = node

unless (unexpected_keys = node.keys - NODE_KEYS).empty?
msg = "Found unexpected key(s) #{unexpected_keys.join(', ')} in node #{node['name']}"
Expand All @@ -75,9 +69,9 @@ def initialize(data)
@logger.warn(msg)
end

next unless node.include?('alias')

aliases = node['alias']
# Add aliases before checking whether target is already in group, otherwise they will
# be left out of the group's map of aliases
aliases = node['alias'] || []
aliases = [aliases] if aliases.is_a?(String)
unless aliases.is_a?(Array)
msg = "Alias entry on #{node['name']} must be a String or Array, not #{aliases.class}"
Expand All @@ -92,6 +86,13 @@ def initialize(data)
end
@aliases[alia] = node['name']
end

if @nodes.include?(node['name'])
@logger.warn("Ignoring duplicate node in #{@name}: #{node}")
next
end

@nodes[node['name']] = node
end

# If node is a string, it can refer to either a node name or alias. Which can't be determined
Expand Down
12 changes: 7 additions & 5 deletions lib/bolt/inventory/group2.rb
Expand Up @@ -112,18 +112,15 @@ def add_target_definition(target)
raise ValidationError.new("Target name must be ASCII characters: #{target}", @name)
end

if local_targets.include?(t_name)
@logger.warn("Ignoring duplicate target in #{@name}: #{target}")
return
end

unless (unexpected_keys = target.keys - TARGET_KEYS).empty?
msg = "Found unexpected key(s) #{unexpected_keys.join(', ')} in target #{t_name}"
@logger.warn(msg)
end

validate_data_keys(target, t_name)

# Add aliases before checking whether target is already in group, otherwise they will
# be left out of the group's map of aliases
if target.include?('alias')
aliases = target['alias']
aliases = [aliases] if aliases.is_a?(String)
Expand All @@ -135,6 +132,11 @@ def add_target_definition(target)
insert_alia(t_name, aliases)
end

if local_targets.include?(t_name)
@logger.warn("Ignoring duplicate target in #{@name}: #{target}")
return
end

@unresolved_targets[t_name] = target
end

Expand Down
8 changes: 8 additions & 0 deletions lib/bolt/inventory/inventory2.rb
Expand Up @@ -84,6 +84,14 @@ def data_hash
}
end

def target_alias(target)
@groups.target_aliases.each_with_object([]) do |(alia, name), acc|
if target.name == name
acc << alia
end
end.uniq
end

#### PRIVATE ####
def group_data_for(target_name)
@groups.group_collect(target_name)
Expand Down
6 changes: 5 additions & 1 deletion lib/bolt/inventory/target.rb
Expand Up @@ -4,7 +4,7 @@ module Bolt
class Inventory
# This class represents the active state of a target within the inventory.
class Target
attr_reader :name, :uri, :safe_name, :target_alias
attr_reader :name, :uri, :safe_name

def initialize(target_data, inventory)
unless target_data['name'] || target_data['uri']
Expand Down Expand Up @@ -163,6 +163,10 @@ def options
transport_config.dup
end

def target_alias
@inventory.target_alias(self)
end

# We only want to look up transport config keys for the configured
# transport
def transport_config
Expand Down
7 changes: 5 additions & 2 deletions spec/bolt/inventory/inventory2_spec.rb
Expand Up @@ -1278,15 +1278,18 @@ def common_data(transport)
'alias' => %w[bar baz],
'config' => { 'ssh' => { 'disconnect-timeout' => 100 } },
'facts' => { 'foo' => 'bar' }
}, {
'uri' => 'foo',
'alias' => %w[bak]
}] }
}

let(:inventory) { Bolt::Inventory.create_version(data, config, plugins) }
let(:target) { get_target(inventory, 'foo') }
let(:target) { inventory.get_target('foo') }
let(:expected_data) {
{ 'name' => 'foo',
'uri' => 'foo',
'alias' => %w[bar baz],
'alias' => %w[bar baz bak],
'config' => {
'transport' => 'ssh',
'ssh' => {
Expand Down
7 changes: 5 additions & 2 deletions spec/bolt/inventory_spec.rb
Expand Up @@ -756,14 +756,17 @@ def common_data(transport)
'alias' => %w[bar baz],
'config' => { 'ssh' => { 'disconnect-timeout' => 100 } },
'facts' => { 'foo' => 'bar' }
}, {
'name' => 'foo',
'alias' => %w[bak]
}] }
}

let(:inventory) { Bolt::Inventory.new(data) }
let(:target) { get_target(inventory, 'foo') }
let(:target) { inventory.get_targets('foo').first }
let(:expected_data) {
{ 'name' => 'foo',
'alias' => %w[bar baz],
'alias' => %w[bar baz bak],
'config' => {
'transport' => 'ssh',
'ssh' => {
Expand Down

0 comments on commit 4da48a9

Please sign in to comment.