Performing Action Operations on the vSphere Environment
Typically, an action requires you to make a change to the vSphere environment, such as changing the power state of a Virtual Machine, creating a new Host object, or deleting a vSphere object. You make changes to the vSphere environment using a Java service in the vSphere Web Client service layer. The command class for your action extension must either import the Java service, or use a service proxy class. Methods in the Java service perform the actual operations in the vSphere environment.
A best practice is to create a new Java service for your action extensions, and add that service to the vSphere Web Client service layer. You can then create a proxy class for the Java service, and use the service proxy in your command class. See Chapter 11, “vSphere Web Client Service Layer,” on page 87.
Obtaining the Action Target Object
For some actions, such as the Edit Chassis action in Example: Example Command Class, you must obtain a reference to the target vSphere object that the user selected before performing the action. You can obtain a reference to the target vSphere object by using the ActionInvocationEvent that the Actions Framework generates when an action is triggered.
The ActionInvocationEvent contains an object of type ActionContext in the Actions Framework library. The ActionContext object contains an array named targetObjects, from which you can obtain references to all vSphere objects that the user has selected in the user interface. In Example: Example Command Class, the method getIResourceReference shows an example of how to obtain a reference to the target object.
Your command class must import the packages com.vmware.actionsfw.ActionContext and com.vmware.actionsfw.events.ActionInvocationEvent to use these vSphere Web Client SDK features.
Updating the Client with Action Operation Results
When your Java service completes the action operation, you must update the vSphere Web Client with the results of that operation. You update the vSphere Web Client by dispatching a ModelChangeEvent in your command class.
To dispatch a ModelChangeEvent, your command class must import the packages com.vmware.data.common.OperationType, com.vmware.data.common.ObjectChangeInfo, and com.vmware.data.common.events.ModelChangeEvent. You must also annotate your command class as follows.
[Event(name="{com.vmware.data.common.events.ModelChangeEvent.MODEL_CHANGE}",
type="com.vmware.data.common.events.ModelChangeEvent")]
You can use a callback method to ensure that your command class dispatches the necessary ModelChangeEvent. Your service proxy class can invoke the callback method in your command class when the action operation finishes. In Example: Example Command Class, the command class passes references to the appropriate callback methods, onEditChassisComplete and onCreateChassisComplete, when it invokes the action operations in the service proxy. The callback methods then process the results of the action operation and dispatch the ModelChangeEvent as necessary.