Completed
Pull Request — master (#462)
by
unknown
02:24
created

VMNicEdit   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 48
Duplicated Lines 0 %
Metric Value
dl 0
loc 48
rs 10
wmc 6

1 Method

Rating   Name   Duplication   Size   Complexity  
B run() 0 46 6
1
#!/usr/bin/env python
2
3
# Licensed to the StackStorm, Inc ('StackStorm') under one or more
4
# contributor license agreements.  See the NOTICE file distributed with
5
# this work for additional information regarding copyright ownership.
6
# The ASF licenses this file to You under the Apache License, Version 2.0
7
# (the "License"); you may not use this file except in compliance with
8
# the License.  You may obtain a copy of the License at
9
#
10
#     http://www.apache.org/licenses/LICENSE-2.0
11
#
12
# Unless required by applicable law or agreed to in writing, software
13
# distributed under the License is distributed on an "AS IS" BASIS,
14
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
# See the License for the specific language governing permissions and
16
# limitations under the License.
17
18
from pyVmomi import vim
19
20
from vmwarelib import inventory
21
from vmwarelib import checkinputs
22
from vmwarelib.actions import BaseAction
23
24
25
class VMNicEdit(BaseAction):
26
27
    def run(self, vm_id, vm_name, network_adapter, network_name):
28
        # check means of finding the VM was provided
29
        #checkinputs.vm_identifier(vm_id, vm_name)
30
        checkinputs.one_of_two_strings(vm_id, vm_name, "ID or Name")
31
        # convert ids to stubs
32
        vm = inventory.get_virtualmachine(self.si_content,
33
                                          moid=vm_id,
34
                                          name=vm_name)
35
        network_obj = inventory.get_network(self.si_content,
36
                                            name=network_name)
37
38
        # find correct NIC
39
        for device in vm.config.hardware.device:
40
            if isinstance(device, vim.vm.device.VirtualEthernetCard)\
41
                    and device.deviceInfo.label == network_adapter:
42
                nic = device
43
44
        #Different test method due to fact that object
45
        #isn't instantiated if not found
46
        try:
47
            nic
48
        except:
49
            raise Exception('Unable to find Network Adapter provided')
50
51
        # Create object for new Specification
52
        new_spec = vim.vm.device.VirtualDeviceSpec()
53
        new_spec.operation = vim.vm.device.VirtualDeviceSpec.Operation.edit
54
        new_spec.device = nic
55
56
        #If network name provided assign new network
57
        # Room to expand the following to set additional flags/values
58
        if network_name:
59
            new_spec.device.backing.network = network_obj
60
            new_spec.device.backing.deviceName = network_obj.name
61
            new_spec.device.deviceInfo.summary = network_obj.name
62
63
        #format changes for config spec update
64
        dev_changes = []
65
        dev_changes.append(new_spec)
66
        spec = vim.vm.ConfigSpec()
67
        spec.deviceChange = dev_changes
68
69
        task = vm.ReconfigVM_Task(spec)
70
        self._wait_for_task(task)
71
72
        return {'state': str(task.info.state)}
73