From cb6792a84daafbe8a3e3bc02391a02510188662e Mon Sep 17 00:00:00 2001 From: Ryan Morgan Date: Mon, 28 Sep 2009 14:57:12 -0700 Subject: [PATCH] Implement Application sync(). --- hqu/hqapi1/app/ApplicationController.groovy | 111 ++++++++---------- src/org/hyperic/hq/hqapi1/ApplicationApi.java | 7 +- .../hq/hqapi1/test/ApplicationTestBase.java | 4 +- .../hqapi1/test/ApplicationUpdate_test.java | 2 - .../hq/hqapi1/tools/ApplicationCommand.java | 39 +----- 5 files changed, 60 insertions(+), 103 deletions(-) diff --git a/hqu/hqapi1/app/ApplicationController.groovy b/hqu/hqapi1/app/ApplicationController.groovy index 05a63043..bc2478ae 100644 --- a/hqu/hqapi1/app/ApplicationController.groovy +++ b/hqu/hqapi1/app/ApplicationController.groovy @@ -79,22 +79,16 @@ class ApplicationController extends ApiController { * the cause. */ private createApplication(xmlApplication) { - - if (!xmlApplication || xmlApplication.size() != 1) { - failureXml = getFailureXML(ErrorCode.INVALID_PARAMETERS) - return null - } - 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 @@ -104,8 +98,7 @@ class ApplicationController extends ApiController { applicationValue.opsContact = appOps applicationValue.businessContact = appBiz - def newApp = null - + def newApp try { applicationValue.applicationType = appMan.findApplicationType(1) newApp = appMan.createApplication(user, applicationValue, new ArrayList()) @@ -132,7 +125,13 @@ class ApplicationController extends ApiController { def createRequest = new XmlParser().parseText(getUpload('postdata')) def xmlApplication = createRequest['Application'] - def newApp = createApplication(xmlApplication) + def newApp + if (!xmlApplication || xmlApplication.size() != 1) { + failureXml = getFailureXML(ErrorCode.INVALID_PARAMETERS, + "Wrong number of Applications") + } else { + newApp = createApplication(xmlApplication[0]) + } renderXml() { ApplicationResponse() { @@ -154,12 +153,7 @@ class ApplicationController extends ApiController { * the cause. */ private updateApplication(xmlApplication) { - if (!xmlApplication || xmlApplication.size() != 1) { - failureXml = getFailureXML(ErrorCode.INVALID_PARAMETERS) - return null - } - - def appId = xmlApplication[0].'@id'?.toInteger() + def appId = xmlApplication.'@id'?.toInteger() if (!appId) { failureXml = getFailureXML(ErrorCode.INVALID_PARAMETERS, "No application id found") @@ -170,14 +164,14 @@ class ApplicationController extends ApiController { 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 = null + def updateApp try { updateApp = appMan.findApplicationById(user, appId) } catch (Exception e) { @@ -189,11 +183,11 @@ class ApplicationController extends ApiController { } 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 { @@ -218,7 +212,13 @@ class ApplicationController extends ApiController { def updateRequest = new XmlParser().parseText(getUpload('postdata')) def xmlApplication = updateRequest['Application'] - def updatedApp = updateApplication(xmlApplication) + def updatedApp + if (!xmlApplication || xmlApplication.size() != 1) { + failureXml = getFailureXML(ErrorCode.INVALID_PARAMETERS, + "Wrong number of Applications") + } else { + updatedApp = updateApplication(xmlApplication[0]) + } renderXml() { ApplicationResponse() { @@ -236,37 +236,30 @@ class ApplicationController extends ApiController { def syncRequest = new XmlParser().parseText(getUpload('postdata')) def applications = [] - - def xmlApplications = syncRequest['Application'] - - if (!xmlApplications || xmlApplications.size() < 1) { - renderXml() { - ApplicationResponse() { - out << getFailureXML(ErrorCode.INVALID_PARAMETERS) - } - } - return - } - - xmlApplications.each { xmlApp -> - // TODO: This needs some work - def appId = xmlApp.'@id'?.toInteger() + for (xmlApplication in syncRequest['Application']) { + def appId = xmlApplication.'@id'?.toInteger() if (!appId) { - print "CREATING: " + xmlApp.'@name' + applications << createApplication(xmlApplication) + } else { + applications << updateApplication(xmlApplication) } - else { - print "UPDATING: " + appId + " " + xmlApp.'@name' + + if (failureXml) { + // Break out early on errors. + break } } - // TODO: This needs some work renderXml() { - ApplicationResponse() { - out << getSuccessXML() - out << Application( - applications.each { app -> - getApplicationXML(app) - }) + ApplicationsResponse() { + if (failureXml) { + out << failureXml + } else { + out << getSuccessXML() + for (app in applications) { + out << getApplicationXML(app) + } + } } } } diff --git a/src/org/hyperic/hq/hqapi1/ApplicationApi.java b/src/org/hyperic/hq/hqapi1/ApplicationApi.java index 931eaf51..acf14bea 100644 --- a/src/org/hyperic/hq/hqapi1/ApplicationApi.java +++ b/src/org/hyperic/hq/hqapi1/ApplicationApi.java @@ -99,10 +99,11 @@ public StatusResponse deleteApplication(int id) /** * Sync a list of {@link org.hyperic.hq.hqapi1.types.Application}s. * - * @param applications The list of applications to sync. + * @param applications The list of Applications to sync. * - * @return {@link org.hyperic.hq.hqapi1.types.ResponseStatus#SUCCESS} if - * all the applications were successfully syced. + * @return On {@link org.hyperic.hq.hqapi1.types.ResponseStatus#SUCCESS}, + * the synced list of Application's are returned via + * {@link org.hyperic.hq.hqapi1.types.ApplicationsResponse#getApplication()}. * * @throws IOException If a network error occurs while making the request. */ diff --git a/src/org/hyperic/hq/hqapi1/test/ApplicationTestBase.java b/src/org/hyperic/hq/hqapi1/test/ApplicationTestBase.java index 0ab510d8..57b391cb 100644 --- a/src/org/hyperic/hq/hqapi1/test/ApplicationTestBase.java +++ b/src/org/hyperic/hq/hqapi1/test/ApplicationTestBase.java @@ -10,6 +10,8 @@ public abstract class ApplicationTestBase extends HQApiTestBase { + static final String UPDATE_PREFIX = "UPDATED-"; + protected static final String APP_NAME = "Test Application"; protected static final String APP_LOCATION = "SFO"; protected static final String APP_DESC = "Test Application Description"; @@ -55,8 +57,6 @@ protected Application createTestApplication(List services) throws Exception { ApplicationApi api = getApi().getApplicationApi(); - - Random r = new Random(); Application a = generateTestApplication(); if (services != null) { diff --git a/src/org/hyperic/hq/hqapi1/test/ApplicationUpdate_test.java b/src/org/hyperic/hq/hqapi1/test/ApplicationUpdate_test.java index 048665bc..a401ecb6 100644 --- a/src/org/hyperic/hq/hqapi1/test/ApplicationUpdate_test.java +++ b/src/org/hyperic/hq/hqapi1/test/ApplicationUpdate_test.java @@ -17,8 +17,6 @@ public class ApplicationUpdate_test extends ApplicationTestBase { - private static final String UPDATE_PREFIX = "UPDATED-"; - public ApplicationUpdate_test(String name) { super(name); } diff --git a/src/org/hyperic/hq/hqapi1/tools/ApplicationCommand.java b/src/org/hyperic/hq/hqapi1/tools/ApplicationCommand.java index 63e8c27e..6cc4e4c9 100644 --- a/src/org/hyperic/hq/hqapi1/tools/ApplicationCommand.java +++ b/src/org/hyperic/hq/hqapi1/tools/ApplicationCommand.java @@ -21,13 +21,6 @@ public class ApplicationCommand extends Command { private static String OPT_ID = "id"; - // Additional sync commands when syncing via command line options. - private static String OPT_NAME = "name"; -// private static String OPT_PROTOTYPE = "prototype"; -// private static String OPT_REGEX = "regex"; -// private static String OPT_DELETEMISSING = "deleteMissing"; -// private static String OPT_DESC = "description"; - private void printUsage() { System.err.println("One of " + Arrays.toString(COMMANDS) + " required"); } @@ -70,38 +63,15 @@ private void sync(String[] args) throws Exception { OptionParser p = getOptionParser(); - p.accepts(OPT_NAME, "The application name to sync"). - withRequiredArg().ofType(String.class); -// p.accepts(OPT_PROTOTYPE, "The resource type to query for group membership"). -// withRequiredArg().ofType(String.class); -// p.accepts(OPT_REGEX, "The regular expression to apply to the " + OPT_PROTOTYPE + -// " flag").withRequiredArg().ofType(String.class); -// p.accepts(OPT_DELETEMISSING, "Remove resources in the group not included in " + -// "the " + OPT_PROTOTYPE + " and " + OPT_REGEX); -// p.accepts(OPT_COMPAT, "If specified, attempt to make the group compatible"); -// p.accepts(OPT_DESC, "If specified, set the description for the group"). -// withRequiredArg().ofType(String.class); - OptionSet options = getOptions(p, args); - if (options.hasArgument(OPT_NAME)) { - syncViaCommandLineArgs(options); - return; - } - - HQApi api = getApi(options); - System.out.println("api: " + api); - - ApplicationApi applicationApi = api.getApplicationApi(); - System.out.println("applicationApi: " + applicationApi); + ApplicationApi applicationApi = getApi(options).getApplicationApi(); InputStream is = getInputStream(options); - System.out.println("is: " + is); ApplicationsResponse resp = XmlUtil.deserialize(ApplicationsResponse.class, is); - System.out.println("resp: " + resp); + List applications = resp.getApplication(); - System.out.println("->" + applications); ApplicationsResponse syncResponse = applicationApi.syncApplications(applications); checkSuccess(syncResponse); @@ -109,11 +79,6 @@ private void sync(String[] args) throws Exception { System.out.println("Successfully synced " + applications.size() + " applications."); } - private void syncViaCommandLineArgs(OptionSet s) throws Exception - { - System.out.println("Feature not implemented."); - } - private void delete(String[] args) throws Exception { OptionParser p = getOptionParser();