VMware Cloud Community
yourdocumentati
Contributor
Contributor

How to create a new reservation in vRO

Basically what the title says. We've been trying to find documentation for how to create a new reservation in vRealize Automation with a Orchestrator workflow, but the documentation and examples one can find online seem to be woefully lacking. Anyone have any basic code/workflow examples to help out?

14 Replies
zwal1986
Enthusiast
Enthusiast

Which version of vRA are you running?

Reply
0 Kudos
yourdocumentati
Contributor
Contributor

Using version 7.3.

Reply
0 Kudos
AishR
VMware Employee
VMware Employee

You can use REST API to create the reservation in vRealize Automation.Create a Reservation

Reply
0 Kudos
kumar6384
Enthusiast
Enthusiast

Hi,  This is very useful to you, please refer the following code. Here is the action to create a reservation:

/***************************************
* Create a Reservation
* Input parameters:
* String businessGroupName
* vCAC:vCACHost iaasHost
* String eSXIHostName
* String reservationName
* Integer memorySize
* Integer storageSize
*
****************************************
*/

// Name of model being used
var modelName = "ManagementModelEntities.svc";

// links to other tables / objects
var links = null;

// no headers needed
var headers = null;

//Get the Business Group Entity
// Entity set that maps to Business Group is called ProvisioningGroups
var entitySetName = "ProvisioningGroups";

// Create property object that contains input value: businessGroup
var properties = {GroupName:businessGroupName };

// Read/retrieve the existing Business Group entity; there should be exactly one
var businessGroupEntities =  vCACEntityManager.readModelEntitiesByCustomFilter(iaasHost.id, modelName, entitySetName, properties, null);

if (businessGroupEntities.length <= 0 ) {
System.error("Business Group: " + businessGroupName + " not found.");
  errorCode = businessGroupName + "does not exist";
  throw businessGroupName + "does not exist";
}

System.debug("Business Group");
System.debug(businessGroupEntities[0].keyString);
System.debug(businessGroupEntities[0].getProperty('GroupName'));
System.debug(businessGroupEntities[0].getProperty('GroupID'));

//Get the eSXI Host entity
entitySetName = "Hosts";

// Create property object that contains input value: eSXIHostName
var properties = {HostName:eSXIHostName };

// Retrieve the existing Business Group entity; there should be exactly one
var eSXIHostEntities =  vCACEntityManager.readModelEntitiesByCustomFilter(iaasHost.id, modelName, entitySetName, properties, null);

if (eSXIHostEntities.length <= 0 ) {
System.error("eSIX Host: " + eSXIHostName + " not found.");
  errorCode = eSXIHostName + "does not exist";
  throw "No eSXI Host";
}

System.debug("eSXIHost");
System.debug(eSXIHostEntities[0].keyString);
System.debug(eSXIHostEntities[0].getProperty('HostID'));
System.debug(eSXIHostEntities[0].getProperty('HostName'));


if (businessGroupEntities.length > 1 ) {
System.error("Business Group: " + businessGroupName + " found " + businessGroupEntities.length + " has duplicates, can't link to reservation");
  errorCode = businessGroupName + " has duplicates, can't link to reservation";
  throw ("Business Group: " + businessGroupName + " found " + businessGroupEntities.length + " ambigious, can't link to reservation");
}

System.debug("Found Business Group: " + businessGroupEntities[0].getProperty('GroupName'));

System.debug("Found eSXI Host: ", eSXIHostEntities[0].getProperty('HostID'));

//Create Reservation
entitySetName = "HostReservations";

links = {ProvisioningGroup:businessGroupEntities[0], Host:eSXIHostEntities[0] };
var headers = null;

var testResProperties = {
  HostReservationName:reservationName,
  };

//Verify that no reservation exists with the same name for this business group
var existsRes = vCACEntityManager.readModelEntitiesByCustomFilter(iaasHost.id, modelName, entitySetName, testResProperties, null);
if (existsRes.length > 0 ) {
  System.error("Reservation : " + reservationName + " already exists; cannot create duplicate");
  throw ("Reservation : " + reservationName + " already exists; cannot create duplicate");
  }

// hard-coded for testing; should become input fields
var reservationProperties = {
  HostReservationName:reservationName,
  Enabled:true,
  ReservationMemorySizeMB:memorySize,
  ReservationStorageSizeGB:storageSize,
  MaxVMsPowerOn:99,
  ReservationPriority:1
  };

// create the Reservation
var resEntity = vCACEntityManager.createModelEntity(iaasHost.id, modelName, entitySetName, reservationProperties, links, headers);

// check Reservation entity created
if (resEntity == null ) {
  System.error("ERROR: Reservation Entity " + reservationName + " not created");
  throw("Create reservation " + reservationName + " failed");
  }
//Verify reservation by retrieving entity
var testRes = vCACEntityManager.readModelEntitiesByCustomFilter(iaasHost.id, modelName, entitySetName, reservationProperties, null);

if (testRes.length < 1 )
  System.error("ERROR: Reservation Entity " + reservationName + " not found");

//Return the new Reservation
System.log("Created Reservation: " + reservationName);
return (resEntity);

kumar6384
Enthusiast
Enthusiast

Hi,  This is very useful to you, please refer the following code.

/***************************************

* Create a Reservation   *

/***************************************

* Input parameters:

---------------------

* String reservationPolicyName

* Attributes parameters

-------------------------

* String allocatedStoragePathLabel

* String businessGroupName

* String computeResourceName

* String networkName

* String networkProfile

* String reservationName

* String eSXIHostName

* String resourcePoolName

* vCAC:vCACHost iaasHost

* vCACCAFE:VCACHost cafeHost

* Integer memorySize

* Integer storageSize

****************************************

*/

var tenantName = cafeHost.tenant;

computeResourceName = getCaseSensitiveComputeResourceName(iaasHost, computeResourceName);

System.log(tenantName);

var cafeReservation = getReservationForComputeResource(cafeHost, computeResourceName);

if(cafeReservation == null){

var reservationTypeId = "Infrastructure.Reservation.Virtual.vSphere";

//var allocatedMemorySize = getComputeResourceMemorySize(iaasHost, computeResourceName);

//var allocatedStorageSize = getStorageFullSize(iaasHost, allocatedStoragePathLabel);

// Client and Services

var cafeReservationClient = cafeHost.createReservationClient();

var reservationService = cafeReservationClient.getReservationReservationService();

var businessGroup = getBusinessGroupByName(cafeHost, businessGroupName)

var businessGroupID = businessGroup.getId();

// Reservation Specification

cafeReservation = new vCACCAFEReservation();

cafeReservation.setName(reservationName);

cafeReservation.setTenantId(tenantName);

cafeReservation.setSubTenantId(businessGroupID);

cafeReservation.setPriority(1);

var reservationPolicyId = getReservationPolicyIdByPolicyName(iaasHost, reservationPolicyName)

cafeReservation.setReservationPolicyId(reservationPolicyId);

cafeReservation.setEnabled(true);

cafeReservation.setReservationTypeId(reservationTypeId);

// Reservation Arets

var alertPrecentageDefaultValue = 80

var reservationAlertPolicy = new vCACCAFEAlertPolicy()

reservationAlertPolicy.setEnabled(true);

var storageAlert = new vCACCAFEAlert();

storageAlert.setAlertPercentLevel(alertPrecentageDefaultValue);

storageAlert.setReferenceResourceId("storage")

var memoryAlert = new vCACCAFEAlert();

memoryAlert.setAlertPercentLevel(alertPrecentageDefaultValue);

memoryAlert.setReferenceResourceId("memory")

var cpuAlert = new vCACCAFEAlert();

cpuAlert.setAlertPercentLevel(alertPrecentageDefaultValue);

cpuAlert.setReferenceResourceId("cpu")

var machineAlert = new vCACCAFEAlert();

machineAlert.setAlertPercentLevel(alertPrecentageDefaultValue);

machineAlert.setReferenceResourceId("machine")

System.getModule("com.vmware.library.vcaccafe.util").addElementToList(reservationAlertPolicy,"getAlerts", storageAlert);

System.getModule("com.vmware.library.vcaccafe.util").addElementToList(reservationAlertPolicy,"getAlerts", memoryAlert);

System.getModule("com.vmware.library.vcaccafe.util").addElementToList(reservationAlertPolicy,"getAlerts", cpuAlert);

System.getModule("com.vmware.library.vcaccafe.util").addElementToList(reservationAlertPolicy,"getAlerts", machineAlert);

cafeReservation.setAlertPolicy(reservationAlertPolicy);

// Set Extension Data

var computeResourceLiteral = buildComputeResource(iaasHost, computeResourceName);

var machineQuota = new vCACCAFEIntegerLiteral(0);

var memoryToAllocateLiteral =  buildMemoryLiteral(memorySize);

var storageReservationPathLiteral = buildReservationStorageMultiLiteral(iaasHost, computeResourceName, allocatedStoragePathLabel, 0, storageSize);

var networkLiteral = buildNetworksMultiLiteral(iaasHost,computeResourceName, networkName, networkProfile);

var extData = new vCACCAFELiteralMap();

extData.put("computeResource", computeResourceLiteral);

extData.put("machineQuota", machineQuota);

extData.put("reservationMemory", memoryToAllocateLiteral);

extData.put("reservationStorages", storageReservationPathLiteral);

extData.put("reservationNetworks", networkLiteral);

cafeReservation.setExtensionData(extData);

reservationService.createReservation(cafeReservation);

cafeReservation = getReservationByName(cafeHost, reservationName);

System.log("Reservation with name " + reservationName + " created successfully.")

}

//--------------------------------------------------

function getCaseSensitiveComputeResourceName(infrastructureHost, targetComputeResourceName){

var modelName = 'ManagementModelEntities.svc';

var entitySetName = "Hosts";

var computeResourceEntities = vCACEntityManager.readModelEntitiesByCustomFilter(infrastructureHost.id, modelName, entitySetName, null, null);

for each(var computeResourceEntity in computeResourceEntities){

var caseSensitiveComputeResourceName = computeResourceEntity.getProperty("HostName");

if(caseSensitiveComputeResourceName.toUpperCase() == targetComputeResourceName.toUpperCase()){

return caseSensitiveComputeResourceName;

}

}

throw("No compute resource with name " + targetComputeResourceName + " was found");

}

function getReservationForComputeResource(tenantCafeHost, computeResource){

System.log("Search for reservation for compute resource " + computeResource);

var reservationClient = tenantCafeHost.createReservationClient();

var reservationService = reservationClient.getReservationReservationService();

var tenantReservationPagedResources = reservationService.getAllReservations(null);

var targetReservation;

for each(var reservation in tenantReservationPagedResources.getContent()){

var extensionData = reservation.getExtensionData();

var reservationComputeResource = extensionData.get("computeResource").getLabel();

System.log("reservation " + reservation.name);

System.log("computeResource " + reservationComputeResource);

}

return targetReservation;

}

function getReservationByName(tenantCafeHost, name){

System.log("Search for reservation with name " + name);

var reservationClient = tenantCafeHost.createReservationClient();

var reservationService = reservationClient.getReservationReservationService();

var tenantReservationPagedResources = reservationService.getAllReservations(null);

for each(var reservation in tenantReservationPagedResources.getContent()){

System.log("Reservation " + reservation.getName())

if(reservation.getName().toLowerCase() == name.toLowerCase()){

return reservation;

}

}

return null;

}

function getReservationPolicyIdByPolicyName(vcacHost, policyName){

var reservationPolicyEntity = getInfrastructureManagementModelEntityByProperty(vcacHost, "HostReservationPolicies", "name", policyName);

var policyId = null;

if(reservationPolicyEntity != null){

System.log("Reservation policy with name  " + policyName + " found.");

policyId = reservationPolicyEntity.getProperty("id");

}

return policyId;

}

function getBusinessGroupByName(cafehost, groupName){

var groups = vCACCAFEEntitiesFinder.getSubtenants(cafeHost);

for each(var group in groups){

if(group.getName() == groupName){

return group;

}

}

throw "Business group with name " + groupName + " was not found"

}

function buildComputeResource(vcacHost, computeResource){

var id = getComputeResourceIdByComputeResourceName(vcacHost, computeResource);

System.log("Compute Resource with properties: name= " + computeResource + " / id= " + id + " found.")

var componentId = null;

var classId = "ComputeResource";

var entityReference = new vCACCAFEEntityReference(componentId, classId, id, computeResource) ;

System.log("buildComputeResource successfull");

return entityReference;

}

function getComputeResourceIdByComputeResourceName(vcacHost, resourceName){

var computeResourceEntity = getInfrastructureManagementModelEntityByProperty(vcacHost, "Hosts", "HostName", resourceName);

if(computeResourceEntity != null){

System.log("Compute Resource with name  " + resourceName + " found.");

}else{

System.log("Compute Resource with name" + resourceName + " not found.");

}

return computeResourceEntity.getProperty("HostID");;

}

function buildMemoryLiteral(memoryReservedSize){

var literalMap = new vCACCAFELiteralMap() ;

literalMap.put("memoryReservedSizeMb", new vCACCAFEIntegerLiteral(memoryReservedSize));

var componentTypeId = "com.vmware.csp.iaas.blueprint.service";

var componentId = null

var classId = "Infrastructure.Reservation.Memory";

var typeFilter = null;

var memoryLiteral = new vCACCAFEComplexLiteral(componentTypeId , componentId , classId , typeFilter, literalMap);

return memoryLiteral;

}

function buildComplexStoragePathLiteral(vcacHost, computeResourceHostName, storagePathLabel, storageReservationPrio, sizeGB){

var literalMap =  new vCACCAFELiteralMap();

literalMap.put("storagePath", getStoragePathEntityReferenceLiteral(vcacHost, computeResourceHostName, storagePathLabel));

literalMap.put("storageReservationPriority", new vCACCAFEIntegerLiteral(storageReservationPrio)) ;

literalMap.put("storageReservedSizeGB", new vCACCAFEIntegerLiteral(sizeGB));

literalMap.put("storageEnabled", vCACCAFEBooleanLiteral.fromBoolean(true)) ;

var componentTypeId = "com.vmware.csp.iaas.blueprint.service";

var componentId = null;

var classId = "Infrastructure.Reservation.Storage";

var typeFilter = null;

var storagePathComplexLiteral = new vCACCAFEComplexLiteral(componentTypeId, componentId, classId, typeFilter, literalMap);

return storagePathComplexLiteral;

}

function buildReservationStorageMultiLiteral(vcacHost, computeResourceHostName, storagePathLabel, storageReservationPrio, sizeGB){

var storagePathLiteralMap = buildComplexStoragePathLiteral(vcacHost, computeResourceHostName, storagePathLabel, storageReservationPrio, sizeGB);

var complexArray =  new Array();

complexArray.push(storagePathLiteralMap);

var typeID = vCACCAFEDataTypeId.valueOf("COMPLEX") ;

var multiLiteral = new vCACCAFEMultipleLiteral(complexArray, typeID);

System.log("buildReservationStorage successfull")

return  multiLiteral;

}

function getStoragePathEntityReferenceLiteral(vcacHost, computeResourceHostName, computeResource){

var id = getStoragePathIdByStoragePathLabel(vcacHost, computeResourceHostName, computeResource);

System.log("Storage Path ID " + id)

var componentId = null;

var classId = "Storage";

var entityReference = new vCACCAFEEntityReference(componentId, classId, id, computeResource);

return entityReference;

}

function getStoragePathIdByStoragePathLabel(vcacHost, computeResourceHostName, storagePathLabel){

var hostToStorages = getInfrastructureManagementModelEntitiesByProperty(vcacHost, "HostToStorage", "StoragePath", storagePathLabel);

var hostToStorage = null;

for each(var hostToStorageEntity in hostToStorages){

var host = hostToStorageEntity.getLink(vcacHost, "Host")[0];

if(host.getProperty("HostName") == computeResourceHostName){

hostToStorage = hostToStorageEntity;

break;

}

}

if(hostToStorage != null){

System.log("Storage with storage path " + hostToStorage.getProperty("StoragePath") + " found.");

}else{

System.log("Storage with storage path " + storagePath + " not found.");

}

return hostToStorage.getProperty("HostToStorageID");

}

function buildComplexNetworkLiteral(vcacHost, computeResourceHostName, netLabel, netProfileLabel){

var literalMap =  new vCACCAFELiteralMap();

literalMap.put("networkPath", getNetworkPathEntityReference(vcacHost, computeResourceHostName, netLabel));

literalMap.put("networkProfile", getNetworkProfileEntityReference(vcacHost, netProfileLabel));

var componentTypeId = "com.vmware.csp.iaas.blueprint.service";

var componentId = null;

var classId = "Infrastructure.Reservation.Network";

var typeFilter = null;

return new vCACCAFEComplexLiteral(componentTypeId, componentId, classId, typeFilter, literalMap);

}

function buildNetworksMultiLiteral(vcacHost, computeResourceHostName, netLabel, netProfileLabel){

var complexNetworkLiteral = buildComplexNetworkLiteral(vcacHost, computeResourceHostName, netLabel, netProfileLabel);

var complexArray =  new Array();

complexArray.push(complexNetworkLiteral);

var typeID = vCACCAFEDataTypeId.valueOf("COMPLEX") ;

var networkMultiLiteral = new vCACCAFEMultipleLiteral(complexArray, typeID);

System.log("buildNetworkLiteral successfull")

return  networkMultiLiteral;

}

function getNetworkPathEntityReference(vcacHost, computeResourceHostName, netLabel){

var id = getNetworkId(vcacHost, netLabel, computeResourceHostName);

System.log("Network ["+netLabel+"] id = " + id);

var componentId = null;

var classId = "Network";

var entityReference = new vCACCAFEEntityReference(componentId, classId, id, netLabel);

return entityReference;

}

function getNetworkProfileEntityReference(vcacHost, netProfileLabel){

var id = getNetworkProfileIdByLabel(vcacHost, netProfileLabel);

var entityReference =null;

if(id){

var componentId = null;

var classId = "NetworkProfile";

entityReference = new vCACCAFEEntityReference(componentId, classId, id, netProfileLabel);

}

return entityReference;

}

function getNetworkId(vcacHost, netLabel, computeResourceHostName){

var networks = getInfrastructureManagementModelEntitiesByProperty(vcacHost, "HostNics", "HostNicName", netLabel);

var resultNetwork = null;

for each(var network in networks){

var host = network.getLink(vcacHost, "Host")[0];

var hostName = host.getProperty("HostName");

if(hostName.toLowerCase() == computeResourceHostName.toLowerCase()){

resultNetwork = network;

break;

}

}

if(resultNetwork == null){

throw("No network with name [" + netLabel + "] was found for compute resource [" + computeResourceHostName +"]");

}

return resultNetwork.getProperty("HostNicID");

}

function getNetworkProfileIdByLabel(vcacHost, netProfileLabel){

var networkProfile = getInfrastructureManagementModelEntityByProperty(vcacHost, "StaticIPv4NetworkProfiles", "StaticIPv4NetworkProfileName", netProfileLabel);

var id = null;

if(networkProfile != null){

System.log("Netowork profile with name " + netProfileLabel + " found.");

id = networkProfile.getProperty("ID");

}else{

System.log("Netowork profile with name " + netProfileLabel + " not found.");

}

return id;

}

function getInfrastructureManagementModelEntitiesByProperty(vcacHost, entityType, propertyName, propertyValue){

var modelName = 'ManagementModelEntities.svc';

var entitySetName = entityType;

var inputProperties = new Properties();

inputProperties.put(propertyName, propertyValue );

return vCACEntityManager.readModelEntitiesByCustomFilter(vcacHost.id, modelName, entitySetName, inputProperties, null);

}

function getInfrastructureManagementModelEntityByProperty(vcacHost, entityType, propertyName, propertyValue){

var modelName = 'ManagementModelEntities.svc';

var entitySetName = entityType;

var inputProperties = new Properties();

inputProperties.put(propertyName, propertyValue);

return getInfrastructureManagementModelEntitiesByProperty(vcacHost, entityType, propertyName, propertyValue)[0];

}

function getInfrastructureManagementModelEntities(vcacHost, entityType, inputProperties){

var modelName = 'ManagementModelEntities.svc';

var entitySetName = entityType;

return vCACEntityManager.readModelEntitiesByCustomFilter(vcacHost.id, modelName, entitySetName, inputProperties, null);

}

ymichalak
Hot Shot
Hot Shot

Thx @

Reply
0 Kudos
ymichalak
Hot Shot
Hot Shot

Other question Smiley Happy

I don't understand this function :

function getReservationForComputeResource(tenantCafeHost, computeResource){

    System.log("Search for reservation for compute resource " + computeResource);

    var reservationClient = tenantCafeHost.createReservationClient();

    var reservationService = reservationClient.getReservationReservationService();

    var tenantReservationPagedResources = reservationService.getAllReservations(null);

    var targetReservation;

    for each(var reservation in tenantReservationPagedResources.getContent()){

        var extensionData = reservation.getExtensionData();

        var reservationComputeResource = extensionData.get("computeResource").getLabel();

        System.log("reservation " + reservation.name);

        System.log("computeResource " + reservationComputeResource);

    }

    

    return targetReservation;

}

It's just a debug ?

Reply
0 Kudos
Czernobog
Expert
Expert

Thanks a lot for this, works like a charm with 7.3.1, I hope VMware builds a script into the default library that comes with the vRA Plugin...
ymichalak
Hot Shot
Hot Shot

thx for your feedback Smiley Happy

We wish a 7.4 or 8.0 version of vRA/vRO but a 7.3.1 version with these element is pretty cool Smiley Wink

Reply
0 Kudos
ymichalak
Hot Shot
Hot Shot

Hi guys 😉

 

Do you know how to retrieve ResourcePoolID linked to a vCenter Cluster from vRO ?

https://communities.vmware.com/thread/585061

 

Thx for your help.

Reply
0 Kudos
LqrsP
Contributor
Contributor

The result of this function is used to check if the reservation isn't already created, only the output is never set.

However, I think this is not intended, and that the getReservationForComputeResource function is indeed only for debugging purposes.

The check

var cafeReservation = getReservationForComputeResource(cafeHost, computeResourceName);

if (cafeReservation == null) {

...

}

will always be true and can be replaced by

var cafeReservation = getReservationByName(cafeHost, reservationName);

if (cafeReservation == null) {

...

}

Reply
0 Kudos
KatG
Contributor
Contributor

Has anyone been able to successfully create a VRO workflow from this script?  I've been grinding away at it for 6 hours without success.  I'm concerned I may be working with a script that doesn't actually work (no offense to the author; a lot has changed in vra/vro).  Thanks
Reply
0 Kudos
KatG
Contributor
Contributor

Finally got the 2nd script working with the exception of the Resource Pool.  The attribute resourcePoolName is set in the script but never applied to the new reservation.  Additionally, I need to set a custom property, any guidance is appreciated.
Reply
0 Kudos
unibn
Contributor
Contributor

Hi ymichalak​ , hi kumar6384​,

Thanks for the script, it works very well!

However, I can't adapt the script so that I can add two (or more) storages to the reservation.

Could one of you customize the script to include multiple storages or networks in the new reservation?

Reply
0 Kudos