Completed
Pull Request — master (#460)
by Manas
02:13
created

VMAddNic   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 62
Duplicated Lines 0 %
Metric Value
dl 0
loc 62
rs 10
wmc 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A run() 0 60 2
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
import uuid
17
18
from pyVmomi import vim
19
20
from vmwarelib import inventory
21
from vmwarelib.actions import BaseAction
22
23
24
class VMAddNic(BaseAction):
25
26
    def run(self, vm_id, network_id, ip, subnet, gateway=None, domain=None):
27
        # convert ids to stubs
28
        virtualmachine = inventory.get_virtualmachine(self.si_content, vm_id)
29
        network = inventory.get_network(self.si_content, network_id)
30
31
        # add new vnic
32
        configspec = vim.vm.ConfigSpec()
33
        nic = vim.vm.device.VirtualDeviceSpec()
34
        nic.operation = vim.vm.device.VirtualDeviceSpec.Operation.add  # or edit if a device exists
35
        nic.device = vim.vm.device.VirtualVmxnet3()
36
        nic.device.wakeOnLanEnabled = True
37
        nic.device.addressType = 'assigned'
38
        # docs recommend using unique negative integers as temporary keys.
39
        # See https://github.com/vmware/pyvmomi/blob/master/docs/vim/vm/device/VirtualDevice.rst
40
        nic.device.key = -1
41
        nic.device.deviceInfo = vim.Description()
42
        nic.device.deviceInfo.label = 'Network Adapter-%s' % (ip)
43
        nic.device.deviceInfo.summary = 'summary'
44
        nic.device.backing = vim.vm.device.VirtualEthernetCard.DistributedVirtualPortBackingInfo()
45
        nic.device.backing.port = vim.dvs.PortConnection()
46
        nic.device.backing.port.switchUuid = network.config.distributedVirtualSwitch.uuid
47
        nic.device.backing.port.portgroupKey = network.config.key
48
        nic.device.connectable = vim.vm.device.VirtualDevice.ConnectInfo()
49
        nic.device.connectable.startConnected = True
50
        nic.device.connectable.allowGuestControl = True
51
        configspec.deviceChange = [nic]
52
        add_vnic_task = virtualmachine.ReconfigVM_Task(configspec)
53
        successfully_added_vnic = self._wait_for_task(add_vnic_task)
54
55
        # customize VM
56
        cust_item = vim.CustomizationSpecItem()
57
        cust_specinfo = vim.CustomizationSpecInfo()
58
        cust_specinfo.name = 'assignip-' + str(uuid.uuid4())
59
        cust_specinfo.type = 'Linux'
60
        cust_item.info = cust_specinfo
61
62
        # fixed ip
63
        cust_spec = vim.vm.customization.Specification()
64
        cust_item.spec = cust_spec
65
        ip_adapter_mapping = vim.vm.customization.AdapterMapping()
66
        ip_adapter_mapping.adapter = vim.vm.customization.IPSettings()
67
        ip_adapter_mapping.adapter.ip = vim.vm.customization.FixedIp()
68
        ip_adapter_mapping.adapter.ip.ipAddress = ip
69
        ip_adapter_mapping.adapter.subnetMask = subnet
70
        ip_adapter_mapping.adapter.gateway = gateway
71
        ip_adapter_mapping.adapter.dnsDomain = domain
72
        cust_spec.nicSettingMap = [ip_adapter_mapping]
73
        cust_spec.identity = vim.vm.customization.LinuxPrep()
74
        cust_spec.identity.hostName = vim.vm.customization.PrefixNameGenerator()
75
        cust_spec.identity.hostName.base = 'st2'
76
        cust_spec.identity.domain = 'demo.net'
77
        cust_spec.globalIPSettings = vim.vm.customization.GlobalIPSettings()
78
79
        try:
80
            self.si_content.customizationSpecManager.CreateCustomizationSpec(cust_item)
81
        except:
82
            self.logger.exception('Failed to create customization spec.')
83
            raise
84
85
        return {'state': successfully_added_vnic}
86