Conditions | 10 |
Total Lines | 57 |
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:
Complex classes like NewNetworkAdapter.get_vm_reconfig_spec() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | import sys |
||
11 | @staticmethod |
||
12 | def get_vm_reconfig_spec(distributed_switch_obj, mac_address, network_obj, port_key, stay_connected, network_type, wake_on_lan): |
||
13 | # creating virtual device |
||
14 | if network_type == 'e1000': |
||
15 | virtual_network = vim.vm.device.VirtualE1000() |
||
16 | elif network_type == 'flexible': |
||
17 | virtual_network = vim.vm.device.VirtualPCNet32() |
||
18 | lance_option = vim.vm.device.VirtualPCNet32Option() |
||
19 | lance_option.supportsMorphing = True; |
||
20 | #TODO and question for Manas: We need to plug in that lance_option on something. I could not find any class that uses VirtualPCNet32Option or any of its upstream classes. |
||
21 | elif network_type == 'vmxnet': |
||
22 | virtual_network = vim.vm.device.VirtualVmxnet() |
||
23 | elif network_type == 'enhancedvmxnet': |
||
24 | virtual_network = vim.vm.device.VirtualVmxnet2() |
||
25 | elif network_type == 'vmxnet3': |
||
26 | virtual_network = vim.vm.device.VirtualVmxnet3() |
||
27 | else: |
||
28 | virtual_network = vim.vm.device.VirtualEthernetCard() |
||
29 | |||
30 | virtual_network.wakeOnLanEnabled = wake_on_lan |
||
31 | connect_info = vim.vm.device.VirtualDevice.ConnectInfo() |
||
32 | connect_info.startConnected = stay_connected |
||
33 | virtual_network.connectable = connect_info |
||
34 | if mac_address: |
||
35 | virtual_network.macAddress = mac_address |
||
36 | virtual_network.addressType = vim.vm.device.VirtualEthernetCardOption.MacTypes.manual |
||
37 | |||
38 | # creating backing info |
||
39 | backing_info = None |
||
40 | if distributed_switch_obj: |
||
41 | dvs_port_connection = vim.dvs.PortConnection() |
||
42 | dvs_port_connection.switchUuid = distributed_switch_obj.uuid |
||
43 | if port_key: |
||
44 | dvs_port_connection.portKey = port_key |
||
45 | port_criteria = vim.dvs.PortCriteria() |
||
46 | port_criteria.portKey = port_key |
||
47 | ports = distributed_switch_obj.FetchDVPorts(port_criteria) |
||
48 | dvs_port_connection.portgroupKey = ports[0].portgroupKey |
||
49 | backing_info = vim.vm.device.VirtualEthernetCard.DistributedVirtualPortBackingInfo() |
||
50 | backing_info.port = dvs_port_connection |
||
51 | |||
52 | if network_obj: |
||
53 | backing_info = vim.vm.device.VirtualEthernetCard.NetworkBackingInfo() |
||
54 | backing_info.network = network_obj |
||
55 | backing_info.deviceName = network_obj.name |
||
56 | |||
57 | virtual_network.backing = backing_info |
||
58 | |||
59 | #creating network device spec |
||
60 | network_spec = vim.vm.device.VirtualDeviceSpec() |
||
61 | network_spec.device = virtual_network |
||
62 | network_spec.operation = vim.vm.device.VirtualDeviceSpec.Operation.add |
||
63 | |||
64 | #creating reconfig spec |
||
65 | vm_reconfig_spec = vim.vm.ConfigSpec() |
||
66 | vm_reconfig_spec.deviceChange = [network_spec] |
||
67 | return vm_reconfig_spec |
||
68 | |||
99 |