Skip to content

Commit

Permalink
Fixes #26536 - Possibility to change host loc/org via hammer
Browse files Browse the repository at this point in the history
  • Loading branch information
ofedoren committed Apr 4, 2019
1 parent 4d6da51 commit e424bcf
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/hammer_cli_foreman/host.rb
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,16 @@ class UpdateCommand < HammerCLIForeman::UpdateCommand
success_message _("Host updated.")
failure_message _("Could not update the host")

def self.create_option_builder
builder = super
%i[locations organizations].each do |resource_name|
builder.builders << UpdateDependentSearchablesOptionBuilder.new(
HammerCLIForeman.foreman_resource(resource_name), searchables
)
end
builder
end

include HammerCLIForeman::Hosts::CommonUpdateOptions
include HammerCLIForeman::Hosts::CommonUpdateHelp
end
Expand Down
20 changes: 20 additions & 0 deletions lib/hammer_cli_foreman/hosts/common_update_options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,18 @@ def request_params
if action == :update
params['host']['compute_attributes']['volumes_attributes'] = nested_attributes(option_volume_list) unless option_volume_list.empty?
params['host']['interfaces_attributes'] = interfaces_attributes unless option_interface_list.empty?
new_location = option_new_location_name || option_new_location_title
if new_location
params['host']['location_id'] = new_location_id(new_location)
else
params['host'].delete('location_id')
end
new_organization = option_new_organization_name || option_new_organization_title
if new_organization
params['host']['organization_id'] = new_organization_id(new_organization)
else
params['host'].delete('organization_id')
end
else
params['host']['compute_attributes']['volumes_attributes'] = nested_attributes(option_volume_list)
params['host']['interfaces_attributes'] = interfaces_attributes
Expand Down Expand Up @@ -110,6 +122,14 @@ def domain_id(name)
resolver.domain_id('option_name' => name) if name
end

def new_location_id(name)
resolver.location_id('option_name' => name) if name
end

def new_organization_id(name)
resolver.organization_id('option_name' => name) if name
end

def parameter_attributes
return [] unless option_parameters
option_parameters.collect do |key, value|
Expand Down
46 changes: 46 additions & 0 deletions lib/hammer_cli_foreman/option_builders.rb
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,53 @@ def aliased(name, resource_name_map)

end

class UpdateDependentSearchablesOptionBuilder < DependentSearchablesOptionBuilder
protected

def dependent_options(resource, resource_name_map)
options = []
searchables = @searchables.for(resource)
resource_name = resource.singular_name
aliased_name = aliased(resource_name, resource_name_map)

unless searchables.empty?
first = searchables[0]
remaining = searchables[1..-1] || []

# First option is named by the resource
# Eg. --organization with accessor option_organization_name
options << option(
optionamize("--new-#{aliased_name}"),
"new_#{aliased_name}_#{first.name}".upcase,
first.description || ' ',
attribute_name: HammerCLI.option_accessor_name("new_#{resource_name}_#{first.name}"),
referenced_resource: resource.singular_name
)

# Other options are named by the resource plus the searchable name
# Eg. --organization-label with accessor option_organization_label
remaining.each do |s|
options << option(
optionamize("--new-#{aliased_name}-#{s.name}"),
"new_#{aliased_name}_#{s.name}".upcase,
s.description || ' ',
attribute_name: HammerCLI.option_accessor_name("new_#{resource_name}_#{s.name}"),
referenced_resource: resource.singular_name
)
end
end

options << option(
optionamize("--new-#{aliased_name}-id"),
"new_#{aliased_name}_id".upcase,
description('id', :show),
attribute_name: HammerCLI.option_accessor_name("new_#{resource_name}_id"),
format: HammerCLI::Options::Normalizers::Number.new,
referenced_resource: resource.singular_name
)
options
end
end

class DependentSearchablesArrayOptionBuilder < DependentSearchablesOptionBuilder

Expand Down
71 changes: 71 additions & 0 deletions test/functional/host_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,77 @@
end
end

describe 'host update' do
let(:cmd) { ['host', 'update'] }
let(:minimal_params) { ['--location-id=1', '--organization-id=1', '--id=1'] }
let(:updated_host) do
{
'id' => '1',
'organization_id' => '5',
'location_id' => '5',
'compute_attributes' => {},
'puppetclass_ids' => []
}
end

it 'updates nothing without host related parameters' do
api_expects(:hosts, :update, 'Update host with no host params').returns({})

expected_result = success_result("Host updated.\n")

result = run_cmd(cmd + minimal_params)
assert_cmd(expected_result, result)
end

it 'updates organization with resolving name to id' do
params = ['--new-organization', 'Org5']

api_expects_search(:organizations, name: 'Org5').returns(
index_response([{ 'id' => '5' }])
)
api_expects(:hosts, :update, 'Update host with new org').with_params(
'id' => '1', 'location_id' => 1, 'organization_id' => 1, 'host' => {
'organization_id' => '5', 'puppetclass_ids' => [], 'compute_attributes' => {}
}
) do |par|
par['id'] == '1' &&
par['host']['organization_id'] == '5' &&
par['host']['puppetclass_ids'] == [] &&
par['host']['compute_attributes'] == {}
end.returns(updated_host)

expected_result = success_result("Host updated.\n")

result = run_cmd(cmd + minimal_params + params)

assert_cmd(expected_result, result)
end

it 'updates location with resolving name to id' do
params = ['--new-location', 'Loc5']

api_expects_search(:locations, name: 'Loc5').returns(
index_response([{ 'id' => '5' }])
)
api_expects(:hosts, :update, 'Update host with new loc').with_params(
'id' => '1', 'location_id' => 1, 'organization_id' => 1, 'host' => {
'location_id' => '5', 'puppetclass_ids' => [], 'compute_attributes' => {}
}
) do |par|
par['id'] == '1' &&
par['host']['location_id'] == '5' &&
par['host']['puppetclass_ids'] == [] &&
par['host']['compute_attributes'] == {}
end.returns(updated_host)

expected_result = success_result("Host updated.\n")

result = run_cmd(cmd + minimal_params + params)

assert_cmd(expected_result, result)
end
end

describe 'host config reports' do
let(:report15) do
{
Expand Down

0 comments on commit e424bcf

Please sign in to comment.