package com.vmware.ops.api.client.examples; import java.util.ArrayList; import java.util.Calendar; import java.util.List; import java.util.TimeZone; import java.util.UUID; import com.vmware.ops.api.client.Client; import com.vmware.ops.api.client.controllers.ResourcesClient; import com.vmware.ops.api.model.property.PropertyContent; import com.vmware.ops.api.model.property.PropertyContents; import com.vmware.ops.api.model.resource.ResourceDto; import com.vmware.ops.api.model.stat.StatContent; import com.vmware.ops.api.model.stat.StatContents; import com.vmware.ops.api.model.resource.ResourceIdentifier; import com.vmware.ops.api.model.resource.ResourceKey; import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import org.apache.commons.io.IOUtils; /** * Example that illustrates the use of API client bindings for resource * CRUD operations */ public class ResourceCRUDExample extends ExampleBase { private ResourcesClient resourcesClient = null; @Override public void run() { Client client = getClient(); this.resourcesClient = client.resourcesClient(); System.out.println("Creating a dummy VM Resource..."); UUID vCenterGUID = UUID.randomUUID(); //vCenterGUID = UUID.fromString("f27dce6c-2867-4b1d-a1dd-a8c9cbf5066b"); //resourcesClient.deleteResource(vCenterGUID); //ResourceDto dto = resourcesClient.get(vCenterGUID); ResourceDto resource = createResource("RCM-COMPLIANCE-TAKE10", "Monitoring RCM Compliance", vCenterGUID, "vm-999"); //ResourceDto resource = createResource("RCM-COMPLIANCE", "Monitoring RCM Compliance", vCenterGUID, "vm-999"); double[] data = new double[1]; long[] timestamps = new long[1]; String[] values = new String[1]; Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC")); long currentTime = cal.getTimeInMillis(); timestamps[0] = currentTime - ((0+1)*300000); UUID uuid = resource.getIdentifier(); System.out.println("UUID to be added= " + uuid.toString() ); String[] discovered630Versions = new String[1]; discovered630Versions[0] = "2.2.13"; String[] discovered730Versions = new String[1]; discovered730Versions[0] = "25.4.0.0020"; String[] rcm630Versions = new String[1]; rcm630Versions[0] = "2.2.14"; String[] rcm730Versions = new String[1]; rcm730Versions[0] = "25.4.0.0021"; //System.out.println("Pushing Numeric Stats data..."); //addStats(null, uuid, "test|test-123|bar1", timestamps, data, null, false); System.out.println("Pushing String Stats data..."); //timestamps[0] = currentTime - ((0+1)*300000); addStats(null, uuid, "VXMADellPERCH730Firmware2|RCM-Version", timestamps, null, rcm730Versions, false); addStats(null, uuid, "VXMADellPERCH730Firmware2|Discovered-Version", timestamps, null, discovered730Versions, false); addStats(null, uuid, "VXMA-DellBIOSforDellR6301|Discovered-Version", timestamps, null, discovered630Versions, false); addStats(null, uuid, "VXMA-DellBIOSforDellR6301|RCM-Version", timestamps, null, rcm630Versions, false); System.out.println("Pushing Numeric Properties data..."); //addProperties(null, uuid, "property|foo1|bar1", timestamps, data, null); System.out.println("Pushing String Properties data..."); //addProperties(null, uuid, "property|foo2|bar2", timestamps, null, values); /*discovered[0] = "25.5.0.0018"; rcm[0] = "25.5.0.0019"; addStats(null, uuid, "VXMA - Dell PERC H730 Firmware|Discovered-Version", timestamps, null, discovered, false); addStats(null, uuid, "VXMA - Dell PERC H730 Firmware|RCM-Version", timestamps, null,rcm , false);*/ System.out.println("Sleep for 20 seconds for resource to get created...zzzz..."); try { Thread.sleep(20*1000); } catch (InterruptedException e) { e.printStackTrace(); } ResourceDto resourceDto= getResourcesWithUUID(resource.getIdentifier()); if(resourceDto.getResourceKey().getName().equals("Foo")) { System.out.println("Resource Foo is indeed created."); } else { System.out.println("Resource Foo is not created."); } /*System.out.println("Updating a dummy VM Resource..."); ResourceKey resourceKey = resource.getResourceKey(); resourceKey.setName("Bar"); resource.setResourceKey(resourceKey); resourceDto = updateResource(resource); System.out.println("Sleep for 20 seconds for resource to get updated...zzzz..."); try { Thread.sleep(20*1000); } catch (InterruptedException e) { e.printStackTrace(); } resourceDto= getResourcesWithUUID(resource.getIdentifier()); if(resourceDto.getResourceKey().getName().equals("Bar")) { System.out.println("Resource Foo is indeed updated to Bar."); } else { System.out.println("Resource Foo is not updated to Bar."); }*/ //System.out.println("Deleting previously created dummy VM resource..."); //deleteResource(resource.getIdentifier()); //System.out.println("Sleep for 20 seconds for resource to get deleted...zzzz..."); try { Thread.sleep(20*1000); } catch (InterruptedException e) { e.printStackTrace(); } resourceDto= getResourcesWithUUID(resource.getIdentifier()); if(resourceDto!=null) { System.out.println("Resource is not deleted successfully."); } else { System.out.println("Resource is deleted successfully."); } } /* private JSONObject getJsonObject(File file){ try { File f = new File("file.json"); if (f.exists()) { InputStream is = new java.io.FileInputStream("file.json"); String jsonTxt = IOUtils.toString(is, "UTF-8"); System.out.println(jsonTxt); JSONObject json = new JSONObject(jsonTxt); String a = json.getString("1000"); System.out.println(a); } } catch(Exception ex){ ex.printStackTrace(); } }*/ /** * Creates a Dummy vSphere Virtual Machine corresponding Resource in vRealize Operations Manager.
* Assumes that the vSphere solution pack has already been installed. * * @param name Name of the Resource * @param description Description for the Resource * @param vCenterGUID UUID of the vCenter where this VM is present * @param moid Mo-id of the VM */ public ResourceDto createResource(String name, String description, UUID vCenterGUID, String moid) { ResourceDto resource = new ResourceDto(); ResourceKey resourceKey = new ResourceKey(); resource.setDescription(description); resourceKey.setName(name); resourceKey.setAdapterKindKey("vCenter Adapter"); //resourceKey.setResourceKindKey("RCM Compliance"); resourceKey.setResourceKindKey("Custom Datacenter"); List resourceIdentifiers = new ArrayList(); /*ResourceIdentifier kv = new ResourceIdentifier("VMEntityVCID", vCenterGUID.toString()); resourceIdentifiers.add(kv); kv = new ResourceIdentifier("VMEntityObjectID", moid); resourceIdentifiers.add(kv); kv = new ResourceIdentifier("VMEntityName", name); resourceIdentifiers.add(kv);*/ resourceKey.setResourceIdentifiers(resourceIdentifiers); resource.setResourceKey(resourceKey); //resource.setIdentifier(UUID.fromString("f27dce6c-2867-4b1d-a1dd-a8c9cbf5066b")); //StatContents stats = new StatContents(); //resourcesClient.findOrCreateResourceAndPushData(resourceKey,"",); return resourcesClient.findOrCreateResourceUsingResourceKey(resourceKey, "ENG-POC-KEY-TAKE-1"); //return resourcesClient.createResource(resource, "ENG-POC-KEY-TAKE-1"); } /** * Delete a resource */ public void deleteResource(UUID resourceId) { resourcesClient.deleteResource(resourceId); } public ResourceDto updateResource(ResourceDto resource) { return resourcesClient.updateResource(resource); } public ResourceDto getResourcesWithUUID(UUID uuid) { try { ResourceDto resourceDto = resourcesClient.get(uuid); return resourceDto; } catch (Exception e) { return null; } } /** * Push stat data for the specified Stat Keys * * @param adapterSourceId the ID of the adapter kind that will push the stats, * may be null which defaults to SuiteAPI adapter * @param resourceUUID vRealize Operations Manager UUID of the Resource * @param statKey Name of the Stat Key * @param timestamps Array of long values as timestamps * @param data Array of double values as data * @param storeOnly true if we want to store the data only, false if we want * analytics processing to be performed */ public void addStats(String adapterSourceId, UUID resourceUUID, String statKey, long[] timestamps, double[] data, String[] values, boolean storeOnly) { System.out.println("Pushing some data for stat key: '" + statKey + "' ..."); StatContents contents = new StatContents(); StatContent content = new StatContent(); content.setStatKey(statKey); content.setData(data); content.setValues(values); content.setTimestamps(timestamps); contents.getStatContents().add(content); if (adapterSourceId == null) { resourcesClient.addStats(resourceUUID, contents, true); } else { resourcesClient.addStats(adapterSourceId, resourceUUID, contents, storeOnly); } } /** * Push stat data for the specified Stat Keys * * @param adapterSourceId the ID of the adapter kind that will push the stats, * may be null which defaults to SuiteAPI adapter * @param resourceUUID vRealize Operations Manager UUID of the Resource * @param statKey Name of the Stat Key * @param timestamps Array of long values as timestamps * @param data Array of double values as data */ public void addProperties(String adapterSourceId, UUID resourceUUID, String statKey, long[] timestamps, double[] data, String[] values) { System.out.println("Pushing some data for property: '" + statKey + "' ..."); PropertyContents contents = new PropertyContents(); PropertyContent content = new PropertyContent(); content.setStatKey(statKey); content.setData(data); content.setValues(values); content.setTimestamps(timestamps); contents.getPropertyContents().add(content); if (adapterSourceId == null) { resourcesClient.addProperties(resourceUUID, contents); } else { resourcesClient.addProperties(adapterSourceId, resourceUUID, contents); } } public static void main(String[] args) { new ResourceCRUDExample().run(); } }