From 92ed48acef771074e05c7e14c8c53a4d7401599e Mon Sep 17 00:00:00 2001 From: Moti Asayag Date: Mon, 14 Aug 2017 15:39:36 +0300 Subject: [PATCH] Fail with descriptive message when no EMS When no EMS is associated with a VM or template, the provision request fails with a non-descriptive message. Such case might happen when the VM or template were removed from the provider side, but still exist as archived on vmdb. The purpose of the PR is to provide a clear failure message when there is no EMS. https://bugzilla.redhat.com/show_bug.cgi?id=1471124 --- app/models/miq_provision_request.rb | 15 +++++++- spec/models/miq_provision_request_spec.rb | 45 ++++++++++++++--------- 2 files changed, 41 insertions(+), 19 deletions(-) diff --git a/app/models/miq_provision_request.rb b/app/models/miq_provision_request.rb index 1e48cee03e6..ee913aa5ccb 100644 --- a/app/models/miq_provision_request.rb +++ b/app/models/miq_provision_request.rb @@ -26,13 +26,24 @@ class MiqProvisionRequest < MiqRequest def self.request_task_class_from(attribs) source_id = MiqRequestMixin.get_option(:src_vm_id, nil, attribs['options']) - vm_or_template = VmOrTemplate.find_by(:id => source_id) - raise MiqException::MiqProvisionError, "Unable to find source Template/Vm with id [#{source_id}]" if vm_or_template.nil? + vm_or_template = source_vm_or_template!(source_id) via = MiqRequestMixin.get_option(:provision_type, nil, attribs['options']) vm_or_template.ext_management_system.class.provision_class(via) end + def self.source_vm_or_template!(source_id) + vm_or_template = VmOrTemplate.find_by(:id => source_id) + if vm_or_template.nil? + raise MiqException::MiqProvisionError, "Unable to find source Template/Vm with id [#{source_id}]" + end + + if vm_or_template.ext_management_system.nil? + raise MiqException::MiqProvisionError, "Source Template/Vm with id [#{source_id}] has no EMS, unable to provision" + end + vm_or_template + end + def self.new_request_task(attribs) klass = request_task_class_from(attribs) klass.new(attribs) diff --git a/spec/models/miq_provision_request_spec.rb b/spec/models/miq_provision_request_spec.rb index 8f8c83050fe..cc2e8de3bca 100644 --- a/spec/models/miq_provision_request_spec.rb +++ b/spec/models/miq_provision_request_spec.rb @@ -1,21 +1,32 @@ describe MiqProvisionRequest do - it ".request_task_class_from" do - ems = FactoryGirl.create(:ems_vmware) - vm = FactoryGirl.create(:vm_vmware, :ext_management_system => ems) - expect(described_class.request_task_class_from('options' => {:src_vm_id => vm.id})).to eq ManageIQ::Providers::Vmware::InfraManager::Provision - expect(described_class.request_task_class_from('options' => {:src_vm_id => vm.id, :provision_type => "pxe"})).to eq ManageIQ::Providers::Vmware::InfraManager::ProvisionViaPxe - - ems = FactoryGirl.create(:ems_redhat) - vm = FactoryGirl.create(:vm_redhat, :ext_management_system => ems) - expect(described_class.request_task_class_from('options' => {:src_vm_id => vm.id})).to eq ManageIQ::Providers::Redhat::InfraManager::Provision - - ems = FactoryGirl.create(:ems_openstack) - vm = FactoryGirl.create(:vm_openstack, :ext_management_system => ems) - expect(described_class.request_task_class_from('options' => {:src_vm_id => vm.id})).to eq ManageIQ::Providers::Openstack::CloudManager::Provision - - ems = FactoryGirl.create(:ems_amazon) - vm = FactoryGirl.create(:vm_amazon, :ext_management_system => ems) - expect(described_class.request_task_class_from('options' => {:src_vm_id => vm.id})).to eq ManageIQ::Providers::Amazon::CloudManager::Provision + context "#request_task_class_from" do + it "retrieves the provision class when the vm has EMS" do + ems = FactoryGirl.create(:ems_vmware) + vm = FactoryGirl.create(:vm_vmware, :ext_management_system => ems) + expect(described_class.request_task_class_from('options' => {:src_vm_id => vm.id})).to eq ManageIQ::Providers::Vmware::InfraManager::Provision + expect(described_class.request_task_class_from('options' => {:src_vm_id => vm.id, :provision_type => "pxe"})).to eq ManageIQ::Providers::Vmware::InfraManager::ProvisionViaPxe + + ems = FactoryGirl.create(:ems_redhat) + vm = FactoryGirl.create(:vm_redhat, :ext_management_system => ems) + expect(described_class.request_task_class_from('options' => {:src_vm_id => vm.id})).to eq ManageIQ::Providers::Redhat::InfraManager::Provision + + ems = FactoryGirl.create(:ems_openstack) + vm = FactoryGirl.create(:vm_openstack, :ext_management_system => ems) + expect(described_class.request_task_class_from('options' => {:src_vm_id => vm.id})).to eq ManageIQ::Providers::Openstack::CloudManager::Provision + + ems = FactoryGirl.create(:ems_amazon) + vm = FactoryGirl.create(:vm_amazon, :ext_management_system => ems) + expect(described_class.request_task_class_from('options' => {:src_vm_id => vm.id})).to eq ManageIQ::Providers::Amazon::CloudManager::Provision + end + + it "fails to retrieve the provision class when the vm has no EMS" do + vm = FactoryGirl.create(:vm_redhat) + expect { described_class.request_task_class_from('options' => {:src_vm_id => vm.id}) }.to raise_error(MiqException::MiqProvisionError) + end + + it "fails to retrieve the provision class when the vm does not exist" do + expect { described_class.request_task_class_from('options' => {:src_vm_id => -1 }) }.to raise_error(MiqException::MiqProvisionError) + end end context "A new provision request," do