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

VMAddNic.get_vm_reconfig_spec()   B

Complexity

Conditions 6

Size

Total Lines 34

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 34
rs 7.5384
cc 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 VMAddNic(BaseAction):
26
27
    def run(self, vm_id, vm_name, network_name,
28
            nictype, stayconnected, wakeonlan):
29
        # create object itmes of key components
30
        checkinputs.one_of_two_strings(vm_id, vm_name, "ID or Name")
31
32
        vm = inventory.get_virtualmachine(self.si_content, vm_id, vm_name)
33
        network_obj = inventory.get_network(self.si_content, name=network_name)
34
35
        vm_reconfig_spec = self.get_vm_reconfig_spec(network_obj,
36
                                                     stayconnected,
37
                                                     nictype,
38
                                                     wakeonlan)
39
40
        add_vnic_task = vm.ReconfigVM_Task(spec=vm_reconfig_spec)
41
        successfully_added_vnic = self._wait_for_task(add_vnic_task)
42
43
        return {'state': successfully_added_vnic}
44
45
    def get_vm_reconfig_spec(self, network_obj,
46
                             stay_connected, network_type, wake_on_lan):
47
        network_spec = vim.vm.device.VirtualDeviceSpec()
48
        network_spec.operation = vim.vm.device.VirtualDeviceSpec.Operation.add
49
50
        if network_type.lower() == 'e1000':
51
            network_spec.device = vim.vm.device.VirtualE1000()
52
        elif network_type.lower() == 'flexible':
53
            network_spec.device = vim.vm.device.VirtualPCNet32()
54
        elif network_type.lower() == 'vmxnet':
55
            network_spec.device = vim.vm.device.VirtualVmxnet()
56
        elif network_type.lower() == 'enhancedvmxnet':
57
            network_spec.device = vim.vm.device.VirtualVmxnet2()
58
        elif network_type.lower() == 'vmxnet3':
59
            network_spec.device = vim.vm.device.VirtualVmxnet3()
60
        else:
61
            network_spec.device = vim.vm.device.VirtualEthernetCard()
62
63
        network_spec.device.wakeOnLanEnabled = wake_on_lan
64
        network_spec.device.deviceInfo = vim.Description()
65
        network_spec.device.backing = \
66
            vim.vm.device.VirtualEthernetCard.NetworkBackingInfo()
67
        network_spec.device.backing.network = network_obj
68
        network_spec.device.backing.deviceName = network_obj.name
69
70
        network_spec.device.connectable = \
71
            vim.vm.device.VirtualDevice.ConnectInfo()
72
        network_spec.device.connectable.startConnected = stay_connected
73
        network_spec.device.connectable.allowGuestControl = True
74
75
        # creating reconfig spec
76
        vm_reconfig_spec = vim.vm.ConfigSpec()
77
        vm_reconfig_spec.deviceChange = [network_spec]
78
        return vm_reconfig_spec
79