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 | from pyVmomi import vim |
||
9 | def run(self, vm_id, network_id, vnic_key, ip, subnet, gateway=None, domain=None): |
||
10 | # convert ids to stubs |
||
11 | virtualmachine = inventory.get_virtualmachine(self.si_content, vm_id) |
||
12 | network = inventory.get_network(self.si_content, network_id) |
||
13 | vnic = self._get_vnic_device(virtualmachine, vnic_key) |
||
14 | |||
15 | # add new vnic |
||
16 | configspec = vim.vm.ConfigSpec() |
||
17 | nic = vim.vm.device.VirtualDeviceSpec() |
||
18 | nic.operation = vim.vm.device.VirtualDeviceSpec.Operation.edit |
||
19 | nic.device = vim.vm.device.VirtualVmxnet3() |
||
20 | nic.device.wakeOnLanEnabled = True |
||
21 | nic.device.addressType = 'assigned' |
||
22 | nic.device.key = vnic.key |
||
23 | nic.device.deviceInfo = vim.Description() |
||
24 | nic.device.deviceInfo.label = 'Network Adapter-%s' % (ip) |
||
25 | nic.device.deviceInfo.summary = 'summary' |
||
26 | nic.device.backing = vim.vm.device.VirtualEthernetCard.DistributedVirtualPortBackingInfo() |
||
27 | nic.device.backing.port = vim.dvs.PortConnection() |
||
28 | nic.device.backing.port.switchUuid = network.config.distributedVirtualSwitch.uuid |
||
29 | nic.device.backing.port.portgroupKey = network.config.key |
||
30 | nic.device.connectable = vim.vm.device.VirtualDevice.ConnectInfo() |
||
31 | nic.device.connectable.startConnected = True |
||
32 | nic.device.connectable.allowGuestControl = True |
||
33 | configspec.deviceChange = [nic] |
||
34 | add_vnic_task = virtualmachine.ReconfigVM_Task(configspec) |
||
35 | successfully_added_vnic = self._wait_for_task(add_vnic_task) |
||
36 | |||
37 | if not successfully_added_vnic: |
||
38 | return self._format_result(successfully_added_vnic, 'Failed to update nic.') |
||
39 | |||
40 | adaptermap = vim.vm.customization.AdapterMapping() |
||
41 | adaptermap.adapter = vim.vm.customization.IPSettings() |
||
42 | adaptermap.adapter.ip = vim.vm.customization.FixedIp() |
||
43 | adaptermap.adapter.ip.ipAddress = ip |
||
44 | adaptermap.adapter.subnetMask = subnet |
||
45 | adaptermap.adapter.gateway = gateway |
||
46 | adaptermap.adapter.dnsDomain = domain |
||
47 | |||
48 | globalip = vim.vm.customization.GlobalIPSettings() |
||
49 | |||
50 | ident = vim.vm.customization.LinuxPrep() |
||
51 | ident.domain = domain |
||
52 | ident.hostName = vim.vm.customization.FixedName() |
||
53 | ident.hostName.name = virtualmachine.name |
||
54 | |||
55 | customspec = vim.vm.customization.Specification() |
||
56 | customspec.identity = ident |
||
57 | customspec.nicSettingMap = [adaptermap] |
||
58 | customspec.globalIPSettings = globalip |
||
59 | |||
60 | try: |
||
61 | customize_task = virtualmachine.Customize(spec=customspec) |
||
62 | successfully_customized = self._wait_for_task(customize_task) |
||
63 | except: |
||
64 | self.logger.exception('Failed to create customization spec.') |
||
65 | raise |
||
66 | msg = 'Updated nic and assigned IP.' if successfully_customized else 'Failed to assign ip.' |
||
67 | return self._format_result(successfully_customized, msg=msg) |
||
68 | |||
89 |