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