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

Allow to receive datastore by name or by id #37

Closed
Closed
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -56,4 +49,20 @@ 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_id = disk_spec[:datastore_id]
storage_name = disk_spec[:datastore]
if storage_name.blank? && storage_id.blank?
raise MiqException::MiqProvisionError, "Storage is required for disk: <#{disk_spec.inspect}>"
end

storage = Storage.search_by_id_or_name(storage_id, storage_name)
if storage.nil?
error = "Unable to find storage: <#{storage_id || storage_name}> for disk: <#{disk_spec.inspect}>"
raise MiqException::MiqProvisionError, error
end

storage
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -164,4 +164,61 @@
@task.poll_add_disks_complete
end
end

context "#find_storage!" do
let(:storage_name) { "test_storage" }
let(:storage_id) { 123 }

context "when storage exists" do
let(:storage) { FactoryGirl.create(:storage, :name => storage_name, :id => storage_id) }

it "finds a storage by its ID" do
disk_spec = { :datastore_id => storage_id }
allow(Storage).to receive(:search_by_id_or_name).with(storage_id, nil).and_return(storage)

expect(@task.send(:find_storage!, disk_spec)).to eq(storage)
end

it "finds a storage by its name" do
disk_spec = { :datastore => storage_name }
allow(Storage).to receive(:search_by_id_or_name).with(nil, storage_name).and_return(storage)

expect(@task.send(:find_storage!, disk_spec)).to eq(storage)
end

it "finds a storage both by its id and name" do
disk_spec = { :datastore_id => storage_id, :datastore => storage_name }
allow(Storage).to receive(:search_by_id_or_name).with(storage_id, storage_name).and_return(storage)

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 ID" do
disk_spec = { :datastore_id => 123 }
allow(Storage).to receive(:search_by_id_or_name).with(storage_id, nil).and_return(nil)

expect { @task.send(:find_storage!, disk_spec) }.to raise_error(MiqException::MiqProvisionError)
end

it "raises an exception when doesn't find storage by its name" do
disk_spec = { :datastore => storage_name }
allow(Storage).to receive(:search_by_id_or_name).with(nil, storage_name).and_return(nil)

expect { @task.send(:find_storage!, disk_spec) }.to raise_error(MiqException::MiqProvisionError)
end

it "raises an exception when doesn't find storage both by its id and name" do
disk_spec = { :datastore_id => storage_id, :datastore => storage_name }
allow(Storage).to receive(:search_by_id_or_name).with(storage_id, storage_name).and_return(nil)

expect { @task.send(:find_storage!, disk_spec) }.to raise_error(MiqException::MiqProvisionError)
end

it "raises an exception when doesn't find storage without specifying id or name" do
expect { @task.send(:find_storage!, {}) }.to raise_error(MiqException::MiqProvisionError)
end
end
end
end