/
ControlController.groovy
126 lines (112 loc) · 4.38 KB
/
ControlController.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
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()
}
}
}
}
}