Conditions | 8 |
Total Lines | 87 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 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 |
||
25 | def run(self, vm_id, vm_name, network_adapter, network_name, vsphere=None): |
||
26 | """ |
||
27 | Edit Network Adapater on Virtual Machine |
||
28 | |||
29 | Args: |
||
30 | - vm_id: Moid of Virtual Machine to edit |
||
31 | - vm_name: Name of Virtual Machine to edit |
||
32 | - vsphere: Pre-configured vsphere connection details (config.yaml) |
||
33 | - network_name: Network to attach adapter to |
||
34 | - network_adapter: Name of Adapter to edit |
||
35 | |||
36 | Returns: |
||
37 | - dict: State true/false |
||
38 | """ |
||
39 | |||
40 | # check means of finding the VM was provided |
||
41 | checkinputs.one_of_two_strings(vm_id, vm_name, "ID or Name") |
||
42 | |||
43 | self.establish_connection(vsphere) |
||
44 | |||
45 | # convert ids to stubs |
||
46 | vm = inventory.get_virtualmachine(self.si_content, |
||
47 | moid=vm_id, |
||
48 | name=vm_name) |
||
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 | # find correct NIC |
||
59 | for device in vm.config.hardware.device: |
||
60 | if isinstance(device, vim.vm.device.VirtualEthernetCard)\ |
||
61 | and device.deviceInfo.label == network_adapter: |
||
62 | nic = device |
||
63 | |||
64 | # Different test method due to fact that object |
||
65 | # isn't instantiated if not found |
||
66 | try: |
||
67 | nic |
||
68 | except: |
||
69 | raise Exception('Unable to find Network Adapter provided') |
||
70 | |||
71 | # Create object for new Specification |
||
72 | new_spec = vim.vm.device.VirtualDeviceSpec() |
||
73 | new_spec.operation = vim.vm.device.VirtualDeviceSpec.Operation.edit |
||
74 | new_spec.device = nic |
||
75 | |||
76 | # If network name provided assign new network |
||
77 | # Room to expand the following to set additional flags/values |
||
78 | if network_name: |
||
79 | # Default functionality is to use the |
||
80 | # Distributed Port Group over a standard group |
||
81 | if nettype == "dist": |
||
82 | new_spec.device.backing = \ |
||
83 | vim.vm.device.VirtualEthernetCard\ |
||
84 | .DistributedVirtualPortBackingInfo() |
||
85 | new_spec.device.backing.port = vim.dvs.PortConnection() |
||
86 | |||
87 | dvs_port_connection = vim.dvs.PortConnection() |
||
88 | dvs_port_connection.portgroupKey = network_obj.key |
||
89 | dvs_port_connection.switchUuid = \ |
||
90 | network_obj.config.distributedVirtualSwitch.uuid |
||
91 | |||
92 | new_spec.device.backing = \ |
||
93 | vim.vm.device.VirtualEthernetCard\ |
||
94 | .DistributedVirtualPortBackingInfo() |
||
95 | new_spec.device.backing.port = dvs_port_connection |
||
96 | else: |
||
97 | new_spec.device.backing = \ |
||
98 | vim.vm.device.VirtualEthernetCard.NetworkBackingInfo() |
||
99 | new_spec.device.backing.network = network_obj |
||
100 | new_spec.device.backing.deviceName = network_obj.name |
||
101 | |||
102 | # format changes for config spec update |
||
103 | dev_changes = [] |
||
104 | dev_changes.append(new_spec) |
||
105 | spec = vim.vm.ConfigSpec() |
||
106 | spec.deviceChange = dev_changes |
||
107 | |||
108 | task = vm.ReconfigVM_Task(spec) |
||
109 | self._wait_for_task(task) |
||
110 | |||
111 | return {'state': str(task.info.state)} |
||
112 |