Skip to content

Commit

Permalink
Merge pull request #19186 from fdupont-redhat/v2v_allow_error_message…
Browse files Browse the repository at this point in the history
…s_in_service_template_orderable

Allow error messages in ServiceTemplate.validate_order
  • Loading branch information
agrare committed Aug 27, 2019
2 parents fb42c49 + e941a51 commit 7b2880c
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 21 deletions.
16 changes: 9 additions & 7 deletions app/models/service_template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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}",
Expand Down
14 changes: 8 additions & 6 deletions app/models/service_template_transformation_plan.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down
19 changes: 15 additions & 4 deletions spec/models/service_template_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
Expand Down Expand Up @@ -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
Expand Down
9 changes: 5 additions & 4 deletions spec/models/service_template_transformation_plan_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 7b2880c

Please sign in to comment.