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