Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[HHQ-3248] Add support for ControlApi.
  • Loading branch information
Ryan Morgan committed Sep 10, 2009
1 parent 6e29814 commit 098a1f6
Show file tree
Hide file tree
Showing 12 changed files with 702 additions and 0 deletions.
3 changes: 3 additions & 0 deletions ChangeLog
@@ -1,6 +1,9 @@

Changes in HQApi 3.0

*) [HHQ-3248] Add ControlApi for running control actions and viewing control
action history.

*) [HHQ-3367] Trim whitespace from properties included in client.properties.

*) [HHQ-3284] Add EventApi for gathering events globally or by resource.
Expand Down
126 changes: 126 additions & 0 deletions hqu/hqapi1/app/ControlController.groovy
@@ -0,0 +1,126 @@
import org.hyperic.hq.hqapi1.ErrorCode;
import org.hyperic.hq.control.server.session.ControlScheduleManagerEJBImpl as CMan
import org.hyperic.util.pager.PageControl

class ControlController extends ApiController {

private Closure getHistoryXML(h) {
{ doc ->
ControlHistory(scheduled: h.scheduled,
dateScheduled: h.dateScheduled,
endTime: h.endTime,
startTime: h.startTime,
status: h.status,
message: h.message,
description: h.description,
args: h.args,
action: h.action)
}
}

private Closure getActionXML(action) {
{ doc ->
Action(action)
}
}

def actions(params) {
def failureXml = null
def resourceId = params.getOne('resourceId')?.toInteger()

def resource = getResource(resourceId)

def actions = []
if (!resource) {
failureXml = getFailureXML(ErrorCode.OBJECT_NOT_FOUND,
"Resource id " + resourceId + " not found")
} else {
// TODO: Seems that permissions are not applied here. XXX: need test
actions = resource.getControlActions(user)
}

renderXml() {
out << ControlActionResponse() {
if (failureXml) {
out << failureXml
} else {
// TODO: If resource has no actions should there be an error?
out << getSuccessXML()
for (action in actions.sort {a, b -> a <=> b}) {
out << getActionXML(action)
}
}
}
}
}

def history(params) {
def failureXml = null
def resourceId = params.getOne('resourceId')?.toInteger()

def resource = getResource(resourceId)
def history = []
if (!resource) {
failureXml = getFailureXML(ErrorCode.OBJECT_NOT_FOUND,
"Resource id " + resourceId + " not found")
} else {
// TODO: This needs to be pulled into ResourceHelper
def cMan = CMan.one
history = cMan.findJobHistory(user, resource.entityId,
PageControl.PAGE_ALL)
}

renderXml() {
out << ControlHistoryResponse() {
if (failureXml) {
out << failureXml
} else {
out << getSuccessXML()
for (h in history) {
out << getHistoryXML(h)
}
}
}
}
}

def execute(params) {
def failureXml = null
def resourceId = params.getOne('resourceId')?.toInteger()
def action = params.getOne('action')
def arguments = params.get('arguments')?.join(",")

def resource = getResource(resourceId)
if (!resource) {
failureXml = getFailureXML(ErrorCode.OBJECT_NOT_FOUND,
"Resource id " + resourceId + " not found")
} else if (!action) {
failureXml = getFailureXML(ErrorCode.INVALID_PARAMETERS,
"No action given")
} else {
try {
resource.runAction(user, action, arguments)
} catch (org.hyperic.hq.product.PluginException e) {
failureXml = getFailureXML(ErrorCode.NOT_SUPPORTED,
"Resource id " + resourceId +
" does not support action " + action)
} catch (org.hyperic.hq.authz.shared.PermissionException e) {
failureXml = getFailureXML(ErrorCode.PERMISSION_DENIED)
} catch (Exception e) {
failureXml = getFailureXML(ErrorCode.UNEXPECTED_ERROR,
"Unexpected error: " + e.getMessage())
}
}

renderXml() {
out << StatusResponse() {
if (failureXml) {
out << failureXml
} else {
out << getSuccessXML()
}
}
}
}
}

117 changes: 117 additions & 0 deletions src/org/hyperic/hq/hqapi1/ControlApi.java
@@ -0,0 +1,117 @@
/*
*
* NOTE: This copyright does *not* cover user programs that use HQ
* program services by normal system calls through the application
* program interfaces provided as part of the Hyperic Plug-in Development
* Kit or the Hyperic Client Development Kit - this is merely considered
* normal use of the program, and does *not* fall under the heading of
* "derived work".
*
* Copyright (C) [2008, 2009], Hyperic, Inc.
* This file is part of HQ.
*
* HQ is free software; you can redistribute it and/or modify
* it under the terms version 2 of the GNU General Public License as
* published by the Free Software Foundation. This program is distributed
* in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA.
*
*/

package org.hyperic.hq.hqapi1;

import org.hyperic.hq.hqapi1.types.ControlHistoryResponse;
import org.hyperic.hq.hqapi1.types.Resource;
import org.hyperic.hq.hqapi1.types.ControlActionResponse;
import org.hyperic.hq.hqapi1.types.StatusResponse;

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

/**
* The Hyperic HQ Control API.
* <br><br>
* This class provides access to the control actions within the HQ system. Each of
* the methods in this class return response objects that wrap the result of the
* method with a {@link org.hyperic.hq.hqapi1.types.ResponseStatus} and a
* {@link org.hyperic.hq.hqapi1.types.ServiceError} that indicates the error
* if the response status is {@link org.hyperic.hq.hqapi1.types.ResponseStatus#FAILURE}.
*/
public class ControlApi extends BaseApi {

ControlApi(HQConnection conn) {
super(conn);
}

/**
* Get the Control history for the given Resource.
*
* @param r The {@link org.hyperic.hq.hqapi1.types.Resource} 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(Resource r)
throws IOException
{
Map<String,String[]> params = new HashMap<String,String[]>();
params.put("resourceId", new String[] { Integer.toString(r.getId())});

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

/**
* Get the Control actions for the given Resource.
*
* @param r The {@link org.hyperic.hq.hqapi1.types.Resource} 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(Resource r)
throws IOException
{
Map<String,String[]> params = new HashMap<String,String[]>();
params.put("resourceId", new String[] { Integer.toString(r.getId())});

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

/**
* Execute a Control action on the given Resource.
*
* @param r The {@link org.hyperic.hq.hqapi1.types.Resource} 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(Resource r, 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("action", new String[] { action });
params.put("arguments", arguments);

return doGet("control/execute.hqu", params, StatusResponse.class);
}
}
13 changes: 13 additions & 0 deletions src/org/hyperic/hq/hqapi1/HQApi.java
Expand Up @@ -49,6 +49,7 @@ public class HQApi {
private final AlertApi _alertApi;
private final MetricDataApi _metricDataApi;
private final EventApi _eventApi;
private final ControlApi _controlApi;

/**
* @param host The hostname of the HQ Server to connect to.
Expand Down Expand Up @@ -76,6 +77,7 @@ public HQApi(String host, int port, boolean isSecure, String user,
_alertApi = new AlertApi(connection);
_metricDataApi = new MetricDataApi(connection);
_eventApi = new EventApi(connection);
_controlApi = new ControlApi(connection);
}

/**
Expand Down Expand Up @@ -208,8 +210,19 @@ public AlertApi getAlertApi() {

/**
* List events in HQ
*
* @return The API for finding Events.
*/
public EventApi getEventApi() {
return _eventApi;
}

/**
* View actions, control history and issue commands on Resources.
*
* @return The API for viewing and issuing control actions.
*/
public ControlApi getControlApi() {
return _controlApi;
}
}
35 changes: 35 additions & 0 deletions src/org/hyperic/hq/hqapi1/test/ControlAction_test.java
@@ -0,0 +1,35 @@
/*
*
* NOTE: This copyright does *not* cover user programs that use HQ
* program services by normal system calls through the application
* program interfaces provided as part of the Hyperic Plug-in Development
* Kit or the Hyperic Client Development Kit - this is merely considered
* normal use of the program, and does *not* fall under the heading of
* "derived work".
*
* Copyright (C) [2008, 2009], Hyperic, Inc.
* This file is part of HQ.
*
* HQ is free software; you can redistribute it and/or modify
* it under the terms version 2 of the GNU General Public License as
* published by the Free Software Foundation. This program is distributed
* in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA.
*
*/

package org.hyperic.hq.hqapi1.test;

public class ControlAction_test extends HQApiTestBase {

public ControlAction_test(String name) {
super(name);
}
}
35 changes: 35 additions & 0 deletions src/org/hyperic/hq/hqapi1/test/ControlExecute_test.java
@@ -0,0 +1,35 @@
/*
*
* NOTE: This copyright does *not* cover user programs that use HQ
* program services by normal system calls through the application
* program interfaces provided as part of the Hyperic Plug-in Development
* Kit or the Hyperic Client Development Kit - this is merely considered
* normal use of the program, and does *not* fall under the heading of
* "derived work".
*
* Copyright (C) [2008, 2009], Hyperic, Inc.
* This file is part of HQ.
*
* HQ is free software; you can redistribute it and/or modify
* it under the terms version 2 of the GNU General Public License as
* published by the Free Software Foundation. This program is distributed
* in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA.
*
*/

package org.hyperic.hq.hqapi1.test;

public class ControlExecute_test extends HQApiTestBase {

public ControlExecute_test(String name) {
super(name);
}
}
35 changes: 35 additions & 0 deletions src/org/hyperic/hq/hqapi1/test/ControlHistory_test.java
@@ -0,0 +1,35 @@
/*
*
* NOTE: This copyright does *not* cover user programs that use HQ
* program services by normal system calls through the application
* program interfaces provided as part of the Hyperic Plug-in Development
* Kit or the Hyperic Client Development Kit - this is merely considered
* normal use of the program, and does *not* fall under the heading of
* "derived work".
*
* Copyright (C) [2008, 2009], Hyperic, Inc.
* This file is part of HQ.
*
* HQ is free software; you can redistribute it and/or modify
* it under the terms version 2 of the GNU General Public License as
* published by the Free Software Foundation. This program is distributed
* in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA.
*
*/

package org.hyperic.hq.hqapi1.test;

public class ControlHistory_test extends HQApiTestBase {

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

0 comments on commit 098a1f6

Please sign in to comment.