diff --git a/app/models/manageiq/providers/redhat/infra_manager/provision/disk.rb b/app/models/manageiq/providers/redhat/infra_manager/provision/disk.rb index b82efb590..b3aed4251 100644 --- a/app/models/manageiq/providers/redhat/infra_manager/provision/disk.rb +++ b/app/models/manageiq/providers/redhat/infra_manager/provision/disk.rb @@ -36,14 +36,7 @@ def prepare_disks_for_add(disks_spec) end def prepare_disk_for_add(disk_spec) - storage_name = disk_spec[:datastore] - raise MiqException::MiqProvisionError, "Storage is required for disk: <#{disk_spec.inspect}>" if storage_name.blank? - - storage = Storage.find_by(:name => storage_name) - if storage.nil? - raise MiqException::MiqProvisionError, "Unable to find storage: <#{storage_name}> for disk: <#{disk_spec.inspect}>" - end - + storage = find_storage!(disk_spec) da_options = { :size_in_mb => disk_spec[:sizeInMB], :storage => storage, @@ -56,4 +49,22 @@ def prepare_disk_for_add(disk_spec) disk_attachment_builder = ManageIQ::Providers::Redhat::InfraManager::DiskAttachmentBuilder.new(da_options) disk_attachment_builder.disk_attachment end + + def find_storage!(disk_spec) + storage_name = disk_spec[:datastore] + if storage_name.blank? + raise MiqException::MiqProvisionError, "Storage is required for disk: <#{disk_spec.inspect}>" + end + + storage = ext_management_system.hosts.collect do |h| + h.writable_storages.find_by(:name => storage_name) + end.uniq.compact.first + + if storage.nil? + error = "Unable to find storage: <#{storage_name}> for disk: <#{disk_spec.inspect}>" + raise MiqException::MiqProvisionError, error + end + + storage + end end diff --git a/spec/models/manageiq/providers/redhat/infra_manager/provision/disk_spec.rb b/spec/models/manageiq/providers/redhat/infra_manager/provision/disk_spec.rb index fec808b18..0878251f2 100644 --- a/spec/models/manageiq/providers/redhat/infra_manager/provision/disk_spec.rb +++ b/spec/models/manageiq/providers/redhat/infra_manager/provision/disk_spec.rb @@ -57,6 +57,7 @@ context "#configure_dialog_disks" do it "adds disks spec as specified in the request" do + allow(@task).to receive(:find_storage!).and_return(storage) @task.configure_dialog_disks expect(@task.options[:disks_add]).to eq(expected_requested_disks) @@ -96,6 +97,7 @@ context "#configure_disks" do it "adds disks as specified in the request" do + allow(@task).to receive(:find_storage!).and_return(storage) expect(@task).to receive(:add_disks).with(expected_requested_disks).once expect(@task).to receive(:poll_add_disks_complete).once @@ -164,4 +166,40 @@ @task.poll_add_disks_complete end end + + context "#find_storage!" do + let(:storage_name) { "test_storage" } + let(:storages) { double("storages") } + let(:host) { double("host", :writable_storages => storages) } + let(:hosts) { [host] } + + before do + allow(ems).to receive(:hosts).and_return(hosts) + end + + context "when storage exists" do + let(:storage) { FactoryGirl.create(:storage_nfs, :name => storage_name) } + + # storage = ext_management_system.hosts.detect { |h| h.writable_storages.find_by(:name => storage_name) } + it "finds a storage by its name" do + allow(storages).to receive(:find_by).with(:name => storage_name).and_return(storage) + disk_spec = { :datastore => storage_name } + + expect(@task.send(:find_storage!, disk_spec)).to eq(storage) + end + end + + context "when storage does not exist" do + it "raises an exception when doesn't find storage by its name" do + allow(storages).to receive(:find_by).with(:name => storage_name).and_return(nil) + disk_spec = { :datastore => storage_name } + + expect { @task.send(:find_storage!, disk_spec) }.to raise_error(MiqException::MiqProvisionError) + end + + it "raises an exception when doesn't find storage without specifying name" do + expect { @task.send(:find_storage!, {}) }.to raise_error(MiqException::MiqProvisionError) + end + end + end end diff --git a/spec/models/manageiq/providers/redhat/infra_manager/provision/state_machine_spec.rb b/spec/models/manageiq/providers/redhat/infra_manager/provision/state_machine_spec.rb index 705596ae5..020ea31de 100644 --- a/spec/models/manageiq/providers/redhat/infra_manager/provision/state_machine_spec.rb +++ b/spec/models/manageiq/providers/redhat/infra_manager/provision/state_machine_spec.rb @@ -7,17 +7,26 @@ let(:rhevm_vm) { double("RHEVM VM") } let(:task) { request.tap(&:create_request_tasks).miq_request_tasks.first } let(:template) { FactoryGirl.create(:template_redhat, :ext_management_system => ems) } + let(:storages) { double("storages") } + let(:storage_name) { "abc" } + let(:host) { double("hostgig", :writable_storages => storages) } + let(:hosts) { [host] } let(:vm) do FactoryGirl.create(:vm_redhat, :ext_management_system => ems, :raw_power_state => "on").tap do |v| allow(v).to receive(:with_provider_object).and_yield(rhevm_vm) allow(ems).to receive(:with_disk_attachments_service).with(v).and_return(disk_attachments_service) allow(ems).to receive(:with_provider_connection).and_return(false) + allow(ems).to receive(:storages).and_return(storages) + allow(storages).to receive(:find_by).with(:name => storage_name).and_return(storage) + allow(ems).to receive(:hosts).and_return(hosts) # TODO: (inventory) write for v4 allow(ems).to receive(:supported_api_versions).and_return([3]) end end - let(:storage) { FactoryGirl.create(:storage_nfs, :ems_ref => "http://example.com/storages/XYZ") } + let(:storage) do + FactoryGirl.create(:storage_nfs, :ems_ref => "http://example.com/storages/XYZ", :name => storage_name) + end let(:options) do {