VMware Cloud Community
aenagy
Hot Shot
Hot Shot

Query vRA 8.7 for (vSphere) virtual machines from vRO 7.6 using JavaScript REST API call

I'm working on a vRA 7 to vRA 8 migration project and need to figure out if the destination vRA 8 instance has visibility to the vRA 7 (vSphere) virtual machine I'm about to onboard. At first I tried using GET '/relocation/api/wo/query-unmanaged-machine' but this complained about missing 'planLink'. This is too soon in my code for an onboarding plan. I tried the GET '/iaas/api/machines' examples in the vRA 8.10 API Programming Guide PDF but the examples with '$filter' fail due to what I'm guessing is unmatched quotes in the example.

 

(1) Does anyone have sample JavaScript that will work in vRO 7.6, preferably using the 'createTransientHostFrom()' call from RESTHostManager, that they would be willing to share?
(2) How do I had a filter for my virtual machine name?

6 Replies
bdamian
Expert
Expert

When you create an onboarding plan, you need to specify a Cloud Account (the vCenter).

bdamian_0-1690664226491.png

When you search for a VM with '/relocation/api/wo/query-unmanaged-machine' you need to tell wich Onboarding Plan you want to use and this will set your specific vCenter account.

Maybe you should manually create an Onborading plan and use it for all your queries passing something like this in the body:

{
  "deployments": [
    {
      "resources": [
        {
          "link": vmLink,
          "name": <<theNameOfTheVM>>,
          "tagLinks": []
        }
      ]
    }
  ],
  "planLink": <<onboardingPlanLink>>
}

But you need to add some code to verify because if you search for a VM called "MyVM", and you have more VMs that start with that name (ex: MyVM2, MyVM clone, etc) they will appears in the response.

 

---
Damián Bacalov
vExpert 2017-2023 (7 years)
https://www.linkedin.com/in/damianbacalov/
https://tecnologiaimasd.blogspot.com/
twitter @bdamian
0 Kudos
aenagy
Hot Shot
Hot Shot

@bdamian :

Thanks. However, at this point in my code I'm simply verifying that the target vRA 8.x instance has visibility to the vRA 7.x virtual machine being migrated -- before creating the onboarding plan.

0 Kudos
bdamian
Expert
Expert

You could make a GET call to:

/deployment/api/resources?size=200&search=MyVmName&resourceTypes=Cloud.vSphere.Machine

You will need to search into the result set.

D.

 

---
Damián Bacalov
vExpert 2017-2023 (7 years)
https://www.linkedin.com/in/damianbacalov/
https://tecnologiaimasd.blogspot.com/
twitter @bdamian
0 Kudos
aenagy
Hot Shot
Hot Shot

@bdamian :

I experimented with '/deployment/api/resources' using the Swagger interface and found that the API call requires additional inputs that I will not know at this point in the migration.

 

I experimented with '/iaas/api/machines' and got the below curl working from Bash (GitBash). My translation to JavaScript isn't working

curl -X GET "$url/iaas/api/machines?apiVersion=$api_version&"'$filter='"deploymentId%20ne%20'*'%20and%20name%20eq%20'vmNameHere'" \ 
-H 'Content-Type: application/json' \ 
-H "Authorization: Bearer $access_token"

 

Javascript&colon;

var contentType = 'application/json' 
var content = "" 
var restHost = RESTHostManager.createHost("DynamicRequest"); 
var transientHost = RESTHostManager.createTransientHostFrom(restHost); 
transientHost.url = baseVra8Url; 
var httpMethod = 'GET' 
var queryString = '/iaas/api/machines' + "?" 
  
if ( inputVra8ApiVersion && "1234-56-78".length == inputVra8ApiVersion.length ) 
{ 
var queryString = queryString + "&apiVersion=" + inputVra8ApiVersion 
} 
if ( inputVra8IaasMachinesFilter && 0 < inputVra8IaasMachinesFilter.length ) 
{ 
var queryString = queryString + "&$filter=" + inputVra8IaasMachinesFilter 
} 
queryString = encodeURI( queryString ) 
// var queryString = '/iaas/api/machines' + "?" +  "apiVersion=" + inputVra8ApiVersion + "$filter=" + inputVra8IaasMachinesFilter 
var requestUrl =  baseVra8Url + queryString 
  
var request = transientHost.createRequest( httpMethod, queryString, content); 
request.setHeader( 'Authorization', 'Bearer ' + inputVra8TokenBearer ); 
  
var request = transientHost.createRequest( httpMethod, queryString, content); 
request.contentType = contentType; 
  
var response = request.execute(); 
statusCode = response.statusCode; 
  
if ( statusCode > 299 ) 
{ 
var errorCode = logHeader + "ERROR: HTTP REST: status code: " + statusCode + ", response.contentAsString = " + response.contentAsString 
System.error( errorCode ); 
throw( errorCode ); 
} 

var contentAsString = response.contentAsString; 
contentAsObject = JSON.parse(contentAsString); 
  
return contentAsString


The error:

Error in (Dynamic Script Module name : getVra8IaasMachine#58) ERROR: HTTP REST: status code: 401, response.contentAsString =  




 

0 Kudos
bdamian
Expert
Expert

You don't need anything else with "/deployment/api/resources'.

Here is a curl example: 

curl 'https://montevideo3.zonamericacloud.com/deployment/api/resources?size=100&search=MyVmName&resourceTypes=Cloud.vSphere.Machine&origin=DISCOVERED' \
--header 'Accept: application/json' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer eyJ0e...'
---
Damián Bacalov
vExpert 2017-2023 (7 years)
https://www.linkedin.com/in/damianbacalov/
https://tecnologiaimasd.blogspot.com/
twitter @bdamian
0 Kudos
aenagy
Hot Shot
Hot Shot

@bdamian :

That curl command works for me too. In both cases (''/deployment/api/resources" and "/iaas/api/machines") using curl works but using Swagger or JavaScript results in 401 even when using the same Bearer Token. I'm still stuck.

0 Kudos