Skip to content

Commit

Permalink
Fixes #28541 - Fix host creation from image and hostgroup (#471)
Browse files Browse the repository at this point in the history
(cherry picked from commit f163dd9)
  • Loading branch information
shiramax authored and ofedoren committed Jan 16, 2020
1 parent f5e5893 commit c8ad14f
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 3 deletions.
16 changes: 16 additions & 0 deletions lib/hammer_cli_foreman/compute_resource/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,22 @@ def self.get_image_uuid(compute_resource_id, image_id)
)
end

def self.get_hostgroup_compute_resource_id(hostgroup_id)
hostgroup = HammerCLIForeman.record_to_common_format(
HammerCLIForeman.foreman_resource(:hostgroups).call(:show, 'id' => hostgroup_id)
)
compute_resource_id = hostgroup['compute_resource_id']
if !hostgroup['compute_resource_name'].to_s.strip.empty? && compute_resource_id.nil?
compute_resource= HammerCLIForeman.record_to_common_format(
HammerCLIForeman.foreman_resource(:compute_resources).call(
:index, :search => "name = \"#{hostgroup['compute_resource_name']}\""
)
)
compute_resource_id = compute_resource['results'][0]['id'] if compute_resource['results'][0]
end
compute_resource_id
end

def self.get_host_compute_resource_id(host_id)
HammerCLIForeman.record_to_common_format(
HammerCLIForeman.foreman_resource(:hosts).call(
Expand Down
11 changes: 8 additions & 3 deletions lib/hammer_cli_foreman/hosts/common_update_options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,14 @@ def request_params
params['host']['compute_attributes']['volumes_attributes'] = nested_attributes(option_volume_list)
params['host']['interfaces_attributes'] = interfaces_attributes
end

if options["option_image_id"]
compute_resource_id = params['host']['compute_resource_id'] || ::HammerCLIForeman::ComputeResources.get_host_compute_resource_id(params['id'])
if options['option_image_id']
if params['host']['compute_resource_id']
compute_resource_id = params['host']['compute_resource_id']
elsif params['id']
compute_resource_id = ::HammerCLIForeman::ComputeResources.get_host_compute_resource_id(params['id'])
elsif params['host']['hostgroup_id']
compute_resource_id = ::HammerCLIForeman::ComputeResources.get_hostgroup_compute_resource_id(params['host']['hostgroup_id'])
end
raise ArgumentError, "Missing argument for 'compute_resource'" if compute_resource_id.nil?
image_uuid = ::HammerCLIForeman::ComputeResources.get_image_uuid(compute_resource_id, options["option_image_id"])
params['host']['compute_attributes']['image_id'] = image_uuid
Expand Down
61 changes: 61 additions & 0 deletions test/functional/host_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,67 @@
result = run_cmd(cmd + minimal_params_without_hostgroup + params)
assert_cmd(expected_result, result)
end

it "Creates host in case the image is provided from the hostgroup and hostgroup is not nested" do
params = ['--name=test',
'--provision-method=image',
'--image-id=8' ,
'--managed=true',
'--hostgroup-id=1',
'--organization-id=1',
'--location-id=1'
]

api_expects(:hostgroups, :show).with_params(
{}
).returns({'compute_resource_name' => "cr", 'compute_resource_id' => 33 })

api_expects(:images, :show).with_params(
{"compute_resource_id" => 33, "id" => 8}
).returns(results: {'uuid' => '111111'})

api_expects(:hosts, :create).with_params(
{"location_id" => 1, "organization_id" => 1, "host" => {"name" => "test", "location_id" => 1, "organization_id" => 1, "puppetclass_ids" => [], "hostgroup_id" => 1, "image_id" => 8, "provision_method" => "image", "managed" => true, "compute_attributes" => {"volumes_attributes" => {}, "image_id" => nil}, "build" => true, "enabled" => true, "overwrite" => true, "interfaces_attributes" => []}}
).returns(results: {'uuid' => '111111'})

expected_result = success_result("Host created.\n")
result = run_cmd(cmd + params)
assert_cmd(expected_result, result)
end

it "Creates host in case the image is provided from the hostgroup and hostgroup is nested" do
params = ['--name=test',
'--provision-method=image',
'--image-id=8' ,
'--managed=true',
'--hostgroup-id=1',
'--organization-id=1',
'--location-id=1'
]

# nested hostgroup will return nil in compute_resource_id
api_expects(:hostgroups, :show).with_params(
{}
).returns({'compute_resource_name' => "cr", 'compute_resource_id' => nil})

api_expects(:compute_resources, :index).with_params(
{:search => "name = \"cr\""}
).returns(results: {'results' => [{'id' => 33, 'name' => "cr"}]})

api_expects(:images, :show).with_params(
{"compute_resource_id" => 33, "id" => 8}
).returns(results: {'uuid' => '111111'})

api_expects(:hosts, :create).with_params(
{"location_id" => 1, "organization_id" => 1, "host" => {"name" => "test", "location_id" => 1, "organization_id" => 1, "puppetclass_ids" => [], "hostgroup_id" => 1, "image_id" => 8, "provision_method" => "image", "managed" => true, "compute_attributes" => {"volumes_attributes" => {}, "image_id" => nil}, "build" => true, "enabled" => true, "overwrite" => true, "interfaces_attributes" => []}}
).returns(results: {'uuid' => '111111'})


expected_result = success_result("Host created.\n")
result = run_cmd(cmd + params)
assert_cmd(expected_result, result)
end

end

describe 'host update' do
Expand Down

0 comments on commit c8ad14f

Please sign in to comment.