Skip to content

Commit

Permalink
Add support for group control actions and history
Browse files Browse the repository at this point in the history
  • Loading branch information
pnguyen committed Sep 24, 2009
1 parent 774f994 commit 073589b
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 6 deletions.
3 changes: 3 additions & 0 deletions hqu/hqapi1/app/ControlController.groovy
Expand Up @@ -100,6 +100,9 @@ class ControlController extends ApiController {
if (!resource) {
failureXml = getFailureXML(ErrorCode.OBJECT_NOT_FOUND,
"Resource id " + resourceId + " not found")
} else if (resource.entityId.isGroup()) {
failureXml = getFailureXML(ErrorCode.NOT_SUPPORTED,
"Group control action execution is not supported")
} else if (!action) {
failureXml = getFailureXML(ErrorCode.INVALID_PARAMETERS,
"No action given")
Expand Down
1 change: 1 addition & 0 deletions hqu/hqapi1/app/GroupController.groovy
Expand Up @@ -6,6 +6,7 @@ class GroupController extends ApiController {
private Closure getGroupXML(g) {
{ doc ->
Group(id : g.id,
resourceId : g.resource.id,
name : g.name,
description : g.description,
location : g.location) {
Expand Down
83 changes: 78 additions & 5 deletions src/org/hyperic/hq/hqapi1/ControlApi.java
Expand Up @@ -28,6 +28,7 @@
package org.hyperic.hq.hqapi1;

import org.hyperic.hq.hqapi1.types.ControlHistoryResponse;
import org.hyperic.hq.hqapi1.types.Group;
import org.hyperic.hq.hqapi1.types.Resource;
import org.hyperic.hq.hqapi1.types.ControlActionResponse;
import org.hyperic.hq.hqapi1.types.StatusResponse;
Expand Down Expand Up @@ -64,13 +65,36 @@ public class ControlApi extends BaseApi {
*/
public ControlHistoryResponse getHistory(Resource r)
throws IOException
{
return getHistory(r.getId());
}

/**
* Get the Control history for the given Group.
*
* @param g The {@link org.hyperic.hq.hqapi1.types.Group} to query.
*
* @return On {@link org.hyperic.hq.hqapi1.types.ResponseStatus#SUCCESS},
* a list of {@link org.hyperic.hq.hqapi1.types.ControlHistory}'s are returned via
* {@link org.hyperic.hq.hqapi1.types.ControlHistoryResponse#getControlHistory()}.
*
* @throws IOException If a network error occurs while making the request.
*/
public ControlHistoryResponse getHistory(Group g)
throws IOException
{
return getHistory(g.getResourceId());
}

private ControlHistoryResponse getHistory(Integer resourceId)
throws IOException
{
Map<String,String[]> params = new HashMap<String,String[]>();
params.put("resourceId", new String[] { Integer.toString(r.getId())});
params.put("resourceId", new String[] { Integer.toString(resourceId)});

return doGet("control/history.hqu", params, ControlHistoryResponse.class);
}

/**
* Get the Control actions for the given Resource.
*
Expand All @@ -84,13 +108,36 @@ public ControlHistoryResponse getHistory(Resource r)
*/
public ControlActionResponse getActions(Resource r)
throws IOException
{
return getActions(r.getId());
}

/**
* Get the Control actions for the given Group.
*
* @param g The {@link org.hyperic.hq.hqapi1.types.Group} to query.
*
* @return On {@link org.hyperic.hq.hqapi1.types.ResponseStatus#SUCCESS},
* a list of actions are returned via
* {@link org.hyperic.hq.hqapi1.types.ControlActionResponse#getAction()}.
*
* @throws IOException If a network error occurs while making the request.
*/
public ControlActionResponse getActions(Group g)
throws IOException
{
return getActions(g.getResourceId());
}

private ControlActionResponse getActions(Integer resourceId)
throws IOException
{
Map<String,String[]> params = new HashMap<String,String[]>();
params.put("resourceId", new String[] { Integer.toString(r.getId())});
params.put("resourceId", new String[] { Integer.toString(resourceId)});

return doGet("control/actions.hqu", params, ControlActionResponse.class);
}

/**
* Execute a Control action on the given Resource.
*
Expand All @@ -106,9 +153,35 @@ public ControlActionResponse getActions(Resource r)
public StatusResponse executeAction(Resource r, String action,
String[] arguments)
throws IOException
{
return executeAction(r.getId(), action, arguments);
}

/**
* Execute a Control action on the given Resource.
*
* @param g The {@link org.hyperic.hq.hqapi1.types.Group} to execute the action on.
* @param action The action to run.
* @param arguments An array of arguments to pass to the action.
*
* @return {@link org.hyperic.hq.hqapi1.types.ResponseStatus#SUCCESS}
* if the action was executed successfully.
*
* @throws IOException If a network error occurs while making the request.
*/
public StatusResponse executeAction(Group g, String action,
String[] arguments)
throws IOException
{
return executeAction(g.getResourceId(), action, arguments);
}

private StatusResponse executeAction(Integer resourceId, String action,
String[] arguments)
throws IOException
{
Map<String,String[]> params = new HashMap<String,String[]>();
params.put("resourceId", new String[] { Integer.toString(r.getId())});
params.put("resourceId", new String[] { Integer.toString(resourceId)});
params.put("action", new String[] { action });
params.put("arguments", arguments);

Expand Down
38 changes: 37 additions & 1 deletion src/org/hyperic/hq/hqapi1/test/ControlTestBase.java
@@ -1,17 +1,20 @@
package org.hyperic.hq.hqapi1.test;

import org.hyperic.hq.hqapi1.types.Group;
import org.hyperic.hq.hqapi1.types.GroupResponse;
import org.hyperic.hq.hqapi1.types.Resource;
import org.hyperic.hq.hqapi1.types.ResourcePrototypeResponse;
import org.hyperic.hq.hqapi1.types.ResourceResponse;
import org.hyperic.hq.hqapi1.types.StatusResponse;
import org.hyperic.hq.hqapi1.GroupApi;
import org.hyperic.hq.hqapi1.HQApi;
import org.hyperic.hq.hqapi1.ResourceApi;

import java.util.Map;
import java.util.HashMap;
import java.util.Random;

public class ControlTestBase extends HQApiTestBase {
public class ControlTestBase extends GroupTestBase {

public ControlTestBase(String name) {
super(name);
Expand Down Expand Up @@ -43,6 +46,22 @@ public Resource createControllableResource(HQApi api)

return resourceCreateResponse.getResource();
}

public Group createControllableGroup(HQApi api)
throws Exception
{
GroupApi groupApi = api.getGroupApi();

Resource controllableResource = createControllableResource(api);
Group g = generateTestGroup();
g.setResourcePrototype(controllableResource.getResourcePrototype());
g.getResource().add(controllableResource);

GroupResponse createGroupResponse = groupApi.createGroup(g);
hqAssertSuccess(createGroupResponse);

return createGroupResponse.getGroup();
}

public void cleanupControllableResource(HQApi api, Resource r)
throws Exception
Expand All @@ -54,4 +73,21 @@ public void cleanupControllableResource(HQApi api, Resource r)
StatusResponse response = rApi.deleteResource(r.getId());
hqAssertSuccess(response);
}

public void cleanupControllableGroup(HQApi api, Group g)
throws Exception
{
pauseTest();

GroupApi groupApi = api.getGroupApi();
ResourceApi rApi = api.getResourceApi();

for (Resource r : g.getResource()) {
StatusResponse response = rApi.deleteResource(r.getId());
hqAssertSuccess(response);
}

StatusResponse response = groupApi.deleteGroup(g.getId());
hqAssertSuccess(response);
}
}
1 change: 1 addition & 0 deletions xsd/HQApi1.xsd
Expand Up @@ -375,6 +375,7 @@
<xs:attribute name="description" type="xs:string"/>
<xs:attribute name="name" type="xs:string" use="required"/>
<xs:attribute name="id" type="xs:int" use="optional" />
<xs:attribute name="resourceId" type="xs:int" use="optional" />
</xs:complexType>

<!-- Group Responses -->
Expand Down

0 comments on commit 073589b

Please sign in to comment.