virtualportal
Enthusiast
Enthusiast

Query vCloud Object from vCO

Jump to solution

Hi,

I am wondering if anybody can help me.

I am trying to query vCloud for both a vCloud:vAppTemplate and a vCloud:Vdc object. The only input parameter I have at the moment is a string containing the vAppTemplate Name and the Org VDC Name.

Is this possible using the built in methods in the vCloud 1.5 Plugin for vCO?

I can see various query methods but am no Javascript/vCO exprt so am trying to figure out how I would be able to return an actual recognised Type. I need to update a workflow attribute with the output, so that it can be used to instantiate a vApp template.

Any help is much appreciated

Thanks

0 Kudos
1 Solution

Accepted Solutions
cdecanini_
VMware Employee
VMware Employee

Here is a sample to get all vApp Templates by name.

var queryService = vcdHost.getQueryService();

var expression = new VclExpression(VclQueryVAppTemplateField.NAME, name, VclExpressionType.EQUALS);
var filter = new VclFilter(expression);
var params = new VclQueryParams();
params.setFilter(filter);

var vAppTemplates = new Array();

var resultSet = queryService.queryRecords(VclQueryRecordType.VAPPTEMPLATE, params);

while (resultSet != null)  {
    var records = resultSet.getRecords(new VclQueryResultVAppTemplateRecord());
    //System.log(records.length + " records found");
    for each (var record in records) {
        var vAppTemplateRef = new VclReference();
        vAppTemplateRef.href = record.href;
        vAppTemplateRef.name = record.name;
        vAppTemplateRef.type = record.type;
        vAppTemplates.push(vcdHost.getEntityByReference(VclEntityType.VAPP_TEMPLATE, vAppTemplateRef));
    }
    resultSet = resultSet.getNextPage();
}

return vAppTemplates;

And here only the ones from a given VDC (calling the first action above).

if (vdc == null) throw "getVApptemplatesByNameFromVdc: vdc is null";

var vAppTemplates = System.getModule("com.vmware.pso.library.vcloud.qs").getVAppTemplatesByName(vdc.getHost(), name);

var vAppTemplatesInVdc = new Array();
for each (var vAppTemplate in vAppTemplates) {
    if (vAppTemplate.parent == vdc) vAppTemplatesInVdc.push(vAppTemplate);
}
return vAppTemplatesInVdc;

You could modify the second one to check on the VDC name instead of the VDC object.

if (vAppTemplate.parent.name == vdcName)

Christophe.

If my answer resolved or helped you, please mark it as Correct or Helpful to award points. Thank you! Visit http://www.vcoteam.info & http://blogs.vmware.com/orchestrator for vCenter Orchestrator tips and tutorials - @vCOTeam on Twitter

View solution in original post

0 Kudos
6 Replies
cdecanini_
VMware Employee
VMware Employee

Here is a sample to get all vApp Templates by name.

var queryService = vcdHost.getQueryService();

var expression = new VclExpression(VclQueryVAppTemplateField.NAME, name, VclExpressionType.EQUALS);
var filter = new VclFilter(expression);
var params = new VclQueryParams();
params.setFilter(filter);

var vAppTemplates = new Array();

var resultSet = queryService.queryRecords(VclQueryRecordType.VAPPTEMPLATE, params);

while (resultSet != null)  {
    var records = resultSet.getRecords(new VclQueryResultVAppTemplateRecord());
    //System.log(records.length + " records found");
    for each (var record in records) {
        var vAppTemplateRef = new VclReference();
        vAppTemplateRef.href = record.href;
        vAppTemplateRef.name = record.name;
        vAppTemplateRef.type = record.type;
        vAppTemplates.push(vcdHost.getEntityByReference(VclEntityType.VAPP_TEMPLATE, vAppTemplateRef));
    }
    resultSet = resultSet.getNextPage();
}

return vAppTemplates;

And here only the ones from a given VDC (calling the first action above).

if (vdc == null) throw "getVApptemplatesByNameFromVdc: vdc is null";

var vAppTemplates = System.getModule("com.vmware.pso.library.vcloud.qs").getVAppTemplatesByName(vdc.getHost(), name);

var vAppTemplatesInVdc = new Array();
for each (var vAppTemplate in vAppTemplates) {
    if (vAppTemplate.parent == vdc) vAppTemplatesInVdc.push(vAppTemplate);
}
return vAppTemplatesInVdc;

You could modify the second one to check on the VDC name instead of the VDC object.

if (vAppTemplate.parent.name == vdcName)

Christophe.

If my answer resolved or helped you, please mark it as Correct or Helpful to award points. Thank you! Visit http://www.vcoteam.info & http://blogs.vmware.com/orchestrator for vCenter Orchestrator tips and tutorials - @vCOTeam on Twitter

View solution in original post

0 Kudos
virtualportal
Enthusiast
Enthusiast

Thanks so much for the quick and detailed response 🙂

I will test this out tomorrow and let you know how I get on.

Thanks again.

Steve

0 Kudos
virtualportal
Enthusiast
Enthusiast

Hi,

I just tried your code (first section)

It worked great for finding a vApp template from a name string..Thanks so much 🙂

I then created a new workflow to find an oVDC from a name string and edited the code as shown below.

NOTE: my input parameter is a string and is called ovdc_name, by output is ovdc_object and is a Vcloud:Vdc type.

var queryService = vcdHost.getQueryService();
var expression = new VclExpression(VclQueryOrgVdcField.NAME, ovdc_name, VclExpressionType.EQUALS);
var filter = new VclFilter(expression);
var params = new VclQueryParams();
params.setFilter(filter);
var ovdcs = new Array();
var resultSet = queryService.queryRecords(VclQueryRecordType.ORGVDC, params);
while (resultSet != null)  {
    var records = resultSet.getRecords(new VclQueryResultOrgVdcRecord());
    System.log(records.length + " oVDC record found");
    for each (var record in records) {
        var ovdcRef = new VclReference();
        ovdcRef.href = record.href;
        ovdcRef.name = record.name;
        ovdcRef.type = record.type;
       ovdcs.push(vcdHost.getEntityByReference(VclEntityType.ORGVDC, ovdcRef));
    }
    resultSet = resultSet.getNextPage();
}
ovdc_object = ovdcs[0];

I cant see why this does not work like the vApp Template query. it seems that the System.Log message tells me there are 0 oVDC found. This means that the resultSet is not NULL. If there is something in it, then why is it not being recognised as an OrgVdc?

I am probably missing something obvious in my code, but anyhelp would be much appreciated again.

Thanks in Advance

Steve

0 Kudos
virtualportal
Enthusiast
Enthusiast

Looks like I was getting confused with the way the API References VDCs. I needed to query for an AdminVDC so the new code is below in case anybody else needs this.

I also took this scriptable task out of the bigger workflow and made it into its own workflow for later use. I am logged out of my lab now, but If I remember I will go in and attach both the vAppTemplate Search and the VDC serach workflows to this discussion.

Everything is working now. Thanks so much for your help.

var queryService = vcdHost.getQueryService();
 
 
 
var expression = new VclExpression(VclQueryAdminVdcField.NAME, ovdc_name, VclExpressionType.EQUALS);
 
var filter = new VclFilter(expression);
 
var params = new VclQueryParams();
 
params.setFilter(filter);
 
 
 
var ovdcs = new Array();
 
 
 
var resultSet = queryService.queryRecords(VclQueryRecordType.ADMINORGVDC, params);
 
 
 
while (resultSet != null{
 
    var records = resultSet.getRecords(new VclQueryResultAdminVdcRecord());
 
    System.log(records.length + " oVDC record found");
 
    for each (var record in records) {
 
        var ovdcRef = new VclReference();
 
        ovdcRef.href = record.href;
 
        ovdcRef.name = record.name;
 
        ovdcRef.type = record.type;
 
       ovdcs.push(vcdHost.getEntityByReference(VclEntityType.VDC, ovdcRef));
 
    }
 
    resultSet = resultSet.getNextPage();
 
}
 
 
 
ovdc_object = ovdcs[0];
 

Steve

0 Kudos
cdecanini_
VMware Employee
VMware Employee

Glad you found the solution by yourself.

Here is a better version of what I have posted yesterday:

var queryService = vcdHost.getQueryService();

var expression = new VclExpression(VclQueryVAppTemplateField.NAME, vAppTemplateName, VclExpressionType.EQUALS);
var filter = new VclFilter(expression);
var params = new VclQueryParams();
params.setFilter(filter);

var vAppTemplates = new Array();
var resultSet = queryService.queryRecords(VclQueryRecordType.VAPPTEMPLATE, params);

while (resultSet != null)  {
    var records = resultSet.getRecords(new VclQueryResultVAppTemplateRecord());
    //System.log(records.length + " records found");
    for each (var record in records) {
        if (record.vdcName == vdcName) {
            var vAppTemplateRef = new VclReference();
            vAppTemplateRef.href = record.href;
            vAppTemplateRef.name = record.name;
            vAppTemplateRef.type = record.type;
            vAppTemplates.push(vcdHost.getEntityByReference(VclEntityType.VAPP_TEMPLATE, vAppTemplateRef));
            }
        }
    resultSet = resultSet.getNextPage();
}

return vAppTemplates;

Here I am only returning the vAppTemplate records having a particular VDC name instead of returning all vAppTemplates with a given name and using a separate action to iterate through all their VDCs.

If my answer resolved or helped you, please mark it as Correct or Helpful to award points. Thank you! Visit http://www.vcoteam.info & http://blogs.vmware.com/orchestrator for vCenter Orchestrator tips and tutorials - @vCOTeam on Twitter
cdecanini_
VMware Employee
VMware Employee

Here the same thing but passing a VDC object and a vApp template name as inputs (so you do not have to pass a VCD host)

var queryService = vdc.getHost().getQueryService();

var expression = new VclExpression(VclQueryVAppTemplateField.NAME, vAppTemplateName, VclExpressionType.EQUALS);
var filter = new VclFilter(expression);
var params = new VclQueryParams();
params.setFilter(filter);

var vAppTemplates = new Array();
var resultSet = queryService.queryRecords(VclQueryRecordType.VAPPTEMPLATE, params);

while (resultSet != null)  {
    var records = resultSet.getRecords(new VclQueryResultVAppTemplateRecord());
    //System.log(records.length + " records found");
    for each (var record in records) {
        if (record.vdc == vdc.getReference().href) {
            var vAppTemplateRef = new VclReference();
            vAppTemplateRef.href = record.href;
            vAppTemplateRef.name = record.name;
            vAppTemplateRef.type = record.type;
            vAppTemplates.push(vdc.getHost().getEntityByReference(VclEntityType.VAPP_TEMPLATE, vAppTemplateRef));
            }
        }
    resultSet = resultSet.getNextPage();
}

return vAppTemplates;

Christophe.

If my answer resolved or helped you, please mark it as Correct or Helpful to award points. Thank you! Visit http://www.vcoteam.info & http://blogs.vmware.com/orchestrator for vCenter Orchestrator tips and tutorials - @vCOTeam on Twitter