vRO Entitlement and Business Group Action Snippets

Mindwatering Incorporated

Author: Tripp W Black

Created: 06/11 at 02:38 PM

 

Category:
VMWare
VRo - VCAC

List of vRO Module Action Samples from VMware collected here so don't have to search for them.

Get Entitlements of a Business Group:
// Custom Action - getBusinessGroups
// return - Array/vCACCAFE:Entitlement
// Action Inputs:
// businessGroup - vCACCAFE:Subtenant
// businessGroup.id - UUID
var host = vCACCAFEEntitiesFinder.getHostForEntity(businessGroup);
// CatalogOrgnizationReference __. BusinessGroup
var cc = host.createCatalogClient();
var ces = cc.getCatalogEntitlementService();

// search groups
var filters = [vCACCAFEFilterParam.equal("organization/subTenant/id", vCACCAFEFilterParam.string(businessGroup.id))];
var query = vCACCAFEOdataQuery.query().addFilter(filters);
var odataRequest = new vCACCAFEPageOdataRequest(query);
var entitlements = ces.get(host.tenant, odataRequest);
// loop entitlements for debug log
for each (var item in entitlements) {
System.log(item.name);
}
// return string to calling workflow
return entitlements;


Get Entitlements:
// Custom Action - getEntitlementNames
// return entitlements - Array/String - list of all the entitlement names
// start with empty array
var entitlements = [];

// Finding the CAFÉ host and the entitlements
var cafeHost = Server.findAllForType("vCACCAFE:VCACHost")[0];
var entitlementlist = vCACCAFEEntitiesFinder.findEntitlements(cafeHost);

// Storing the entitlement names in the variable entitlements
for ( i=0; i<entitlementlist.length; i++){
entitlements.push(entitlementlist[i].name)
}
// returning the variable with entitlements name
return entitlements;


Get Business Groups Names:
// Custom Action - getBusinessGroupNames
// return bgNames - Array/String - list of all the business group names
// start with empty array
var bgNames = [];

// Finding the CAFÉ host and then the Business Groups in that Tenant
var cafeHost = Server.findAllForType("vCACCAFE:VCACHost")[0];
var businessGroups = vCACCAFEEntitiesFinder.getBusinessGroups(cafeHost);

// Storing the names of all the Business Groups in the variable bgNames
for (i=0; i < businessGroups.length; i++) {
bgNames.push(businessGroups[i].name);
}

// returning the variable bgNames
return bgNames;


Get Business Group Object By Name:
// Custom Action - getBusinessGroup
// returns finalBG/vCACCAFE:Subtenant / vCACCAFE:BusinessGroup
// Action Input:
// host:vCACCAFE:VCACHost
// bgName:String
businessGroups = vCACCAFEEntitiesFinder.getBusinessGroups(vCACACAFEHost);
// Locate and return group in list of groups
for(i=0; i< businessGroups.length;i++){
if (bgName == businessGroups[i].name){
var finalBG = businessGroups[i];
break;
}
}
return finalBG;

Note:
To do a lowercase match update the if comparison to:
if (bgName.toLowerCase() == businessGroups[i].name.toLowerCase()){


Is User in Business Group:
// Custom Action - isUserInBusinessGroup
// return alreadyExists:boolean
// Action Input:
// bg / vCACCAFE:BusinessGroup
// userName / String
var userNames = finalBG.getUsers();
var size = 0;
if (userNames){
size = userNames.length;
}
var alreadyExists = false;

if (size) {
// Checking to see if the user already exists in the Business group
for (var j = 0 ; j < size ; j++ ) {
if (userName == userNames[j]) {
System.log("User " + userName + " is member of Business Group " + finalBG.getName() + ".");
alreadyExists = true;
break;
}
}
}
return alreadyExists;


Is User in Entitlement:
// Custom Action - isUserInBusinessGroup
// return alreadyExists:boolean
// Action Input:
// entitlement / vCACCAFE:Entitlement
// userName / String
var alreadyExists = false;
for (var j = 0 ; j < entitlement.getPrincipals().length ; j++ ) {
if (userName == entitlement.getPrincipals()[j].getRef()) {
System.log("User " + userName + " is member of entitlement " + entitlement.getName() + ". ");
alreadyExists = true;
break;
}
}
return alreadyExists;



_______________________


Workflow Snippets:

Find if User is Member of AD group, 'someUserGroup':
var grpNmChk = "SomeUserGroup";
var adGrps = new Array();
var alreadyMbr = false;
adGrps = usrObj.getArrayAttribute('memberOf');
for each (dn in adGrps) {
System.log("Group dn " + dn);
// check if matches group name
if (dn ==grpNmChk) {
alreadyMbr = true;
break;
}
}
return alreadyMbr;


Add User to group:
// In:
// userName / string - name off of form
// bGrp / vCACCAFE:BusinessGroup

try {
userNames[size] = userName;
bGrp.setUsers(userNames);
if( !bGrp.activeDirectoryContainer){
bGrp.setActiveDirectoryContainer("");
}
// add the user to Business Group
System.log("Adding user, " + userName + ", to Business Group " + bGrp.getName() + "...");
vCACACAFEHost.createInfrastructureClient().getInfrastructureBusinessGroupsService().update(bGrp);
System.log("User, " + userName + ", added to Business Group, " + bGrp.getName()) + ".";
}
catch(errorCode){
// In case of any error throw the error
System.error(errorCode);
throw errorCode;
}


Add User to Entitlement:
// In:
// userName / string - user id/name off of form or previous workflow out
// bgName/ string - group name off of form or previous workflow out
// entitlementNames / string - the selected names from the form or previous workflow out

try {
// getting a list of all the entitlements
entitlementslist = vCACCAFEEntitiesFinder.findEntitlements(vCACACAFEHost);

// Running the loop to get the entitlement which user specified
// entitlementnames holds all the entitlement names which user specified
// entitlementlist holds the list of all the entitlements.
// note entitlementnames is an array of strings while entitlementslist is an array of entitlement object
for( i =0; i<entitlementnames.length; i++){
for( j=0; j<entitlementslist.length; j++){
if ( entitlementNames[i] == entitlementslist[j].name){
entitlement = entitlementslist[j];
break;
}
}
// at this stage we found our entitlement object
// validating the object
System.getModule("com.vmware.library.vcaccafe.util").validateObject(entitlement, "Entitlement");

// Getting the CAFE host object for the entitlement
var host = vCACCAFEEntitiesFinder.getHostForEntity(entitlement);
// Getting the entitlement service object
var client = host.createCatalogClient().getCatalogEntitlementService();

var alreadyExists = false;

// Checking to see if the user already exists in the entitlement
for (var j = 0 ; j < entitlement.getPrincipals().length ; j++ ) {
if (userName == entitlement.getPrincipals()[j].getRef()) {
System.warn("User " + userName + " is already assigned to the entitlement "+entitlement.getName()+". The user will be skipped.");
alreadyExists = true;
break;
}
}
if (!alreadyExists) {
// This means user is not already in Entitlement
var principal = new vCACCAFECatalogPrincipal();
principal.setType(vCACCAFEPrincipalType.USER);
principal.setRef(userName);
principal.setTenantName(host.tenant);
System.getModule("com.vmware.library.vcaccafe.util").addElementToList(entitlement, "getPrincipals", principal);
System.log("Assigning user to entitlement " + entitlement.getName() + "...");
client.update(entitlement);
System.log("User assigned to entitlement " + entitlement.getName());
}
}
catch(errorCode){
System.error(errorCode);
throw errorCode;
}



Find AD Group:
var grpNm = "GrpToFind";
var grpUObj = Server.findAllForType("AD:UserGroup", grpNm); // find group by name, but only if a user type group
var grpObj = Server.findAllForType("AD:Group", grpNm); // find any group

Find AD User:
var usrNm = "tom.test@mydomain.com";
var usrObj = Server.findAllForType("AD:User", usrNm);
... or ...
var usrObj = ActiveDirectory.searchExactMatch("AD:User", usrNm);
... or ...
var svrAD= System.getModule("mydomain.activeDirectory").getAdHost(domain);
System.debug(adHost);
var usrObjs = ActiveDiretory.search("User", usrNm, svrAD);
if (usrObjs == null) {
return [];
}
System.log("Users objects found: " + usrObjs.length);

Get Users:
var svrAD= System.getModule("mydomain.activeDirectory").getAdHost(domain);
var usrObjs = ActiveDiretory.search("User", "", svrAD);
if (usrObjs == null) {
return [];
}
System.log("Users objects found: " + usrObjs.length);


Find a Computer (Name):
var vmNm = "Josh-02";
var vmObjs= ActiveDirectory.search("ComputerAD", vmNm, svrAD); // this can return more than one object for the name
System.log("Computer objects found: " + vmObjs.length);




previous page