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