Skip to content

Commit

Permalink
Add disk relocate spec start
Browse files Browse the repository at this point in the history
  • Loading branch information
d-m-u committed Aug 1, 2019
1 parent c259580 commit d36dbe9
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ def prepare_for_clone_task
:pool => dest_resource_pool,
:storage_profile => dest_storage_profile,
:config => build_config_spec,
:customization => build_customization_spec,
:transform => build_transform_spec
:customization => build_customization_spec
}

# Determine if we are doing a linked-clone provision
Expand Down Expand Up @@ -120,7 +119,7 @@ def start_clone(clone_options)
:template => self.create_template?
}

[:transform, :config, :customization, :linked_clone].each { |key| vim_clone_options[key] = clone_options[key] }
[:config, :customization, :linked_clone].each { |key| vim_clone_options[key] = clone_options[key] }

[:folder, :host, :pool, :snapshot].each do |key|
ci = clone_options[key]
Expand All @@ -129,6 +128,7 @@ def start_clone(clone_options)
end

vim_clone_options[:datastore] = datastore_ems_ref(clone_options)
vim_clone_options[:disk] = build_disk_relocate_spec(vim_clone_options[:datastore])
vim_clone_options[:storage_profile] = build_storage_profile(clone_options[:storage_profile]) unless clone_options[:storage_profile].nil?

task_mor = clone_vm(vim_clone_options)
Expand Down Expand Up @@ -192,13 +192,6 @@ def get_selected_snapshot
ss
end

def build_transform_spec
case get_option(:disk_format)
when 'thin' then VimString.new('sparse', "VirtualMachineRelocateTransformation")
when 'thick' then VimString.new('flat', "VirtualMachineRelocateTransformation")
end
end

def build_storage_profile(storage_profile)
VimArray.new('ArrayOfVirtualMachineProfileSpec') do |vm_profile_spec_array|
vm_profile_spec_array << VimHash.new('VirtualMachineDefinedProfileSpec') do |vm_profile_spec|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,38 @@ def build_config_disk_spec(vmcs)
end
end

def get_scsi_controller_info
inventory_hash = source.with_provider_connection do |vim|
vim.virtualMachineByMor(source.ems_ref_obj)
def build_disk_relocate_spec(datastore)
VimArray.new('ArrayOfVirtualMachineRelocateSpecDiskLocator') do |relocate_spec_array|
(disks + get_new_disks).map do |disk|
# new disks aren't being taken into account here ... yet
relocate_spec_array << VimHash.new('VirtualMachineRelocateSpecDiskLocator') do |disk_locator|
disk_locator.diskId = disk.key
disk_locator.datastore = datastore
disk_locator.diskBackingInfo = VimHash.new("VirtualDiskFlatVer2BackingInfo") do |bck|
bck.fileName = datastore
bck.diskMode = "persistent"
case get_option(:disk_format)
when 'thin'
bck.thinProvisioned = "true"
when 'thick'
bck.thinProvisioned = "false"
bck.eagerlyScrub = "false"
when 'thick_eager'
bck.eagerlyScrub = "true"
bck.thinProvisioned = "false"
end
end
end
end
end
end

def disks
devices.select { |d| d.xsiType == "VirtualDisk" }
end

devs = inventory_hash.fetch_path("config", "hardware", "device") || []
devs.each_with_object({}) do |dev, h|
def get_scsi_controller_info
devices.each_with_object({}) do |dev, h|
next unless dev.fetch_path("deviceInfo", "label").to_s =~ /^SCSI\s[Cc]ontroller\s.*$/
h[dev['busNumber'].to_i] = dev
end
Expand Down Expand Up @@ -79,4 +104,14 @@ def add_disk(vmcs, disk, controller, new_dev_key)
end
end
end

private

def devices
inventory_hash = source.with_provider_connection do |vim|
vim.virtualMachineByMor(source.ems_ref_obj)
end

@devices ||= inventory_hash.fetch_path("config", "hardware", "device") || []
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
end

context "VMware provisioning" do
let(:device_list) { [VimHash.new("VirtualDisk") { |d| d.key = "2000" }, VimHash.new("NotAVirtualDisk") { |d| d.key = "2000" }] }
before(:each) do
@ems = FactoryBot.create(:ems_vmware_with_authentication, :api_version => '6.0')
@vm_template = FactoryBot.create(:template_vmware, :name => "template1", :ext_management_system => @ems, :operating_system => @os, :cpu_limit => -1, :cpu_reserve => 0)
Expand Down Expand Up @@ -44,13 +45,20 @@
expect(spec["annotation"]).to include(@vm_prov.phase_context[:new_vm_validation_guid])
end

it "should return a transform spec" do
spec = @vm_prov.build_transform_spec
expect(spec).to be_nil
it "should return a disk_relocate spec" do
expect(@vm_prov).to receive(:disks).and_return(device_list)
@vm_prov.options[:disk_format] = 'thin'
spec = @vm_prov.build_transform_spec
expect(spec).to be_kind_of(VimString)
expect(spec.vimType).to eq('VirtualMachineRelocateTransformation')
spec = @vm_prov.build_disk_relocate_spec('datastore-1729')
expect(spec).to be_kind_of(VimArray)
expect(spec.first.diskBackingInfo.thinProvisioned).to eq("true")
end

it "should return a disk_relocate spec" do
expect(@vm_prov).to receive(:disks).and_return(device_list)
@vm_prov.options[:disk_format] = 'thick'
spec = @vm_prov.build_disk_relocate_spec('datastore-1729')
expect(spec).to be_kind_of(VimArray)
expect(spec.first.diskBackingInfo.thinProvisioned).to eq("false")
end

it "should detect when a reconfigure_hardware_on_destination call is required" do
Expand Down

0 comments on commit d36dbe9

Please sign in to comment.