segadson
Contributor
Contributor

Adding a Property to a virtual machine

Jump to solution

Hello I would like to add a property to a VM in vCAC with vCO so far I am using the code below with the inputs of a virtualMachine, propertyName, propertyValue

var model = "ManagementModelEntities.svc";

var entitySet = "VirtualMachineProperties";

var found = false;

var properties = new Properties();

properties.put("VirtualMachineID", virtualMachine.virtualMachineID);

var virtualMachineEntity = vCACEntityManager.readModelEntity(host.id, model, "VirtualMachineProperties", properties, null);

//Find property by property name

var virtualMachinePropertiesEntities = virtualMachineEntity.getLink(host, entitySet);

for each (var virtualMachinePropertiesEntity in virtualMachinePropertiesEntities) {

    if (virtualMachinePropertiesEntity.getProperty("PropertyName") == propertyName) {

        found = true;

        break;

    }   

}

var properties = new Properties();

properties.put("PropertyName", propertyName);

properties.put("PropertyValue", propertyValue);

properties.put("IsHidden", propertyIsHidden);

properties.put("IsRuntime", propertyIsRuntime);

properties.put("IsEncrypted", propertyIsEncrypted);

var links = new Properties();

links.put("virtualMachine",virtualMachineEntity);

if (found == true) {

    if (doNotUpdate == false && propertyValue != "") {

        var entityKey = virtualMachinePropertiesEntity.entityKey;

        var entityKeyId = entityKey.get("Id");

        System.log("Update entity id " + entityKeyId + " with property " + propertyName + " : " + propertyValue + " on virtualMachine entity " + blueprint.displayName);

        }

        else    {

            if (doNotUpdate == true) System.log("doNotUpdate set to true - not updating property " + propertyName);

            if (propertyValue == "") System.log("not updating property " + propertyName + " with empty value");       

        }

}

else {

    System.log("Create property " + propertyName + " : " + propertyValue + " on virtualMachine entity " + blueprint.displayName);

}

I keep on getting an exception error any help would be apprciated

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
jbweber2
Enthusiast
Enthusiast

You can use an inbuilt action to handle this which is addUpdatePropertyFromVirtualMachineEntity in com.vmware.library.vcac

View solution in original post

0 Kudos
2 Replies
jbweber2
Enthusiast
Enthusiast

You can use an inbuilt action to handle this which is addUpdatePropertyFromVirtualMachineEntity in com.vmware.library.vcac

0 Kudos
chrisheartland
Enthusiast
Enthusiast

Were you able to fully get this figured out? I want to do the same thing. I am trying to use a workflow called "create / update property on virtual machine entity" which is in essence just calling the "addUpdatePropertyFromVirtualMachineEntity" action for you. I am setting the property name to "VirtualMachine.Network0.Name" and the value is my network label. When it runs however I'm getting the error "The validated object is null (Dynamic Script Module name : addUpdatePropertyFromVirtualMachineEntity#7).

0 Kudos