/
ApplicationController.groovy
393 lines (347 loc) · 13.7 KB
/
ApplicationController.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
import org.hyperic.hq.hqapi1.ErrorCode
import org.hyperic.hq.appdef.server.session.ApplicationManagerEJBImpl as AppMan
import org.hyperic.hq.bizapp.server.session.AppdefBossEJBImpl as ABoss
import org.hyperic.hq.authz.server.session.ResourceManagerEJBImpl as ResMan
import org.hyperic.util.pager.PageControl
import org.hyperic.hq.auth.shared.SessionManager
import org.hyperic.hq.appdef.shared.AppdefEntityID
import org.hyperic.hq.appdef.shared.ApplicationValue
import org.hyperic.hq.appdef.shared.ServiceValue
import org.hyperic.dao.DAOFactory
import org.hyperic.hq.appdef.server.session.AppServiceDAO
import org.hyperic.hq.appdef.shared.AppdefDuplicateNameException;
class ApplicationController extends ApiController {
def appMan = AppMan.one
def aBoss = ABoss.one
def resMan = ResMan.one
def failureXml = null
private Closure getApplicationXML(a) {
{ doc ->
Application(id : a.id,
name : a.name,
location : a.location,
description : a.description,
engContact : a.engContact,
opsContact : a.opsContact,
bizContact : a.businessContact) {
def sessionId = SessionManager.instance.put(user)
def resHelper = resourceHelper
def legacy = true
def inventory = aBoss.findServiceInventoryByApplication(sessionId, a.id, PageControl.PAGE_ALL)
for (appService in inventory) {
if (appService instanceof ServiceValue) {
if (appService.metaClass.respondsTo(appService, "getResourceId")) {
legacy = false
}
break
}
}
for (appService in inventory) {
if (appService instanceof ServiceValue) {
def resourceId = null
if (legacy) {
def resource = resHelper.find('service':appService.id)
resourceId = resource.id
} else {
resourceId = appService.resourceId
}
Resource(id : resourceId,
name : appService.name,
description : appService.description)
}
}
}
}
}
def list(params) {
renderXml() {
out << ApplicationsResponse() {
out << getSuccessXML()
for (app in appMan.getAllApplications(user, PageControl.PAGE_ALL)) {
out << getApplicationXML(app)
}
}
}
}
/**
* Validate <Resource> XML within an Application to ensure all passed
* resources are service types.
* @return true if Resources are valid, false otherwise.
*/
private validateApplicationServices(xmlApplication) {
for (xmlResource in xmlApplication['Resource']) {
def rid = xmlResource.'@id'?.toInteger()
def resource = resourceHelper.findById(rid)
if (!resource.isService()) {
failureXml = getFailureXML(ErrorCode.INVALID_PARAMETERS,
"Invalid resource passed to create, " +
resource.name + " is not a service")
return false
}
}
return true
}
private buildServiceList(resources) {
def svcList = [] // List of AppdefEntityID's to add to the application
if (resources) {
resources.each { res ->
def rid = res.'@id'?.toInteger()
def sid = resMan.findResourceById(rid)?.instanceId
def entId = AppdefEntityID.newServiceID(sid)
if (!svcList.contains(entId)) {
svcList.add(entId)
}
}
}
svcList
}
/**
* Create an Application via XML.
*
* @return the Created application or null if Application creation
* failed. In that case the caller should use failureXml to determine
* the cause.
*/
private createApplication(xmlApplication) {
if (!validateApplicationServices(xmlApplication)) {
return null
}
def appName = xmlApplication.'@name'
def appLoc = xmlApplication.'@location'
def appDesc = xmlApplication.'@description'
def appEng = xmlApplication.'@engContact'
def appOps = xmlApplication.'@opsContact'
def appBiz = xmlApplication.'@bizContact'
def applicationValue = new ApplicationValue()
applicationValue.name = appName
applicationValue.location = appLoc
applicationValue.description = appDesc
applicationValue.engContact = appEng
applicationValue.opsContact = appOps
applicationValue.businessContact = appBiz
// Build resource list prior to update to avoid dirty checking of
// all Resource objects.
def resources = buildServiceList(xmlApplication['Resource'])
def newApp
try {
applicationValue.applicationType = appMan.findApplicationType(1)
newApp = appMan.createApplication(user, applicationValue, new ArrayList())
// Initialize appServices to avoid NPE
newApp.appServices = new ArrayList()
} catch (AppdefDuplicateNameException e) {
failureXml = getFailureXML(ErrorCode.OBJECT_EXISTS,
"Existing application with name " + appName +
"already exists.")
return null
} catch (Exception e) {
log.error("Error creating application", e)
failureXml = getFailureXML(ErrorCode.UNEXPECTED_ERROR,
"Error creating application: " + e.message)
return null
}
updateAppServices(newApp, resources)
return newApp
}
def create(params) {
def createRequest = new XmlParser().parseText(getPostData())
def xmlApplication = createRequest['Application']
def newApp
if (!xmlApplication || xmlApplication.size() != 1) {
failureXml = getFailureXML(ErrorCode.INVALID_PARAMETERS,
"Wrong number of Applications")
} else {
newApp = createApplication(xmlApplication[0])
}
renderXml() {
ApplicationResponse() {
if (failureXml) {
out << failureXml
} else {
out << getSuccessXML()
out << getApplicationXML(newApp.applicationValue)
}
}
}
}
/**
* Update an Application via XML.
*
* @return the Created application or null if Application creation
* failed. In that case the caller should use failureXml to determine
* the cause.
*/
private updateApplication(xmlApplication) {
def appId = xmlApplication.'@id'?.toInteger()
if (!appId) {
failureXml = getFailureXML(ErrorCode.INVALID_PARAMETERS,
"No application id found")
return null
}
if (!validateApplicationServices(xmlApplication)) {
return null
}
def appName = xmlApplication.'@name'
def appLoc = xmlApplication.'@location'
def appDesc = xmlApplication.'@description'
def appEng = xmlApplication.'@engContact'
def appOps = xmlApplication.'@opsContact'
def appBiz = xmlApplication.'@bizContact'
def updateApp
try {
updateApp = appMan.findApplicationById(user, appId)
} catch (Exception e) {
log.error("Error finding application" + e)
failureXml = getFailureXML(ErrorCode.OBJECT_NOT_FOUND,
"Unable to find application with " +
"id " + appId)
return null
}
def applicationValue = updateApp.getApplicationValue()
applicationValue.name = appName
applicationValue.location = appLoc
applicationValue.description = appDesc
applicationValue.engContact = appEng
applicationValue.opsContact = appOps
applicationValue.businessContact = appBiz
// Build resource list prior to update to avoid dirty checking of
// all Resource objects.
def resources = buildServiceList(xmlApplication['Resource'])
try {
appMan.updateApplication(user, applicationValue)
} catch (AppdefDuplicateNameException e) {
failureXml = getFailureXML(ErrorCode.INVALID_PARAMETERS,
"There is already an application named " +
appName)
return null
} catch (Exception e) {
log.error("Error updating application", e)
failureXml = getFailureXML(ErrorCode.UNEXPECTED_ERROR)
return null
}
updateAppServices(updateApp, resources)
return getApplication(appId)
}
def update(params) {
def updateRequest = new XmlParser().parseText(getPostData())
def xmlApplication = updateRequest['Application']
def updatedApp
if (!xmlApplication || xmlApplication.size() != 1) {
failureXml = getFailureXML(ErrorCode.INVALID_PARAMETERS,
"Wrong number of Applications")
} else {
updatedApp = updateApplication(xmlApplication[0])
}
renderXml() {
ApplicationResponse() {
if (failureXml) {
out << failureXml
} else {
out << getSuccessXML()
out << getApplicationXML(updatedApp.applicationValue)
}
}
}
}
def sync(params) {
def syncRequest = new XmlParser().parseText(getPostData())
def applications = []
for (xmlApplication in syncRequest['Application']) {
def appId = xmlApplication.'@id'?.toInteger()
if (!appId) {
applications << createApplication(xmlApplication)
} else {
applications << updateApplication(xmlApplication)
}
if (failureXml) {
// Break out early on errors.
break
}
}
renderXml() {
ApplicationsResponse() {
if (failureXml) {
out << failureXml
} else {
out << getSuccessXML()
for (app in applications) {
out << getApplicationXML(app)
}
}
}
}
}
def delete(params) {
def id = params.getOne('id')?.toInteger()
if (id == null) {
renderXml() {
out << StatusResponse() {
out << getFailureXML(ErrorCode.INVALID_PARAMETERS)
}
}
return
}
def app = getApplication(id)
if (!app) {
renderXml() {
out << StatusResponse() {
out << getFailureXML(ErrorCode.OBJECT_NOT_FOUND)
}
}
return
}
try {
appMan.removeApplication(user, id)
} catch (Exception e) {
renderXml() {
log.error("Error removing application", e)
StatusResponse() {
out << getFailureXML(ErrorCode.UNEXPECTED_ERROR)
}
}
return
}
renderXml() {
StatusResponse() {
out << getSuccessXML()
}
}
}
private updateAppServices(app, svcList) {
// Setting the application services does not remove any app services
// that may have been removed from the application. It will also add
// duplicates, so we need to iterate the list, first removing services
// not present in the list, then adding the new entries.
def sessionId = SessionManager.instance.put(user)
def dao = new AppServiceDAO(DAOFactory.getDAOFactory());
def svcListExisting = [] // List of AppdefEntityID's existing in the Application
def svcListToRemove = [] // List of app service id's to remove
for (appService in aBoss.findServiceInventoryByApplication(sessionId, app.id, PageControl.PAGE_ALL)) {
if (appService instanceof ServiceValue) {
def entId = AppdefEntityID.newServiceID(appService.id)
svcListExisting << entId
if (!svcList.contains(entId)) {
def appSvc = dao.findByAppAndService(app.id, appService.id)
svcListToRemove << appSvc.id
}
}
}
def toAdd = []
for (svc in svcList) {
if (!svcListExisting.contains(svc)) {
toAdd << svc
}
}
appMan.setApplicationServices(user, app.id, toAdd)
// Remove all deleted services
for (appSvcId in svcListToRemove) {
appMan.removeAppService(user, app.id, appSvcId)
}
}
private getApplication(id) {
try {
return appMan.findApplicationById(user, id)
} catch (Exception e) {
return null
}
}
}