1
|
|
|
from pyVmomi import vim |
2
|
|
|
|
3
|
|
|
from vmwarelib import inventory |
4
|
|
|
from vmwarelib import checkinputs |
5
|
|
|
from vmwarelib.actions import BaseAction |
6
|
|
|
|
7
|
|
|
|
8
|
|
|
class VMAddNic(BaseAction): |
9
|
|
|
|
10
|
|
|
def run(self, vm_id, vm_name, network_name, |
11
|
|
|
nictype, stayconnected, wakeonlan): |
12
|
|
|
# create object itmes of key components |
13
|
|
|
checkinputs.vm_identifier(vm_id, vm_name) |
14
|
|
|
|
15
|
|
|
vm = inventory.get_virtualmachine(self.si_content, vm_id, vm_name) |
16
|
|
|
network_obj = inventory.get_network(self.si_content, name=network_name) |
17
|
|
|
|
18
|
|
|
vm_reconfig_spec = self.get_vm_reconfig_spec(network_obj, |
19
|
|
|
stayconnected, |
20
|
|
|
nictype, |
21
|
|
|
wakeonlan) |
22
|
|
|
|
23
|
|
|
add_vnic_task = vm.ReconfigVM_Task(spec=vm_reconfig_spec) |
24
|
|
|
successfully_added_vnic = self._wait_for_task(add_vnic_task) |
25
|
|
|
|
26
|
|
|
return {'state': successfully_added_vnic} |
27
|
|
|
|
28
|
|
|
def get_vm_reconfig_spec(self, network_obj, |
29
|
|
|
stay_connected, network_type, wake_on_lan): |
30
|
|
|
network_spec = vim.vm.device.VirtualDeviceSpec() |
31
|
|
|
network_spec.operation = vim.vm.device.VirtualDeviceSpec.Operation.add |
32
|
|
|
|
33
|
|
|
if network_type.lower() == 'e1000': |
34
|
|
|
network_spec.device = vim.vm.device.VirtualE1000() |
35
|
|
|
elif network_type.lower() == 'flexible': |
36
|
|
|
network_spec.device = vim.vm.device.VirtualPCNet32() |
37
|
|
|
elif network_type.lower() == 'vmxnet': |
38
|
|
|
network_spec.device = vim.vm.device.VirtualVmxnet() |
39
|
|
|
elif network_type.lower() == 'enhancedvmxnet': |
40
|
|
|
network_spec.device = vim.vm.device.VirtualVmxnet2() |
41
|
|
|
elif network_type.lower() == 'vmxnet3': |
42
|
|
|
network_spec.device = vim.vm.device.VirtualVmxnet3() |
43
|
|
|
else: |
44
|
|
|
network_spec.device = vim.vm.device.VirtualEthernetCard() |
45
|
|
|
|
46
|
|
|
network_spec.device.wakeOnLanEnabled = wake_on_lan |
47
|
|
|
network_spec.device.deviceInfo = vim.Description() |
48
|
|
|
network_spec.device.backing = \ |
49
|
|
|
vim.vm.device.VirtualEthernetCard.NetworkBackingInfo() |
50
|
|
|
network_spec.device.backing.network = network_obj |
51
|
|
|
network_spec.device.backing.deviceName = network_obj.name |
52
|
|
|
|
53
|
|
|
network_spec.device.connectable = \ |
54
|
|
|
vim.vm.device.VirtualDevice.ConnectInfo() |
55
|
|
|
network_spec.device.connectable.startConnected = stay_connected |
56
|
|
|
network_spec.device.connectable.allowGuestControl = True |
57
|
|
|
|
58
|
|
|
#creating reconfig spec |
59
|
|
|
vm_reconfig_spec = vim.vm.ConfigSpec() |
60
|
|
|
vm_reconfig_spec.deviceChange = [network_spec] |
61
|
|
|
return vm_reconfig_spec |
62
|
|
|
|