VMware Cloud Community
vMarkusK1985
Expert
Expert
Jump to solution

My first steps with VEEAM RESTapi

Hello,

I started to play a little bit with the RESTapi of VEEAM Ent. Server.

But by question is not pointed to the Application. It’s more about the Handling of a RESTapi get results.

My Script:

var PostResponse = BRHost.createRequest("POST", "/sessionMngr/?v=v1_1", null).execute();

var TenantsResponse = BRHost.createRequest("GET", "/cloud/tenants", null).execute();

//System.log("Tenants " + TenantsResponse.contentAsString);

var XMLFile = XMLManager.fromString(TenantsResponse.contentAsString);

var XMLelement = XMLFile.documentElement.getElementsByTagName("Ref");

for(i=0;i<XMLelement.getLength();i++){

  var Tenant = XMLelement.item(i).getAttribute("Name")

if ( Tenant == TenantName){

  var UIDTenantFull = XMLelement.item(i).getAttribute("UID")

  var UIDTenant = UIDTenantFull.slice(-36)

  var RessourceResponse = BRHost.createRequest("GET", "/cloud/tenants/" + UIDTenant + "/resources", null).execute();

  //System.log("Tenant Ressources: " + VMResponse.contentAsString);

  //System.log("Content as string: " + RessourceResponse.contentAsString);

  var RESdoc = XMLManager.fromString(RessourceResponse.contentAsString);

  var RESlist = RESdoc.documentElement.getElementsByTagName("CloudTenantResource");

  System.error(RESlist.getLength());

  for(j=0;j < RESlist.getLength();j++){

  var RessourceID = RESlist.item(j).getAttribute("Id");

  System.log("Ressource ID: " + RessourceID);

  var RessourceResponseDetail = BRHost.createRequest("GET", "/cloud/tenants/" + UIDTenant + "/resources/" + RessourceID, null).execute();

  System.log("RessourceResponseDetail: " + RessourceResponseDetail.contentAsString);

  }

}

}

A Result Example:

<CloudTenantResource xmlns="http://www.veeam.com/ent/v1.0" Type="CloudTenantResource" Href="http://localhost:9399/api/cloud/tenants/4f90635a-7ecc-49fe-beb6-60b37eb4bd89/resources/1c7c6cd7-0cd0..." Id="1c7c6cd7-0cd0-4e20-8d15-a94d23299a93">

  <Links>

    <Link Rel="Delete" Type="CloudTenantResource" Href="http://localhost:9399/api/cloud/tenants/4f90635a-7ecc-49fe-beb6-60b37eb4bd89/resources/1c7c6cd7-0cd0..." Name="Cloud repository 3" />

    <Link Rel="Up" Type="CloudTenant" Href="http://localhost:9399/api/cloud/tenants/4f90635a-7ecc-49fe-beb6-60b37eb4bd89?format=Entity" Name="ABC Company" />

    <Link Rel="Up" Type="BackupServer" Href="http://localhost:9399/api/backupServers/8ea5406b-a6e9-42d9-bca5-44b6a5d94af1?format=Entity" Name="localhost" />

  </Links>

  <RepositoryQuota>

    <DisplayName>Cloud repository 3</DisplayName>

    <RepositoryUid>urn:veeam:Repository:82db96c3-445c-4a7e-9587-f2d523e839f4</RepositoryUid>

    <Quota>1024</Quota>

    <UsedQuota>0</UsedQuota>

  </RepositoryQuota>

</CloudTenantResource>

Source: http://helpcenter.veeam.com/backup/80/rest/_tenants_id_resources_2.html

My Result dos exactly look like that.

Now is the Question how to deal with the Result.

First I want to try to extract all “ <RepositoryQuota>” fields In to an array.

What is the best way to do that?

https://mycloudrevolution.com | https://twitter.com/vMarkus_K | https://github.com/vMarkusK
1 Solution

Accepted Solutions
iiliev
VMware Employee
VMware Employee
Jump to solution

Hi,

The easiest way to consume XML in Javascript is via E4X. Here is a sample code:

default xml namespace = "http://www.veeam.com/ent/v1.0";

var doc = new XML(RessourceResponseDetail.contentAsString);

System.log(doc.Links.Link[1].@Type);

System.log(doc.RepositoryQuota.RepositoryUid);

  • On the first line, we declare the default namespace (xmlns attribute in your XML)
  • On the second line, we load the XML content into a Javascript variable named doc, which gives you hierarchical access to your XML data
  • Third and fourth line demonstrate how to access array element by index (Link[1] means the second Link element), attributes (@Type means the value of Type attribute) and elements (RepositoryQuota and RepositoryUid).

View solution in original post

2 Replies
iiliev
VMware Employee
VMware Employee
Jump to solution

Hi,

The easiest way to consume XML in Javascript is via E4X. Here is a sample code:

default xml namespace = "http://www.veeam.com/ent/v1.0";

var doc = new XML(RessourceResponseDetail.contentAsString);

System.log(doc.Links.Link[1].@Type);

System.log(doc.RepositoryQuota.RepositoryUid);

  • On the first line, we declare the default namespace (xmlns attribute in your XML)
  • On the second line, we load the XML content into a Javascript variable named doc, which gives you hierarchical access to your XML data
  • Third and fourth line demonstrate how to access array element by index (Link[1] means the second Link element), attributes (@Type means the value of Type attribute) and elements (RepositoryQuota and RepositoryUid).
vMarkusK1985
Expert
Expert
Jump to solution

Hello,

thanks a lot. Exacly what I am looking for.

Kind Regards,

Markus

https://mycloudrevolution.com | https://twitter.com/vMarkus_K | https://github.com/vMarkusK
0 Kudos