Dynamically Retrieving Compute Resource Locations | Writing about tech and anything else I find interesting

Dynamically Retrieving Compute Resource Locations

One of the most common queries I get is how to make the Location value mandatory.

Following closely on the heels of this query is how you can use the Location value as a means of filtering other elements as part of a request.

The bad news is that you can’t do either of these things with the default Location dropdown. The good news is that you can work around it with the use of the Vrm.DataCenter.Location property. The downside of using the custom property is that it’s typically looking at a static list – whether you define a drop down list in vRA, or pull the list from a configuration element in vRO. Storing a list for either of these methods requires updating it whenever you add a new location, and may list location values that are no longer in use. Wouldn’t it be great if you could create a dropdown that would query the Compute Resources in vRA, and return the assigned, valid locations?

This was the challenge put to me recently, and here is the action that I put together to do it.

Note that you will still need to update the DatacenterLocations.xml file and restart the Manager Service to make locations assignable in the first place.

function locations() {
  var host = Server.findAllForType("vCAC:vCACHost")[0];
  var locs = [];
  var model = "ManagementModelEntities.svc";
  var entitySetName = "HostProperties";
  var property = new Properties();
  property.put("PropertyName", "Vrm.DataCenter.Location");
  var computeResources = vCACEntityManager.readModelEntitiesByCustomFilter(host.id, model, entitySetName, property, null);
  for each (var computeResource in computeResources)
    System.log("Found the following locations " + locs);
	System.log("Removing duplicate locations");
	uLocs = locs.filter( function( loc, index, inputArray ) {
           return inputArray.indexOf(loc) == index;
	System.log("Unique location list: " + uLocs);
	return uLocs

if (locations().length >=1)
  return locations();
  return ["No locations have been configured, please contact an administrator."];

Drop a note in the comments if you have any questions.