VMware Cloud Community
qc4vmware
Virtuoso
Virtuoso
Jump to solution

Retreive item list with the vCAC 6.0.1 plugin via the CAFE server? Can't seem to achieve it with vCACCAFEEntitiesFinder

What I would like to do is query against the list of items I see in the items folder.  If you go into the inventory and select vCloud Automation Center, select your CAFE server, and look in the "Items" folder you'll presumably see all of the same items you can interact with in the gui.  I want to develop scripts that can do things with these items.  I tried to put together a simple query to grab all items for a particular owner but I can't figure out how to do it.  Hopefully I am just missing something very obvious.  I started with the vCACCAFEEntitiesFinder and it seems to be able to target everything except for the items list.  Is there some way of achieving this?  I don't see vCO type for these objects either.

1 Solution

Accepted Solutions
sanchezs
VMware Employee
VMware Employee
Jump to solution

The elements from the "Items" folder in the plug-in are catalog resources (vCACCAFECatalogResource), so that if you have the host configured with the right credentials (the user you want to get the catalog resources from), then you can do:

var items = vCACCAFEEntitiesFinder.getCatalogResources(host);

or to find specific ones by name/description with:

var items = vCACCAFEEntitiesFinder.findCatalogResources(host, query);


And if you want to go directly yo the underlying API you can get the catalog consumer resource service and to check the methods available there:


var service = host.createCatalogClient().getCatalogConsumerResourceService();

// where service is of type vCACCAFECatalogConsumerResourceService

View solution in original post

0 Kudos
11 Replies
sanchezs
VMware Employee
VMware Employee
Jump to solution

The elements from the "Items" folder in the plug-in are catalog resources (vCACCAFECatalogResource), so that if you have the host configured with the right credentials (the user you want to get the catalog resources from), then you can do:

var items = vCACCAFEEntitiesFinder.getCatalogResources(host);

or to find specific ones by name/description with:

var items = vCACCAFEEntitiesFinder.findCatalogResources(host, query);


And if you want to go directly yo the underlying API you can get the catalog consumer resource service and to check the methods available there:


var service = host.createCatalogClient().getCatalogConsumerResourceService();

// where service is of type vCACCAFECatalogConsumerResourceService

0 Kudos
qc4vmware
Virtuoso
Virtuoso
Jump to solution

Awesome... is all of this stuff in the plugin documentation?  I didn't bother to look since I have found the documentation for nearly all the plugins to be basically useless.  I think they should all have some nice samples of typical usage like this!  Very helpful!  Thanks!

0 Kudos
qc4vmware
Virtuoso
Virtuoso
Jump to solution

So I was able to get things working and querying on a name of a resource but I can't seem to query successfully on the owner.  How do I construct a query which goes down a level in the object.  The vCACCAFECatalogResource.owners is an array of vCACCAFECatalogPrincipal and that has the user id and name of the user available in the getRef() and getValue() methods.  Is it possible to construct this query or do I have to retrieve everything and loop through it?

0 Kudos
cdecanini_
VMware Employee
VMware Employee
Jump to solution

This should help (you will have to set tenant and owner as inputs)

filteredCatalogResources.push(catalogResource);

for each (var catalogResource in catalogResources) {

    var catalogPrincipals = catalogResource.owners;

    for each (var catalogPrincipal in catalogPrincipals) {

        System.log(catalogPrincipal.getTenantName() + " : " +  catalogPrincipal.getRef());

        if (catalogPrincipal.getTenantName() == tenant && catalogPrincipal.getRef() == owner) {

          filteredCatalogResources.push(catalogResource);

   }

  }

}

}

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
qc4vmware
Virtuoso
Virtuoso
Jump to solution

If I am reading this right this just loops through the results right?  What I was hoping to do was use vCACCAFEEntitiesFinder.findCatalogResources(host, query) and supply a query that would work with the owner.  The only thing I have seen it work with so far is the resource name.  I am fine looping through if that is my only option but it would be great to be able to supply a query for some other attributes.  I tried making the query something like "owners[0].getRef() eq "myname@mycompany.com"" but that didn't work.  I took a random guess at the format.

0 Kudos
qc4vmware
Virtuoso
Virtuoso
Jump to solution

Ha!  This plugin documentation actually does have some good examples in it!  My apologies. I just read through it.  Maybe using the host.createCatalogClient().getCatalogConsumerResourceService(); I will be able to get the query I want to work?  I'm still wrapping my head around how all of this works and its confusing going back and forth between the CAFE stuff as opposed to the IaaS portion of the plugin since they are vastly different yet related.

sanchezs
VMware Employee
VMware Employee
Jump to solution

The methods from the vCACCAFEEntitiesFinder are the ones that the plug-in uses to do its own internal stuff, basically finding objects from the inventory when running workflows (usually by id) and finding objects from the inventory when searching from the workflows presentations (usually by name or description). That's why usually you have the methods:

  • vCACCAFEEntitiesFinder.getX(host) - retrieves all the objects of type X from a specific host
  • vCACCAFEEntitiesFinder.getX(host, id) - retrieves the object of type X with the specific id found in the specific host
  • vCACCAFEEntitiesFinder.findX(host, query) - retrieves all the objects of type X matching the specific query (by name and/or description) found in the specific host

Again, those are the methods that the plug-in internally needs, but they are exposed via scripting in case they can be useful for someone.

Now, your question about passing specific queries directly to the services: Well, yes, it's possible, but even if most of the services accept queries, for many of the methods, in the backend (always OData-like queries), it depends on the services themselves and their methods. The plug-in is "just" (and mainly) exposing the APIs that vCAC offers through the vCAC CAFE SDK and by extension through the vCAC REST API. In your case you should be able to create a query like that:

var service = host.createCatalogClient().getCatalogConsumerResourceService();

var filter = new Array();

filter[0] = vCACCAFEFilterParam.equal("name", vCACCAFEFilterParam.string("name_of_the_resource_here"));

var query = vCACCAFEOdataQuery.query().addFilter(filter);

var items = service.getResourcesList(new vCACCAFEPageOdataRequest(query));

In that case you'd be querying by name. For the owners can be more tricky because internally you have a set of owners and the query should be like "x in set of owners", which I don't know right now how to directly translate it to OData and I should test it. I'll try to do some tests when I have some time and get back to you.

And about more/better public documentation and examples, and adding reasonable features (e.g. extending the queries of the vCACCAFEEntitiesFinder, simplifying the way the queries are built, etc.), I completely agree, but as you can imagine, it's always a matter of priorities, time and resources. Nevertheless, as part of the dev team, I'll try to push for them and to work, at least, on the examples.

0 Kudos
qc4vmware
Virtuoso
Virtuoso
Jump to solution

Thanks.  This is all extremely useful!  I really appreciate you taking the time to go over this stuff!  Querying these objects is something I imagine us doing quite a bit of especially as we get more into creating advanced services and custom actions.  Right now there is really no way for me to perform a task through the gui on a selection of items but I could build an advanced service for this.  I imagine we will also want to create services where the service "does something" for all "vSphere Machines" lets say.  It would be great to have a quick query that can gather this stuff for me.  Both the vCD and vSphere plugins have query services that I can typically form a query for that does most of the work.  Sometimes I end up needing to loop through a very small set of values returned but more often than not I can form a query that gets me exactly what I need quickly.  As our inventory grows I fear using the "grab everything then filter through it" method will be come unusably slow.  To me an attribute like "owners" is similar to "name" where we would want to be able to query/filter on it quickly and easily.  So hopefully you'll come back with a working example using the existing OData style queries which I am unfamiliar with.  I'll do some research myself.

0 Kudos
sanchezs
VMware Employee
VMware Employee
Jump to solution

No problem. I managed to move the query by owner to the service with this filter:

var filter = new Array();

filter[0] = vCACCAFEFilterParam.substringOf("owners/ref", vCACCAFEFilterParam.string("user@domain.com"));

var query = vCACCAFEOdataQuery.query().addFilter(filter);

var items = service.getResourcesList(new vCACCAFEPageOdataRequest(query));

The important part is the "owners/ref", which depends on the internal structure and fields of the catalog resources. Let me know it that works for you.

qc4vmware
Virtuoso
Virtuoso
Jump to solution

I will give this a shot later today.  I'm not sure how I could have ever put that all together on my own.  There are definitely no examples for this sort of thing in the plugin guide. I think pretty much any customer is going to want to do complex queries on various aspects of their environment so having a guide to that would be really helpful.  After seeing your example it looks relatively straight forward and I am sure this is much faster than sucking down the whole inventory and looping through it!

Thanks again!

Paul

0 Kudos
qc4vmware
Virtuoso
Virtuoso
Jump to solution

OK... I got a chance to test this and it works perfectly!  Just what I was looking for!

0 Kudos