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