Skip to content

Commit

Permalink
Merge branch 'HHQ-3076' into hqapi-1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan Morgan committed Sep 28, 2009
2 parents 600e3d7 + 6e315bf commit bd6a008
Show file tree
Hide file tree
Showing 7 changed files with 433 additions and 164 deletions.
292 changes: 159 additions & 133 deletions hqu/hqapi1/app/ApplicationController.groovy
Expand Up @@ -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,
Expand All @@ -40,198 +42,224 @@ 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 appDesc = xmlApplication[0].'@description'
def appEng = xmlApplication[0].'@engContact'
def appOps = xmlApplication[0].'@opsContact'
def appBiz = xmlApplication[0].'@bizContact'
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.name = appName
applicationValue.location = appLoc
applicationValue.description = appDesc
applicationValue.engContact = appEng
applicationValue.opsContact = appOps
applicationValue.businessContact = appBiz

def newApp;

def newApp
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) {
renderXml() {
log.error("Error creating application", e)
ApplicationResponse() {
out << getFailureXML(ErrorCode.UNEXPECTED_ERROR)
}
}
return
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)

renderXml() {
ApplicationResponse() {
out << getSuccessXML()
out << getApplicationXML(newApp.applicationValue)
}
}
return newApp
}

def update(params) {
def updateRequest = new XmlParser().parseText(getUpload('postdata'))
def xmlApplication = updateRequest['Application']
def create(params) {
def createRequest = new XmlParser().parseText(getUpload('postdata'))
def xmlApplication = createRequest['Application']

def newApp
if (!xmlApplication || xmlApplication.size() != 1) {
renderXml() {
ApplicationResponse() {
out << getFailureXML(ErrorCode.INVALID_PARAMETERS)
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)
}
}
return
}
}

def appId = xmlApplication[0].'@id'?.toInteger()
/**
* 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) {
renderXml() {
ApplicationResponse() {
out << getFailureXML(ErrorCode.INVALID_PARAMETERS,
"No application id found")
}
}
return
failureXml = getFailureXML(ErrorCode.INVALID_PARAMETERS,
"No application id found")
return null
}

// Validate Resources
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
}
if (!validateApplicationServices(xmlApplication)) {
return null
}

def appName = xmlApplication[0].'@name'
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 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)
renderXml() {
ApplicationResponse() {
out << getFailureXML(ErrorCode.OBJECT_NOT_FOUND)
}
}
return
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.name = appName
applicationValue.location = appLoc
applicationValue.description = appDesc
applicationValue.engContact = appEng
applicationValue.opsContact = appOps
applicationValue.businessContact = appBiz

try {
appMan.updateApplication(user, applicationValue)
} catch (AppdefDuplicateNameException e) {
renderXml() {
ApplicationResponse() {
out << getFailureXML(ErrorCode.INVALID_PARAMETERS,
"There is already an application named " +
appName)
}
}
return
failureXml = getFailureXML(ErrorCode.INVALID_PARAMETERS,
"There is already an application named " +
appName)
return null
} catch (Exception e) {
renderXml() {
log.error("Error updating application", e)
ApplicationResponse() {
out << getFailureXML(ErrorCode.UNEXPECTED_ERROR)
}
}
return
log.error("Error updating application", e)
failureXml = getFailureXML(ErrorCode.UNEXPECTED_ERROR)
return null
}

def resources = xmlApplication['Resource']
updateAppServices(updateApp, resources)
return getApplication(appId)
}

def update(params) {
def updateRequest = new XmlParser().parseText(getUpload('postdata'))
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() {
out << getSuccessXML()
// Must relookup the app to get updated services
out << getApplicationXML(applicationValue)
if (failureXml) {
out << failureXml
} else {
out << getSuccessXML()
out << getApplicationXML(updatedApp.applicationValue)
}
}
}
}

def sync(params) {
def syncRequest = new XmlParser().parseText(getUpload('postdata'))

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)
}
}
}
}
}
Expand All @@ -249,8 +277,6 @@ class ApplicationController extends ApiController {
}

def app = getApplication(id)
def failureXml = null

if (!app) {
renderXml() {
out << StatusResponse() {
Expand Down

0 comments on commit bd6a008

Please sign in to comment.