Skip to content

Commit

Permalink
[HHQ-3658] Add MetricApi.reschedule() API.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan Morgan committed Jan 9, 2010
1 parent 525c0dc commit a008896
Show file tree
Hide file tree
Showing 6 changed files with 197 additions and 1 deletion.
2 changes: 2 additions & 0 deletions ChangeLog
@@ -1,6 +1,8 @@

Changes in HQApi 2.4

*) [HHQ-3658] Add MetricApi.reschedule() API.

*) [HHQ-3603] Add support for location field for Resources.

*) [HHQ-3583] Fix handling of -s and --secure CLI options.
Expand Down
44 changes: 44 additions & 0 deletions hqu/hqapi1/app/MetricController.groovy
@@ -1,6 +1,8 @@
import org.hyperic.hq.hqu.rendit.BaseController

import org.hyperic.hq.hqapi1.ErrorCode;
import org.hyperic.hq.zevents.ZeventManager;
import org.hyperic.hq.appdef.server.session.ResourceRefreshZevent;

class MetricController extends ApiController {

Expand Down Expand Up @@ -529,4 +531,46 @@ class MetricController extends ApiController {
}
}
}

def reschedule(params) {

def schedRequest = new XmlParser().parseText(getUpload('postdata'))
def xmlResources = schedRequest['Resource']

if (!xmlResources) {
renderXml() {
StatusResponse() {
out << getFailureXML(ErrorCode.INVALID_PARAMETERS)
}
}
return
}

def zevents = []
for (res in xmlResources) {
def resource = getResource(res.'@id'.toInteger())
if (!resource) {
renderXml() {
StatusResponse() {
out << getFailureXML(ErrorCode.OBJECT_NOT_FOUND,
"Resource with id " + id +
" not found")
}
}
return
}

def zevent = new ResourceRefreshZevent(user,
resource.entityId);
zevents.add(zevent);
}

ZeventManager.getInstance().enqueueEvents(zevents);

renderXml() {
StatusResponse() {
out << getSuccessXML()
}
}
}
}
40 changes: 40 additions & 0 deletions src/org/hyperic/hq/hqapi1/MetricApi.java
Expand Up @@ -40,11 +40,13 @@
import org.hyperic.hq.hqapi1.types.Resource;
import org.hyperic.hq.hqapi1.types.ResourcePrototype;
import org.hyperic.hq.hqapi1.types.StatusResponse;
import org.hyperic.hq.hqapi1.types.MetricsRescheduleRequest;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.ArrayList;

/**
* The Hyperic HQ Metric API.
Expand Down Expand Up @@ -304,4 +306,42 @@ public MetricsDataResponse getMetricData(int[] resourceIds, int templateId,
return doGet("metric/getResourceData.hqu", params,
MetricsDataResponse.class);
}

// Helper function to unroll a resource and it's children into a single list.
private List<Resource> getFlattenResources(List<Resource> resources) {
List<Resource> result = new ArrayList<Resource>();

for (Resource r : resources) {
result.add(r);
if (r.getResource().size() > 0) {
result.addAll(getFlattenResources(r.getResource()));
}
}
return result;
}

/**
* Reschedule metrics for the given list of {@link org.hyperic.hq.hqapi1.types.Resource}s.
* If the passed in List has child resources, those Resources will also be
* rescheduled.
*
* @param resources A List of {@link org.hyperic.hq.hqapi1.types.Resource}'s
* to reschedule.
*
* @return {@link org.hyperic.hq.hqapi1.types.ResponseStatus#SUCCESS} if the
* metrics were successfully retrieved.
*
* @throws IOException If a network error occurs while making the request.
*/
public StatusResponse reschedule(List<Resource> resources)
throws IOException {

List<Resource> flattened = getFlattenResources(resources);

MetricsRescheduleRequest request = new MetricsRescheduleRequest();
request.getResource().addAll(flattened);

return doPost("metric/reschedule.hqu", request,
StatusResponse.class);
}
}
62 changes: 62 additions & 0 deletions src/org/hyperic/hq/hqapi1/test/MetricReschedule_test.java
@@ -0,0 +1,62 @@
package org.hyperic.hq.hqapi1.test;

import org.hyperic.hq.hqapi1.MetricApi;
import org.hyperic.hq.hqapi1.types.StatusResponse;
import org.hyperic.hq.hqapi1.types.Resource;

import java.util.List;
import java.util.ArrayList;

public class MetricReschedule_test extends MetricTestBase {

public MetricReschedule_test(String name) {
super(name);
}

public void testRescheduleSingle() throws Exception {
MetricApi api = getApi().getMetricApi();

Resource platform = getLocalPlatformResource(false, false);
List<Resource> resources = new ArrayList<Resource>();
resources.add(platform);

StatusResponse response = api.reschedule(resources);
hqAssertSuccess(response);
}

public void testRescheduleMulti() throws Exception {
MetricApi api = getApi().getMetricApi();

// Recurse flag will cause all resources on this platform to be
// rescheduled.
Resource platform = getLocalPlatformResource(false, true);
List<Resource> resources = new ArrayList<Resource>();
resources.add(platform);

StatusResponse response = api.reschedule(resources);
hqAssertSuccess(response);
}

public void testRescheduleInvalidResource() throws Exception {
MetricApi api = getApi().getMetricApi();

List<Resource> resources = new ArrayList<Resource>();

Resource r = new Resource();
r.setId(Integer.MAX_VALUE);
resources.add(r);

StatusResponse response = api.reschedule(resources);
hqAssertFailureObjectNotFound(response);
}

public void testRescheduleNoResources() throws Exception {

MetricApi api = getApi().getMetricApi();

List<Resource> resources = new ArrayList<Resource>();

StatusResponse response = api.reschedule(resources);
hqAssertFailureInvalidParameters(response);
}
}
40 changes: 39 additions & 1 deletion src/org/hyperic/hq/hqapi1/tools/MetricCommand.java
Expand Up @@ -37,17 +37,21 @@
import org.hyperic.hq.hqapi1.types.MetricsResponse;
import org.hyperic.hq.hqapi1.types.ResourceResponse;
import org.hyperic.hq.hqapi1.types.StatusResponse;
import org.hyperic.hq.hqapi1.types.ResourcesResponse;
import org.hyperic.hq.hqapi1.types.Resource;

import java.io.InputStream;
import java.util.Arrays;
import java.util.List;
import java.util.ArrayList;

public class MetricCommand extends Command {

private static String CMD_LIST = "list";
private static String CMD_SYNC = "sync";
private static String CMD_RESCHEDULE = "reschedule";

private static String[] COMMANDS = { CMD_LIST, CMD_SYNC };
private static String[] COMMANDS = { CMD_LIST, CMD_SYNC, CMD_RESCHEDULE };

private static final String OPT_RESOURCE_ID = "id";
private static final String OPT_ENABLED = "enabled";
Expand All @@ -66,6 +70,8 @@ protected void handleCommand(String[] args) throws Exception {
list(trim(args));
} else if (args[0].equals(CMD_SYNC)) {
sync(trim(args));
} else if (args[0].equals(CMD_RESCHEDULE)) {
reschedule(trim(args));
} else {
printUsage();
System.exit(-1);
Expand Down Expand Up @@ -123,4 +129,36 @@ private void sync(String[] args) throws Exception {

System.out.println("Successfully synced " + metrics.size() + " metrics.");
}

private long getResourceCount(List<Resource> resources) {
long num = 0;
for (Resource r : resources) {
num++;
if (r.getResource().size() > 0) {
num += getResourceCount(r.getResource());
}
}
return num;
}

private void reschedule(String args[]) throws Exception {

OptionParser p = getOptionParser();
OptionSet options = getOptions(p, args);

HQApi api = getApi(options);

MetricApi metricApi = api.getMetricApi();

InputStream is = getInputStream(options);

ResourcesResponse resp = XmlUtil.deserialize(ResourcesResponse.class, is);
checkSuccess(resp);

StatusResponse response = metricApi.reschedule(resp.getResource());
checkSuccess(response);

System.out.println("Successfully rescheduled " + getResourceCount(resp.getResource()) +
" resources");
}
}
10 changes: 10 additions & 0 deletions xsd/HQApi1.xsd
Expand Up @@ -504,6 +504,16 @@
</xs:complexType>
</xs:element>

<!-- Reschedule request -->

<xs:element name="MetricsRescheduleRequest">
<xs:complexType>
<xs:sequence>
<xs:element name="Resource" type="Resource" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>

<!-- MetricTemplate objects -->

<xs:complexType name="MetricTemplate">
Expand Down

0 comments on commit a008896

Please sign in to comment.