You define the ESX
agency scope of a solution by passing the managed object references (MoRefs) of
the vSphere compute resources to the solution.
You set the initial ESX agency
scope in the
scope property of the
AgencyConfigInfo
object. You can change the scope when a solution runs by calling the
Agency.update() method.
For example, in the EAM Sample Solution, users select the
ESXi hosts on
which to run the solution from a list on the EAM Sample Solution Configuration
page. The EAM Sample Solution updates the scope of the sample ESX agency
according to the hosts that the user selects.
The EAM Sample Solution
defines a function to update the scope of the ESX agency in the
AgentHandler.java
class.
Prerequisites
-
Download the vSphere ESX Agent Manager SDK.
-
Verify that you have set up
and started the EAM Sample Solution in an application server.
-
Verify that you have opened
eam_work_folder\src\com\vmware\eam\sample\solution\AgentHandler.java
in an editor.
- Open
eam_work_folder\src\com\vmware\eam\sample\solution\utils\VcUtilsjava
in an editor.
Procedure
-
Write a function that
implements the vSphere Web Services API to detect compute resources on which to
run the solution.
The EAM Sample
Solution provides a helper class,
VcUtils.java, that
defines functions to obtain the compute resources on which to run the solution.
AgentHandler.java
calls the
VcUtils.getComputeResources()
method to obtain a list of
ManagedObjectReference
objects for the
ESXi hosts
running in
vCenter Server.
public void updateConfig(String[] updates) throws RuntimeFaultFaultMsg {
waitForSetup();
boolean changed = false;
Map<String, ManagedObjectReference> crs = _vcUtils.getComputeResources();
-
Add the
ManagedObjectReference
objects for the compute resources to a
HashSet that defines
the ESX agency scope.
The
AgentHandler.java
class adds the list of
ManagedObjectReference
objects that the
VcUtils.getComputeResources()
method returns to the existing scope and updates the list if additional compute
resources are present.
Set<ManagedObjectReference> newScope = new HashSet<ManagedObjectReference>();
for (String update : updates) {
String[] kv = update.split("=", 2);
if (kv[0].equals("scope")) {
try {
ManagedObjectReference cr = crs.get(kv[1]);
if (cr == null) {
continue;
}
ManagedObjectReference moRef = new ManagedObjectReference();
moRef.setType(cr.getType());
moRef.setValue(cr.getValue());
newScope.add(moRef);
}
catch (NullPointerException e) {
// ignore
}
}
}
-
Create an
AgencyComputeResourceScope
instance to contain the scope
HashSet.
AgencyComputeResourceScope scopeDO = (AgencyComputeResourceScope) _agencyConfigInfo.getScope();
Set<ManagedObjectReference> oldScope = new HashSet<ManagedObjectReference>(scopeDO.getComputeResource());
-
Compare the old scope to
the new scope to establish whether any compute resources have been added or
removed.
The
AgentHandler.java
class compares the size of the new scope to the initial scope and adds any new
compute resources to the
HashSet of
ManagedObjectReference
objects.
if (!oldScope.containsAll(newScope) || oldScope.size() != newScope.size()) {
AgencyComputeResourceScope scope = new AgencyComputeResourceScope();
scope.getComputeResource().addAll(newScope);
agencyConfigInfo.setScope(scope);
changed = true;
}
-
If the new scope differs
from the old scope, call
Agency.update() to add
the new scope to the ESX agency.
if (changed) {
assert _agency != null;
try {
_eamConnection.getStub().update(_agency, agencyConfigInfo);
} catch (Exception e) {
_log.error("Failed to update agency. Reason: " + e.getMessage());
}
updateConfiguration();
}
}
Results
You defined a function in a
solution to detect changes of scope and to update an ESX agency.