VMware {code} Community
travispalmer
Contributor
Contributor

QueryVMField.VAPPNAME causes VCloudException

I am trying to use the QueryService's method QueryVmReferences(QueryParams) and I am getting an exception with a message 'Bad request: Unknown property name vappName.'

Here is the chunk of code that is causing the error:

            QueryService qs = VCloudClient.GetQueryService();

            QueryParams<QueryVMField> query = new QueryParams<QueryVMField>();

            query.Fields.Add(QueryVMField.VAPPNAME);

            query.Filter = new Filter(new Expression(QueryVMField.VAPPNAME, vAppRefs[0].name, ExpressionType.EQUALS));

            List<ReferenceType> vMRefs = qs.QueryVmReferences(query).GetReferences();

Am I doing something wrong? Any help would be greatly appreciated! Thanks.

t

Tags (3)
Reply
0 Kudos
4 Replies
kvp511
Enthusiast
Enthusiast

Okay I have an example for you. I'm thinking that you are trying to get the vApps with a particular name. Either way, whenever you use this query service you need to make sure your fields and types are correct.

_client is my vCloudClient and _admin = _client.GetVcloudAdmin();

private List<ReferenceType> GetQueryvAppReferences(string vAppName)

        {

            var adminExtension = _client.GetVcloudAdminExtension();

            var queryService = adminExtension.GetExtensionQueryService();

            var fields = new List<QueryAdminVAppField> { QueryAdminVAppField.NAME };

            var expression = new vcutil.Expression(QueryAdminVAppField.NAME, vAppName, ExpressionType.EQUALS);

            var filter = new vcutil.Filter(expression);

            var queryParams = new vcutil.QueryParams<QueryAdminVAppField>

                {

                    Filter = filter,

                    Fields = fields

                };

            var referenceResult = queryService.QueryReferences(QueryReferenceType.ADMINVAPP, queryParams);

            return referenceResult.GetReferences();

        }

Reply
0 Kudos
travispalmer
Contributor
Contributor

kvp511,

Thank you for the response. I apologize, I should have been more clear in my post. I currently have a list of vAppReferences, and for each vAppReference, I would like to get the corresponding VMs using the QueryService. Here are the steps I am taking:

VCloudClient = new vCloudClient(mVCUrl, com.vmware.vcloud.sdk.constants.Version.V5_1);

VCloudClient.Login(mVCUsername + "@" + mVCOrg, mVCPassword);

List<ReferenceType> vAppRefs = VCloudClient.GetQueryService().QueryvAppReferences().GetReferences();

QueryService qs = VCloudClient.GetQueryService();

QueryParams<QueryVMField> query = new QueryParams<QueryVMField>();

query.Fields.Add(QueryVMField.VAPPNAME);

query.Filter = new Filter(new Expression(QueryVMField.VAPPNAME, vAppRefs[0].name, ExpressionType.EQUALS));

List<ReferenceType> vMRefs = qs.QueryVmReferences(query).GetReferences();


Since I am using the QueryVMField class to define the field and types, is it possible to use wrong ones? Wouldn't the compiler throw a compilation error?


Thanks again for you help, I appreciate the time.


t


Reply
0 Kudos
kvp511
Enthusiast
Enthusiast

Ok I too ran into that same error and didn't see why that was happening. Maybe that field is not available however its in the enum (its actually a struct but you get my point). There is always another way to skin the cat though. Once you have your references you could simple do this:

          //using the method I posted above

          var vApps = GetQueryvAppReferences("MyVAppName");

            foreach (var vApp in vApps)

            {

                    //get the reference to the vapp here

                     var vAppObject = Vapp.GetVappByReference(_client, vApp);

                    //now its easy to get the children vms

                     var vms = vAppObject.GetChildrenVms();

            }

I find using the query service you have to write way more lines of code then you need to.

Reply
0 Kudos
travispalmer
Contributor
Contributor

kvp511,

Haha, yea it does take a few more lines of code. However, the QueryService seems to be a lot faster for getting all the VApp references than:

  List<ReferenceType> vAppRefs = VCloudVirtualDataCenter.GetVappRefs();

So I was hoping that the same would be true for getting the VM references using the QueryService. We have 50+ VApps, that each have 10 or more VMs. The init time for my application is a bit lengthy, so I was just trying to speed things up a bit.

Thank you for verifying that you also encounter the error. Glad it is not just me ;}

t

Reply
0 Kudos