|
| 1 | +import org.hyperic.hq.hqapi1.ErrorCode |
| 2 | +import org.hyperic.hq.appdef.server.session.ApplicationManagerEJBImpl as AppMan |
| 3 | +import org.hyperic.hq.bizapp.server.session.AppdefBossEJBImpl as ABoss |
| 4 | +import org.hyperic.hq.authz.server.session.ResourceManagerEJBImpl as ResMan |
| 5 | +import org.hyperic.util.pager.PageControl |
| 6 | +import org.hyperic.hq.auth.shared.SessionManager |
| 7 | +import org.hyperic.hq.appdef.shared.AppdefEntityID |
| 8 | +import org.hyperic.hq.appdef.shared.ApplicationValue |
| 9 | +import org.hyperic.hq.appdef.shared.ServiceValue |
| 10 | +import org.hyperic.dao.DAOFactory |
| 11 | +import org.hyperic.hq.appdef.server.session.AppServiceDAO |
| 12 | + |
| 13 | +class ApplicationController extends ApiController { |
| 14 | + |
| 15 | + def appMan = AppMan.one |
| 16 | + def aBoss = ABoss.one |
| 17 | + def resMan = ResMan.one |
| 18 | + |
| 19 | + private Closure getApplicationXML(a) { |
| 20 | + { doc -> |
| 21 | + Application(id : a.id, |
| 22 | + name : a.name, |
| 23 | + location : a.location, |
| 24 | + description : a.description, |
| 25 | + engContact : a.engContact, |
| 26 | + opsContact : a.opsContact, |
| 27 | + bizContact : a.businessContact) { |
| 28 | + def sessionId = SessionManager.instance.put(user) |
| 29 | + for (appService in aBoss.findServiceInventoryByApplication(sessionId, a.id, PageControl.PAGE_ALL)) { |
| 30 | + if (appService instanceof ServiceValue) { |
| 31 | + def resource = resourceHelper.find('service':appService.id) |
| 32 | + Resource(id : resource.id, |
| 33 | + name : resource.name, |
| 34 | + description : resource.description) |
| 35 | + } |
| 36 | + } |
| 37 | + } |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + def list(params) { |
| 42 | + def failureXml = null |
| 43 | + |
| 44 | + renderXml() { |
| 45 | + out << ApplicationsResponse() { |
| 46 | + if (failureXml) { |
| 47 | + out << failureXml |
| 48 | + } else { |
| 49 | + out << getSuccessXML() |
| 50 | + for (app in appMan.getAllApplications(user, PageControl.PAGE_ALL)) { |
| 51 | + out << getApplicationXML(app) |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + def create(params) { |
| 59 | + def createRequest = new XmlParser().parseText(getUpload('postdata')) |
| 60 | + def xmlApplication = createRequest['Application'] |
| 61 | + |
| 62 | + if (!xmlApplication || xmlApplication.size() != 1) { |
| 63 | + renderXml() { |
| 64 | + ApplicationResponse() { |
| 65 | + out << getFailureXML(ErrorCode.INVALID_PARAMETERS) |
| 66 | + } |
| 67 | + } |
| 68 | + return |
| 69 | + } |
| 70 | + |
| 71 | + // Validate Resources |
| 72 | + for (xmlResource in xmlApplication['Resource']) { |
| 73 | + def rid = xmlResource.'@id'?.toInteger() |
| 74 | + def resource = resourceHelper.findById(rid) |
| 75 | + if (!resource.isService()) { |
| 76 | + renderXml() { |
| 77 | + ApplicationResponse() { |
| 78 | + out << getFailureXML(ErrorCode.INVALID_PARAMETERS, |
| 79 | + "Invalid resource passed to create, " + |
| 80 | + r.name + " is not a service") |
| 81 | + } |
| 82 | + } |
| 83 | + return |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + def appName = xmlApplication[0].'@name' |
| 88 | + def appLoc = xmlApplication[0].'@location' |
| 89 | + def appDesc = xmlApplication[0].'@description' |
| 90 | + def appEng = xmlApplication[0].'@engContact' |
| 91 | + def appOps = xmlApplication[0].'@opsContact' |
| 92 | + def appBiz = xmlApplication[0].'@bizContact' |
| 93 | + |
| 94 | + def applicationValue = new ApplicationValue() |
| 95 | + applicationValue.name = appName |
| 96 | + applicationValue.location = appLoc |
| 97 | + applicationValue.description = appDesc |
| 98 | + applicationValue.engContact = appEng |
| 99 | + applicationValue.opsContact = appOps |
| 100 | + applicationValue.businessContact = appBiz |
| 101 | + |
| 102 | + def newApp; |
| 103 | + |
| 104 | + try { |
| 105 | + applicationValue.applicationType = appMan.findApplicationType(1) |
| 106 | + newApp = appMan.createApplication( user, applicationValue, new ArrayList()) |
| 107 | + // Initialize appServices to avoid NPE |
| 108 | + newApp.appServices = new ArrayList() |
| 109 | + } catch (Exception e) { |
| 110 | + renderXml() { |
| 111 | + log.error("Error creating application", e) |
| 112 | + ApplicationResponse() { |
| 113 | + out << getFailureXML(ErrorCode.UNEXPECTED_ERROR) |
| 114 | + } |
| 115 | + } |
| 116 | + return |
| 117 | + } |
| 118 | + |
| 119 | + def resources = xmlApplication['Resource'] |
| 120 | + updateAppServices(newApp, resources) |
| 121 | + |
| 122 | + renderXml() { |
| 123 | + ApplicationResponse() { |
| 124 | + out << getSuccessXML() |
| 125 | + out << getApplicationXML(newApp.applicationValue) |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + def update(params) { |
| 131 | + def updateRequest = new XmlParser().parseText(getUpload('postdata')) |
| 132 | + def xmlApplication = updateRequest['Application'] |
| 133 | + |
| 134 | + if (!xmlApplication || xmlApplication.size() != 1) { |
| 135 | + renderXml() { |
| 136 | + ApplicationResponse() { |
| 137 | + out << getFailureXML(ErrorCode.INVALID_PARAMETERS) |
| 138 | + } |
| 139 | + } |
| 140 | + return |
| 141 | + } |
| 142 | + |
| 143 | + def appId = xmlApplication[0].'@id'?.toInteger() |
| 144 | + if (!appId) { |
| 145 | + renderXml() { |
| 146 | + ApplicationResponse() { |
| 147 | + out << getFailureXML(ErrorCode.INVALID_PARAMETERS, |
| 148 | + "No application id found") |
| 149 | + } |
| 150 | + } |
| 151 | + return |
| 152 | + } |
| 153 | + |
| 154 | + // Validate Resources |
| 155 | + for (xmlResource in xmlApplication['Resource']) { |
| 156 | + def rid = xmlResource.'@id'?.toInteger() |
| 157 | + def resource = resourceHelper.findById(rid) |
| 158 | + if (!resource.isService()) { |
| 159 | + renderXml() { |
| 160 | + ApplicationResponse() { |
| 161 | + out << getFailureXML(ErrorCode.INVALID_PARAMETERS, |
| 162 | + "Invalid resource passed to create, " + |
| 163 | + r.name + " is not a service") |
| 164 | + } |
| 165 | + } |
| 166 | + return |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + def appName = xmlApplication[0].'@name' |
| 171 | + def appLoc = xmlApplication[0].'@location' |
| 172 | + def appDesc = xmlApplication[0].'@description' |
| 173 | + def appEng = xmlApplication[0].'@engContact' |
| 174 | + def appOps = xmlApplication[0].'@opsContact' |
| 175 | + def appBiz = xmlApplication[0].'@bizContact' |
| 176 | + |
| 177 | + def updateApp |
| 178 | + try { |
| 179 | + updateApp = appMan.findApplicationById(user, appId) |
| 180 | + } catch (Exception e) { |
| 181 | + log.error("Error finding application" + e) |
| 182 | + renderXml() { |
| 183 | + ApplicationResponse() { |
| 184 | + out << getFailureXML(ErrorCode.OBJECT_NOT_FOUND) |
| 185 | + } |
| 186 | + } |
| 187 | + return |
| 188 | + } |
| 189 | + |
| 190 | + def applicationValue = updateApp.getApplicationValue() |
| 191 | + applicationValue.name = appName |
| 192 | + applicationValue.location = appLoc |
| 193 | + applicationValue.description = appDesc |
| 194 | + applicationValue.engContact = appEng |
| 195 | + applicationValue.opsContact = appOps |
| 196 | + applicationValue.businessContact = appBiz |
| 197 | + |
| 198 | + try { |
| 199 | + appMan.updateApplication(user, applicationValue) |
| 200 | + } catch (Exception e) { |
| 201 | + renderXml() { |
| 202 | + log.error("Error updating application", e) |
| 203 | + ApplicationResponse() { |
| 204 | + out << getFailureXML(ErrorCode.UNEXPECTED_ERROR) |
| 205 | + } |
| 206 | + } |
| 207 | + return |
| 208 | + } |
| 209 | + |
| 210 | + def resources = xmlApplication['Resource'] |
| 211 | + updateAppServices(updateApp, resources) |
| 212 | + |
| 213 | + renderXml() { |
| 214 | + ApplicationResponse() { |
| 215 | + out << getSuccessXML() |
| 216 | + // Must relookup the app to get updated services |
| 217 | + out << getApplicationXML(applicationValue) |
| 218 | + } |
| 219 | + } |
| 220 | + } |
| 221 | + |
| 222 | + def delete(params) { |
| 223 | + def id = params.getOne('id')?.toInteger() |
| 224 | + |
| 225 | + if (id == null) { |
| 226 | + renderXml() { |
| 227 | + out << StatusResponse() { |
| 228 | + out << getFailureXML(ErrorCode.INVALID_PARAMETERS) |
| 229 | + } |
| 230 | + } |
| 231 | + return |
| 232 | + } |
| 233 | + |
| 234 | + def app = getApplication(id) |
| 235 | + def failureXml = null |
| 236 | + |
| 237 | + if (!app) { |
| 238 | + renderXml() { |
| 239 | + out << StatusResponse() { |
| 240 | + out << getFailureXML(ErrorCode.OBJECT_NOT_FOUND) |
| 241 | + } |
| 242 | + } |
| 243 | + return |
| 244 | + } |
| 245 | + |
| 246 | + try { |
| 247 | + appMan.removeApplication(user, id) |
| 248 | + } catch (Exception e) { |
| 249 | + renderXml() { |
| 250 | + log.error("Error removing application", e) |
| 251 | + StatusResponse() { |
| 252 | + out << getFailureXML(ErrorCode.UNEXPECTED_ERROR) |
| 253 | + } |
| 254 | + } |
| 255 | + return |
| 256 | + } |
| 257 | + |
| 258 | + renderXml() { |
| 259 | + StatusResponse() { |
| 260 | + out << getSuccessXML() |
| 261 | + } |
| 262 | + } |
| 263 | + } |
| 264 | + |
| 265 | + private updateAppServices(app, resources) { |
| 266 | + def svcList = [] |
| 267 | + |
| 268 | + if (resources) { |
| 269 | + resources.each { res -> |
| 270 | + def rid = res.'@id'?.toInteger() |
| 271 | + def sid = resMan.findResourceById(rid)?.instanceId |
| 272 | + def svcAeid = AppdefEntityID.newServiceID(sid) |
| 273 | + svcList.add(svcAeid) |
| 274 | + } |
| 275 | + } |
| 276 | + |
| 277 | + appMan.setApplicationServices(user, app.id, svcList) |
| 278 | + |
| 279 | + // Setting the application services does not remove any app services |
| 280 | + // that may have been removed from the application. We must look up |
| 281 | + // the tree and remove one-by-one. |
| 282 | + // TODO: Fix me - Need manager APIs for Service -> AppService mappings |
| 283 | + def sessionId = SessionManager.instance.put(user) |
| 284 | + def dao = new AppServiceDAO(DAOFactory.getDAOFactory()); |
| 285 | + for (appService in aBoss.findServiceInventoryByApplication(sessionId, app.id, PageControl.PAGE_ALL)) { |
| 286 | + if (appService instanceof ServiceValue) { |
| 287 | + def entId = AppdefEntityID.newServiceID(appService.id) |
| 288 | + if (!svcList.contains(entId)) { |
| 289 | + def appSvc = dao.findByAppAndService(app.id, appService.id) |
| 290 | + appMan.removeAppService(user, app.id, appSvc.id) |
| 291 | + } |
| 292 | + } |
| 293 | + } |
| 294 | + } |
| 295 | + |
| 296 | + private getApplication(id) { |
| 297 | + try { |
| 298 | + return appMan.findApplicationById(user, id) |
| 299 | + } catch (Exception e) { |
| 300 | + return null |
| 301 | + } |
| 302 | + } |
| 303 | +} |
0 commit comments