Conditions | 4 |
Total Lines | 59 |
Lines | 0 |
Ratio | 0 % |
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:
If many parameters/temporary variables are present:
1 | # Licensed to the StackStorm, Inc ('StackStorm') under one or more |
||
24 | def run(self, vm_id, network_id, vnic_key, ip, subnet, gateway=None, domain=None): |
||
25 | # convert ids to stubs |
||
26 | virtualmachine = inventory.get_virtualmachine(self.si_content, vm_id) |
||
27 | network = inventory.get_network(self.si_content, network_id) |
||
28 | vnic = self._get_vnic_device(virtualmachine, vnic_key) |
||
29 | |||
30 | # add new vnic |
||
31 | configspec = vim.vm.ConfigSpec() |
||
32 | nic = vim.vm.device.VirtualDeviceSpec() |
||
33 | nic.operation = vim.vm.device.VirtualDeviceSpec.Operation.edit |
||
34 | nic.device = vim.vm.device.VirtualVmxnet3() |
||
35 | nic.device.wakeOnLanEnabled = True |
||
36 | nic.device.addressType = 'assigned' |
||
37 | nic.device.key = vnic.key |
||
38 | nic.device.deviceInfo = vim.Description() |
||
39 | nic.device.deviceInfo.label = 'Network Adapter-%s' % (ip) |
||
40 | nic.device.deviceInfo.summary = 'summary' |
||
41 | nic.device.backing = vim.vm.device.VirtualEthernetCard.DistributedVirtualPortBackingInfo() |
||
42 | nic.device.backing.port = vim.dvs.PortConnection() |
||
43 | nic.device.backing.port.switchUuid = network.config.distributedVirtualSwitch.uuid |
||
44 | nic.device.backing.port.portgroupKey = network.config.key |
||
45 | nic.device.connectable = vim.vm.device.VirtualDevice.ConnectInfo() |
||
46 | nic.device.connectable.startConnected = True |
||
47 | nic.device.connectable.allowGuestControl = True |
||
48 | configspec.deviceChange = [nic] |
||
49 | add_vnic_task = virtualmachine.ReconfigVM_Task(configspec) |
||
50 | successfully_added_vnic = self._wait_for_task(add_vnic_task) |
||
51 | |||
52 | if not successfully_added_vnic: |
||
53 | return self._format_result(successfully_added_vnic, 'Failed to update nic.') |
||
54 | |||
55 | adaptermap = vim.vm.customization.AdapterMapping() |
||
56 | adaptermap.adapter = vim.vm.customization.IPSettings() |
||
57 | adaptermap.adapter.ip = vim.vm.customization.FixedIp() |
||
58 | adaptermap.adapter.ip.ipAddress = ip |
||
59 | adaptermap.adapter.subnetMask = subnet |
||
60 | adaptermap.adapter.gateway = gateway |
||
61 | adaptermap.adapter.dnsDomain = domain |
||
62 | |||
63 | globalip = vim.vm.customization.GlobalIPSettings() |
||
64 | |||
65 | ident = vim.vm.customization.LinuxPrep() |
||
66 | ident.domain = domain |
||
67 | ident.hostName = vim.vm.customization.FixedName() |
||
68 | ident.hostName.name = virtualmachine.name |
||
69 | |||
70 | customspec = vim.vm.customization.Specification() |
||
71 | customspec.identity = ident |
||
72 | customspec.nicSettingMap = [adaptermap] |
||
73 | customspec.globalIPSettings = globalip |
||
74 | |||
75 | try: |
||
76 | customize_task = virtualmachine.Customize(spec=customspec) |
||
77 | successfully_customized = self._wait_for_task(customize_task) |
||
78 | except: |
||
79 | self.logger.exception('Failed to create customization spec.') |
||
80 | raise |
||
81 | msg = 'Updated nic and assigned IP.' if successfully_customized else 'Failed to assign ip.' |
||
82 | return self._format_result(successfully_customized, msg=msg) |
||
83 | |||
104 |