1
|
|
|
import sys |
2
|
|
|
|
3
|
|
|
from pyVmomi import vim |
4
|
|
|
|
5
|
|
|
from vmwarelib import inventory |
6
|
|
|
from vmwarelib.actions import BaseAction |
7
|
|
|
|
8
|
|
|
|
9
|
|
|
class NewNetworkAdapter(BaseAction): |
10
|
|
|
|
11
|
|
|
@staticmethod |
12
|
|
|
def get_vm_reconfig_spec(distributed_switch_obj, mac_address, network_obj, port_key, stay_connected, network_type, wake_on_lan): |
13
|
|
|
# creating virtual device |
14
|
|
|
if network_type == 'e1000': |
15
|
|
|
virtual_network = vim.vm.device.VirtualE1000() |
16
|
|
|
elif network_type == 'flexible': |
17
|
|
|
virtual_network = vim.vm.device.VirtualPCNet32() |
18
|
|
|
lance_option = vim.vm.device.VirtualPCNet32Option() |
19
|
|
|
lance_option.supportsMorphing = True; |
20
|
|
|
#TODO and question for Manas: We need to plug in that lance_option on something. I could not find any class that uses VirtualPCNet32Option or any of its upstream classes. |
21
|
|
|
elif network_type == 'vmxnet': |
22
|
|
|
virtual_network = vim.vm.device.VirtualVmxnet() |
23
|
|
|
elif network_type == 'enhancedvmxnet': |
24
|
|
|
virtual_network = vim.vm.device.VirtualVmxnet2() |
25
|
|
|
elif network_type == 'vmxnet3': |
26
|
|
|
virtual_network = vim.vm.device.VirtualVmxnet3() |
27
|
|
|
else: |
28
|
|
|
virtual_network = vim.vm.device.VirtualEthernetCard() |
29
|
|
|
|
30
|
|
|
virtual_network.wakeOnLanEnabled = wake_on_lan |
31
|
|
|
connect_info = vim.vm.device.VirtualDevice.ConnectInfo() |
32
|
|
|
connect_info.startConnected = stay_connected |
33
|
|
|
virtual_network.connectable = connect_info |
34
|
|
|
if mac_address: |
35
|
|
|
virtual_network.macAddress = mac_address |
36
|
|
|
virtual_network.addressType = vim.vm.device.VirtualEthernetCardOption.MacTypes.manual |
37
|
|
|
|
38
|
|
|
# creating backing info |
39
|
|
|
backing_info = None |
40
|
|
|
if distributed_switch_obj: |
41
|
|
|
dvs_port_connection = vim.dvs.PortConnection() |
42
|
|
|
dvs_port_connection.switchUuid = distributed_switch_obj.uuid |
43
|
|
|
if port_key: |
44
|
|
|
dvs_port_connection.portKey = port_key |
45
|
|
|
port_criteria = vim.dvs.PortCriteria() |
46
|
|
|
port_criteria.portKey = port_key |
47
|
|
|
ports = distributed_switch_obj.FetchDVPorts(port_criteria) |
48
|
|
|
dvs_port_connection.portgroupKey = ports[0].portgroupKey |
49
|
|
|
backing_info = vim.vm.device.VirtualEthernetCard.DistributedVirtualPortBackingInfo() |
50
|
|
|
backing_info.port = dvs_port_connection |
51
|
|
|
|
52
|
|
|
if network_obj: |
53
|
|
|
backing_info = vim.vm.device.VirtualEthernetCard.NetworkBackingInfo() |
54
|
|
|
backing_info.network = network_obj |
55
|
|
|
backing_info.deviceName = network_obj.name |
56
|
|
|
|
57
|
|
|
virtual_network.backing = backing_info |
58
|
|
|
|
59
|
|
|
#creating network device spec |
60
|
|
|
network_spec = vim.vm.device.VirtualDeviceSpec() |
61
|
|
|
network_spec.device = virtual_network |
62
|
|
|
network_spec.operation = vim.vm.device.VirtualDeviceSpec.Operation.add |
63
|
|
|
|
64
|
|
|
#creating reconfig spec |
65
|
|
|
vm_reconfig_spec = vim.vm.ConfigSpec() |
66
|
|
|
vm_reconfig_spec.deviceChange = [network_spec] |
67
|
|
|
return vm_reconfig_spec |
68
|
|
|
|
69
|
|
|
def run(self, vms, distributed_switch=None, mac_address=None, network_name=None, port_key=None, |
70
|
|
|
stay_connected=False, network_type='Flexible', wake_on_lan=False): |
71
|
|
|
network_type = network_type.lower() if network_type else None |
72
|
|
|
si = self.si |
73
|
|
|
si_content = si.RetrieveContent() |
74
|
|
|
|
75
|
|
|
vm_objs = [vim.VirtualMachine(moid, stub=si._stub) for moid in vms] |
76
|
|
|
vm_names = [vm_obj.name for vm_obj in vm_objs] # by checking the name property, the vms' existance is checked. |
77
|
|
|
|
78
|
|
|
distributed_switch_obj = None |
79
|
|
|
if distributed_switch: |
80
|
|
|
distributed_switch_obj = vim.DistributedVirtualSwitch(distributed_switch, stub=si._stub) |
81
|
|
|
distributed_switch_obj.name# by checking the name property, the distributed switch existence is checked. |
82
|
|
|
|
83
|
|
|
network_obj = None |
84
|
|
|
if network_name: |
85
|
|
|
network_obj = inventory.get_network(si_content, name=network_name) |
86
|
|
|
network_obj.name # by checking the name property, the network existence is checked. |
87
|
|
|
|
88
|
|
|
result=[] |
89
|
|
|
for vm in vm_objs: |
90
|
|
|
vm_reconfig_spec = NewNetworkAdapter.get_vm_reconfig_spec(distributed_switch_obj, mac_address, network_obj, port_key, stay_connected, network_type, wake_on_lan) |
91
|
|
|
add_disk_task = vm.ReconfigVM_Task(spec=vm_reconfig_spec) |
92
|
|
|
successfully_added_network = self._wait_for_task(add_disk_task) |
93
|
|
|
result.append({ |
94
|
|
|
"vm_moid":vm._GetMoId(), |
95
|
|
|
"success":successfully_added_network |
96
|
|
|
}) |
97
|
|
|
|
98
|
|
|
return result |
99
|
|
|
|