Completed
Pull Request — master (#405)
by Edward
02:11
created

GetVMs   B

Complexity

Total Complexity 47

Size/Duplication

Total Lines 119
Duplicated Lines 0 %
Metric Value
dl 0
loc 119
rs 8.439
wmc 47

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __add_vm_properties_to_map_from_vm_array() 0 8 3
F run() 0 108 44

How to fix   Complexity   

Complex Class

Complex classes like GetVMs 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
2
3
from pyVmomi import vim
4
5
from vmwarelib import inventory
6
from vmwarelib.actions import BaseAction
7
8
class GetVMs(BaseAction):
9
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
            vm_moids = [moid.strip() for moid in ids.split(',')]
25
            vms_from_vmids = [vim.VirtualMachine(moid, stub=si._stub) for moid in vm_moids]
26
            GetVMs.__add_vm_properties_to_map_from_vm_array(moid_to_vm, vms_from_vmids)
27
28
        #getting vms by their names
29
        vms_from_names = []
30
        if names:
31
            vm_names_set = Set([name.strip() for name in names.split(',')])
32
            container = si_content.viewManager.CreateContainerView(si_content.rootFolder, [vim.VirtualMachine], True)
33
            for vm in container.view:
34
                if vm.name in vm_names_set:
35
                    vms_from_names.append(vm)
36
            GetVMs.__add_vm_properties_to_map_from_vm_array(moid_to_vm, vms_from_names)
37
38
        #getting vms from datastore objects
39
        vms_from_datastores = []
40
        if datastores:
41
            datastore_moids = [moid.strip() for moid in datastores.split(',')]
42
            vim_datastores = [vim.Datastore(moid, stub=si._stub) for moid in datastore_moids]
43
            for ds in vim_datastores:
44
                vms_from_datastores.extend(ds.vm)
45
            GetVMs.__add_vm_properties_to_map_from_vm_array(moid_to_vm, vms_from_datastores)
46
47
        #getting vms from datastore cluster objects
48
        vms_from_datastore_clusters = []
49
        if datastore_clusters:
50
            datastore_cluster_moids = [moid.strip() for moid in datastore_clusters.split(',')]
51
            vim_datastore_clusters = [vim.StoragePod(moid, stub=si._stub) for moid in datastore_cluster_moids]
52
            for ds_cl in vim_datastore_clusters:
53
                for ds in ds_cl.childEntity:
54
                  vms_from_datastore_clusters.extend(ds.vm)
55
            GetVMs.__add_vm_properties_to_map_from_vm_array(moid_to_vm, vms_from_datastore_clusters)
56
57
        #getting vms from virtual switch objects
58
        vms_from_virtual_switches = []
59
        if virtual_switches:
60
            virtual_switch_moids = [moid.strip() for moid in virtual_switches.split(',')]
61
            vim_virtual_switches = [vim.DistributedVirtualSwitch(moid, stub=si._stub) for moid in virtual_switch_moids]
62
            for vswitch in vim_virtual_switches:
63
                for pg in vswitch.portgroup:
64
                  vms_from_virtual_switches.extend(pg.vm)
65
            GetVMs.__add_vm_properties_to_map_from_vm_array(moid_to_vm, vms_from_virtual_switches)
66
67
        #getting vms from containers (location param)
68
        vms_from_containers = []
69
        containers = []
70
71
        if resource_pools:
72
            container_moids = [moid.strip() for moid in resource_pools.split(',')]
73
            containers += [vim.ResourcePool(moid, stub=si._stub) for moid in container_moids]
74
75
        if vapps:
76
            container_moids = [moid.strip() for moid in vapps.split(',')]
77
            containers += [vim.VirtualApp(moid, stub=si._stub) for moid in container_moids]
78
79
        if hosts:
80
            container_moids = [moid.strip() for moid in hosts.split(',')]
81
            containers += [vim.HostSystem(moid, stub=si._stub) for moid in container_moids]
82
83
        if folders:
84
            container_moids = [moid.strip() for moid in folders.split(',')]
85
            containers += [vim.Folder(moid, stub=si._stub) for moid in container_moids]
86
87
        if clusters:
88
            container_moids = [moid.strip() for moid in clusters.split(',')]
89
            containers += [vim.ComputeResource(moid, stub=si._stub) for moid in container_moids]
90
91
        if datacenters:
92
            container_moids = [moid.strip() for moid in datacenters.split(',')]
93
            containers += [vim.Datacenter(moid, stub=si._stub) for moid in container_moids]
94
95
        for cont in containers:
96
            objView = si_content.viewManager.CreateContainerView(cont, [vim.VirtualMachine], not no_recursion)
97
            tSpec = vim.PropertyCollector.TraversalSpec(name='tSpecName', path='view', skip=False, type=vim.view.ContainerView)
98
            pSpec = vim.PropertyCollector.PropertySpec(all=False, pathSet=props, type=vim.VirtualMachine)
99
            oSpec = vim.PropertyCollector.ObjectSpec(obj=objView, selectSet=[tSpec], skip=False)
100
            pfSpec = vim.PropertyCollector.FilterSpec(objectSet=[oSpec], propSet=[pSpec], reportMissingObjectsInResults=False)
101
            retOptions = vim.PropertyCollector.RetrieveOptions()
102
            retProps = si_content.propertyCollector.RetrievePropertiesEx(specSet=[pfSpec], options=retOptions)
103
            vms_from_containers += retProps.objects
104
            while retProps.token:
105
                retProps = si_content.propertyCollector.ContinueRetrievePropertiesEx(token=retProps.token)
106
                vms_from_containers += retProps.objects
107
            objView.Destroy()
108
109
        for vm in vms_from_containers:
110
            if vm.obj._GetMoId() not in moid_to_vm:
111
                moid_to_vm[vm.obj._GetMoId()] = {
112
                    "moid": vm.obj._GetMoId(),
113
                    "name": vm.propSet[0].val,
114
                    "runtime.powerState": vm.propSet[1].val
115
                }
116
117
        return moid_to_vm.values()
118
119
    @staticmethod
120
    def __add_vm_properties_to_map_from_vm_array(vm_map, vm_array):
121
        for vm in vm_array:
122
            if vm._GetMoId() not in vm_map:
123
                vm_map[vm._GetMoId()] = {
124
                      "moid": vm._GetMoId(),
125
                      "name": vm.name,
126
                      "runtime.powerState": vm.runtime.powerState
127
                      }
128