diff --git a/app/models/service_template.rb b/app/models/service_template.rb index 59e2bc71278..a04315b13bd 100644 --- a/app/models/service_template.rb +++ b/app/models/service_template.rb @@ -86,7 +86,13 @@ class ServiceTemplate < ApplicationRecord scope :displayed, -> { where(:display => true) } scope :public_service_templates, -> { where(:internal => [false, nil]) } - supports :order + supports :order do + unsupported_reason_add(:order, 'Service template does not belong to a service catalog') unless service_template_catalog + unsupported_reason_add(:order, 'Service template is not configured to be displayed') unless display + end + alias orderable? supports_order? + alias validate_order supports_order? + def self.with_tenant(tenant_id) tenant = Tenant.find(tenant_id) @@ -352,11 +358,6 @@ def validate_template end.try(:resource).try(:validate_template) || {:valid => true, :message => nil} end - def validate_order - service_template_catalog && display - end - alias orderable? validate_order - def provision_action resource_actions.find_by(:action => "Provision") end @@ -428,7 +429,8 @@ def order(user_or_id, options = nil, request_options = {}, schedule_time = nil) time = Time.parse(schedule_time).utc errors = workflow.validate_dialog - return {:errors => errors} unless errors.blank? + errors << unsupported_reason(:order) + return {:errors => errors} if errors.compact.present? schedule = MiqSchedule.create!( :name => "Order #{self.class.name} #{id} at #{time}", diff --git a/app/models/service_template_transformation_plan.rb b/app/models/service_template_transformation_plan.rb index 64c2b545566..2eed062c7c8 100644 --- a/app/models/service_template_transformation_plan.rb +++ b/app/models/service_template_transformation_plan.rb @@ -1,6 +1,14 @@ class ServiceTemplateTransformationPlan < ServiceTemplate include_concern 'ValidateConfigInfo' virtual_has_one :transformation_mapping + + supports :order do + unmigrated_vms = vm_resources.reject { |res| res.resource.is_tagged_with?('transformation_status/migrated', :ns => '/managed') } + unsupported_reason_add(:order, 'All VMs of the migration plan have already been successfully migrated') if unmigrated_vms.blank? + end + alias orderable? supports_order? + alias validate_order supports_order? + def request_class ServiceTemplateTransformationPlanRequest end @@ -23,12 +31,6 @@ def transformation_mapping_resource service_resources.where(:resource_type => 'TransformationMapping') end - def validate_order - # Service template should not be orderable if all VMs have already been migrated - vm_resources.reject { |res| res.resource.is_tagged_with?('transformation_status/migrated', :ns => '/managed') }.present? - end - alias orderable? validate_order - def self.default_provisioning_entry_point(_service_type) '/Transformation/StateMachines/VMTransformation/Transformation' end diff --git a/spec/models/service_template_spec.rb b/spec/models/service_template_spec.rb index 3d8563fa218..482549cd908 100644 --- a/spec/models/service_template_spec.rb +++ b/spec/models/service_template_spec.rb @@ -915,7 +915,8 @@ context "#order" do let(:user) { FactoryBot.create(:user) } let(:resource_action) { FactoryBot.create(:resource_action, :action => "Provision") } - let(:service_template) { FactoryBot.create(:service_template, :resource_actions => [resource_action]) } + let(:service_template_catalog) { FactoryBot.create(:service_template_catalog) } + let(:service_template) { FactoryBot.create(:service_template, :resource_actions => [resource_action], :service_template_catalog => service_template_catalog, :display => true) } let(:resource_action_options) { {:target => service_template, :initiator => 'control', :submit_workflow => true} } let(:miq_request) { FactoryBot.create(:service_template_provision_request) } let!(:resource_action_workflow) { ResourceActionWorkflow.new({}, user, resource_action, resource_action_options) } @@ -1115,9 +1116,19 @@ end context "#supports_order?" do - it "returns the expected boolean value" do - st = FactoryBot.create(:service_template) - expect(st.supports_order?).to eql(true) + context 'when service_template cannot be displayed' do + it "returns the expected boolean value" do + st = FactoryBot.create(:service_template, :service_template_catalog => FactoryBot.create(:service_template_catalog), :display => false) + expect(st.supports_order?).to eql(false) + expect(st.unsupported_reason(:order)).to eq('Service template is not configured to be displayed') + end + end + + context 'when service_template can be displayed' do + it "returns the expected boolean value" do + st = FactoryBot.create(:service_template, :service_template_catalog => FactoryBot.create(:service_template_catalog), :display => true) + expect(st.supports_order?).to eql(true) + end end end end diff --git a/spec/models/service_template_transformation_plan_spec.rb b/spec/models/service_template_transformation_plan_spec.rb index 8b469c426c3..d1576576a4d 100644 --- a/spec/models/service_template_transformation_plan_spec.rb +++ b/spec/models/service_template_transformation_plan_spec.rb @@ -107,15 +107,16 @@ let(:service_template) { described_class.create_catalog_item(catalog_item_options) } it 'allows a plan to be ordered if all VMs have not been migrated' do - expect(service_template.validate_order).to be_truthy - expect(service_template.orderable?).to be_truthy # alias + expect(service_template.validate_order).to eql(true) + expect(service_template.orderable?).to eql(true) # alias end it 'denies a plan from bring ordered if all VMs have been migrated' do vm1.tag_add('transformation_status/migrated', :ns => '/managed') vm2.tag_add('transformation_status/migrated', :ns => '/managed') - expect(service_template.validate_order).to be_falsey - expect(service_template.orderable?).to be_falsey # alias + expect(service_template.validate_order).to eql(false) + expect(service_template.orderable?).to eql(false) # alias + expect(service_template.unsupported_reason(:order)).to eq('All VMs of the migration plan have already been successfully migrated') end end