Creating Data View
Extensions
When you create
data view extensions for the
vSphere Client
user interface layer, follow these general recommendations:
- You do not need to change the Data Adapter services running in the service layer.
- You can use the genericDataAccessControllerJava class provided with each generated plug-in project to handle HTTP JSONGETdata requests.
- You must access data through thevSphere Clientserver and avoid calling directly your back end services or database.
Common Data Access
Pattern
You can use the pattern
demonstrated in the
html-sample
in the
SDK to access data from the
vCenter Server
system from your plug-ins:
- The AjaxGETrequest created in your JavaScript code has the following format:
, where/plugin_context_path/rest/data/properties/objectId?properties=properties-listobjectIdis the object ID of the currently selected vSphere object, andproperties-listis the comma-separated list of properties that must be retrieved for that object. - Theweb.xmldeployment descriptor located in theWEB-INFfolder of the UI bundle of your plug-in contains the<servlet-mapping>element that defines the/rest/*URL pattern for invoking thespringServletservlet.<servlet-mapping> <servlet-name>springServlet</servlet-name> <url-pattern>/rest/*</url-pattern> </servlet-mapping>
- Thebundle-context.xmlfile located in theWEB-INF\springfolder declares thedataAccessControllerbean for theDataAccessControllerclass that is available in the Java service bundle of your plug-in.<bean name="dataAccessController" class="com.vmware.samples.vspherewssdk.mvc.DataAccessController" />
- TheDataAccessControllerclass included in the Java service bundle of your plug-in has the@RequestMappingannotation set to process the HTTP JSONGETfor the/dataendpoint. ThegetProperties()generic method has the@RequestMappingannotation set to the/properties/{objectId}value to handle the AjaxGETrequests created in your JavaScript code.... @Controller @RequestMapping(value = "/data", method = RequestMethod.GET) public class DataAccessController { ... @RequestMapping(value = "/properties/{objectId}") @ResponseBody public Map<String, Object> getProperties( @PathVariable("objectId") String encodedObjectId, @RequestParam(value = "properties", required = true) String properties) ...
- ThegetProperties()generic method uses theQueryUtilclass to create a Data Service query for the requested list of vSphere object properties. The query results are returned to the Web browser as JSON data.... Object ref = getDecodedReference(encodedObjectId); String objectId = _objectReferenceService.getUid(ref); String[] props = properties.split(","); PropertyValue[] pvs = QueryUtil.getProperties(_dataService, ref, props); Map<String, Object> propsMap = new HashMap<String, Object>(); propsMap.put(OBJECT_ID, objectId); for (PropertyValue pv : pvs) { propsMap.put(pv.propertyName, pv.value); } return propsMap; }
- The JavaScript code can display the data returned by the AjaxGETrequest as needed.