Last active
August 29, 2015 14:16
-
-
Save michaelrice/42f915e8809fd0c36e49 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Copyright 2015 Michael Rice <[email protected]> | |
| # | |
| # Licensed under the Apache License, Version 2.0 (the "License"); | |
| # you may not use this file except in compliance with the License. | |
| # You may obtain a copy of the License at | |
| # | |
| # http://www.apache.org/licenses/LICENSE-2.0 | |
| # | |
| # Unless required by applicable law or agreed to in writing, software | |
| # distributed under the License is distributed on an "AS IS" BASIS, | |
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| # See the License for the specific language governing permissions and | |
| # limitations under the License. | |
| """ | |
| Utility functions for the vSphere API | |
| """ | |
| from pyVmomi import vim, vmodl | |
| def build_full_traversal(): | |
| """ | |
| Builds full traversal spec similar to the one being used in yavijava. | |
| :return: | |
| """ | |
| traversal_spec = vmodl.query.PropertyCollector.TraversalSpec | |
| selection_spec = vmodl.query.PropertyCollector.SelectionSpec | |
| # Go through all ResourcePools | |
| rp_to_rp = traversal_spec( | |
| name="rp_to_rp", | |
| type=vim.ResourcePool, | |
| path="resourcePool", | |
| skip=False | |
| ) | |
| rp_to_rp.selectSet.extend( | |
| (selection_spec(name="rp_to_rp"), | |
| selection_spec(name="rp_to_vm")) | |
| ) | |
| # Resource Pools | |
| rp_to_vm = traversal_spec( | |
| name="rp_to_vm", | |
| type=vim.ResourcePool, | |
| path="vm", | |
| skip=False | |
| ) | |
| # Traversal through ResourcePool branch | |
| cr_to_rp = traversal_spec( | |
| name="cr_to_rp", | |
| type=vim.ComputeResource, | |
| path="resourcePool", | |
| skip=False | |
| ) | |
| cr_to_rp.selectSet.extend( | |
| (selection_spec(name="rp_to_rp"), | |
| selection_spec(name="rp_to_vm")) | |
| ) | |
| # Traversal through ResourcePool branch | |
| vapp_to_rp = traversal_spec( | |
| name="vapp_to_rp", | |
| type=vim.VirtualApp, | |
| path="resourcePool", | |
| skip=False | |
| ) | |
| vapp_to_rp.selectSet.extend( | |
| (selection_spec(name="rp_to_rp"), | |
| selection_spec(name="vapp_to_rp")) | |
| ) | |
| # Traversal through host branch | |
| cr_to_h = traversal_spec( | |
| name="cr_to_h", | |
| type=vim.ComputeResource, | |
| path="host", | |
| skip=False | |
| ) | |
| # Traversal through vmFolder branch | |
| dc_to_vmf = traversal_spec( | |
| name="dc_to_vmf", | |
| type=vim.Datacenter, | |
| path="vmFolder", | |
| skip=False | |
| ) | |
| dc_to_vmf.selectSet.extend( | |
| [selection_spec(name="visit_folders")] | |
| ) | |
| # Traversal through hostFolder branch | |
| dc_to_hf = traversal_spec( | |
| name="dc_to_hf", | |
| type=vim.Datacenter, | |
| path="hostFolder", | |
| skip=False | |
| ) | |
| dc_to_hf.selectSet.extend( | |
| [selection_spec(name="visit_folders")] | |
| ) | |
| # Traversal through network folder branch | |
| dc_to_netf = traversal_spec( | |
| name="dc_to_netf", | |
| type=vim.Datacenter, | |
| path="networkFolder", | |
| skip=False | |
| ) | |
| dc_to_netf.selectSet.extend( | |
| [selection_spec(name="visit_folders")] | |
| ) | |
| # Traversal through datastore branch | |
| dc_to_ds = traversal_spec( | |
| name="dc_to_ds", | |
| type=vim.Datacenter, | |
| path="datastoreFolder", | |
| skip=False | |
| ) | |
| dc_to_ds.selectSet.extend( | |
| [selection_spec(name="visit_folders")] | |
| ) | |
| # Recurse through all hosts | |
| h_to_vm = traversal_spec( | |
| name="h_to_vm", | |
| type=vim.HostSystem, | |
| path="vm", | |
| skip=False | |
| ) | |
| h_to_vm.selectSet.extend( | |
| [selection_spec(name="visit_folders")] | |
| ) | |
| # Recurse through the folders | |
| visit_folders = traversal_spec( | |
| name="visit_folders", | |
| type=vim.Folder, | |
| path="childEntity", | |
| skip=False | |
| ) | |
| visit_folders.selectSet.extend( | |
| ( | |
| selection_spec(name="visit_folders"), | |
| selection_spec(name="dc_to_hf"), | |
| selection_spec(name="dc_to_vmf"), | |
| selection_spec(name="dc_to_netf"), | |
| selection_spec(name="cr_to_h"), | |
| selection_spec(name="cr_to_rp"), | |
| selection_spec(name="dc_to_ds"), | |
| selection_spec(name="h_to_vm"), | |
| selection_spec(name="rp_to_vm") | |
| ) | |
| ) | |
| full_traversal = selection_spec.Array( | |
| (visit_folders, dc_to_hf, dc_to_vmf, dc_to_netf, cr_to_h, cr_to_rp, dc_to_ds, rp_to_rp, h_to_vm, rp_to_vm) | |
| ) | |
| return full_traversal |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment