Hi there !!
It’s been a very long timmmmmeee since my last post.
As usual, just a post to help in saving time. Today we’ll deal with REST API and mainly how to get information registered vCAC VMs in a huge number of pages.
When we want to query your CAFE host and obtain the number of deployments, we very often a response that does not fit on a single page.
In vRA API REST guide, VMware tell us to make as many requests as we have pages to browse. Let me show you how I do this…
var restClient = vCACCafeHost.createRestClient(endpoint); var deploymentResourceUrl = "/consumer/resources/types/Infrastructure.Virtual/"; var parentDeploymentInfos = new Properties(); var allDeployedVm = new Array(); var deployedVmCount = 0; function preparingRestContext(url) { jsonContent = getRestResponseInJsonFormat(url) var responseTotalPages = jsonContent.metadata.totalPages return responseTotalPages } function getRestResponseInJsonFormat(url) { var response = restClient.get(url); var jsonContent = response.getBodyAsJson(); return jsonContent } System.log("===================================================") System.log("**** Collecting all resources of type \"Deployment\" ***") System.log("===================================================") var numberOfPagesToParse = preparingRestContext(deploymentResourceUrl) System.log("Number of page to parse : " + numberOfPagesToParse + ".") for(i=1;i<=numberOfPagesToParse;i++) { requestUrl = "/consumer/resources/types/Infrastructure.Virtual/?page=" + i; var jsonContent = getRestResponseInJsonFormat(requestUrl) for each (var resource in jsonContent.content) { vmEnvironment = (resource.name).substring(4,5) var vmDeploymentProp = new Properties(); vmDeploymentProp.put("Name",resource.name) vmDeploymentProp.put("ParentLabel",resource.parentResourceRef.label) vmDeploymentProp.put("Owner",resource.owners[0].ref) vmDeploymentProp.put("Environnement",vmEnvironment) allDeployedVm.push(vmDeploymentProp); deployedVmCount = deployedVmCount + 1; } } System.log("") System.log("== Collecting resources information to identify deployment environment ==") System.log("VM deployed number : " + deployedVmCount)