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): |
26
|
|
|
# check means of finding the VM was provided |
27
|
|
|
checkinputs.one_of_two_strings(vm_id, vm_name, "ID or Name") |
28
|
|
|
# convert ids to stubs |
29
|
|
|
vm = inventory.get_virtualmachine(self.si_content, |
30
|
|
|
moid=vm_id, |
31
|
|
|
name=vm_name) |
32
|
|
|
network_obj = inventory.get_network(self.si_content, |
33
|
|
|
name=network_name) |
34
|
|
|
|
35
|
|
|
# find correct NIC |
36
|
|
|
for device in vm.config.hardware.device: |
37
|
|
|
if isinstance(device, vim.vm.device.VirtualEthernetCard)\ |
38
|
|
|
and device.deviceInfo.label == network_adapter: |
39
|
|
|
nic = device |
40
|
|
|
|
41
|
|
|
# Different test method due to fact that object |
42
|
|
|
# isn't instantiated if not found |
43
|
|
|
try: |
44
|
|
|
nic |
45
|
|
|
except: |
46
|
|
|
raise Exception('Unable to find Network Adapter provided') |
47
|
|
|
|
48
|
|
|
# Create object for new Specification |
49
|
|
|
new_spec = vim.vm.device.VirtualDeviceSpec() |
50
|
|
|
new_spec.operation = vim.vm.device.VirtualDeviceSpec.Operation.edit |
51
|
|
|
new_spec.device = nic |
52
|
|
|
|
53
|
|
|
# If network name provided assign new network |
54
|
|
|
# Room to expand the following to set additional flags/values |
55
|
|
|
if network_name: |
56
|
|
|
new_spec.device.backing.network = network_obj |
57
|
|
|
new_spec.device.backing.deviceName = network_obj.name |
58
|
|
|
new_spec.device.deviceInfo.summary = network_obj.name |
59
|
|
|
|
60
|
|
|
# format changes for config spec update |
61
|
|
|
dev_changes = [] |
62
|
|
|
dev_changes.append(new_spec) |
63
|
|
|
spec = vim.vm.ConfigSpec() |
64
|
|
|
spec.deviceChange = dev_changes |
65
|
|
|
|
66
|
|
|
task = vm.ReconfigVM_Task(spec) |
67
|
|
|
self._wait_for_task(task) |
68
|
|
|
|
69
|
|
|
return {'state': str(task.info.state)} |
70
|
|
|
|