Conditions | 33 |
Total Lines | 97 |
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:
Complex classes like GetVMs.run() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | from sets import Set |
||
10 | def run(self, ids=None, names=None, datastores=None, datastore_clusters=None, resource_pools=None, vapps=None, hosts=None, folders=None, clusters=None, datacenters=None, virtual_switches=None, no_recursion=False ): |
||
11 | #TODO: food for thought. PowerCli contains additional parameters that are not present here for the folliwing reason: |
||
12 | #<server> - we may need to bring it in if we decide to have connections to more than 1 VC. |
||
13 | #<tag> - Tags in VC are not the same as tags you see in Web Client for the reason, that those tags are stored in Inventory Service only. PowerCli somehow can access it, from vSphere SDK there is no way. |
||
14 | |||
15 | si = self.si |
||
16 | si_content = si.RetrieveContent() |
||
17 | props = ['name', 'runtime.powerState'] |
||
18 | result = [] |
||
19 | moid_to_vm = {} |
||
20 | |||
21 | #getting vms by their ids |
||
22 | vms_from_vmids = [] |
||
23 | if ids: |
||
24 | vms_from_vmids = [vim.VirtualMachine(moid, stub=si._stub) for moid in ids] |
||
25 | GetVMs.__add_vm_properties_to_map_from_vm_array(moid_to_vm, vms_from_vmids) |
||
26 | |||
27 | #getting vms by their names |
||
28 | vms_from_names = [] |
||
29 | if names: |
||
30 | container = si_content.viewManager.CreateContainerView(si_content.rootFolder, [vim.VirtualMachine], True) |
||
31 | for vm in container.view: |
||
32 | if vm.name in names: |
||
33 | vms_from_names.append(vm) |
||
34 | GetVMs.__add_vm_properties_to_map_from_vm_array(moid_to_vm, vms_from_names) |
||
35 | |||
36 | #getting vms from datastore objects |
||
37 | vms_from_datastores = [] |
||
38 | if datastores: |
||
39 | vim_datastores = [vim.Datastore(moid, stub=si._stub) for moid in datastores] |
||
40 | for ds in vim_datastores: |
||
41 | vms_from_datastores.extend(ds.vm) |
||
42 | GetVMs.__add_vm_properties_to_map_from_vm_array(moid_to_vm, vms_from_datastores) |
||
43 | |||
44 | #getting vms from datastore cluster objects |
||
45 | vms_from_datastore_clusters = [] |
||
46 | if datastore_clusters: |
||
47 | vim_datastore_clusters = [vim.StoragePod(moid, stub=si._stub) for moid in datastore_clusters] |
||
48 | for ds_cl in vim_datastore_clusters: |
||
49 | for ds in ds_cl.childEntity: |
||
50 | vms_from_datastore_clusters.extend(ds.vm) |
||
51 | GetVMs.__add_vm_properties_to_map_from_vm_array(moid_to_vm, vms_from_datastore_clusters) |
||
52 | |||
53 | #getting vms from virtual switch objects |
||
54 | vms_from_virtual_switches = [] |
||
55 | if virtual_switches: |
||
56 | vim_virtual_switches = [vim.DistributedVirtualSwitch(moid, stub=si._stub) for moid in virtual_switches] |
||
57 | for vswitch in vim_virtual_switches: |
||
58 | for pg in vswitch.portgroup: |
||
59 | vms_from_virtual_switches.extend(pg.vm) |
||
60 | GetVMs.__add_vm_properties_to_map_from_vm_array(moid_to_vm, vms_from_virtual_switches) |
||
61 | |||
62 | #getting vms from containers (location param) |
||
63 | vms_from_containers = [] |
||
64 | containers = [] |
||
65 | |||
66 | if resource_pools: |
||
67 | containers += [vim.ResourcePool(moid, stub=si._stub) for moid in resource_pools] |
||
68 | |||
69 | if vapps: |
||
70 | containers += [vim.VirtualApp(moid, stub=si._stub) for moid in vapps] |
||
71 | |||
72 | if hosts: |
||
73 | containers += [vim.HostSystem(moid, stub=si._stub) for moid in hosts] |
||
74 | |||
75 | if folders: |
||
76 | containers += [vim.Folder(moid, stub=si._stub) for moid in folders] |
||
77 | |||
78 | if clusters: |
||
79 | containers += [vim.ComputeResource(moid, stub=si._stub) for moid in clusters] |
||
80 | |||
81 | if datacenters: |
||
82 | containers += [vim.Datacenter(moid, stub=si._stub) for moid in datacenters] |
||
83 | |||
84 | for cont in containers: |
||
85 | objView = si_content.viewManager.CreateContainerView(cont, [vim.VirtualMachine], not no_recursion) |
||
86 | tSpec = vim.PropertyCollector.TraversalSpec(name='tSpecName', path='view', skip=False, type=vim.view.ContainerView) |
||
87 | pSpec = vim.PropertyCollector.PropertySpec(all=False, pathSet=props, type=vim.VirtualMachine) |
||
88 | oSpec = vim.PropertyCollector.ObjectSpec(obj=objView, selectSet=[tSpec], skip=False) |
||
89 | pfSpec = vim.PropertyCollector.FilterSpec(objectSet=[oSpec], propSet=[pSpec], reportMissingObjectsInResults=False) |
||
90 | retOptions = vim.PropertyCollector.RetrieveOptions() |
||
91 | retProps = si_content.propertyCollector.RetrievePropertiesEx(specSet=[pfSpec], options=retOptions) |
||
92 | vms_from_containers += retProps.objects |
||
93 | while retProps.token: |
||
94 | retProps = si_content.propertyCollector.ContinueRetrievePropertiesEx(token=retProps.token) |
||
95 | vms_from_containers += retProps.objects |
||
96 | objView.Destroy() |
||
97 | |||
98 | for vm in vms_from_containers: |
||
99 | if vm.obj._GetMoId() not in moid_to_vm: |
||
100 | moid_to_vm[vm.obj._GetMoId()] = { |
||
101 | "moid": vm.obj._GetMoId(), |
||
102 | "name": vm.propSet[0].val, |
||
103 | "runtime.powerState": vm.propSet[1].val |
||
104 | } |
||
105 | |||
106 | return moid_to_vm.values() |
||
107 | |||
117 |