From 200ff706ab5c9b2ea25fbf848f1ea2319fe9174a Mon Sep 17 00:00:00 2001 From: Jillian Tullo Date: Fri, 24 Feb 2017 10:19:17 -0500 Subject: [PATCH] edit service template api update service template to return the reload --- .../api/service_templates_controller.rb | 7 ++ app/models/service_template.rb | 5 +- spec/requests/api/service_templates_spec.rb | 67 ++++++++++++++++--- 3 files changed, 67 insertions(+), 12 deletions(-) diff --git a/app/controllers/api/service_templates_controller.rb b/app/controllers/api/service_templates_controller.rb index 05ffb612729..bb4ecefac7a 100644 --- a/app/controllers/api/service_templates_controller.rb +++ b/app/controllers/api/service_templates_controller.rb @@ -14,6 +14,13 @@ def create_resource(_type, _id, data) raise BadRequestError, "Could not create Service Template - #{err}" end + def edit_resource(type, id, data) + catalog_item = resource_search(id, type, collection_class(:service_templates)) + catalog_item.update_catalog_item(data.deep_symbolize_keys, @auth_user) + rescue => err + raise BadRequestError, "Could not update Service Template - #{err}" + end + private def set_additional_attributes diff --git a/app/models/service_template.rb b/app/models/service_template.rb index a8744732fe9..30020b506e6 100644 --- a/app/models/service_template.rb +++ b/app/models/service_template.rb @@ -93,7 +93,10 @@ def self.class_from_request_data(data) def update_catalog_item(options, auth_user = nil) config_info = validate_update_config_info(options) - return update_attributes!(options) unless config_info + unless config_info + update_attributes!(options) + return reload + end transaction do update_from_options(options) diff --git a/spec/requests/api/service_templates_spec.rb b/spec/requests/api/service_templates_spec.rb index 618c370d3b7..a763ec9bc8b 100644 --- a/spec/requests/api/service_templates_spec.rb +++ b/spec/requests/api/service_templates_spec.rb @@ -79,11 +79,44 @@ end describe "Service Templates edit" do + let(:ems) { FactoryGirl.create(:ems_amazon) } + let(:new_vm) { FactoryGirl.create(:vm_amazon, :ext_management_system => ems) } + let(:vm) { FactoryGirl.create(:vm_amazon, :ems_id => ems.id) } + let(:flavor) { FactoryGirl.create(:flavor_amazon) } + let(:dialog) { FactoryGirl.create(:miq_dialog_provision) } + let(:request_dialog) { FactoryGirl.create(:miq_dialog_provision) } + let(:service_dialog) { FactoryGirl.create(:dialog) } + let(:updated_catalog_item_options) do + { + :name => 'Updated Template Name', + :display => 'false', + :description => 'a description', + :config_info => { + :miq_request_dialog_name => request_dialog.name, + :placement_auto => [true, 1], + :number_of_vms => [1, '1'], + :src_vm_id => [new_vm.id, new_vm.name], + :vm_name => new_vm.name, + :schedule_type => ['immediately', 'Immediately on Approval'], + :instance_type => [flavor.id, flavor.name], + :src_ems_id => [ems.id, ems.name], + :provision => { + :fqname => ra1.fqname, + :dialog_id => nil + }, + :reconfigure => { + :fqname => ra2.fqname, + :dialog_id => service_dialog.id + } + } + } + end + it "rejects requests without appropriate role" do api_basic_authorize st = FactoryGirl.create(:service_template, :name => "st") - run_post(service_templates_url(st.id), gen_request(:edit, "name" => "sample service template")) + run_post(service_templates_url(st.id), gen_request(:edit, updated_catalog_item_options)) expect(response).to have_http_status(:forbidden) end @@ -92,10 +125,10 @@ api_basic_authorize collection_action_identifier(:service_templates, :edit) st = FactoryGirl.create(:service_template, :name => "st1") - run_post(service_templates_url(st.id), gen_request(:edit, "name" => "updated st1")) + run_post(service_templates_url(st.id), gen_request(:edit, updated_catalog_item_options)) - expect_single_resource_query("id" => st.id, "href" => service_templates_url(st.id), "name" => "updated st1") - expect(st.reload.name).to eq("updated st1") + expect_single_resource_query("id" => st.id, "href" => service_templates_url(st.id), "name" => "Updated Template Name") + expect(st.reload.name).to eq("Updated Template Name") end it "supports edits of multiple resources" do @@ -104,17 +137,29 @@ st1 = FactoryGirl.create(:service_template, :name => "st1") st2 = FactoryGirl.create(:service_template, :name => "st2") - run_post(service_templates_url, gen_request(:edit, - [{"href" => service_templates_url(st1.id), "name" => "updated st1"}, - {"href" => service_templates_url(st2.id), "name" => "updated st2"}])) + run_post(service_templates_url, gen_request(:edit, [updated_catalog_item_options.merge('id' => st1.id), + updated_catalog_item_options.merge('id' => st2.id)])) expect(response).to have_http_status(:ok) expect_results_to_match_hash("results", - [{"id" => st1.id, "name" => "updated st1"}, - {"id" => st2.id, "name" => "updated st2"}]) + [{"id" => st1.id, "name" => "Updated Template Name"}, + {"id" => st2.id, "name" => "Updated Template Name"}]) + expect(st1.reload.name).to eq("Updated Template Name") + expect(st2.reload.name).to eq("Updated Template Name") + end - expect(st1.reload.name).to eq("updated st1") - expect(st2.reload.name).to eq("updated st2") + it 'can update without config info' do + api_basic_authorize collection_action_identifier(:service_templates, :edit) + st1 = FactoryGirl.create(:service_template, :name => 'st1') + + run_post(service_templates_url(st1.id), gen_request(:edit, 'name' => 'updated template')) + + expected = { + 'id' => st1.id, + 'name' => 'updated template' + } + expect(response).to have_http_status(:ok) + expect(response.parsed_body).to include(expected) end end