forked from lgeeklee/pyvmomi-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_vm_properties.py
54 lines (47 loc) · 2.32 KB
/
get_vm_properties.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
"""
This file provides the get_properties function for import into another module.
"""
from __future__ import print_function
from pyVmomi import vmodl, vim
def get_properties(content, viewType, props, specType):
# Build a view and get basic properties for all Virtual Machines
"""
Obtains a list of specific properties for a particular Managed Object Reference data object.
:param content: ServiceInstance Managed Object
:param viewType: Type of Managed Object Reference that should populate the View
:param props: A list of properties that should be retrieved for the entity
:param specType: Type of Managed Object Reference that should be used for the Property Specification
"""
objView = content.viewManager.CreateContainerView(content.rootFolder, viewType, True)
tSpec = vim.PropertyCollector.TraversalSpec(name='tSpecName', path='view', skip=False, type=vim.view.ContainerView)
pSpec = vim.PropertyCollector.PropertySpec(all=False, pathSet=props, type=specType)
oSpec = vim.PropertyCollector.ObjectSpec(obj=objView, selectSet=[tSpec], skip=False)
pfSpec = vim.PropertyCollector.FilterSpec(objectSet=[oSpec], propSet=[pSpec], reportMissingObjectsInResults=False)
retOptions = vim.PropertyCollector.RetrieveOptions()
totalProps = []
retProps = content.propertyCollector.RetrievePropertiesEx(specSet=[pfSpec], options=retOptions)
totalProps += retProps.objects
while retProps.token:
retProps = content.propertyCollector.ContinueRetrievePropertiesEx(token=retProps.token)
totalProps += retProps.objects
objView.Destroy()
# Turn the output in retProps into a usable dictionary of values
gpOutput = []
for eachProp in totalProps:
propDic = {}
for prop in eachProp.propSet:
propDic[prop.name] = prop.val
propDic['moref'] = eachProp.obj
gpOutput.append(propDic)
return gpOutput
def main():
print('''
This module contains the function to get the properties of an object from vSphere.
Import this function when needed.
Example usage:
retProps = get_properties(content, [vim.VirtualMachine], ['name', 'snapshot'], vim.VirtualMachine)
retProps = get_properties(content, [vim.VirtualMachine], ['name', 'network'], vim.VirtualMachine)
''')
# Start program
if __name__ == "__main__":
main()