Update the Agency scope of a solution

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 your solution, a user can select the
ESXi
hosts on which to run the solution from a list on the solution Configuration page. The solution can update the scope of the sample ESX agency according to the hosts that the user selects.
Your solution can define a function to update the scope of the ESX agency in the
MyAgentHandler.java
class.
  1. Write a function that implements the vSphere Web Services API to detect compute resources on which to run the solution.
    Your solution can provide a helper class,
    MyVcUtils.java
    , that defines functions to obtain the compute resources on which to run the solution.
    MyAgentHandler.java
    calls the
    MyVcUtils.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 = _myvcUtils.getComputeResources();
  2. Add the
    ManagedObjectReference
    objects for the compute resources to a
    HashSet
    that defines the ESX agency scope.
    The
    MyAgentHandler.java
    class adds the list of
    ManagedObjectReference
    objects that the
    MyVcUtils.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 } } }
  3. Create an
    AgencyComputeResourceScope
    instance to contain the scope
    HashSet
    .
    AgencyComputeResourceScope scopeDO = (AgencyComputeResourceScope) _agencyConfigInfo.getScope(); Set<ManagedObjectReference> oldScope = new HashSet<ManagedObjectReference>(scopeDO.getComputeResource());
  4. Compare the old scope to the new scope to establish whether any compute resources have been added or removed.
    The
    MyAgentHandler.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; }
  5. 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(); } }
You defined a function in a solution to detect changes of scope and to update an ESX agency.