From c8ad14fc8bf25773c6417d861ecffda025b00ac4 Mon Sep 17 00:00:00 2001 From: Shira Maximov Date: Thu, 16 Jan 2020 16:58:07 +0200 Subject: [PATCH] Fixes #28541 - Fix host creation from image and hostgroup (#471) (cherry picked from commit f163dd9fc9a232f711621f81a16752624edd5de7) --- .../compute_resource/utils.rb | 16 +++++ .../hosts/common_update_options.rb | 11 +++- test/functional/host_test.rb | 61 +++++++++++++++++++ 3 files changed, 85 insertions(+), 3 deletions(-) diff --git a/lib/hammer_cli_foreman/compute_resource/utils.rb b/lib/hammer_cli_foreman/compute_resource/utils.rb index c2f9afedb..1762e57e9 100644 --- a/lib/hammer_cli_foreman/compute_resource/utils.rb +++ b/lib/hammer_cli_foreman/compute_resource/utils.rb @@ -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( diff --git a/lib/hammer_cli_foreman/hosts/common_update_options.rb b/lib/hammer_cli_foreman/hosts/common_update_options.rb index 6194dc7be..404501062 100644 --- a/lib/hammer_cli_foreman/hosts/common_update_options.rb +++ b/lib/hammer_cli_foreman/hosts/common_update_options.rb @@ -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 diff --git a/test/functional/host_test.rb b/test/functional/host_test.rb index 7e430a619..51aca3212 100644 --- a/test/functional/host_test.rb +++ b/test/functional/host_test.rb @@ -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