Conditions | 5 |
Total Lines | 60 |
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 | #!/usr/bin/env python |
||
27 | def run(self, vm_id, vm_name, datastore_cluster, |
||
28 | datastore, disk_size, provision_type): |
||
29 | # ensure that minimal inputs are provided |
||
30 | checkinputs.one_of_two_strings(vm_id, vm_name, "ID or Name") |
||
31 | |||
32 | vm = inventory.get_virtualmachine(self.si_content, vm_id, vm_name) |
||
33 | spec = vim.vm.ConfigSpec() |
||
34 | hdd_unit_number = self.get_next_unit_number(vm) |
||
35 | ctrl_key = self.get_controller_key(vm) |
||
36 | |||
37 | # Prepare new Disk configuration |
||
38 | disk_changes = [] |
||
39 | disk_spec = vim.vm.device.VirtualDeviceSpec() |
||
40 | disk_spec.fileOperation = "create" |
||
41 | disk_spec.operation = vim.vm.device.VirtualDeviceSpec.Operation.add |
||
42 | disk_spec.device = vim.vm.device.VirtualDisk() |
||
43 | disk_spec.device.backing =\ |
||
44 | vim.vm.device.VirtualDisk.FlatVer2BackingInfo() |
||
45 | disk_spec.device.backing.diskMode = "persistent" |
||
46 | |||
47 | if provision_type == 'thin': |
||
48 | disk_spec.device.backing.thinProvisioned = True |
||
49 | |||
50 | disk_spec.device.unitNumber = hdd_unit_number |
||
51 | disk_spec.device.capacityInKB = int(disk_size) * 1024 * 1024 |
||
52 | disk_spec.device.controllerKey = ctrl_key |
||
53 | |||
54 | # If Datastore Cluster is provided attach Disk via that |
||
55 | if datastore_cluster: |
||
56 | ds_clust_obj = inventory.get_datastore_cluster( |
||
57 | self.si_content, name=datastore_cluster) |
||
58 | disk_changes.append(disk_spec) |
||
59 | spec.deviceChange = disk_changes |
||
60 | srm = self.si_content.storageResourceManager |
||
61 | |||
62 | storage_placement_spec = self.get_storage_placement_spec( |
||
63 | ds_clust_obj, vm, spec) |
||
64 | datastores = srm.RecommendDatastores( |
||
65 | storageSpec=storage_placement_spec) |
||
66 | |||
67 | if not datastores.recommendations: |
||
68 | sys.stderr.write('Skipping as No datastore Recommendations') |
||
69 | |||
70 | add_disk_task = srm.ApplyStorageDrsRecommendation_Task( |
||
71 | datastores.recommendations[0].key) |
||
72 | |||
73 | elif datastore: |
||
74 | datastore_obj = inventory.get_datastore(self.si_content, |
||
75 | name=datastore) |
||
76 | disk_spec.device.backing.datastore = datastore_obj |
||
77 | disk_changes.append(disk_spec) |
||
78 | spec.deviceChange = disk_changes |
||
79 | add_disk_task = vm.ReconfigVM_Task(spec) |
||
80 | else: |
||
81 | disk_changes.append(disk_spec) |
||
82 | spec.deviceChange = disk_changes |
||
83 | add_disk_task = vm.ReconfigVM_Task(spec) |
||
84 | |||
85 | successfully_added_disk = self._wait_for_task(add_disk_task) |
||
86 | return {'state': successfully_added_disk} |
||
87 | |||
125 |