Last active
October 26, 2015 00:33
-
-
Save yfauser/aa8a218f6fc5b5ca6147 to your computer and use it in GitHub Desktop.
This is an example on how to retrieve several vCenter moids, including the DC, Cluster, RP, DVS, Datastore and Port-Group Objects
This file contains 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
#!/usr/bin/env python | |
# coding=utf-8 | |
# | |
# Copyright © 2015 VMware, Inc. All Rights Reserved. | |
# | |
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated | |
# documentation files (the "Software"), to deal in the Software without restriction, including without limitation | |
# the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and | |
# to permit persons to whom the Software is furnished to do so, subject to the following conditions: | |
# | |
# The above copyright notice and this permission notice shall be included in all copies or substantial portions | |
# of the Software. | |
# | |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED | |
# TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF | |
# CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS | |
# IN THE SOFTWARE. | |
__author__ = 'yfauser' | |
from pyVim.connect import SmartConnect | |
from pyVmomi import vim, vmodl | |
VIM_TYPES = {'datacenter': [vim.Datacenter], | |
'dvs': [vim.dvs.VmwareDistributedVirtualSwitch], | |
'datastore': [vim.Datastore], | |
'resourcepool': [vim.ResourcePool]} | |
def get_cluster_mo(datacenter_mo, name): | |
host_folder = datacenter_mo.hostFolder | |
for child in host_folder.childEntity: | |
if child.name == name: | |
return child | |
return None | |
def get_portgroup_mo(datacenter_mo, name): | |
network_folder = datacenter_mo.network | |
for network in network_folder: | |
if network.name == name: | |
return network | |
return None | |
def get_mo(content, searchedname, vim_type_list): | |
mo = get_all_objs(content, vim_type_list) | |
for object in mo: | |
if object.name == searchedname: | |
return object | |
return None | |
def get_all_objs(content, vimtype): | |
obj = {} | |
container = content.viewManager.CreateContainerView(content.rootFolder, vimtype, True) | |
for managed_object_ref in container.view: | |
obj.update({managed_object_ref: managed_object_ref.name}) | |
return obj | |
def connect_to_api(vchost, user='root', pwd='vmware'): | |
service_instance = SmartConnect(host=vchost, user=user, pwd=pwd) | |
return service_instance.RetrieveContent() | |
def main(): | |
content = connect_to_api('172.17.100.60') | |
datacenter_mo = get_mo(content, 'nsxlabdc', VIM_TYPES['datacenter']) | |
datacenter_moid = datacenter_mo._moId | |
cluster_mo = get_cluster_mo(datacenter_mo, 'compute') | |
cluster_moid = cluster_mo._moId | |
resourcepool_mo = get_mo(content, 'test_rp', VIM_TYPES['resourcepool']) | |
resourcepool_moid = resourcepool_mo._moId | |
dvs_mo = get_mo(content, 'TransportVDS', VIM_TYPES['dvs']) | |
dvs_moid = dvs_mo._moId | |
portgroup_mo = get_portgroup_mo(datacenter_mo, 'VM Network') | |
portgroup_moid = portgroup_mo._moId | |
datastore_mo = get_mo(content, 'NFS-Storage03', VIM_TYPES['datastore']) | |
datastore_moid = datastore_mo._moId | |
print datacenter_moid | |
print cluster_moid | |
print resourcepool_moid | |
print dvs_moid | |
print datastore_moid | |
print portgroup_moid | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment