Permalink
Browse files

Implement Application sync().

  • Loading branch information...
1 parent 6570b7d commit cb6792a84daafbe8a3e3bc02391a02510188662e Ryan Morgan committed Sep 28, 2009
@@ -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)
+ }
+ }
}
}
}
@@ -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.
*/
@@ -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<Resource> services)
throws Exception
{
ApplicationApi api = getApi().getApplicationApi();
-
- Random r = new Random();
Application a = generateTestApplication();
if (services != null) {
@@ -17,8 +17,6 @@
public class ApplicationUpdate_test extends ApplicationTestBase {
- private static final String UPDATE_PREFIX = "UPDATED-";
-
public ApplicationUpdate_test(String name) {
super(name);
}
@@ -21,13 +21,6 @@
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,50 +63,22 @@ 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<Application> applications = resp.getApplication();
- System.out.println("->" + applications);
ApplicationsResponse syncResponse = applicationApi.syncApplications(applications);
checkSuccess(syncResponse);
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();

0 comments on commit cb6792a

Please sign in to comment.