Filter examples with getting the VC:VirtualMachine or a Deployment via the REST API
Warning:
The REST API has up to a delay for properties or other attributes updated coming back into vRA. The delay is equivalent to the interval set for a Cloud Account in vRA.
Example to Get Deployment By Deployment ID:
Inputs:
vRAHost - REST:RESTHost
depID - String
Script:
var restMethod = 'GET';
var restPathURI = '/iaas/api/deployments?$filter=id+eq+' + depID;
var restPayload = null;
var restHeaders = new Properties();
restHeaders.put('Content-Type', 'application/json');
try {
var restClient = vRAHost.createRestClient();
var restRequest = restClient.createRequest( restMethod, restPathURI, restPayload);
var hKeys = restHeaders.keys;
for (var curKey in hKeys) {
restRequest.setHeader( hKeys[curKey], restHeaders.get( hKeys[curKey]) );
}
// perform response
var restResp = restClient.execute(restRequest);
// get response info
var statusCode = restResp.statusCode;
System.debug('Status Code: ' + statusCode );
statusMsg = restResp.statusMessage;
System.debug('Status Message: ' + statusMessage );
var respContent = restResponse.contentAsString;
System.debug('Content: ' + respContent );
if (statusCode >= 400 ) {
var errMsg = 'REST call failed. Status Code: ' + statusCode;
System.error( errMsg );
throw errMsg;
}
// return response content
return respContent
} catche(e) {
System.error('Unhandled exception: ' + e);
throw e;
}
Get Machine by VMName:
var restPathURI = '/iaas/api/machines?$filter=name+eq+' + vmName;
var restMethod = 'GET';
var restInputHdrs = new Properties();
restInputHdrs.put('Content-Type', 'application/json');
// GET = '', for PATCH, PUT, POST, populate JSON payload
restPayloadJSON = '';
restPayload = JSON.stringify(restPayloadJSON);
Other operators:
ne - not equal
or - logical or
and - logical and
startswith(name,'teststring')
endswith(name,'teststring')
substringof('testsubstring',name)
length(name)+eq+4
// get VM properties from the response (see action below)
var vmMachID = '';
var vmCustomProp = '';
var jsonDetails = JSON.parse(restResp);
var respContent = jsonDetails.content[0];
if (respContent == null || respContent == undefined) {
// failed to find VM
System.warn('No VM object returned for ' + vmName '. respContent empty.');
} else {
vmMachID = respContent.id;
vmCustomProp = respContent.customProperties.custompropname;
...
}
______________________
Get a managed deployment's machine:
var restPathURI = '/iaas/api/machines?$filter=deploymentId+eq+*';
var restMethod = 'GET';
var restInputHdrs = new Properties();
restInputHdrs.put('Content-Type', 'application/json');
...
______________________
REST Action:
Assumes an input that is linked to a configuration item that is the the vRA:Host, and the variables passed into the script are above.
Action: DoHOSTREST
Inputs:
restMethod (string)
restPathURI (string)
restPayload (string)
restInputHdrs (Properties)
vRAHost (vRA:Host)
// create client REST request
var restClient = vRAHost.createRestClient();
var req = restClient.createRequest(restMethod, restPathURI, restPayload);
var hdrKeys = restInputHdrs.keys;
// add headers
for (var hdrKey in hdrKeys) {
req.setHeader(hdrKeys[hdrKey], restInputHeaders.get(hdrKeys[hdrKey]));
}
// run and get response
var resp = restClient.execute(req);
var respStatusCode = resp.statusCode;
var respStatusMsg = resp.statusMessage;
System.debug( 'Status: ' + respStatusCode + ', Message: ' + restStatusMsg);
var respContent = resp.contentAsString;
System.debug( 'Response text: ' + respContent );
respHdrs = resp.getHeadersValues('Access-Control-Allow-Headers');
for (var respHdr in respHdrs) {
System.debug( 'Header: ' + respHdr.toString();
}
if (repStatusCode >= 400) {
// uncomment throw if you wish action to fail workflow
System.warn( 'REST called failed. See debug lines above.');
// throw 'REST called failed. See debug lines above.';
}
return respContent;
______________________
vRA8.6 - Other REST Endpoints:
Get the vRA:Host:
var restPathURI = '/vco/api/catalog/VRA?$filter=name+eq+Default';
Get deployments By Deployment ID:
var restPathURI = '/iaas/api/deployments?$filter=id+eq+mydeploymentid';
Note: deployments can be filtered by eq/ne/and/or
Get requests for a specific deployment:
var restPathURI = '/deployment/api/deployments/1a23bc4d-123a-321b-bcd2-1a2bcdef9876/requests';
Note: the last request, will be the Create (provision). Any Day2 actions will be in cronological order above the provision.
Get workflow:
var restPathURI = '/vco/api/workflows?conditions=name=myworkflowname';
or
var restPathURI = '/vco/api/workflows?conditions=categoryName=mwportalapp';
Get projects:
var restPathURI = '/iaas/api/projects/";
______________________
vRA 8.6 - Login Steps:
In vRA 8, the login is split into two steps. We have to get an auth/access token, and then use that to get a bearer token.
1. Auth Access Token:
var restPathURI = 'https://' + vRAFQDN + '/csp/gateway/am/api/login?access_token';
headers:
content-Type: application/json
accept: application/json
Type: POST
POST body:
Pass the username, password, and domain variables, like below:
{"username": "{{username}}, "password": "{{password}}", "domain" {{domain}}"}
Response from vRA:
{"refresh_token":"1Ab2c001bc9A1BcDeFabCDEfabCdEFABCD"}
We are going to save this refresh_token into a variable named loginToken, for the next query.
2. Bearer Token:
Now using that first token, we have to do a second request to get the bearer token to actually do requests:
var restPathURI = 'https://' + vRAFQDN + '/csp/gateway/am/api/login?access_token';
headers:
content-Type: application/json
accept: application/json
Type: POST
POST body:
Pass the username, password, and domain variables, like below:
{"refreshToken": "{{loginToken}}"}
Response from vRA:
{
"tokenType": "Bearer",
"token": "verylongstringcontainingthetoken"
}
We are saving this token, into a variable named bearerToken, for all subsequent requests.
3. Get a VM's Props:
Using the bearer token, we can now do real work. For example we can get a VM and its status and properties:
var restPathURI = 'https://' + vRAFQDN + '/iaas/api/machines?$filter=name+eq+' + vmName;
headers:
content-Type: application/json
accept: application/json
authorization: {{bearerToken}}
Type: GET
Notes on the restPathURI:
The $, in $filter may need to be encoded to the %24.
If the name of the VM contains a space, you'll need to wrap in single quotes, or %27
Response from vrA:
{
"content": [
{
...
<properties and states of the VM>
...
}
],
"totalElements": 1,
"numberOfElements": 1
}
Keywords:
vRA 8, vRA8, vRO
previous page
|