Conditions | 8 |
Total Lines | 56 |
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 |
||
118 | def run(self, vms, persistence='Persistent', disk_type='flat', capacity_gb=1, datastore=None, |
||
119 | datastore_cluster=None, device_name=None, disk_path='', storage_format='Thin'): |
||
120 | # TODO: 'controller' parameter is missing here. The reason is because we do not support |
||
121 | # passing real objects like PowerCli and there is no uuid to find and address the |
||
122 | # controller in the system. |
||
123 | persistence = persistence.lower() |
||
124 | disk_type = disk_type.lower() |
||
125 | storage_format = storage_format.lower() |
||
126 | |||
127 | si = self.si |
||
128 | si_content = si.RetrieveContent() |
||
129 | vm_objs = [vim.VirtualMachine(moid, stub=si._stub) for moid in vms] |
||
130 | # by checking the name property, the vms' existance is checked. |
||
131 | [vm_obj.name for vm_obj in vm_objs] |
||
132 | datastore_obj = None |
||
133 | if datastore: |
||
134 | datastore_obj = vim.Datastore(datastore, stub=si._stub) |
||
135 | # by checking the name property, the vms' existance is checked. |
||
136 | datastore_obj.name |
||
137 | |||
138 | result = [] |
||
139 | |||
140 | if datastore_cluster: |
||
141 | ds_clust_obj = vim.StoragePod(datastore_cluster, stub=si._stub) |
||
142 | # by retrieving the name property, the existance is checked. |
||
143 | ds_clust_obj.name |
||
144 | srm = si_content.storageResourceManager |
||
145 | |||
146 | for vm in vm_objs: |
||
147 | vm_reconfig_spec = NewHardDisk.get_vm_reconfig_spec(vm, datastore_obj, disk_type, |
||
148 | storage_format, persistence, disk_path, device_name, capacity_gb) |
||
149 | storage_placement_spec = NewHardDisk.get_storage_placement_spec(ds_clust_obj, vm, |
||
150 | vm_reconfig_spec) |
||
151 | datastores = srm.RecommendDatastores(storageSpec=storage_placement_spec) |
||
152 | if not datastores.recommendations: |
||
153 | sys.stderr.write('Skipping %s as there is no datastore recommendation' % |
||
154 | vm.obj._GetMoId()) |
||
155 | add_disk_task = srm.ApplyStorageDrsRecommendation_Task( |
||
156 | datastores.recommendations[0].key) |
||
157 | successfully_added_disk = self._wait_for_task(add_disk_task) |
||
158 | result.append({ |
||
159 | "vm_moid": vm._GetMoId(), |
||
160 | "success": successfully_added_disk |
||
161 | }) |
||
162 | else: |
||
163 | for vm in vm_objs: |
||
164 | vm_reconfig_spec = NewHardDisk.get_vm_reconfig_spec(vm, datastore_obj, disk_type, |
||
165 | storage_format, persistence, disk_path, device_name, capacity_gb) |
||
166 | add_disk_task = vm.ReconfigVM_Task(spec=vm_reconfig_spec) |
||
167 | successfully_added_disk = self._wait_for_task(add_disk_task) |
||
168 | result.append({ |
||
169 | "vm_moid": vm._GetMoId(), |
||
170 | "success": successfully_added_disk |
||
171 | }) |
||
172 | |||
173 | return result |
||
174 |