-
Notifications
You must be signed in to change notification settings - Fork 900
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13318 from masayag/add_disks_via_restapi_provisio…
…n_vm_for_rhevm Allow adding disks to vm provision via api and automation (cherry picked from commit b1ded2f) https://bugzilla.redhat.com/show_bug.cgi?id=1414893
- Loading branch information
1 parent
9d876b9
commit 4932243
Showing
14 changed files
with
524 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
app/models/manageiq/providers/redhat/infra_manager/disk_attachment_builder.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
class ManageIQ::Providers::Redhat::InfraManager::DiskAttachmentBuilder | ||
def initialize(options = {}) | ||
@size_in_mb = options[:size_in_mb] | ||
@storage = options[:storage] | ||
@name = options[:name] | ||
@thin_provisioned = BooleanParameter.new(options[:thin_provisioned]) | ||
@bootable = BooleanParameter.new(options[:bootable]) | ||
@active = options[:active] | ||
@interface = options[:interface] | ||
end | ||
|
||
def disk_attachment | ||
thin_provisioned = @thin_provisioned.true? | ||
{ | ||
:bootable => @bootable.true?, | ||
:interface => @interface || "VIRTIO", | ||
:active => @active, | ||
:disk => { | ||
:name => @name, | ||
:provisioned_size => @size_in_mb.to_i.megabytes, | ||
:sparse => thin_provisioned, | ||
:format => self.class.disk_format_for(@storage, thin_provisioned), | ||
:storage_domains => [:id => ManageIQ::Providers::Redhat::InfraManager.extract_ems_ref_id(@storage.ems_ref)] | ||
} | ||
} | ||
end | ||
|
||
FILE_STORAGE_TYPE = %w(NFS GLUSTERFS VMFS).to_set.freeze | ||
BLOCK_STORAGE_TYPE = %w(FCP ISCSI).to_set.freeze | ||
|
||
def self.disk_format_for(storage, thin_provisioned) | ||
if FILE_STORAGE_TYPE.include?(storage.store_type) | ||
"raw" | ||
elsif BLOCK_STORAGE_TYPE.include?(storage.store_type) | ||
thin_provisioned ? "cow" : "raw" | ||
else | ||
"raw" | ||
end | ||
end | ||
|
||
class BooleanParameter | ||
def initialize(param) | ||
@value = param.to_s == "true" | ||
end | ||
|
||
def true? | ||
@value | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
app/models/manageiq/providers/redhat/infra_manager/provision/disk.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
module ManageIQ::Providers::Redhat::InfraManager::Provision::Disk | ||
def configure_dialog_disks | ||
added_disks = options[:disk_scsi] | ||
return nil if added_disks.blank? | ||
|
||
options[:disks_add] = prepare_disks_for_add(added_disks) | ||
end | ||
|
||
def add_disks(disks) | ||
destination.ext_management_system.with_disk_attachments_service(destination) do |service| | ||
disks.each { |disk| service.add(disk) } | ||
end | ||
end | ||
|
||
def destination_disks_locked? | ||
destination.ext_management_system.with_provider_connection(:version => 4) do |connection| | ||
system_service = connection.system_service | ||
disks = system_service.vms_service.vm_service(destination.uid_ems).disk_attachments_service.list | ||
disks.each do |disk| | ||
fetched_disk = system_service.disks_service.disk_service(disk.id).get | ||
return true unless fetched_disk.try(:status) == "ok" | ||
end | ||
end | ||
|
||
false | ||
end | ||
|
||
private | ||
|
||
def prepare_disks_for_add(disks_spec) | ||
disks_spec.collect do |disk_spec| | ||
disk = prepare_disk_for_add(disk_spec) | ||
_log.info("disk: #{disk.inspect}") | ||
disk | ||
end.compact | ||
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 | ||
|
||
da_options = { | ||
:size_in_mb => disk_spec[:sizeInMB], | ||
:storage => storage, | ||
:name => disk_spec[:filename], | ||
:thin_provisioned => disk_spec[:backing] && disk_spec[:backing][:thinprovisioned], | ||
:bootable => disk_spec[:bootable], | ||
:interface => disk_spec[:interface] | ||
} | ||
|
||
disk_attachment_builder = ManageIQ::Providers::Redhat::InfraManager::DiskAttachmentBuilder.new(da_options) | ||
disk_attachment_builder.disk_attachment | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 4 additions & 1 deletion
5
...mation_engine/service_models/miq_ae_service_manageiq-providers-redhat-infra_manager-vm.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,7 @@ | ||
module MiqAeMethodService | ||
class MiqAeServiceManageIQ_Providers_Redhat_InfraManager_Vm < MiqAeServiceVmInfra | ||
class MiqAeServiceManageIQ_Providers_Redhat_InfraManager_Vm < MiqAeServiceManageIQ_Providers_InfraManager_Vm | ||
def add_disk(disk_name, disk_size_mb, options = {}) | ||
sync_or_async_ems_operation(options[:sync], "add_disk", [disk_name, disk_size_mb, options]) | ||
end | ||
end | ||
end |
27 changes: 27 additions & 0 deletions
27
..._engine/service_methods/miq_ae_service_manageiq-providers-redhat-infra_manager-vm_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
module MiqAeServiceManageIQ_Providers_Redhat_InfraManager_VmSpec | ||
describe MiqAeMethodService::MiqAeServiceManageIQ_Providers_Redhat_InfraManager_Vm do | ||
let(:vm) { FactoryGirl.create(:vm_redhat) } | ||
let(:service_vm) { MiqAeMethodService::MiqAeServiceManageIQ_Providers_Redhat_InfraManager_Vm.find(vm.id) } | ||
|
||
before do | ||
allow(MiqServer).to receive(:my_zone).and_return('default') | ||
@base_queue_options = { | ||
:class_name => vm.class.name, | ||
:instance_id => vm.id, | ||
:zone => 'default', | ||
:role => 'ems_operations', | ||
:task_id => nil | ||
} | ||
end | ||
|
||
it "#add_disk" do | ||
service_vm.add_disk('disk_1', 100, :interface => "IDE", :bootable => true) | ||
|
||
expect(MiqQueue.first).to have_attributes( | ||
@base_queue_options.merge( | ||
:method_name => 'add_disk', | ||
:args => ['disk_1', 100, {:interface => "IDE", :bootable => true}]) | ||
) | ||
end | ||
end | ||
end |
95 changes: 95 additions & 0 deletions
95
spec/models/manageiq/providers/redhat/infra_manager/disk_attachment_builder_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
describe ManageIQ::Providers::Redhat::InfraManager::DiskAttachmentBuilder do | ||
context "#disk_format_for" do | ||
context "when storage type is file system" do | ||
let(:storage) { FactoryGirl.build(:storage_nfs) } | ||
it "returns 'raw' format for FS storage type" do | ||
expect(described_class.disk_format_for(storage, false)).to eq("raw") | ||
end | ||
|
||
it "returns 'raw' format for thin provisioned" do | ||
expect(described_class.disk_format_for(storage, true)).to eq("raw") | ||
end | ||
end | ||
|
||
context "when storage type is block" do | ||
let(:storage) { FactoryGirl.build(:storage_block) } | ||
|
||
it "returns 'cow' format for block storage type and thin provisioned" do | ||
expect(described_class.disk_format_for(storage, true)).to eq("cow") | ||
end | ||
|
||
it "returns 'raw' format for block storage type and thick provisioned" do | ||
expect(described_class.disk_format_for(storage, false)).to eq("raw") | ||
end | ||
end | ||
|
||
context "when storage type is not file system and not blcok" do | ||
let(:storage) { FactoryGirl.build(:storage_unknown) } | ||
it "returns 'raw' format as default" do | ||
expect(described_class.disk_format_for(storage, false)).to eq("raw") | ||
end | ||
end | ||
end | ||
|
||
context "#disk_attachment" do | ||
let(:storage) { FactoryGirl.build(:storage_nfs, :ems_ref => "http://example.com/storages/XYZ") } | ||
|
||
it "creates disk attachment" do | ||
builder = described_class.new(:size_in_mb => 10, :storage => storage, :name => "disk-1", | ||
:thin_provisioned => true, :bootable => true, :active => false, :interface => "IDE") | ||
expected_disk_attachment = { | ||
:bootable => true, | ||
:interface => "IDE", | ||
:active => false, | ||
:disk => { | ||
:name => "disk-1", | ||
:provisioned_size => 10 * 1024 * 1024, | ||
:sparse => true, | ||
:format => "raw", | ||
:storage_domains => [:id => "XYZ"] | ||
} | ||
} | ||
|
||
expect(builder.disk_attachment).to eq(expected_disk_attachment) | ||
end | ||
end | ||
|
||
describe ManageIQ::Providers::Redhat::InfraManager::DiskAttachmentBuilder::BooleanParameter do | ||
let(:param) { nil } | ||
subject { described_class.new(param).true? } | ||
|
||
context "param is true" do | ||
let(:param) { true } | ||
|
||
it { is_expected.to be_truthy } | ||
end | ||
|
||
context "param is false" do | ||
let(:param) { false } | ||
|
||
it { is_expected.to be_falsey } | ||
end | ||
|
||
context "param is 'true'" do | ||
let(:param) { "true" } | ||
|
||
it { is_expected.to be_truthy } | ||
end | ||
|
||
context "param is 'false'" do | ||
let(:param) { "false" } | ||
|
||
it { is_expected.to be_falsey } | ||
end | ||
|
||
context "param is nil" do | ||
it { is_expected.to be_falsey } | ||
end | ||
|
||
context "param is 'invalid'" do | ||
let(:param) { 'invalid' } | ||
|
||
it { is_expected.to be_falsey } | ||
end | ||
end | ||
end |
Oops, something went wrong.