| Conditions | 5 | 
| Total Lines | 80 | 
| 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 | ||
| 26 | def run(self, vm_name, cluster, datastore_cluster, | ||
| 27 | datastore, resourcepool, cpu_size, ram_size, | ||
| 28 | guestos, version, description): | ||
| 29 | # Setup Identifiers for objects | ||
| 30 | si = self.si | ||
| 31 | si_content = si.RetrieveContent() | ||
| 32 | # checkinputs.vm_storage(datastore_cluster, datastore) | ||
| 33 | checkinputs.one_of_two_strings(datastore_cluster, | ||
| 34 | datastore, | ||
| 35 | "Datastore Cluster or Datastore") | ||
| 36 | |||
| 37 | data_center = self.si_content.rootFolder.childEntity[0] | ||
| 38 | cluster = inventory.get_cluster(self.si_content, name=cluster) | ||
| 39 | data_store_cluster = inventory.get_datastore_cluster( | ||
| 40 | self.si_content, | ||
| 41 | name=datastore_cluster) | ||
| 42 | # data_store = inventory.get_datastore(self.si_content, name=datastore) | ||
| 43 | target_folder = data_center.vmFolder | ||
| 44 | |||
| 45 | # If No Resource Pool issued the Default one for | ||
| 46 | # the Cluster will be selected. | ||
| 47 | if resourcepool: | ||
| 48 | resource_pool = inventory.get_resource_pool(self.si_content, | ||
| 49 | name=resourcepool) | ||
| 50 | else: | ||
| 51 | resource_pool = cluster.resourcePool | ||
| 52 | |||
| 53 | # Config created that is required for DS selection | ||
| 54 | # Config is BareBones, CPU RAM no more. | ||
| 55 | config = vim.vm.ConfigSpec(name=vm_name, | ||
| 56 | memoryMB=(ram_size * 1024), | ||
| 57 | numCPUs=cpu_size, | ||
| 58 | guestId=guestos, | ||
| 59 | version=version, | ||
| 60 | cpuHotAddEnabled=True, | ||
| 61 | memoryHotAddEnabled=True, | ||
| 62 | annotation=description) | ||
| 63 | |||
| 64 | # if Datastore cluster is provided it will find the | ||
| 65 | # recommended Datastore to store VM files | ||
| 66 | if datastore_cluster: | ||
| 67 | podsel = vim.storageDrs.PodSelectionSpec() | ||
| 68 | podsel.storagePod = data_store_cluster | ||
| 69 | |||
| 70 | storage_spec = vim.storageDrs.StoragePlacementSpec( | ||
| 71 | type='create', | ||
| 72 | configSpec=config, | ||
| 73 | resourcePool=resource_pool, | ||
| 74 | podSelectionSpec=podsel, | ||
| 75 | folder=target_folder) | ||
| 76 | |||
| 77 | # Create Instance of Storage Resource Manager - | ||
| 78 | # This is used to identify a Recommended Datastore from a Cluster | ||
| 79 | srm = si_content.storageResourceManager | ||
| 80 | results = srm.RecommendDatastores(storageSpec=storage_spec) | ||
| 81 | rec_ds = results.recommendations[0].action[0]\ | ||
| 82 | .relocateSpec.datastore | ||
| 83 | datastore_path = '[' + rec_ds.name + '] ' + vm_name | ||
| 84 | else: | ||
| 85 | # No Datastore Cluster has been offered so using the D | ||
| 86 | if datastore: | ||
| 87 | datastore_path = '[' + datastore + '] ' + vm_name | ||
| 88 | else: | ||
| 89 |                 raise Exception('Error No Storage Data Provided') | ||
| 90 | |||
| 91 | # Now Datastore is known the remaining | ||
| 92 | # of VM Config can be setup and added | ||
| 93 | vmx_file = vim.vm.FileInfo(logDirectory=None, | ||
| 94 | snapshotDirectory=None, | ||
| 95 | suspendDirectory=None, | ||
| 96 | vmPathName=datastore_path) | ||
| 97 | config.files = vmx_file | ||
| 98 | |||
| 99 | # Create task to Build actual Machine | ||
| 100 | task = target_folder.CreateVM_Task(config=config, pool=resource_pool) | ||
| 101 | self._wait_for_task(task) | ||
| 102 | if task.info.state != vim.TaskInfo.State.success: | ||
| 103 | raise Exception(task.info.error.msg) | ||
| 104 | |||
| 105 |         return {'vm_id': task.info.result._moId} | ||
| 106 |