Skip to content

Instantly share code, notes, and snippets.

@michaelrice
Created February 13, 2015 23:20
Show Gist options
  • Select an option

  • Save michaelrice/1683108ddcc3d3354ab3 to your computer and use it in GitHub Desktop.

Select an option

Save michaelrice/1683108ddcc3d3354ab3 to your computer and use it in GitHub Desktop.
public void lookupVmsByDataCenterRoot(Folder datacenterRoot, ServiceInstance si){
// Get the pcMOR MOR
ManagedObjectReference pcMOR = datacenterRoot.serverConnection.getServiceInstance().getPropertyCollector().getMOR()
// Get the root folder MOR
ManagedObjectReference dcRootFolder = datacenterRoot.MOR
// Create the property list
def props = ["name", "runtime.powerState", "config.template", "runtime.connectionState"]
PropertySpec vmPropSpec = new PropertySpec()
vmPropSpec.setAll(false)
vmPropSpec.setType('VirtualMachine')
vmPropSpec.setPathSet(props as String[])
def dcProps = ["name"]
PropertySpec dcSpec = new PropertySpec()
dcSpec.setAll(false)
dcSpec.setType("Datacenter")
dcSpec.setPathSet(dcProps as String[])
// Set the search base and search type
ObjectSpec objectSpec = new ObjectSpec()
objectSpec.setObj(dcRootFolder)
objectSpec.setSelectSet(PropertyCollectorUtil.buildFullTraversalV4())
// Create the filter spec
PropertyFilterSpec[] pfSpec = new PropertyFilterSpec[1]
pfSpec[0] = new PropertyFilterSpec()
ObjectSpec[] oo = [objectSpec]
pfSpec[0].setObjectSet(oo)
PropertySpec[] pp = [vmPropSpec, dcSpec]
pfSpec[0].setPropSet(pp)
log.trace("Making call to vcenter for property retrieval.")
def result = (List) si.serverConnection.getVimService().retrieveProperties(pcMOR, pfSpec)
//pfSpec[0].setObjectSet([objectSpec].toArray(new ObjectSpec[0]))
//pfSpec[0].setPropSet([vmPropSpec].toArray(new PropertySpec[0]))
result.each { ObjectContent objectContent ->
println(objectContent.obj.val)
objectContent.propSet.each {
println it.name + " -> " + it.val
}
}
// Do the search
//TODO: Add time logging
//ObjectContent[] results = si.serverConnection.vimService.retrieveProperties(pcMOR, [pfSpec].toArray(new PropertyFilterSpec[0]))
println "hi: ${result}"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment