The Problem
Since vRA 7.5 we have the option to change the Name & Description of our Deployment via the UI as shown below:
But what if we would like to Change a Deployment’s Name programatically? What if we would like to set it during provisioning to match our VM’s Name for example? Can we do that? The short answer: Yes! With an undocumented API call (at least at time of writing this article).
I’ve came across a couple of Use Cases in the past that would like to have this functionality. The only way that I found one could edit the Deployment Name was to do it via the UI or by setting the __deploymentName Custom Property on the Blueprint at request time. After numerous tests with ‘__deploymentName‘ and a lot of Google Magic, it did not seem possible to modify that Custom Property during Provisioning with EBS for example.
The Solution
The solution to this problem is to use an Undocumented API Call:
METHOD: PUT HEADERS: Accept : application/json | Content-Type : application/json | Authorization : Bearer <Your_Token> URL: https://<your_vra_fqdn>/catalog-service/api/consumer/deployments/{id} BODY: { "name":"NEW Name", "description":"NEW Description" }
Luckily for us we can do REST API Calls to our vRA Appliance with vRO. You have a couple of possibilities in vRO for achieving this functionality:
- Using the REST Host functionality of vRO
- Using the vRA CafeHost functionality of vRO
In this Blog Post I’ll explain the 2nd Option. Hang on!
How to implement
vRO Work
- Create new Action called ‘getDeploymentByResourceID‘
INPUT: cafeHost – vCACCAFE:VCACHost | requestId – string
CODE://Declare Variables var requestService = cafeHost.createCatalogClient().getCatalogConsumerRequestService(); var deployment = null; try{ //Get the Resource(s) provisioned by specific Request ID var results = requestService.getResourcesProvisionedByRequest(requestId); var resources = results.getContent(); //Find the Deployment Resource based on the Request ID for each (resource in resources){ if (resource.resourceTypeRef.getLabel() == "Deployment"){ deployment = resource; break; } } }catch(exception){ System.log("Error: Could not find Deployment Resource for Request ID: "+requestId+" See error message: "+exception); } return deployment;
- Create new Action called ‘setDeploymentName‘
INPUT: cafeHost – VCACCAFE:VCACHost | resourceId : string | newName : string
CODE://Create the REST Client based on the vRA CafeHost for the given Tenant restClient = cafeHost.createRestClient("com.vmware.csp.core.cafe.catalog.api"); //Creating new Properties object to put new Name and / or Description in var props = new Properties(); props.put("name", newName); //props.put("description", "New Description generated during lifecycle with EBS via vRO"); //Perform PUT call var response = restClient.put("/consumer/deployments/" + resourceId, props); //Process statusCode / Error Handling statusCode = response.getStatus(); System.log("Status code: " + statusCode); if (statusCode >= 400) { throw new Error ("Error setting New Deployment Name: " + response.contentAsString); }else { var updatedName = response.getProperty("name"); System.log("Name successfully changed. See JSON Response for NAME property: "+updatedName); }
- Create a Workflow that will be triggered by Event Broker Subscription during the REQUESTED-POST phase for example. Named ‘Machine Requested – POST – Change Deployment Name‘
WORKFLOW INPUT: payload – Properties
ATTRIBUTES: cafeHost – VCACCAFE:VCACHost (SET THIS STATICALLY OR DYNAMICALLY WITH CODE) | cafeRequestID : string | vmName : string
WORKFLOW OUTPUT: N/A - vRO Workflow:
SCRIPTABLE TASK 1: Set Vars & Log
INPUT: payload
OUTPUT: vmName (attr) & cafeRequestID (attr)
CODE://Logging the Payload var machine = payload.get("machine"); var vmProperties = machine.get("properties"); if(vmProperties != null){ vmProperties.keys .sort() .forEach(function(key) { System.log(key + ": " + vmProperties.get(key)); }); } //Setting the Variables cafeRequestID = vmProperties.get("__Cafe.Root.Request.Id"); vmName = machine.get("name"); if (! cafeRequestID) { throw new Error ("Cannot find Cafe Root Request ID from the Catalog Request!"); } if (! vmName) { throw new Error ("Cannot find VM Name from the Catalog Request!"); } System.log("Root Cafe Request ID from Catalog Request: "+cafeRequestID); System.log("VM Name from Catalog Request: "+vmName);
- vRO Workflow: SCRIPTABLE TASK 2: Update Deployment Name
INPUT: cafeHost (attr) & vmName (attr) & cafeRequestID (attr)
OUTPUT: N/A
CODE:System.log("Setting Deployment Name for Deployment with Request ID: "+cafeRequestID); var deployment = System.getModule("<Path_To_Your_Action>").getDeploymentByResourceID(cafeHost, cafeRequestID); if (deployment){ //Get Resource ID from Deployment var resourceId = deployment.id; //Retrieve current Deployment Name + set new one var currentDeploymentName = deployment.name; var newDeploymentName = vmName; System.log("Current Deployment Name is: " + currentDeploymentName); System.log("New Deployment Name is: " + newDeploymentName); try{ System.getModule("<Path_To_Your_Action").setDeploymentName(cafeHost, resourceId, newDeploymentName); }catch(e){ System.log("Failed to update deployment name with error: " + e); } }
- vRO Overview Result:
vRA Work
- Now modify your existing Event Broker Subscription setup to include your newly created vRO workflow or set up a new one!
- Example setup below:
EVENT TOPIC: Machine Provisioning
CONDITIONS:
Data > Lifecycle State > State Phase Equals POST
Data > Lifecycle State > Life Cycle State Name Equals VMPSMasterWorkflow32.Requested
vRO WORKFLOW: Machine Requested – POST – Change Deployment Name - vRA Result Overview:
END RESULT
Now when you provision a new machine, the Deployment Name will be changed to the VM name (or something else you’ve specified). See screenshots below:
Hopefully this could be of assistance for you.
I did not test it out on vRA versions lower than 7.5. If you have tested it, feel free to share!
If there are any questions / comments, don’t hesitate to let us know.
Have a nice day!
nice article
LikeLiked by 1 person