Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
12 changed files
with
702 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
Oops, something went wrong.