Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #34121 - Add/Delete a single Ansible role to Host(group)
- Loading branch information
Showing
6 changed files
with
312 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
lib/hammer_cli_foreman_ansible/associated_ansible_resource.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
module HammerCLIForemanAnsible | ||
module AssociatedAnsibleResource | ||
def get_current_ids | ||
resources = HammerCLIForeman.record_to_common_format(resource.call(association_name(true), { id: get_identifier })) | ||
resources.map { |resource| resource['id'] || resource[:id] } | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
require File.join(File.dirname(__FILE__), 'test_helper') | ||
|
||
describe 'host' do | ||
describe 'add ansible role' do | ||
let(:cmd) { %w[host ansible-roles add] } | ||
let(:ansible_role_1) do | ||
{ | ||
'id': 1, | ||
'name': 'role1' | ||
} | ||
end | ||
|
||
it 'requires host --id' do | ||
expected_result = usage_error_result( | ||
cmd, | ||
'At least one of options --name, --id is required.', | ||
'Could not associate the Ansible role' | ||
) | ||
|
||
api_expects_no_call | ||
|
||
result = run_cmd(cmd) | ||
assert_cmd(expected_result, result) | ||
end | ||
|
||
it 'requires host id and asnible role id' do | ||
params = %w[--id=1] | ||
expected_result = usage_error_result( | ||
cmd, | ||
'At least one of options --ansible-role, --ansible-role-id is required.', | ||
'Could not associate the Ansible role' | ||
) | ||
|
||
api_expects_no_call | ||
|
||
result = run_cmd(cmd + params) | ||
assert_cmd(expected_result, result) | ||
end | ||
|
||
it 'associates ansible role with host' do | ||
params = %w[--id=1 --ansible-role-id=2] | ||
|
||
api_expects(:hosts, :ansible_roles) do |par| | ||
par[:id] == '1' | ||
end.returns([ansible_role_1]) | ||
|
||
api_expects(:hosts, :update) do |par| | ||
par['id'] == '1' && | ||
par['host']['ansible_role_ids'] == %w[1 2] | ||
end | ||
|
||
result = run_cmd(cmd + params) | ||
assert_cmd(success_result("Ansible role has been associated.\n"), result) | ||
end | ||
end | ||
|
||
describe 'remove ansible role' do | ||
let(:cmd) { %w[host ansible-roles remove] } | ||
let(:ansible_roles) do | ||
[ | ||
{ | ||
'id': 1, | ||
'name': 'role1' | ||
}, | ||
{ | ||
'id': 2, | ||
'name': 'role2' | ||
} | ||
] | ||
end | ||
|
||
it 'requires host --id' do | ||
expected_result = usage_error_result( | ||
cmd, | ||
'At least one of options --name, --id is required.', | ||
'Could not disassociate the Ansible role' | ||
) | ||
|
||
api_expects_no_call | ||
|
||
result = run_cmd(cmd) | ||
assert_cmd(expected_result, result) | ||
end | ||
|
||
it 'requires host id and asnible role id' do | ||
params = %w[--id=1] | ||
expected_result = usage_error_result( | ||
cmd, | ||
'At least one of options --ansible-role, --ansible-role-id is required.', | ||
'Could not disassociate the Ansible role' | ||
) | ||
|
||
api_expects_no_call | ||
|
||
result = run_cmd(cmd + params) | ||
assert_cmd(expected_result, result) | ||
end | ||
|
||
it 'disassociates ansible role from host' do | ||
params = %w[--id=1 --ansible-role-id=2] | ||
|
||
api_expects(:hosts, :ansible_roles) do |par| | ||
par[:id] == '1' | ||
end.returns(ansible_roles) | ||
|
||
api_expects(:hosts, :update) do |par| | ||
par['id'] == '1' && | ||
par['host']['ansible_role_ids'] == %w[1] | ||
end | ||
|
||
result = run_cmd(cmd + params) | ||
assert_cmd(success_result("Ansible role has been disassociated.\n"), result) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
require File.join(File.dirname(__FILE__), 'test_helper') | ||
|
||
describe 'hostgroup' do | ||
describe 'add ansible role' do | ||
let(:cmd) { %w[hostgroup ansible-roles add] } | ||
let(:ansible_role_1) do | ||
{ | ||
'id': 1, | ||
'name': 'role1' | ||
} | ||
end | ||
|
||
it 'requires hostgroup --id' do | ||
expected_result = usage_error_result( | ||
cmd, | ||
'At least one of options --name, --title, --id is required.', | ||
'Could not associate the Ansible role' | ||
) | ||
|
||
api_expects_no_call | ||
|
||
result = run_cmd(cmd) | ||
assert_cmd(expected_result, result) | ||
end | ||
|
||
it 'requires hostgroup id and asnible role id' do | ||
params = %w[--id=1] | ||
expected_result = usage_error_result( | ||
cmd, | ||
'At least one of options --ansible-role, --ansible-role-id is required.', | ||
'Could not associate the Ansible role' | ||
) | ||
|
||
api_expects_no_call | ||
|
||
result = run_cmd(cmd + params) | ||
assert_cmd(expected_result, result) | ||
end | ||
|
||
it 'associates ansible role with hostgroup' do | ||
params = %w[--id=1 --ansible-role-id=2] | ||
|
||
api_expects(:hostgroups, :ansible_roles) do |par| | ||
par[:id] == '1' | ||
end.returns([ansible_role_1]) | ||
|
||
api_expects(:hostgroups, :update) do |par| | ||
par['id'] == '1' && | ||
par['hostgroup']['ansible_role_ids'] == %w[1 2] | ||
end | ||
|
||
result = run_cmd(cmd + params) | ||
assert_cmd(success_result("Ansible role has been associated.\n"), result) | ||
end | ||
end | ||
|
||
describe 'remove ansible role' do | ||
let(:cmd) { %w[hostgroup ansible-roles remove] } | ||
let(:ansible_roles) do | ||
[ | ||
{ | ||
'id': 1, | ||
'name': 'role1' | ||
}, | ||
{ | ||
'id': 2, | ||
'name': 'role2' | ||
} | ||
] | ||
end | ||
|
||
it 'requires hostgroup --id' do | ||
expected_result = usage_error_result( | ||
cmd, | ||
'At least one of options --name, --title, --id is required.', | ||
'Could not disassociate the Ansible role' | ||
) | ||
|
||
api_expects_no_call | ||
|
||
result = run_cmd(cmd) | ||
assert_cmd(expected_result, result) | ||
end | ||
|
||
it 'requires hostgroup id and asnible role id' do | ||
params = %w[--id=1] | ||
expected_result = usage_error_result( | ||
cmd, | ||
'At least one of options --ansible-role, --ansible-role-id is required.', | ||
'Could not disassociate the Ansible role' | ||
) | ||
|
||
api_expects_no_call | ||
|
||
result = run_cmd(cmd + params) | ||
assert_cmd(expected_result, result) | ||
end | ||
|
||
it 'disassociates ansible role from hostgroup' do | ||
params = %w[--id=1 --ansible-role-id=2] | ||
|
||
api_expects(:hostgroups, :ansible_roles) do |par| | ||
par[:id] == '1' | ||
end.returns(ansible_roles) | ||
|
||
api_expects(:hostgroups, :update) do |par| | ||
par['id'] == '1' && | ||
par['hostgroup']['ansible_role_ids'] == %w[1] | ||
end | ||
|
||
result = run_cmd(cmd + params) | ||
assert_cmd(success_result("Ansible role has been disassociated.\n"), result) | ||
end | ||
end | ||
end |