1
|
|
|
# Licensed to the StackStorm, Inc ('StackStorm') under one or more |
2
|
|
|
# contributor license agreements. See the NOTICE file distributed with |
3
|
|
|
# this work for additional information regarding copyright ownership. |
4
|
|
|
# The ASF licenses this file to You under the Apache License, Version 2.0 |
5
|
|
|
# (the "License"); you may not use this file except in compliance with |
6
|
|
|
# the License. You may obtain a copy of the License at |
7
|
|
|
# |
8
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0 |
9
|
|
|
# |
10
|
|
|
# Unless required by applicable law or agreed to in writing, software |
11
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS, |
12
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13
|
|
|
# See the License for the specific language governing permissions and |
14
|
|
|
# limitations under the License. |
15
|
|
|
|
16
|
|
|
from pyVmomi import vim |
17
|
|
|
|
18
|
|
|
from vmwarelib import inventory |
19
|
|
|
from vmwarelib import checkinputs |
20
|
|
|
from vmwarelib.actions import BaseAction |
21
|
|
|
|
22
|
|
|
|
23
|
|
|
class VMNicEdit(BaseAction): |
24
|
|
|
|
25
|
|
|
def run(self, vm_id, vm_name, network_adapter, network_name, vsphere=None): |
26
|
|
|
""" |
27
|
|
|
Edit Network Adapater on Virtual Machine |
28
|
|
|
|
29
|
|
|
Args: |
30
|
|
|
- vm_id: Moid of Virtual Machine to edit |
31
|
|
|
- vm_name: Name of Virtual Machine to edit |
32
|
|
|
- vsphere: Pre-configured vsphere connection details (config.yaml) |
33
|
|
|
- network_name: Network to attach adapter to |
34
|
|
|
- network_adapter: Name of Adapter to edit |
35
|
|
|
|
36
|
|
|
Returns: |
37
|
|
|
- dict: State true/false |
38
|
|
|
""" |
39
|
|
|
|
40
|
|
|
# check means of finding the VM was provided |
41
|
|
|
checkinputs.one_of_two_strings(vm_id, vm_name, "ID or Name") |
42
|
|
|
|
43
|
|
|
self.establish_connection(vsphere) |
44
|
|
|
|
45
|
|
|
# convert ids to stubs |
46
|
|
|
vm = inventory.get_virtualmachine(self.si_content, |
47
|
|
|
moid=vm_id, |
48
|
|
|
name=vm_name) |
49
|
|
|
try: |
50
|
|
|
nettype = "dist" |
51
|
|
|
network_obj = inventory.get_distributedportgroup(self.si_content, |
52
|
|
|
name=network_name) |
53
|
|
|
except: |
54
|
|
|
nettype = "std" |
55
|
|
|
network_obj = inventory.get_network(self.si_content, |
56
|
|
|
name=network_name) |
57
|
|
|
|
58
|
|
|
# find correct NIC |
59
|
|
|
for device in vm.config.hardware.device: |
60
|
|
|
if isinstance(device, vim.vm.device.VirtualEthernetCard)\ |
61
|
|
|
and device.deviceInfo.label == network_adapter: |
62
|
|
|
nic = device |
63
|
|
|
|
64
|
|
|
# Different test method due to fact that object |
65
|
|
|
# isn't instantiated if not found |
66
|
|
|
try: |
67
|
|
|
nic |
68
|
|
|
except: |
69
|
|
|
raise Exception('Unable to find Network Adapter provided') |
70
|
|
|
|
71
|
|
|
# Create object for new Specification |
72
|
|
|
new_spec = vim.vm.device.VirtualDeviceSpec() |
73
|
|
|
new_spec.operation = vim.vm.device.VirtualDeviceSpec.Operation.edit |
74
|
|
|
new_spec.device = nic |
75
|
|
|
|
76
|
|
|
# If network name provided assign new network |
77
|
|
|
# Room to expand the following to set additional flags/values |
78
|
|
|
if network_name: |
79
|
|
|
# Default functionality is to use the |
80
|
|
|
# Distributed Port Group over a standard group |
81
|
|
|
if nettype == "dist": |
82
|
|
|
new_spec.device.backing = \ |
83
|
|
|
vim.vm.device.VirtualEthernetCard\ |
84
|
|
|
.DistributedVirtualPortBackingInfo() |
85
|
|
|
new_spec.device.backing.port = vim.dvs.PortConnection() |
86
|
|
|
|
87
|
|
|
dvs_port_connection = vim.dvs.PortConnection() |
88
|
|
|
dvs_port_connection.portgroupKey = network_obj.key |
89
|
|
|
dvs_port_connection.switchUuid = \ |
90
|
|
|
network_obj.config.distributedVirtualSwitch.uuid |
91
|
|
|
|
92
|
|
|
new_spec.device.backing = \ |
93
|
|
|
vim.vm.device.VirtualEthernetCard\ |
94
|
|
|
.DistributedVirtualPortBackingInfo() |
95
|
|
|
new_spec.device.backing.port = dvs_port_connection |
96
|
|
|
else: |
97
|
|
|
new_spec.device.backing = \ |
98
|
|
|
vim.vm.device.VirtualEthernetCard.NetworkBackingInfo() |
99
|
|
|
new_spec.device.backing.network = network_obj |
100
|
|
|
new_spec.device.backing.deviceName = network_obj.name |
101
|
|
|
|
102
|
|
|
# format changes for config spec update |
103
|
|
|
dev_changes = [] |
104
|
|
|
dev_changes.append(new_spec) |
105
|
|
|
spec = vim.vm.ConfigSpec() |
106
|
|
|
spec.deviceChange = dev_changes |
107
|
|
|
|
108
|
|
|
task = vm.ReconfigVM_Task(spec) |
109
|
|
|
self._wait_for_task(task) |
110
|
|
|
|
111
|
|
|
return {'state': str(task.info.state)} |
112
|
|
|
|