Skip to content

Commit

Permalink
(puppetlabsGH-1379) Show all target aliases in inventory show --detail
Browse files Browse the repository at this point in the history
This fixes a bug that caused `inventory show --detail` to only show the
rightmost alias for a given target. Previously, when the target data
hash was being merged during target lookup, aliases would be overridden
during each merge. Now, aliases are collected and combined into a single
array during the lookup process.
  • Loading branch information
beechtom committed Nov 13, 2019
1 parent 17d88e7 commit b5dcfd1
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 11 deletions.
3 changes: 2 additions & 1 deletion lib/bolt/inventory/group2.rb
Expand Up @@ -162,7 +162,8 @@ def data_merge(data1, data2)
'config' => Bolt::Util.deep_merge(data1['config'], data2['config']),
'name' => data1['name'] || data2['name'],
'uri' => data1['uri'] || data2['uri'],
'alias' => data1['alias'] || data2['alias'],
# Collect all aliases across all groups for each target uri
'alias' => [*data1['alias'], *data2['alias']].sort,
# Shallow merge instead of deep merge so that vars with a hash value
# are assigned a new hash, rather than merging the existing value
# with the value meant to replace it
Expand Down
15 changes: 15 additions & 0 deletions spec/bolt/inventory/group2_spec.rb
Expand Up @@ -490,6 +490,21 @@
it { expect(group.target_aliases).to eq('alias1' => 'target1', 'alias2' => 'target1', 'alias3' => 'target2') }
end

context 'multiple aliases in multiple groups' do
let(:data) do
{
'name' => 'root',
'groups' => [
{ 'name' => 'group1', 'targets' => [{ 'name' => 'target', 'alias' => 'alias1' }] },
{ 'name' => 'group2', 'targets' => [{ 'name' => 'target', 'alias' => 'alias2' }] }
]
}
end

it { expect(group.target_aliases).to eq('alias1' => 'target', 'alias2' => 'target') }
it { expect(group.target_collect('target')['alias']).to eq(%w[alias1 alias2]) }
end

context 'redundant targets' do
let(:data) do
{
Expand Down
19 changes: 14 additions & 5 deletions spec/bolt/inventory/inventory2_spec.rb
Expand Up @@ -1273,11 +1273,20 @@ def common_data(transport)
context 'when using inventory show' do
let(:data) {
{ 'version' => 2,
'targets' => [{
'uri' => 'foo',
'alias' => %w[bar baz],
'config' => { 'ssh' => { 'disconnect-timeout' => 100 } },
'facts' => { 'foo' => 'bar' }
'groups' => [{
'name' => 'group1',
'targets' => [{
'uri' => 'foo',
'alias' => %w[bar]
}]
}, {
'name' => 'group2',
'targets' => [{
'uri' => 'foo',
'alias' => %w[baz],
'config' => { 'ssh' => { 'disconnect-timeout' => 100 } },
'facts' => { 'foo' => 'bar' }
}]
}] }
}

Expand Down
19 changes: 14 additions & 5 deletions spec/bolt/inventory_spec.rb
Expand Up @@ -751,11 +751,20 @@ def common_data(transport)

context 'when using inventory show' do
let(:data) {
{ 'nodes' => [{
'name' => 'foo',
'alias' => %w[bar baz],
'config' => { 'ssh' => { 'disconnect-timeout' => 100 } },
'facts' => { 'foo' => 'bar' }
{ 'groups' => [{
'name' => 'group1',
'nodes' => [{
'name' => 'foo',
'alias' => %w[bar]
}]
}, {
'name' => 'group2',
'nodes' => [{
'name' => 'foo',
'alias' => %w[baz],
'config' => { 'ssh' => { 'disconnect-timeout' => 100 } },
'facts' => { 'foo' => 'bar' }
}]
}] }
}

Expand Down

0 comments on commit b5dcfd1

Please sign in to comment.