To use this function add it to the class library file named vRealizeAutomation.ts
GitHub Repository: https://github.com/SimonSparksUK/Orchestrator
Filename: vRealizeAutomation.ts
Function addCustomPropertiesToVM
Description: The following code add Custom Properties to a VCACEntity Virtual Machine.
public addCustomPropertiesToVM (objVCACEntityVirtualMachine: VCACEntity, objProperties: Properties):void
{
let arrVCACEntityVirtualMachineProperty: VCACEntity[] = objVCACEntityVirtualMachine.getLink(this.objVCACHost, "VirtualMachineProperties");
let arrKey: string[] = objProperties.keys;
let objPropertiesLinks: Properties = new Properties();
objPropertiesLinks.put("VirtualMachine", objVCACEntityVirtualMachine);
let objPropertiesToAdd: Properties = new Properties();
objPropertiesToAdd.put("IsHidden", false);
objPropertiesToAdd.put("IsRuntime", false);
objPropertiesToAdd.put("IsEncrypted", false);
let strVirtualMachineName: string = objVCACEntityVirtualMachine.getProperty("VirtualMachineName");
arrKey.forEach((strKey: string):void =>
{
let strValue: string = objProperties.get(strKey);
objPropertiesToAdd.put("PropertyName", strKey);
objPropertiesToAdd.put("PropertyValue", strValue);
System.log("'" + strVirtualMachineName + "' - Creating Custom Property '" + strKey + "' to '" + strValue + "'.");
try
{
let objVCACEntityVirtualMachineProperty: VCACEntity = arrVCACEntityVirtualMachineProperty.find((objVCACEntityVirtualMachineProperty: VCACEntity):VCACEntity =>
{
if (objVCACEntityVirtualMachineProperty.getProperty("PropertyName") == strKey)
{
System.log("'" + strVirtualMachineName + "' - Found Existing Custom Property '" + strKey + "'.");
return objVCACEntityVirtualMachineProperty;
}
});
let objPropertiesHeaders: Properties = new Properties();
if (objVCACEntityVirtualMachineProperty)
{
let objEntityKey: Properties = objVCACEntityVirtualMachineProperty.entityKey;
let strEntityKeyId: string = objEntityKey.get("Id");
System.log("'" + strVirtualMachineName + "' - Updating Custom Property '" + strKey + "' with value '" + strValue + "'.");
vCACEntityManager.updateModelEntityBySerializedKey(this.objVCACHost.id, "ManagementModelEntities.svc", "VirtualMachineProperties", strEntityKeyId, objPropertiesToAdd, objPropertiesLinks, objPropertiesHeaders);
}
else
{
System.log("'" + strVirtualMachineName + "' - Creating Custom Property '" + strKey + "' with value '" + strValue + "'.");
vCACEntityManager.createModelEntity(this.objVCACHost.id, "ManagementModelEntities.svc", "VirtualMachineProperties", objPropertiesToAdd, objPropertiesLinks, objPropertiesHeaders);
}
}
catch (objException)
{
System.warn("'" + strVirtualMachineName + "' - Failed to Create Custom Property '" + strKey + "' to '" + strValue + "'.");
System.error(objException);
}
System.log("'" + strVirtualMachineName + "' - Successfully Created Custom Property '" + strKey + "' to '" + strValue + "'.");
});
}Discover more from Cloud Build Tools
Subscribe to get the latest posts sent to your email.
