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

Reconfigure VM: Add / Remove Network Adapters #163

Merged
merged 5 commits into from
Feb 19, 2018
Merged
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
1 change: 1 addition & 0 deletions app/models/manageiq/providers/vmware/infra_manager/vm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class ManageIQ::Providers::Vmware::InfraManager::Vm < ManageIQ::Providers::Infra
end

supports :reconfigure_disks
supports :reconfigure_network_adapters

supports :reconfigure_disksize

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,15 @@ def build_config_spec(options)
set_spec_option(vmcs, :memoryMB, options[:vm_memory], :to_i)
set_spec_option(vmcs, :numCPUs, options[:number_of_cpus], :to_i)

if options[:disk_remove] || options[:disk_add] || options[:disk_resize]
if options[:disk_remove] || options[:disk_add] || options[:disk_resize] || options[:network_adapter_add] || options[:network_adapter_remove]
with_provider_object do |vim_obj|
hardware = vim_obj.getHardware

remove_disks(vim_obj, vmcs, hardware, options[:disk_remove]) if options[:disk_remove]
resize_disks(vim_obj, vmcs, hardware, options[:disk_resize]) if options[:disk_resize]
add_disks(vim_obj, vmcs, hardware, options[:disk_add]) if options[:disk_add]
remove_network_adapters(vim_obj, vmcs, options[:network_adapter_remove]) if options[:network_adapter_remove]
add_network_adapters(vmcs, options[:network_adapter_add]) if options[:network_adapter_add]
end
end
end
Expand Down Expand Up @@ -149,6 +151,18 @@ def add_disks(vim_obj, vmcs, hardware, disks)
end
end

def remove_network_adapters(vim_obj, vmcs, network_adapters)
network_adapters.each do |n|
remove_network_adapter_config_spec(vim_obj, vmcs, n)
end
end

def add_network_adapters(vmcs, network_adapters)
network_adapters.each do |n|
add_network_adapter_config_spec(vmcs, n)
end
end

def scsi_controller_units(controller_key)
[*0..6, *8..15].each.collect do |unit_number|
[controller_key, unit_number]
Expand Down Expand Up @@ -246,6 +260,36 @@ def add_disk_config_spec(vmcs, options)
end
end

def add_network_adapter_config_spec(vmcs, options)
add_device_config_spec(vmcs, VirtualDeviceConfigSpecOperation::Add) do |vdcs|
vdcs.device = VimHash.new("VirtualVmxnet3") do |dev|
dev.key = next_device_idx # negative integer as temporary key
dev.unitNumber = 0
dev.addressType = "Generated"
dev.wakeOnLanEnabled = "true"
dev.connectable = VimHash.new("VirtualDeviceConnectInfo") do |con|
con.allowGuestControl = "true"
con.connected = "true"
con.startConnected = "true"
end
lan = Lan.find_by(:name => options[:network], :switch_id => HostSwitch.where(:host_id => host.id).pluck(:switch_id))
raise MiqException::MiqVmError, "Network [#{options[:network]}] is not available on target" if lan.nil?
dev.backing = if lan.switch.shared
VimHash.new("VirtualEthernetCardDistributedVirtualPortBackingInfo") do |bck|
bck.port = VimHash.new("DistributedVirtualSwitchPortConnection") do |pc|
pc.switchUuid = lan.switch.switch_uuid
pc.portgroupKey = lan.uid_ems
end
end
else
VimHash.new('VirtualEthernetCardNetworkBackingInfo') do |bck|
bck.deviceName = options[:network]
end
end
end
end
end

def remove_disk_config_spec(vim_obj, vmcs, hardware, options)
raise "remove_disk_config_spec: disk filename is required." unless options[:disk_name]

Expand Down Expand Up @@ -288,6 +332,19 @@ def resize_disk_config_spec(vim_obj, vmcs, hardware, options)
end
end

def remove_network_adapter_config_spec(vim_obj, vmcs, options)
raise "remove_network_adapter_config_spec: network_adapter name is required." unless options[:network][:name]
network_adapter_label = options[:network][:name]
controller_key, key, unit_number = vim_obj.send(:getDeviceKeysByLabel, network_adapter_label)
add_device_config_spec(vmcs, VirtualDeviceConfigSpecOperation::Remove) do |vdcs|
vdcs.device = VimHash.new("VirtualEthernetCard") do |dev|
dev.key = key
dev.controllerKey = controller_key
dev.unitNumber = unit_number
end
end
end

def add_device_config_spec(vmcs, operation)
vmcs_vca = vmcs.deviceChange ||= VimArray.new('ArrayOfVirtualDeviceConfigSpec')
vmcs_vca << VimHash.new('VirtualDeviceConfigSpec') do |vdcs|
Expand All @@ -313,4 +370,9 @@ def set_spec_option(obj, property, value, modifier = nil)
end
end
end

def next_device_idx
@new_device_idx ||= -100
@new_device_idx -= 1
end
end