I'm trying to get all the Related Objects -> Hosts from vSphere web services for my application. However when I do so, I get an error, and no hosts are returned.
I looked at the sample code for ChassisB and I found the function ChassisRackVSphereDataAdapter::initializeHostRelations(), which gets Host references from vCenter and adds them to the Chassis during initialization. The function getHosts() creates a QuerySpec to getData from the dataService. BTW, the function initializeHostRelations() is called whenever processQuery is invoked, and returns after initialization.
I tried to reuse this logic for my application. However, when I attempt to call the getHosts() function during the startup of my app, I get the following errors:
com.vmware.vise.util.session.SessionUtil Requesting threadlocal http request before it's been set
com.vmware.vise.util.session.SessionUtil Existing httpRequest already null. Possible unbalanced clear.
Any ideas what this means? Or suggestions?
Thanks,
Eric
initializeHostRelations() is a "hack" for this sample which doesn't have any back-end. There is not reason to use it in your case. If your custom objects have hosts relations that data should come from your back-end, not from your java plugin. Without a better description of your use case that's all I can offer 🙂
I totally recognize the initializeHostRelations sample code is a hack. However, I still want to be able to query the vCenter for a list of all Hosts. I assume I should still be able to use the getHosts() code that constructs a QuerySpec, and passes this query to the DataService to return a list of HostSystem objects.
All I can assume is there is some race condition or threading issue that is interfering with the actual request. I've searched the vSphere/VMware forums, and googled elsewhere with no success yet.
Is there a location for the com.vmware.vise.util.session.SessionUtil.java source? Perhaps if I could see the source, the context of the error message explain what is going on?
By the way, is there a preferred method to get the hosts from vCenter? The following sample code borrowed from ChassisB looks legit as far as I can tell. Is there a better way?
// create QuerySpec | |||||
QuerySpec qs = new QuerySpec(); | |||||
qs.resourceSpec = new ResourceSpec(); | |||||
qs.resourceSpec.constraint = new Constraint(); | |||||
// HostSystem is the targetType | |||||
qs.resourceSpec.constraint.targetType = "HostSystem"; // request the name property // maxResultCount # hosts -> ideally get all Hosts PropertySpec pSpec = new PropertySpec(); pSpec.propertyNames = new String[]{"name"}; qs.resourceSpec.propertySpecs = new PropertySpec[]{pSpec}; qs.resultSpec = new ResultSpec(); qs.resultSpec.maxResultCount = new Integer(maxResultCount); // use default ordering qs.resultSpec.order = new OrderingCriteria(); qs.resultSpec.order.orderingProperties = new OrderingPropertySpec[0]; // get data from DataService RequestSpec requestSpec = new RequestSpec(); requestSpec.querySpec = new QuerySpec[]{qs}; Response response = _dataService.getData(requestSpec); ResultItem[] items = response.resultSet[0].items; if (items == null) { _logger.info("getHosts: resultSet null" ); return new Object[0]; } return items; // HostSystem |
Your problem is because you make that query during the initialization of your java class at startup, i.e. outside any valid user session.
You cannot access vCenter data without proper user authentication. See that in the chassis sample this hosts initialization is done the first time a query comes in from the client, from an authenticated user.
Thanks, that makes sense. I'll try moving the getHosts query outside the app initialization, and only until there is a valid user session.
Laurent, your reply helped. Quick question: How do I mark this thread as 'Answered'? I don't see any relevant buttons or links.