Completed
Push — master ( a80d3a...90bb8a )
by Edward
03:19
created

VMAddNic.get_vm_reconfig_spec()   C

Complexity

Conditions 7

Size

Total Lines 54

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
c 0
b 0
f 0
dl 0
loc 54
rs 6.8325

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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, vsphere=None):
27
        """
28
        Add Network Adapter to Virtual Machine
29
30
        Args:
31
        - vm_id: Moid of Virtual Machine to edit
32
        - vm_name: Name of Virtual Machine to edit
33
        - vsphere: Pre-configured vsphere connection details (config.yaml)
34
        - network_name: vsphere network to connect to
35
        - nictype: Nic type to add
36
        - stayconnected: Nic connected on boot
37
        - wakeonlan: Wake on Lan
38
39
        Returns:
40
        - dict: state true/false
41
        """
42
        # create object itmes of key components
43
        checkinputs.one_of_two_strings(vm_id, vm_name, "ID or Name")
44
45
        self.establish_connection(vsphere)
46
47
        vm = inventory.get_virtualmachine(self.si_content, vm_id, vm_name)
48
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
        vm_reconfig_spec = self.get_vm_reconfig_spec(network_obj,
59
                                                     stayconnected,
60
                                                     nictype,
61
                                                     wakeonlan,
62
                                                     nettype)
63
64
        add_vnic_task = vm.ReconfigVM_Task(spec=vm_reconfig_spec)
65
        successfully_added_vnic = self._wait_for_task(add_vnic_task)
66
67
        return {'state': successfully_added_vnic}
68
69
    def get_vm_reconfig_spec(self, network_obj,
70
                             stay_connected, network_type,
71
                             wake_on_lan, nettype):
72
        network_spec = vim.vm.device.VirtualDeviceSpec()
73
        network_spec.operation = vim.vm.device.VirtualDeviceSpec.Operation.add
74
75
        if network_type.lower() == 'e1000':
76
            network_spec.device = vim.vm.device.VirtualE1000()
77
        elif network_type.lower() == 'flexible':
78
            network_spec.device = vim.vm.device.VirtualPCNet32()
79
        elif network_type.lower() == 'vmxnet':
80
            network_spec.device = vim.vm.device.VirtualVmxnet()
81
        elif network_type.lower() == 'enhancedvmxnet':
82
            network_spec.device = vim.vm.device.VirtualVmxnet2()
83
        elif network_type.lower() == 'vmxnet3':
84
            network_spec.device = vim.vm.device.VirtualVmxnet3()
85
        else:
86
            network_spec.device = vim.vm.device.VirtualEthernetCard()
87
88
        network_spec.device.wakeOnLanEnabled = wake_on_lan
89
        network_spec.device.deviceInfo = vim.Description()
90
91
        # Default functionality is to use the
92
        # Distributed Port Group over a standard group
93
        if nettype == "dist":
94
            network_spec.device.backing = \
95
                vim.vm.device.VirtualEthernetCard\
96
                .DistributedVirtualPortBackingInfo()
97
            network_spec.device.backing.port = vim.dvs.PortConnection()
98
99
            dvs_port_connection = vim.dvs.PortConnection()
100
            dvs_port_connection.portgroupKey = network_obj.key
101
            dvs_port_connection.switchUuid = \
102
                network_obj.config.distributedVirtualSwitch.uuid
103
104
            network_spec.device.backing = \
105
                vim.vm.device.VirtualEthernetCard\
106
                .DistributedVirtualPortBackingInfo()
107
            network_spec.device.backing.port = dvs_port_connection
108
        else:
109
            network_spec.device.backing = \
110
                vim.vm.device.VirtualEthernetCard.NetworkBackingInfo()
111
            network_spec.device.backing.network = network_obj
112
            network_spec.device.backing.deviceName = network_obj.name
113
114
        network_spec.device.connectable = \
115
            vim.vm.device.VirtualDevice.ConnectInfo()
116
        network_spec.device.connectable.startConnected = stay_connected
117
        network_spec.device.connectable.allowGuestControl = True
118
119
        # creating reconfig spec
120
        vm_reconfig_spec = vim.vm.ConfigSpec()
121
        vm_reconfig_spec.deviceChange = [network_spec]
122
        return vm_reconfig_spec
123