Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a validation for conversion hosts #18135

Merged
merged 4 commits into from
Oct 31, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions app/models/service_template_transformation_plan_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ def source_vms
vm_resources.where(:status => [ServiceResource::STATUS_QUEUED, ServiceResource::STATUS_FAILED]).pluck(:resource_id)
end

def validate_conversion_hosts
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will valid_conversion_hosts? be a more obvious method name?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Name already validated in ManageIQ/manageiq-automation_engine#263. It would mean changing it again.

transformation_mapping.transformation_mapping_items.each do |item|
next unless %w(EmsCluster CloudTenant).include?(item.source_type)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can do transformation_mapping.transformation_mapping_items.reject { |item| %w(EmsCluster CloudTenant).include?(item.source_type).each ...

ems = item.destination_type.constantize.find(item.destination_id).ext_management_system
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can just be item.destination.ext_management_system, :destination is polymorphic so rails basically does _type.find(_id) for you

conversion_hosts = ConversionHost.all.select { |ch| ch.ext_management_system == ems }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm this is unfortunate, something like ConversionHost.select { |ch| ch.ext_management_system == ems } would be nicer but we should really add an ems_id to ConversionHost if we need to do this sort of thing so we don't need to load every conversion host.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it'd be better to add associations to ext_management_system like:

  has_many :host_conversion_hosts, :through => :hosts, :source => :conversion_host
  has_many :vm_conversion_hosts, :through => :vms, :source => :conversion_host

  def conversion_hosts
    host_conversion_hosts + vm_conversion_hosts
  end

return false if conversion_hosts.empty?
end
true
end

def validate_vm(_vm_id)
# TODO: enhance the logic to determine whether this VM can be included in this request
true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,32 @@
ServiceResource.new(:resource => vm, :status => status)
end
end

let(:dst_ems) { FactoryGirl.create(:ext_management_system) }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason not to move these lets to be inside Line57?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

let(:src_cluster) { FactoryGirl.create(:ems_cluster) }
let(:dst_cluster) { FactoryGirl.create(:ems_cluster, :ext_management_system => dst_ems) }

let(:mapping) do
FactoryGirl.create(
:transformation_mapping,
:transformation_mapping_items => [TransformationMappingItem.new(:source => src_cluster, :destination => dst_cluster)]
)
end

let(:catalog_item_options) do
{
:name => 'Transformation Plan',
:description => 'a description',
:config_info => {
:transformation_mapping_id => mapping.id,
:actions => [
{:vm_id => vms.first.id.to_s, :pre_service => false, :post_service => false},
{:vm_id => vms.last.id.to_s, :pre_service => false, :post_service => false},
],
}
}
end

let(:plan) { FactoryGirl.create(:service_template_transformation_plan, :service_resources => vm_requests) }
let(:request) { FactoryGirl.create(:service_template_transformation_plan_request, :source => plan) }

Expand All @@ -28,6 +54,25 @@
end
end

describe '#validate_conversion_hosts' do
let(:plan) { ServiceTemplateTransformationPlan.create_catalog_item(catalog_item_options) }
let(:request) { FactoryGirl.create(:service_template_transformation_plan_request, :source => plan) }

context 'no conversion host exists in EMS' do
let(:host) { FactoryGirl.create(:host, :ext_management_system => FactoryGirl.create(:ext_management_system, :zone => FactoryGirl.create(:zone))) }
let(:conversion_host) { FactoryGirl.create(:conversion_host, :resource => host) }

it { expect(request.validate_conversion_hosts).to eq(false) }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use be false instead of eq(false)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

end

context 'conversion host exists in EMS' do
let(:host) { FactoryGirl.create(:host, :ext_management_system => dst_ems) }
let(:conversion_host) { FactoryGirl.create(:conversion_host, :resource => host) }

it { expect(request.validate_conversion_hosts).to eq(false) }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

be false

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

end
end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a test for using vm as conversion host?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.


describe '#validate_vm' do
it { expect(request.validate_vm(vms[0].id)).to be_truthy }
end
Expand Down