From cebab02f23077496a12913e5ed97026c72c23443 Mon Sep 17 00:00:00 2001 From: Joe Rafaniello Date: Mon, 29 Apr 2019 16:21:33 -0400 Subject: [PATCH] Merge pull request #18689 from d-m-u/service_template_picture Service template picture (cherry picked from commit 5f37ae764a60ae0985235d4f0b70a791b5b43afa) Fixes https://bugzilla.redhat.com/show_bug.cgi?id=1702479 --- app/models/service_template.rb | 21 ++++++------ spec/models/service_template_spec.rb | 49 ++++++++++++++++++++-------- 2 files changed, 46 insertions(+), 24 deletions(-) diff --git a/app/models/service_template.rb b/app/models/service_template.rb index d02fcf8e9c6..06a541b6261 100644 --- a/app/models/service_template.rb +++ b/app/models/service_template.rb @@ -199,7 +199,7 @@ def create_service(service_task, parent_svc = nil) nh['initiator'] = service_task.options[:initiator] if service_task.options[:initiator] - service = Service.create(nh) do |svc| + service = Service.create!(nh) do |svc| svc.service_template = self set_ownership(svc, service_task.get_user) @@ -378,11 +378,7 @@ def create_resource_actions(ae_endpoints) end def self.create_from_options(options) - create(options.except(:config_info, :picture).merge(:options => { :config_info => options[:config_info] })).tap do |tmp| - if options[:picture] - tmp.update_attributes!(:picture => Picture.create(:content => options[:picture][:content], :extension => options[:picture][:extension])) - end - end + create!(options.except(:config_info).merge(:options => { :config_info => options[:config_info] })) end private_class_method :create_from_options @@ -393,6 +389,14 @@ def provision_request(user, options = nil, request_options = {}) result[:request] end + def picture=(value) + if value + super unless value.kind_of?(Hash) + + super(create_picture(value)) + end + end + def queue_order(user_id, options, request_options) MiqQueue.submit_job( :class_name => self.class.name, @@ -500,10 +504,7 @@ def resource_action_list def update_from_options(params) options[:config_info] = params[:config_info] - if params[:picture] - update_attributes!(:picture => Picture.create(:content => params[:picture][:content], :extension => params[:picture][:extension])) - end - update_attributes!(params.except(:config_info, :picture)) + update_attributes!(params.except(:config_info)) end def construct_config_info diff --git a/spec/models/service_template_spec.rb b/spec/models/service_template_spec.rb index 8857ffa2223..1ecdc73ae9d 100644 --- a/spec/models/service_template_spec.rb +++ b/spec/models/service_template_spec.rb @@ -773,6 +773,7 @@ expect(service_template.name).to eq('Atomic Service Template') expect(service_template.service_resources.count).to eq(1) + expect(Base64.strict_encode64(service_template.picture.content)).to start_with('aVZCT1J3MEtHZ29BQUFBTlNVaEVVZ0FBQUFFQUFBQUJDQVlBQ') expect(service_template.service_resources.first.resource_type).to eq('MiqRequest') expect(service_template.dialogs.first).to eq(service_dialog) expect(service_template.resource_actions.pluck(:action)).to include('Provision', 'Retirement') @@ -818,20 +819,40 @@ @catalog_item.update_attributes!(:options => @catalog_item.options.merge(:foo => 'bar')) end - it 'updates the catalog item' do - updated = @catalog_item.update_catalog_item(updated_catalog_item_options, user) - - # Removes Retirement / Adds Reconfigure - expect(updated.resource_actions.pluck(:action)).to match_array(%w(Provision Reconfigure)) - expect(updated.resource_actions.first.dialog_id).to be_nil # Removes the dialog from Provision - expect(updated.resource_actions.first.fqname).to eq('/a1/b1/c1') - expect(updated.resource_actions.last.dialog).to eq(service_dialog) - expect(updated.resource_actions.last.fqname).to eq('/x1/y1/z1') - expect(updated.name).to eq('Updated Template Name') - expect(Base64.strict_encode64(updated.picture.content)).to eq('aVZCT1J3MEtHZ29BQUFBTg==') - expect(updated.service_resources.first.resource.source_id).to eq(new_vm.id) # Validate request update - expect(updated.config_info).to eq(updated_catalog_item_options[:config_info]) - expect(updated.options.key?(:foo)).to be_truthy # Test that the options were merged + context "without config info" do + it 'updates the catalog item' do + updated = @catalog_item.update_catalog_item(updated_catalog_item_options, user) + + # Removes Retirement / Adds Reconfigure + expect(updated.resource_actions.pluck(:action)).to match_array(%w[Provision Reconfigure]) + expect(updated.resource_actions.first.dialog_id).to be_nil # Removes the dialog from Provision + expect(updated.resource_actions.first.fqname).to eq('/a1/b1/c1') + expect(updated.resource_actions.last.dialog).to eq(service_dialog) + expect(updated.resource_actions.last.fqname).to eq('/x1/y1/z1') + expect(updated.name).to eq('Updated Template Name') + expect(Base64.strict_encode64(updated.picture.content)).to eq('aVZCT1J3MEtHZ29BQUFBTg==') + expect(updated.service_resources.first.resource.source_id).to eq(new_vm.id) # Validate request update + expect(updated.config_info).to eq(updated_catalog_item_options[:config_info]) + expect(updated.options.key?(:foo)).to be_truthy # Test that the options were merged + end + end + + context "with config info" do + it 'updates the catalog item' do + @catalog_item.update_attributes!(:options => @catalog_item.options.merge(:config_info => 'bar')) + updated = @catalog_item.update_catalog_item(updated_catalog_item_options, user) + + expect(updated.resource_actions.pluck(:action)).to match_array(%w[Provision Reconfigure]) + expect(updated.resource_actions.first.dialog_id).to be_nil # Removes the dialog from Provision + expect(updated.resource_actions.first.fqname).to eq('/a1/b1/c1') + expect(updated.resource_actions.last.dialog).to eq(service_dialog) + expect(updated.resource_actions.last.fqname).to eq('/x1/y1/z1') + expect(updated.name).to eq('Updated Template Name') + expect(Base64.strict_encode64(updated.picture.content)).to eq('aVZCT1J3MEtHZ29BQUFBTg==') + expect(updated.service_resources.first.resource.source_id).to eq(new_vm.id) # Validate request update + expect(updated.config_info).to eq(updated_catalog_item_options[:config_info]) + expect(updated.options.key?(:foo)).to be_truthy # Test that the options were merged + end end it 'does not allow service_type to be changed' do