Hello,
What I want to do is find a set a VMs in vCAC and based on the property value I want to perform an action on them so far this is what I have but keep o getting an error. Any help would be great!
if (host == null){
System.log("No host was defined");
}
//get Virutal Machines from host
var virtualMachines = Server.findAllForType("vCAC:VirtualMachine");
for each (var virtualMachine in virtualMachines){
if (virtualMachine.isManaged == true) System.log("Found Virtual Machine" + " " + virtualMachine);
var properties = new Properties();
properties.put("VirtualMachineID", virtualMachine.virtualMachineID);
var virtualMachineEntity = vCACEntityManager.readModelEntity(host.id, "ManagementModelEntities.svc", "VirtualMachines", properties, null);
var virtualMachinePropertiesEntities = virtualMachineEntity.getLink(host, "VirtualMachineProperties");
for each (var virtualMachinePropertiesEntity in virtualMachinePropertiesEntities) {
if (virtualMachinePropertiesEntity.getProperty("PropertyName") == propertyName) {
}
}
}
Instead of trying to search for machines to find the properties, you can search for the properties to find the machine. In vCAC the Custom Properties are their own Entities just like Virtual Machines:
vmPropsProperties = new Properties();
vmPropsProperties.put("PropertyName", property_name);
var modelName = "ManagementModelEntities.svc";
var entitySetName = "VirtualMachineProperties";
var propertyEntities = vCACEntityManager.readModelEntitiesByCustomFilter(vcacHost.id, modelName, entitySetName, vmPropsProperties, null);
var virtualMachineEntities = new Array();
for each (var propertyEntity in propertyEntities) {
var propertyValue = propertyEntity.getProperty("PropertyValue");
if (propertyValue == desiredValue) {
virtualMachineEntities.push(propertyEntity.getLink(vcacHost, "VirtualMachine")[0]);
}
}
Instead of trying to search for machines to find the properties, you can search for the properties to find the machine. In vCAC the Custom Properties are their own Entities just like Virtual Machines:
vmPropsProperties = new Properties();
vmPropsProperties.put("PropertyName", property_name);
var modelName = "ManagementModelEntities.svc";
var entitySetName = "VirtualMachineProperties";
var propertyEntities = vCACEntityManager.readModelEntitiesByCustomFilter(vcacHost.id, modelName, entitySetName, vmPropsProperties, null);
var virtualMachineEntities = new Array();
for each (var propertyEntity in propertyEntities) {
var propertyValue = propertyEntity.getProperty("PropertyValue");
if (propertyValue == desiredValue) {
virtualMachineEntities.push(propertyEntity.getLink(vcacHost, "VirtualMachine")[0]);
}
}
Thanks that works. So I guess once I get that then the return value is vCAC VM entities for the VM's with that value. Once I have that then can I just sort those properties to just get vm's?
In my example virtualMachineEntities is an array of VirtualMachine entities (type VCAC:Entity). To get the VCAC:vCACVirtualMachine object of each, loop through that array and call .getInventoryObject() on each.
Ahhh ok makes sense. I am guessing you can do the samething by finding VMs through the entity sets for example using the current task of the VM to do updates. So in a nut shell I am trying to create a monitor solution to interact with a os build service. Because vCO doesnt really have a good switch statement type of workflows so what I want to do is run a series of schduled workflows that actions based on VM properties and looking at the current task that is on the vm entity. Thoughts?
Some small additions to filter the value directly in the query and optionally get all the properties for the vm results
function findVirtualMachinesWithPropertyValue(vcacHost, propertyName, propertyValue, getProperties) {
return vCACEntityManager.readModelEntitiesBySystemQuery(
vcacHost.id,
"ManagementModelEntities.svc",
"VirtualMachineProperties",
"PropertyName eq '" + propertyName + "' and PropertyValue eq '" + propertyValue + "'"
).reduce(function (vms, propertyEntity) {
// get the VirtualMachineEntity
var virtualMachine = propertyEntity.getLink(vcacHost, "VirtualMachine").shift()
var result = { vm: virtualMachine, properties: new Properties() }
// optionally create a properties object containing the vms properties
if (getProperties) {
virtualMachine.getLink(host, "VirtualMachineProperties").forEach(function (prop) {
result.properties.put(prop.getProperty("PropertyName"), prop.getProperty("PropertyValue"))
})
}
return vms.concat(result)
}, [])
}
Called like
var results = findVirtualMachinesWithPropertyValue(host, "__Cafe.Root.Request.Id", "some-uuid-here", true)
each result contains the vm entity and properties