Conditions | 5 |
Total Lines | 82 |
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 | #data_store = inventory.get_datastore(self.si_content, |
||
88 | #name=datastore) |
||
89 | datastore_path = '[' + datastore + '] ' + vm_name |
||
90 | else: |
||
91 | raise Exception('Error No Storage Data Provided') |
||
92 | |||
93 | #Now Datastore is known the remaining |
||
94 | #of VM Config can be setup and added |
||
95 | vmx_file = vim.vm.FileInfo(logDirectory=None, |
||
96 | snapshotDirectory=None, |
||
97 | suspendDirectory=None, |
||
98 | vmPathName=datastore_path) |
||
99 | config.files = vmx_file |
||
100 | |||
101 | #Create task to Build actual Machine |
||
102 | task = target_folder.CreateVM_Task(config=config, pool=resource_pool) |
||
103 | self._wait_for_task(task) |
||
104 | if task.info.state != vim.TaskInfo.State.success: |
||
105 | raise Exception(task.info.error.msg) |
||
106 | |||
107 | return {'vm_id': task.info.result._moId} |
||
108 |