Plug-ins that support dynamic extensions must implement the SPI that the client code uses to determine which dynamic extensions to display in the browser. The plug-in server can use any criterion chosen by the plug-in developer. The following example illustrates server code that uses two kinds of criteria to decide whether dynamic extensions should be visible..

Two useful criteria for filtering dynamic extensions are properties of vSphere objects and the authorization level of the vSphere Client user. This example tests the run state of a virtual machine and the user's authorization with respect to the virtual machine.

This example depends on a manifest file that configures two dynamic extensions. One extension provides a dynamic Monitor view of a virtual machine. The other extension provides a dynamic Menu action to run a virtual machine.

The following example code illustrates a controller that serves dynamic extension endpoints. The controller accesses the JSON in the request body by using the DynamicItemsRequestModel object passed in for the specific REST endpoint.

package com.example.remote.controllers;
import java.util.ArrayList;
import java.util.List;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.vmware.sample.remote.model.DynamicItem;
import com.vmware.sample.remote.model.DynamicItemsRequestModel;
import com.vmware.sample.remote.model.PluginServerDynamicItemsResponse;
import com.vmware.sample.remote.vim25.services.AuthorizationService;

/**
* Provide public endpoints for vSphere Client to query about
* UI visibility for dynamic views/actions.
*/
@RestController
@RequestMapping(value = "/dynamicItems",
                method = RequestMethod.POST,
                consumes = MediaType.APPLICATION_JSON_VALUE,
                produces = MediaType.APPLICATION_JSON_VALUE)
public class DynamicItemsController {
   private static final String MANAGE_VM_PRIVILEGE = "com.vmware.sample.remote.1.0.0.ManageVm";
   private final AuthorizationService authorizationService;
   private final VmRunstateService vmRunstateService;

   public DynamicItemsController(final AuthorizationService authorizationService) {
      this.authorizationService = authorizationService;
      this.vmRunstateService = vmRunstateService;
   }

   /* This action should be visible if user has authorization. */
   @RequestMapping(value = "/vm/actions")
   public PluginServerDynamicItemsResponse retrieveVmActions(
      @RequestBody DynamicItemsRequestModel payload) {
      final boolean hasPrivilege = authorizationService
            .hasPrivilege(payload.objectIds, MANAGE_VM_PRIVILEGE);
      final List<DynamicItem> dynamicItems = new ArrayList<>();
      dynamicItems.add(new DynamicItem("TakeAction", hasPrivilege));
      return new PluginServerDynamicItemsResponse("1.0.0", dynamicItems);
   }

   /* This VM view should be visible only if it is NOT currently running. */
   @RequestMapping(value = "/vm/monitor")
   public PluginServerDynamicItemsResponse retrieveVmMonitorViews(
      @RequestBody DynamicItemsRequestModel payload) {
      final String objectId = payload.objectIds.get(0);
      final boolean makeVisible = !isVmStateRunning(objectId));
      final List<DynamicItem> dynamicItems = new ArrayList<>();
      dynamicItems.add(new DynamicItem("MonitorDynView", makeVisible));
      return new PluginServerDynamicItemsResponse("1.0.0", dynamicItems);
   }
}