Permalink
Browse files

Pull Application creation into a private function for use by sync().

  • Loading branch information...
1 parent 18b7c82 commit 4919808f29293937a3fbf2ce4f28e0175144cf66 Ryan Morgan committed Sep 28, 2009
Showing with 70 additions and 58 deletions.
  1. +70 −58 hqu/hqapi1/app/ApplicationController.groovy
@@ -14,9 +14,11 @@ import org.hyperic.hq.appdef.shared.AppdefDuplicateNameException;
class ApplicationController extends ApiController {
def appMan = AppMan.one
- def aBoss = ABoss.one
+ def aBoss = ABoss.one
def resMan = ResMan.one
+ def failureXml = null
+
private Closure getApplicationXML(a) {
{ doc ->
Application(id : a.id,
@@ -40,97 +42,110 @@ class ApplicationController extends ApiController {
}
def list(params) {
- def failureXml = null
-
renderXml() {
out << ApplicationsResponse() {
- if (failureXml) {
- out << failureXml
- } else {
- out << getSuccessXML()
- for (app in appMan.getAllApplications(user, PageControl.PAGE_ALL)) {
- out << getApplicationXML(app)
- }
+ out << getSuccessXML()
+ for (app in appMan.getAllApplications(user, PageControl.PAGE_ALL)) {
+ out << getApplicationXML(app)
}
}
}
}
- def create(params) {
- def createRequest = new XmlParser().parseText(getUpload('postdata'))
- def xmlApplication = createRequest['Application']
-
- if (!xmlApplication || xmlApplication.size() != 1) {
- renderXml() {
- ApplicationResponse() {
- out << getFailureXML(ErrorCode.INVALID_PARAMETERS)
- }
- }
- return
- }
-
- // Validate Resources
+ /**
+ * 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()) {
- renderXml() {
- ApplicationResponse() {
- out << getFailureXML(ErrorCode.INVALID_PARAMETERS,
- "Invalid resource passed to create, " +
- resource.name + " is not a service")
- }
- }
- return
+ failureXml = getFailureXML(ErrorCode.INVALID_PARAMETERS,
+ "Invalid resource passed to create, " +
+ resource.name + " is not a service")
+ return false
}
}
+ return true
+ }
+
+ /**
+ * 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[0].'@name'
- def appLoc = xmlApplication[0].'@location'
+ def appLoc = xmlApplication[0].'@location'
def appDesc = xmlApplication[0].'@description'
- def appEng = xmlApplication[0].'@engContact'
- def appOps = xmlApplication[0].'@opsContact'
- def appBiz = xmlApplication[0].'@bizContact'
+ def appEng = xmlApplication[0].'@engContact'
+ def appOps = xmlApplication[0].'@opsContact'
+ def appBiz = xmlApplication[0].'@bizContact'
def applicationValue = new ApplicationValue()
- applicationValue.name = appName
- applicationValue.location = appLoc
- applicationValue.description = appDesc
- applicationValue.engContact = appEng
- applicationValue.opsContact = appOps
+ applicationValue.name = appName
+ applicationValue.location = appLoc
+ applicationValue.description = appDesc
+ applicationValue.engContact = appEng
+ applicationValue.opsContact = appOps
applicationValue.businessContact = appBiz
- def newApp;
+ def newApp = null
try {
applicationValue.applicationType = appMan.findApplicationType(1)
- newApp = appMan.createApplication( user, applicationValue, new ArrayList())
+ newApp = appMan.createApplication(user, applicationValue, new ArrayList())
// Initialize appServices to avoid NPE
newApp.appServices = new ArrayList()
} catch (AppdefDuplicateNameException e) {
- renderXml() {
- ApplicationResponse() {
- out << getFailureXML(ErrorCode.OBJECT_EXISTS)
- }
- }
- return
+ 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
+ }
+
+ def resources = xmlApplication['Resource']
+ updateAppServices(newApp, resources)
+ return newApp
+ }
+
+ def create(params) {
+ def createRequest = new XmlParser().parseText(getUpload('postdata'))
+ def xmlApplication = createRequest['Application']
+
+ if (!xmlApplication || xmlApplication.size() != 1) {
renderXml() {
- log.error("Error creating application", e)
ApplicationResponse() {
- out << getFailureXML(ErrorCode.UNEXPECTED_ERROR)
+ out << getFailureXML(ErrorCode.INVALID_PARAMETERS)
}
}
return
}
- def resources = xmlApplication['Resource']
- updateAppServices(newApp, resources)
+ def newApp = createApplication(xmlApplication)
renderXml() {
ApplicationResponse() {
- out << getSuccessXML()
- out << getApplicationXML(newApp.applicationValue)
+ if (failureXml) {
+ out << failureXml
+ } else {
+ out << getSuccessXML()
+ out << getApplicationXML(newApp.applicationValue)
+ }
}
}
}
@@ -230,7 +245,6 @@ class ApplicationController extends ApiController {
renderXml() {
ApplicationResponse() {
out << getSuccessXML()
- // Must relookup the app to get updated services
out << getApplicationXML(applicationValue)
}
}
@@ -288,8 +302,6 @@ class ApplicationController extends ApiController {
}
def app = getApplication(id)
- def failureXml = null
-
if (!app) {
renderXml() {
out << StatusResponse() {

0 comments on commit 4919808

Please sign in to comment.