Completed
Pull Request — master (#460)
by Manas
02:32
created

GetVMs   B

Complexity

Total Complexity 36

Size/Duplication

Total Lines 108
Duplicated Lines 0 %
Metric Value
wmc 36
dl 0
loc 108
rs 8.8

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __add_vm_properties_to_map_from_vm_array() 0 8 3
F run() 0 97 33
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
            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
108
    @staticmethod
109
    def __add_vm_properties_to_map_from_vm_array(vm_map, vm_array):
110
        for vm in vm_array:
111
            if vm._GetMoId() not in vm_map:
112
                vm_map[vm._GetMoId()] = {
113
                      "moid": vm._GetMoId(),
114
                      "name": vm.name,
115
                      "runtime.powerState": vm.runtime.powerState
116
                      }
117