From 25f16b1ac11cbcc78860d8ef454a6dbf9b12c1e4 Mon Sep 17 00:00:00 2001 From: Ryan Morgan Date: Mon, 14 Sep 2009 15:09:34 -0700 Subject: [PATCH 01/20] Stub out ApplicationApi. --- hqu/hqapi1/app/ApplicationController.groovy | 60 +++++++++++ src/org/hyperic/hq/hqapi1/ApplicationApi.java | 101 ++++++++++++++++++ src/org/hyperic/hq/hqapi1/HQApi.java | 13 ++- .../hqapi1/test/ApplicationCreate_test.java | 23 ++++ .../hqapi1/test/ApplicationDelete_test.java | 19 ++++ .../hq/hqapi1/test/ApplicationList_test.java | 19 ++++ .../hqapi1/test/ApplicationUpdate_test.java | 22 ++++ xsd/HQApi1.xsd | 69 ++++++++++++ 8 files changed, 325 insertions(+), 1 deletion(-) create mode 100644 hqu/hqapi1/app/ApplicationController.groovy create mode 100644 src/org/hyperic/hq/hqapi1/ApplicationApi.java create mode 100644 src/org/hyperic/hq/hqapi1/test/ApplicationCreate_test.java create mode 100644 src/org/hyperic/hq/hqapi1/test/ApplicationDelete_test.java create mode 100644 src/org/hyperic/hq/hqapi1/test/ApplicationList_test.java create mode 100644 src/org/hyperic/hq/hqapi1/test/ApplicationUpdate_test.java diff --git a/hqu/hqapi1/app/ApplicationController.groovy b/hqu/hqapi1/app/ApplicationController.groovy new file mode 100644 index 00000000..2f402afa --- /dev/null +++ b/hqu/hqapi1/app/ApplicationController.groovy @@ -0,0 +1,60 @@ +import org.hyperic.hq.hqapi1.ErrorCode; + +class ApplicationController extends ApiController { + + def list(params) { + def failureXml = null + + renderXml() { + ApplicationsResponse() { + if (failureXml) { + out << failureXml + } else { + out << getSuccessXML() + } + } + } + } + + def create(params) { + def failureXml = null + + renderXml() { + ApplicationResponse() { + if (failureXml) { + out << failureXml + } else { + out << getSuccessXML() + } + } + } + } + + def update(params) { + def failureXml = null + + renderXml() { + ApplicationResponse() { + if (failureXml) { + out << failureXml + } else { + out << getSuccessXML() + } + } + } + } + + def delete(params) { + def failureXml = null + + renderXml() { + StatusResponse() { + if (failureXml) { + out << failureXml + } else { + out << getSuccessXML() + } + } + } + } +} \ No newline at end of file diff --git a/src/org/hyperic/hq/hqapi1/ApplicationApi.java b/src/org/hyperic/hq/hqapi1/ApplicationApi.java new file mode 100644 index 00000000..20120340 --- /dev/null +++ b/src/org/hyperic/hq/hqapi1/ApplicationApi.java @@ -0,0 +1,101 @@ +package org.hyperic.hq.hqapi1; + +import org.hyperic.hq.hqapi1.types.ApplicationsResponse; +import org.hyperic.hq.hqapi1.types.ApplicationResponse; +import org.hyperic.hq.hqapi1.types.ApplicationRequest; +import org.hyperic.hq.hqapi1.types.Application; +import org.hyperic.hq.hqapi1.types.StatusResponse; + +import java.io.IOException; +import java.util.Map; +import java.util.HashMap; + +/** + * The Hyperic HQ Application API. + *

+ * This class provides access to the applications within the HQ system. Each of + * the methods in this class return response objects that wrap the result of the + * method with a {@link org.hyperic.hq.hqapi1.types.ResponseStatus} and a + * {@link org.hyperic.hq.hqapi1.types.ServiceError} that indicates the error + * if the response status is {@link org.hyperic.hq.hqapi1.types.ResponseStatus#FAILURE}. + */ +public class ApplicationApi extends BaseApi { + + ApplicationApi(HQConnection conn) { + super(conn); + } + + /** + * Get all {@link org.hyperic.hq.hqapi1.types.Application}'s. + * + * @return On {@link org.hyperic.hq.hqapi1.types.ResponseStatus#SUCCESS}, + * Applications are returned via + * {@link org.hyperic.hq.hqapi1.types.ApplicationsResponse#getApplication()}. + * + * @throws IOException If a network error occurs while making the request. + */ + public ApplicationsResponse listApplications() + throws IOException + { + Map params = new HashMap(); + return doGet("application/list.hqu", params, ApplicationsResponse.class); + } + + /** + * Create an {@link org.hyperic.hq.hqapi1.types.Application}. + * + * @param app The Application to create. + * + * @return On {@link org.hyperic.hq.hqapi1.types.ResponseStatus#SUCCESS}, + * the created Application is returned via + * {@link org.hyperic.hq.hqapi1.types.ApplicationResponse#getApplication()}. + * + * @throws IOException If a network error occurs while making the request. + */ + public ApplicationResponse createApplication(Application app) + throws IOException + { + ApplicationRequest appRequest = new ApplicationRequest(); + appRequest.setApplication(app); + return doPost("application/create.hqu", appRequest, + ApplicationResponse.class); + } + + /** + * Update an {@link org.hyperic.hq.hqapi1.types.Application}. + * + * @param app The Application to update. + * + * @return On {@link org.hyperic.hq.hqapi1.types.ResponseStatus#SUCCESS}, + * the updated Application is returned via + * {@link org.hyperic.hq.hqapi1.types.ApplicationResponse#getApplication()}. + * + * @throws IOException If a network error occurs while making the request. + */ + public ApplicationResponse updateApplication(Application app) + throws IOException + { + ApplicationRequest appRequest = new ApplicationRequest(); + appRequest.setApplication(app); + return doPost("application/update.hqu", appRequest, + ApplicationResponse.class); + } + + /** + * Delete an {@link org.hyperic.hq.hqapi1.types.Application}. + * + * @param id The id of the Application to delete. + * + * @return {@link org.hyperic.hq.hqapi1.types.ResponseStatus#SUCCESS} if + * the Application was successfully deleted. + * + * @throws IOException If a network error occurs while making the request. + */ + public StatusResponse deleteApplication(int id) + throws IOException + { + Map params = new HashMap(); + params.put("id", new String[] { Integer.toString(id)}); + return doGet("application/delete.hqu", params, StatusResponse.class); + } +} diff --git a/src/org/hyperic/hq/hqapi1/HQApi.java b/src/org/hyperic/hq/hqapi1/HQApi.java index f8dd86db..de352578 100644 --- a/src/org/hyperic/hq/hqapi1/HQApi.java +++ b/src/org/hyperic/hq/hqapi1/HQApi.java @@ -16,7 +16,8 @@ public class HQApi { private final ResourceApi _resourceApi; private final AgentApi _agentApi; private final AlertDefinitionApi _alertDefinitionApi; - + private final ApplicationApi _applApi; + /** * @param host The hostname of the HQ Server to connect to. * @param port The port on the HQ server to connect to. @@ -37,6 +38,7 @@ public HQApi(String host, int port, boolean isSecure, String user, _resourceApi = new ResourceApi(connection); _agentApi = new AgentApi(connection); _alertDefinitionApi = new AlertDefinitionApi(connection); + _applApi = new ApplicationApi(connection); } /** @@ -119,4 +121,13 @@ public AgentApi getAgentApi() { public AlertDefinitionApi getAlertDefinitionApi() { return _alertDefinitionApi; } + + /** + * List, create and update Applications. + * + * @return The API for operating on Applications. + */ + public ApplicationApi getApplicationApi() { + return _applApi; + } } diff --git a/src/org/hyperic/hq/hqapi1/test/ApplicationCreate_test.java b/src/org/hyperic/hq/hqapi1/test/ApplicationCreate_test.java new file mode 100644 index 00000000..594e30f5 --- /dev/null +++ b/src/org/hyperic/hq/hqapi1/test/ApplicationCreate_test.java @@ -0,0 +1,23 @@ +package org.hyperic.hq.hqapi1.test; + +import org.hyperic.hq.hqapi1.ApplicationApi; +import org.hyperic.hq.hqapi1.types.Application; +import org.hyperic.hq.hqapi1.types.ApplicationResponse; + +public class ApplicationCreate_test extends HQApiTestBase { + + public ApplicationCreate_test(String name) { + super(name); + } + + // TODO: Stub + public void testApplicationCreate() throws Exception { + + ApplicationApi api = getApi().getApplicationApi(); + + Application a = new Application(); + + ApplicationResponse response = api.createApplication(a); + hqAssertSuccess(response); + } +} diff --git a/src/org/hyperic/hq/hqapi1/test/ApplicationDelete_test.java b/src/org/hyperic/hq/hqapi1/test/ApplicationDelete_test.java new file mode 100644 index 00000000..6d9571b9 --- /dev/null +++ b/src/org/hyperic/hq/hqapi1/test/ApplicationDelete_test.java @@ -0,0 +1,19 @@ +package org.hyperic.hq.hqapi1.test; + +import org.hyperic.hq.hqapi1.ApplicationApi; +import org.hyperic.hq.hqapi1.types.StatusResponse; + +public class ApplicationDelete_test extends HQApiTestBase { + + public ApplicationDelete_test(String name) { + super(name); + } + + // TODO: Stub + public void testDelete() throws Exception { + ApplicationApi api = getApi().getApplicationApi(); + + StatusResponse response = api.deleteApplication(Integer.MAX_VALUE); + hqAssertSuccess(response); + } +} diff --git a/src/org/hyperic/hq/hqapi1/test/ApplicationList_test.java b/src/org/hyperic/hq/hqapi1/test/ApplicationList_test.java new file mode 100644 index 00000000..2b11cfd0 --- /dev/null +++ b/src/org/hyperic/hq/hqapi1/test/ApplicationList_test.java @@ -0,0 +1,19 @@ +package org.hyperic.hq.hqapi1.test; + +import org.hyperic.hq.hqapi1.ApplicationApi; +import org.hyperic.hq.hqapi1.types.ApplicationsResponse; + +public class ApplicationList_test extends HQApiTestBase { + + public ApplicationList_test(String name) { + super(name); + } + + // TODO: Stub + public void testList() throws Exception { + ApplicationApi api = getApi().getApplicationApi(); + + ApplicationsResponse response = api.listApplications(); + hqAssertSuccess(response); + } +} diff --git a/src/org/hyperic/hq/hqapi1/test/ApplicationUpdate_test.java b/src/org/hyperic/hq/hqapi1/test/ApplicationUpdate_test.java new file mode 100644 index 00000000..890da2e1 --- /dev/null +++ b/src/org/hyperic/hq/hqapi1/test/ApplicationUpdate_test.java @@ -0,0 +1,22 @@ +package org.hyperic.hq.hqapi1.test; + +import org.hyperic.hq.hqapi1.ApplicationApi; +import org.hyperic.hq.hqapi1.types.ApplicationResponse; +import org.hyperic.hq.hqapi1.types.Application; + +public class ApplicationUpdate_test extends HQApiTestBase { + + public ApplicationUpdate_test(String name) { + super(name); + } + + // TODO: Stub + public void testUpdate() throws Exception { + ApplicationApi api = getApi().getApplicationApi(); + + Application a = new Application(); + + ApplicationResponse response = api.updateApplication(a); + hqAssertSuccess(response); + } +} diff --git a/xsd/HQApi1.xsd b/xsd/HQApi1.xsd index fa286e59..74eb07de 100644 --- a/xsd/HQApi1.xsd +++ b/xsd/HQApi1.xsd @@ -11,4 +11,73 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + From 335955ca500a69eee7a07ea5d9c144dad2e0cd37 Mon Sep 17 00:00:00 2001 From: Thomas Risberg Date: Wed, 16 Sep 2009 11:17:31 -0400 Subject: [PATCH 02/20] first cut of listApplications --- hqu/hqapi1/app/ApplicationController.groovy | 39 ++++++++++++++++++- .../hq/hqapi1/test/ApplicationList_test.java | 11 +++++- 2 files changed, 47 insertions(+), 3 deletions(-) diff --git a/hqu/hqapi1/app/ApplicationController.groovy b/hqu/hqapi1/app/ApplicationController.groovy index 2f402afa..92dcc6cd 100644 --- a/hqu/hqapi1/app/ApplicationController.groovy +++ b/hqu/hqapi1/app/ApplicationController.groovy @@ -1,16 +1,51 @@ -import org.hyperic.hq.hqapi1.ErrorCode; +import org.hyperic.hq.hqapi1.ErrorCode +//import org.hyperic.hq.hqapi1.types.Application +import org.hyperic.hq.appdef.server.session.ApplicationManagerEJBImpl +import org.hyperic.hq.appdef.shared.ApplicationManagerLocal class ApplicationController extends ApiController { + ApplicationManagerLocal applicationManager = null; + + private Closure getApplicationXML(a) { + { doc -> + Application(id : a.id, + name : a.name, + location : a.location, + description : a.description, + engContact : a.engContact, + opsContact : a.opsContact, + bizContact : a.businessContact) + // TODO: include ApplicationServices and ApplicationGroups +// a.getAppServiceValues().each { asv -> +// print "Resource: " + asv +// if (!asv.isCluster) { +// Resource(id : asv.service.id, +// name : asv.service.name, +// description : asv.service.description) +// } +// } + } + } + + protected void init() { + applicationManager = ApplicationManagerEJBImpl.getOne() + // TODO: remove next debugging line + print "Number of Applications: " + applicationManager.getApplicationCount() + } + def list(params) { def failureXml = null renderXml() { - ApplicationsResponse() { + out << ApplicationsResponse() { if (failureXml) { out << failureXml } else { out << getSuccessXML() + for (app in applicationManager.getAllApplications(user, null)) { + out << getApplicationXML(app) + } } } } diff --git a/src/org/hyperic/hq/hqapi1/test/ApplicationList_test.java b/src/org/hyperic/hq/hqapi1/test/ApplicationList_test.java index 2b11cfd0..4ba4bf40 100644 --- a/src/org/hyperic/hq/hqapi1/test/ApplicationList_test.java +++ b/src/org/hyperic/hq/hqapi1/test/ApplicationList_test.java @@ -2,6 +2,7 @@ import org.hyperic.hq.hqapi1.ApplicationApi; import org.hyperic.hq.hqapi1.types.ApplicationsResponse; +import org.hyperic.hq.hqapi1.types.Resource; public class ApplicationList_test extends HQApiTestBase { @@ -14,6 +15,14 @@ public void testList() throws Exception { ApplicationApi api = getApi().getApplicationApi(); ApplicationsResponse response = api.listApplications(); - hqAssertSuccess(response); + // TODO: remove debugging lines + System.out.println("TEST " + response); + System.out.println("TEST " + response.getApplication().size()); + if (response.getApplication().size() > 0) { + System.out.println("TEST " + response.getApplication().get(0).getLocation()); + System.out.println("TEST " + response.getApplication().get(0).getDescription()); + System.out.println("Resources: " + response.getApplication().get(0).getApplicationServices()); + } + hqAssertSuccess(response); } } From 3eb8f30b12b1439e9ce23a338699b31165dfc346 Mon Sep 17 00:00:00 2001 From: Thomas Risberg Date: Wed, 16 Sep 2009 15:51:55 -0400 Subject: [PATCH 03/20] started on deleteApplication --- hqu/hqapi1/app/ApplicationController.groovy | 14 ++++++++++++++ .../hq/hqapi1/test/ApplicationList_test.java | 9 +++++---- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/hqu/hqapi1/app/ApplicationController.groovy b/hqu/hqapi1/app/ApplicationController.groovy index 92dcc6cd..622ebc04 100644 --- a/hqu/hqapi1/app/ApplicationController.groovy +++ b/hqu/hqapi1/app/ApplicationController.groovy @@ -80,8 +80,22 @@ class ApplicationController extends ApiController { } def delete(params) { + def id = params.getOne('id')?.toInteger() + + if (id == null) { + renderXml() { + out << StatusResponse() { + out << getFailureXML(ErrorCode.INVALID_PARAMETERS) + } + } + return + } + def failureXml = null + // TODO: remove next debugging line + print "ID: " + id + renderXml() { StatusResponse() { if (failureXml) { diff --git a/src/org/hyperic/hq/hqapi1/test/ApplicationList_test.java b/src/org/hyperic/hq/hqapi1/test/ApplicationList_test.java index 4ba4bf40..95291c8b 100644 --- a/src/org/hyperic/hq/hqapi1/test/ApplicationList_test.java +++ b/src/org/hyperic/hq/hqapi1/test/ApplicationList_test.java @@ -17,11 +17,12 @@ public void testList() throws Exception { ApplicationsResponse response = api.listApplications(); // TODO: remove debugging lines System.out.println("TEST " + response); - System.out.println("TEST " + response.getApplication().size()); + System.out.println("TEST - Apps: " + response.getApplication().size()); if (response.getApplication().size() > 0) { - System.out.println("TEST " + response.getApplication().get(0).getLocation()); - System.out.println("TEST " + response.getApplication().get(0).getDescription()); - System.out.println("Resources: " + response.getApplication().get(0).getApplicationServices()); + System.out.println("APP 1: " + response.getApplication().get(0).getId()); + System.out.println("APP 1: " + response.getApplication().get(0).getLocation()); + System.out.println("APP 1: " + response.getApplication().get(0).getDescription()); + System.out.println("APP 1 Resources: " + response.getApplication().get(0).getApplicationServices()); } hqAssertSuccess(response); } From 4bba8473cae625b2a3cb3bf0fad16d3123e562ec Mon Sep 17 00:00:00 2001 From: Ryan Morgan Date: Wed, 16 Sep 2009 16:35:12 -0700 Subject: [PATCH 04/20] Add Resource and Group elements to Application list. --- hqu/hqapi1/app/ApplicationController.groovy | 58 ++++++++++--------- .../hq/hqapi1/test/ApplicationList_test.java | 3 +- xsd/HQApi1.xsd | 4 +- 3 files changed, 35 insertions(+), 30 deletions(-) diff --git a/hqu/hqapi1/app/ApplicationController.groovy b/hqu/hqapi1/app/ApplicationController.groovy index 92dcc6cd..3d90dae9 100644 --- a/hqu/hqapi1/app/ApplicationController.groovy +++ b/hqu/hqapi1/app/ApplicationController.groovy @@ -1,39 +1,43 @@ import org.hyperic.hq.hqapi1.ErrorCode -//import org.hyperic.hq.hqapi1.types.Application -import org.hyperic.hq.appdef.server.session.ApplicationManagerEJBImpl -import org.hyperic.hq.appdef.shared.ApplicationManagerLocal +import org.hyperic.hq.appdef.server.session.ApplicationManagerEJBImpl as AppMan +import org.hyperic.hq.bizapp.server.session.AppdefBossEJBImpl as ABoss +import org.hyperic.util.pager.PageControl +import org.hyperic.hq.auth.shared.SessionManager +import org.hyperic.hq.appdef.shared.AppdefGroupValue +import org.hyperic.hq.appdef.shared.ServiceValue class ApplicationController extends ApiController { - ApplicationManagerLocal applicationManager = null; + def appMan = AppMan.one + def aBoss = ABoss.one private Closure getApplicationXML(a) { { doc -> - Application(id : a.id, - name : a.name, - location : a.location, - description : a.description, - engContact : a.engContact, - opsContact : a.opsContact, - bizContact : a.businessContact) - // TODO: include ApplicationServices and ApplicationGroups -// a.getAppServiceValues().each { asv -> -// print "Resource: " + asv -// if (!asv.isCluster) { -// Resource(id : asv.service.id, -// name : asv.service.name, -// description : asv.service.description) -// } -// } + Application(id : a.id, + name : a.name, + location : a.location, + description : a.description, + engContact : a.engContact, + opsContact : a.opsContact, + bizContact : a.businessContact) { + def sessionId = SessionManager.instance.put(user) + for (appService in aBoss.findServiceInventoryByApplication(sessionId, a.id, PageControl.PAGE_ALL)) { + if (appService instanceof ServiceValue) { + def resource = resourceHelper.find('service':appService.id) + Resource(id : resource.id, + name : resource.name, + description : resource.description) + } else { + Group(id : appService.id, + name : appService.name, + description : appService.description, + location : appService.location) + } + } + } } } - protected void init() { - applicationManager = ApplicationManagerEJBImpl.getOne() - // TODO: remove next debugging line - print "Number of Applications: " + applicationManager.getApplicationCount() - } - def list(params) { def failureXml = null @@ -43,7 +47,7 @@ class ApplicationController extends ApiController { out << failureXml } else { out << getSuccessXML() - for (app in applicationManager.getAllApplications(user, null)) { + for (app in appMan.getAllApplications(user, PageControl.PAGE_ALL)) { out << getApplicationXML(app) } } diff --git a/src/org/hyperic/hq/hqapi1/test/ApplicationList_test.java b/src/org/hyperic/hq/hqapi1/test/ApplicationList_test.java index 4ba4bf40..8dee9d36 100644 --- a/src/org/hyperic/hq/hqapi1/test/ApplicationList_test.java +++ b/src/org/hyperic/hq/hqapi1/test/ApplicationList_test.java @@ -21,7 +21,8 @@ public void testList() throws Exception { if (response.getApplication().size() > 0) { System.out.println("TEST " + response.getApplication().get(0).getLocation()); System.out.println("TEST " + response.getApplication().get(0).getDescription()); - System.out.println("Resources: " + response.getApplication().get(0).getApplicationServices()); + System.out.println("Resources: " + response.getApplication().get(0).getResource().size()); + System.out.println("Groups: " + response.getApplication().get(0).getGroup().size()); } hqAssertSuccess(response); } diff --git a/xsd/HQApi1.xsd b/xsd/HQApi1.xsd index 74eb07de..46cb0299 100644 --- a/xsd/HQApi1.xsd +++ b/xsd/HQApi1.xsd @@ -16,8 +16,8 @@ - - + + From 8984b17c81e842299ed1db516669f7b395555650 Mon Sep 17 00:00:00 2001 From: Thomas Risberg Date: Wed, 16 Sep 2009 21:30:13 -0400 Subject: [PATCH 05/20] completed deleteApplication --- hqu/hqapi1/app/ApplicationController.groovy | 58 ++++++++++++++------- 1 file changed, 40 insertions(+), 18 deletions(-) diff --git a/hqu/hqapi1/app/ApplicationController.groovy b/hqu/hqapi1/app/ApplicationController.groovy index 622ebc04..e9866c8b 100644 --- a/hqu/hqapi1/app/ApplicationController.groovy +++ b/hqu/hqapi1/app/ApplicationController.groovy @@ -5,7 +5,7 @@ import org.hyperic.hq.appdef.shared.ApplicationManagerLocal class ApplicationController extends ApiController { - ApplicationManagerLocal applicationManager = null; + ApplicationManagerLocal applicationManager = ApplicationManagerEJBImpl.getOne() private Closure getApplicationXML(a) { { doc -> @@ -17,23 +17,17 @@ class ApplicationController extends ApiController { opsContact : a.opsContact, bizContact : a.businessContact) // TODO: include ApplicationServices and ApplicationGroups -// a.getAppServiceValues().each { asv -> -// print "Resource: " + asv + a.getAppServiceValues().each { asv -> + print "Resource: " + asv // if (!asv.isCluster) { // Resource(id : asv.service.id, // name : asv.service.name, // description : asv.service.description) // } -// } + } } } - protected void init() { - applicationManager = ApplicationManagerEJBImpl.getOne() - // TODO: remove next debugging line - print "Number of Applications: " + applicationManager.getApplicationCount() - } - def list(params) { def failureXml = null @@ -91,19 +85,47 @@ class ApplicationController extends ApiController { return } + def app = getApplication(id) def failureXml = null - // TODO: remove next debugging line - print "ID: " + id + if (!app) { + renderXml() { + out << StatusResponse() { + out << getFailureXML(ErrorCode.OBJECT_NOT_FOUND) + } + } + return + } + + try { + removeApplication(id) + } catch (Exception e) { + renderXml() { + log.error("Error removing application", e) + StatusResponse() { + out << getFailureXML(ErrorCode.UNEXPECTED_ERROR) + } + } + return + } renderXml() { StatusResponse() { - if (failureXml) { - out << failureXml - } else { - out << getSuccessXML() - } + out << getSuccessXML() } } } -} \ No newline at end of file + + private getApplication(id) { + try { + return applicationManager.findApplicationById(user, id) + } + catch (Exception e) { + return null + } + } + + private removeApplication(id) { + applicationManager.removeApplication(user, id) + } +} From 25cf0d7244a3031800e2076677c0288668d9c784 Mon Sep 17 00:00:00 2001 From: trisberg Date: Thu, 17 Sep 2009 16:39:19 -0400 Subject: [PATCH 06/20] implemented a basic add without groups or services --- hqu/hqapi1/app/ApplicationController.groovy | 54 ++++++++++++++++--- .../hqapi1/test/ApplicationCreate_test.java | 9 ++++ .../hq/hqapi1/test/ApplicationList_test.java | 14 ++--- 3 files changed, 65 insertions(+), 12 deletions(-) diff --git a/hqu/hqapi1/app/ApplicationController.groovy b/hqu/hqapi1/app/ApplicationController.groovy index 86828e26..e4a35b49 100644 --- a/hqu/hqapi1/app/ApplicationController.groovy +++ b/hqu/hqapi1/app/ApplicationController.groovy @@ -1,10 +1,15 @@ import org.hyperic.hq.hqapi1.ErrorCode +import org.hyperic.hq.appdef.server.session.Application +import org.hyperic.hq.appdef.server.session.ApplicationType import org.hyperic.hq.appdef.server.session.ApplicationManagerEJBImpl as AppMan import org.hyperic.hq.bizapp.server.session.AppdefBossEJBImpl as ABoss import org.hyperic.util.pager.PageControl import org.hyperic.hq.auth.shared.SessionManager import org.hyperic.hq.appdef.shared.AppdefGroupValue +import org.hyperic.hq.appdef.shared.AppdefEntityConstants import org.hyperic.hq.appdef.shared.ServiceValue +import org.hyperic.hq.appdef.shared.ApplicationValue + class ApplicationController extends ApiController { @@ -56,15 +61,52 @@ class ApplicationController extends ApiController { } def create(params) { - def failureXml = null + def createRequest = new XmlParser().parseText(getUpload('postdata')) + def xmlApplication = createRequest['Application'] + + if (!xmlApplication || xmlApplication.size() != 1) { + renderXml() { + ApplicationResponse() { + out << getFailureXML(ErrorCode.INVALID_PARAMETERS) + } + } + return + } + + 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 applicationValue = new ApplicationValue() + applicationValue.name = appName + applicationValue.location = appLoc + applicationValue.description = appDesc + applicationValue.engContact = appEng + applicationValue.opsContact = appOps + applicationValue.businessContact = appBiz + + Application newApp = null; + + try { + applicationValue.applicationType = appMan.findApplicationType(1) + newApp = appMan.createApplication( user, applicationValue, []) + } catch (Exception e) { + renderXml() { + log.error("Error creating application", e) + ApplicationResponse() { + out << getFailureXML(ErrorCode.UNEXPECTED_ERROR) + } + } + return + } renderXml() { ApplicationResponse() { - if (failureXml) { - out << failureXml - } else { - out << getSuccessXML() - } + out << getSuccessXML() + out << getApplicationXML(newApp.applicationValue) } } } diff --git a/src/org/hyperic/hq/hqapi1/test/ApplicationCreate_test.java b/src/org/hyperic/hq/hqapi1/test/ApplicationCreate_test.java index 594e30f5..c113e6e1 100644 --- a/src/org/hyperic/hq/hqapi1/test/ApplicationCreate_test.java +++ b/src/org/hyperic/hq/hqapi1/test/ApplicationCreate_test.java @@ -16,8 +16,17 @@ public void testApplicationCreate() throws Exception { ApplicationApi api = getApi().getApplicationApi(); Application a = new Application(); + a.setName("A new app"); + a.setLocation("Doylestown"); + a.setDescription("A test app created using the API"); + a.setEngContact("the Engineer"); + a.setBizContact("the Businessman"); + a.setOpsContact("the Ops Man"); ApplicationResponse response = api.createApplication(a); + hqAssertSuccess(response); + + System.out.println("NEW APP: " + response.getApplication().getId()); } } diff --git a/src/org/hyperic/hq/hqapi1/test/ApplicationList_test.java b/src/org/hyperic/hq/hqapi1/test/ApplicationList_test.java index ddb7ecf9..35c5421e 100644 --- a/src/org/hyperic/hq/hqapi1/test/ApplicationList_test.java +++ b/src/org/hyperic/hq/hqapi1/test/ApplicationList_test.java @@ -3,6 +3,7 @@ import org.hyperic.hq.hqapi1.ApplicationApi; import org.hyperic.hq.hqapi1.types.ApplicationsResponse; import org.hyperic.hq.hqapi1.types.Resource; +import org.hyperic.hq.hqapi1.types.Application; public class ApplicationList_test extends HQApiTestBase { @@ -18,12 +19,13 @@ public void testList() throws Exception { // TODO: remove debugging lines System.out.println("TEST " + response); System.out.println("TEST - Apps: " + response.getApplication().size()); - if (response.getApplication().size() > 0) { - System.out.println("APP 1: " + response.getApplication().get(0).getId()); - System.out.println("APP 1: " + response.getApplication().get(0).getLocation()); - System.out.println("APP 1: " + response.getApplication().get(0).getDescription()); - System.out.println("APP 1 Resources: " + response.getApplication().get(0).getResource().size()); - System.out.println("APP 1 Groups: " + response.getApplication().get(0).getGroup().size()); + for (Application app : response.getApplication()) { + Integer id = app.getId(); + System.out.println("APP " + id + " name: " + app.getName()); + System.out.println(" location: " + app.getLocation()); + System.out.println(" desription: " + app.getDescription()); + System.out.println(" Resources: " + app.getResource().size()); + System.out.println(" Groups: " + app.getGroup().size()); } hqAssertSuccess(response); } From da1e96cd9a0b026ef73d7ff2b336f7e2f7282966 Mon Sep 17 00:00:00 2001 From: trisberg Date: Thu, 17 Sep 2009 16:57:14 -0400 Subject: [PATCH 07/20] improved create and delete tests --- .../hqapi1/test/ApplicationCreate_test.java | 4 ++-- .../hqapi1/test/ApplicationDelete_test.java | 23 ++++++++++++++++--- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/src/org/hyperic/hq/hqapi1/test/ApplicationCreate_test.java b/src/org/hyperic/hq/hqapi1/test/ApplicationCreate_test.java index c113e6e1..a641322a 100644 --- a/src/org/hyperic/hq/hqapi1/test/ApplicationCreate_test.java +++ b/src/org/hyperic/hq/hqapi1/test/ApplicationCreate_test.java @@ -17,7 +17,7 @@ public void testApplicationCreate() throws Exception { Application a = new Application(); a.setName("A new app"); - a.setLocation("Doylestown"); + a.setLocation("Tahiti"); a.setDescription("A test app created using the API"); a.setEngContact("the Engineer"); a.setBizContact("the Businessman"); @@ -27,6 +27,6 @@ public void testApplicationCreate() throws Exception { hqAssertSuccess(response); - System.out.println("NEW APP: " + response.getApplication().getId()); + api.deleteApplication(response.getApplication().getId()); } } diff --git a/src/org/hyperic/hq/hqapi1/test/ApplicationDelete_test.java b/src/org/hyperic/hq/hqapi1/test/ApplicationDelete_test.java index b3481dd5..650f171c 100644 --- a/src/org/hyperic/hq/hqapi1/test/ApplicationDelete_test.java +++ b/src/org/hyperic/hq/hqapi1/test/ApplicationDelete_test.java @@ -2,6 +2,8 @@ import org.hyperic.hq.hqapi1.ApplicationApi; import org.hyperic.hq.hqapi1.types.StatusResponse; +import org.hyperic.hq.hqapi1.types.Application; +import org.hyperic.hq.hqapi1.types.ApplicationResponse; public class ApplicationDelete_test extends HQApiTestBase { @@ -9,13 +11,28 @@ public ApplicationDelete_test(String name) { super(name); } - // TODO: Stub - public void testDelete() throws Exception { + public void testDeleteExistingApp() throws Exception { + ApplicationApi api = getApi().getApplicationApi(); + + Application a = new Application(); + a.setName("App to delete"); + a.setLocation("Hawaii"); + a.setDescription("A test app created using the API"); + a.setEngContact("the Engineer"); + a.setBizContact("the Businessman"); + a.setOpsContact("the Ops Man"); + ApplicationResponse appResponse = api.createApplication(a); + + StatusResponse response = api.deleteApplication(appResponse.getApplication().getId()); + hqAssertSuccess(response); + + } + + public void testDeleteNonExistingApp() throws Exception { ApplicationApi api = getApi().getApplicationApi(); StatusResponse response = api.deleteApplication(Integer.MAX_VALUE); hqAssertFailureObjectNotFound(response); - // TODO: add application and then delete it } } From 65260eecc2a5311236091b0c2c92d8b92184a192 Mon Sep 17 00:00:00 2001 From: Thomas Risberg Date: Fri, 18 Sep 2009 21:54:34 -0400 Subject: [PATCH 08/20] implemented the update feature and test --- hqu/hqapi1/app/ApplicationController.groovy | 82 ++++++++++++++++--- .../hqapi1/test/ApplicationUpdate_test.java | 20 ++++- 2 files changed, 88 insertions(+), 14 deletions(-) diff --git a/hqu/hqapi1/app/ApplicationController.groovy b/hqu/hqapi1/app/ApplicationController.groovy index e4a35b49..1fd46342 100644 --- a/hqu/hqapi1/app/ApplicationController.groovy +++ b/hqu/hqapi1/app/ApplicationController.groovy @@ -112,17 +112,77 @@ class ApplicationController extends ApiController { } def update(params) { - def failureXml = null - - renderXml() { - ApplicationResponse() { - if (failureXml) { - out << failureXml - } else { - out << getSuccessXML() - } - } - } + def updateRequest = new XmlParser().parseText(getUpload('postdata')) + def xmlApplication = updateRequest['Application'] + + if (!xmlApplication || xmlApplication.size() != 1) { + renderXml() { + ApplicationResponse() { + out << getFailureXML(ErrorCode.INVALID_PARAMETERS) + } + } + return + } + + def appId = xmlApplication[0].'@id'?.toInteger() + if (!appId) { + renderXml() { + ApplicationResponse() { + out << getFailureXML(ErrorCode.INVALID_PARAMETERS) + } + } + return + } + + 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 updateApp = null + try { + updateApp = appMan.findApplicationById(user, appId) + } catch (Exception e) { + log.error("ERROR: " + e) + renderXml() { + ApplicationResponse() { + out << getFailureXML(ErrorCode.OBJECT_NOT_FOUND) + } + } + return + } + + def applicationValue = updateApp.getApplicationValue() + + applicationValue.name = appName + applicationValue.location = appLoc + applicationValue.description = appDesc + applicationValue.engContact = appEng + applicationValue.opsContact = appOps + applicationValue.businessContact = appBiz + + def retAppValue = null; + + try { + retAppValue = appMan.updateApplication(user, applicationValue) + } catch (Exception e) { + renderXml() { + log.error("Error updating application", e) + ApplicationResponse() { + out << getFailureXML(ErrorCode.UNEXPECTED_ERROR) + } + } + return + } + + renderXml() { + ApplicationResponse() { + out << getSuccessXML() + out << getApplicationXML(retAppValue) + } + } } def delete(params) { diff --git a/src/org/hyperic/hq/hqapi1/test/ApplicationUpdate_test.java b/src/org/hyperic/hq/hqapi1/test/ApplicationUpdate_test.java index 890da2e1..99ffd26c 100644 --- a/src/org/hyperic/hq/hqapi1/test/ApplicationUpdate_test.java +++ b/src/org/hyperic/hq/hqapi1/test/ApplicationUpdate_test.java @@ -15,8 +15,22 @@ public void testUpdate() throws Exception { ApplicationApi api = getApi().getApplicationApi(); Application a = new Application(); - - ApplicationResponse response = api.updateApplication(a); - hqAssertSuccess(response); + a.setName("A new app"); + a.setLocation("Tahiti"); + a.setDescription("A test app created using the API"); + a.setEngContact("the Engineer"); + a.setBizContact("the Businessman"); + a.setOpsContact("the Ops Man"); + + ApplicationResponse newResponse = api.createApplication(a); + + Application a2 = newResponse.getApplication(); + a2.setBizContact("new biz contact"); + + ApplicationResponse response = api.updateApplication(a2); + hqAssertSuccess(response); + assertEquals("new biz contact", newResponse.getApplication().getBizContact()); + + api.deleteApplication(newResponse.getApplication().getId()); } } From 4689410243e708e3d0af12c4ba9845a737d62458 Mon Sep 17 00:00:00 2001 From: Thomas Risberg Date: Mon, 21 Sep 2009 13:33:45 -0400 Subject: [PATCH 09/20] implemented list and delete for the shell --- .../hq/hqapi1/tools/ApplicationCommand.java | 88 +++++++++++++++++++ src/org/hyperic/hq/hqapi1/tools/Shell.java | 1 + 2 files changed, 89 insertions(+) create mode 100644 src/org/hyperic/hq/hqapi1/tools/ApplicationCommand.java diff --git a/src/org/hyperic/hq/hqapi1/tools/ApplicationCommand.java b/src/org/hyperic/hq/hqapi1/tools/ApplicationCommand.java new file mode 100644 index 00000000..19fbde05 --- /dev/null +++ b/src/org/hyperic/hq/hqapi1/tools/ApplicationCommand.java @@ -0,0 +1,88 @@ +package org.hyperic.hq.hqapi1.tools; + +import joptsimple.OptionParser; +import joptsimple.OptionSet; +import org.hyperic.hq.hqapi1.ApplicationApi; +import org.hyperic.hq.hqapi1.HQApi; +import org.hyperic.hq.hqapi1.XmlUtil; +import org.hyperic.hq.hqapi1.types.*; + +import java.util.Arrays; + +public class ApplicationCommand extends Command { + + private static String CMD_LIST = "list"; + private static String CMD_DELETE = "delete"; + + private static String[] COMMANDS = { CMD_LIST, CMD_DELETE }; + + 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"); + } + + protected void handleCommand(String[] args) throws Exception { + if (args.length == 0) { + printUsage(); + System.exit(-1); + } + + if (args[0].equals(CMD_LIST)) { + list(trim(args)); + } else if (args[0].equals(CMD_DELETE)) { + delete(trim(args)); + } else { + printUsage(); + System.exit(-1); + } + } + + private void list(String[] args) throws Exception { + + OptionParser p = getOptionParser(); + + OptionSet options = getOptions(p, args); + + HQApi api = getApi(options); + ApplicationApi groupApi = api.getApplicationApi(); + + ApplicationsResponse applications; + + applications = groupApi.listApplications(); + + XmlUtil.serialize(applications, System.out, Boolean.TRUE); + } + + private void delete(String[] args) throws Exception { + + OptionParser p = getOptionParser(); + + p.accepts(OPT_ID, "The resource id to delete"). + withRequiredArg().ofType(Integer.class); + + OptionSet options = getOptions(p, args); + + if (!options.has(OPT_ID)) { + System.err.println("Required argument " + OPT_ID + " not given"); + System.exit(-1); + } + + HQApi api = getApi(options); + ApplicationApi groupApi = api.getApplicationApi(); + + Integer id = (Integer)options.valueOf(OPT_ID); + + StatusResponse response = groupApi.deleteApplication(id); + checkSuccess(response); + + System.out.println("Successfully deleted group id " + id); + } +} \ No newline at end of file diff --git a/src/org/hyperic/hq/hqapi1/tools/Shell.java b/src/org/hyperic/hq/hqapi1/tools/Shell.java index b25113c6..17b00836 100644 --- a/src/org/hyperic/hq/hqapi1/tools/Shell.java +++ b/src/org/hyperic/hq/hqapi1/tools/Shell.java @@ -10,6 +10,7 @@ public class Shell { static { _commands.put("agent", new AgentCommand()); _commands.put("alertdefinition", new AlertDefinitionCommand()); + _commands.put("application", new ApplicationCommand()); _commands.put("autodiscovery", new AutoDiscoveryCommand()); _commands.put("escalation", new EscalationCommand()); _commands.put("group", new GroupCommand()); From a9cbe46d396e7c8b19828d65341134ed868aa587 Mon Sep 17 00:00:00 2001 From: Thomas Risberg Date: Thu, 24 Sep 2009 10:58:14 -0400 Subject: [PATCH 10/20] completed the add/remove services and groups --- hqu/hqapi1/app/ApplicationController.groovy | 234 ++++++++++++------ .../hqapi1/test/ApplicationCreate_test.java | 59 ++++- .../hq/hqapi1/tools/ApplicationCommand.java | 2 +- 3 files changed, 217 insertions(+), 78 deletions(-) diff --git a/hqu/hqapi1/app/ApplicationController.groovy b/hqu/hqapi1/app/ApplicationController.groovy index 1fd46342..dfd18484 100644 --- a/hqu/hqapi1/app/ApplicationController.groovy +++ b/hqu/hqapi1/app/ApplicationController.groovy @@ -3,18 +3,28 @@ import org.hyperic.hq.appdef.server.session.Application import org.hyperic.hq.appdef.server.session.ApplicationType import org.hyperic.hq.appdef.server.session.ApplicationManagerEJBImpl as AppMan import org.hyperic.hq.bizapp.server.session.AppdefBossEJBImpl as ABoss +import org.hyperic.hq.authz.server.session.ResourceManagerEJBImpl as ResMan +import org.hyperic.hq.authz.server.session.ResourceGroupManagerEJBImpl as GroupMan import org.hyperic.util.pager.PageControl +import org.hyperic.util.config.ConfigResponse import org.hyperic.hq.auth.shared.SessionManager import org.hyperic.hq.appdef.shared.AppdefGroupValue import org.hyperic.hq.appdef.shared.AppdefEntityConstants -import org.hyperic.hq.appdef.shared.ServiceValue +import org.hyperic.hq.appdef.shared.AppdefEntityID +import org.hyperic.hq.appdef.shared.AppdefResourceValue import org.hyperic.hq.appdef.shared.ApplicationValue - +import org.hyperic.hq.appdef.shared.AppServiceValue +import org.hyperic.hq.appdef.shared.DependencyTree +import org.hyperic.hq.appdef.shared.ServiceValue +import org.hyperic.hibernate.PageInfo +import org.hyperic.hq.authz.server.session.ResourceGroupSortField class ApplicationController extends ApiController { def appMan = AppMan.one def aBoss = ABoss.one + def resMan = ResMan.one + def groupMan = GroupMan.one private Closure getApplicationXML(a) { { doc -> @@ -91,8 +101,13 @@ class ApplicationController extends ApiController { Application newApp = null; try { - applicationValue.applicationType = appMan.findApplicationType(1) - newApp = appMan.createApplication( user, applicationValue, []) + applicationValue.applicationType = appMan.findApplicationType(1) + newApp = appMan.createApplication( user, applicationValue, new ArrayList()) + //def sessionId = SessionManager.instance.put(user) + //def newAppValue = aBoss.createApplication( sessionId, applicationValue, new ArrayList(), new ConfigResponse()) + //newApp = appMan.findApplicationById(user, newAppValue.id) + // had to initialize appServices to avoid NPE + newApp.appServices = new ArrayList() } catch (Exception e) { renderXml() { log.error("Error creating application", e) @@ -103,6 +118,15 @@ class ApplicationController extends ApiController { return } + def resource = xmlApplication['Resource'] + if (resource) { + updateAppServices(newApp, resource) + } + def group = xmlApplication['Group'] + if (group) { + updateAppGroups(newApp, group) + } + renderXml() { ApplicationResponse() { out << getSuccessXML() @@ -112,77 +136,86 @@ class ApplicationController extends ApiController { } def update(params) { - def updateRequest = new XmlParser().parseText(getUpload('postdata')) - def xmlApplication = updateRequest['Application'] - - if (!xmlApplication || xmlApplication.size() != 1) { - renderXml() { - ApplicationResponse() { - out << getFailureXML(ErrorCode.INVALID_PARAMETERS) - } - } - return - } - - def appId = xmlApplication[0].'@id'?.toInteger() - if (!appId) { - renderXml() { - ApplicationResponse() { - out << getFailureXML(ErrorCode.INVALID_PARAMETERS) - } - } - return - } - - 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 updateApp = null - try { - updateApp = appMan.findApplicationById(user, appId) - } catch (Exception e) { - log.error("ERROR: " + e) - renderXml() { - ApplicationResponse() { - out << getFailureXML(ErrorCode.OBJECT_NOT_FOUND) - } - } - return - } - - def applicationValue = updateApp.getApplicationValue() - - applicationValue.name = appName - applicationValue.location = appLoc - applicationValue.description = appDesc - applicationValue.engContact = appEng - applicationValue.opsContact = appOps - applicationValue.businessContact = appBiz - - def retAppValue = null; - - try { - retAppValue = appMan.updateApplication(user, applicationValue) - } catch (Exception e) { - renderXml() { - log.error("Error updating application", e) - ApplicationResponse() { - out << getFailureXML(ErrorCode.UNEXPECTED_ERROR) - } - } - return - } - - renderXml() { - ApplicationResponse() { - out << getSuccessXML() - out << getApplicationXML(retAppValue) - } - } + def updateRequest = new XmlParser().parseText(getUpload('postdata')) + def xmlApplication = updateRequest['Application'] + + if (!xmlApplication || xmlApplication.size() != 1) { + renderXml() { + ApplicationResponse() { + out << getFailureXML(ErrorCode.INVALID_PARAMETERS) + } + } + return + } + + def appId = xmlApplication[0].'@id'?.toInteger() + if (!appId) { + renderXml() { + ApplicationResponse() { + out << getFailureXML(ErrorCode.INVALID_PARAMETERS) + } + } + return + } + + 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 updateApp = null + try { + updateApp = appMan.findApplicationById(user, appId) + } catch (Exception e) { + log.error("ERROR: " + e) + renderXml() { + ApplicationResponse() { + out << getFailureXML(ErrorCode.OBJECT_NOT_FOUND) + } + } + return + } + + def applicationValue = updateApp.getApplicationValue() + + applicationValue.name = appName + applicationValue.location = appLoc + applicationValue.description = appDesc + applicationValue.engContact = appEng + applicationValue.opsContact = appOps + applicationValue.businessContact = appBiz + + def retAppValue = null; + + try { + retAppValue = appMan.updateApplication(user, applicationValue) + } catch (Exception e) { + renderXml() { + log.error("Error updating application", e) + ApplicationResponse() { + out << getFailureXML(ErrorCode.UNEXPECTED_ERROR) + } + } + return + } + + def resource = xmlApplication['Resource'] + if (resource) { + updateAppServices(updateApp, resource) + } + def group = xmlApplication['Group'] + if (group) { + updateAppGroups(updateApp, group) + } + + renderXml() { + ApplicationResponse() { + out << getSuccessXML() + out << getApplicationXML(retAppValue) + } + } } def delete(params) { @@ -228,6 +261,57 @@ class ApplicationController extends ApiController { } } + private updateAppServices(app, resources) { + def svcList = new ArrayList() + resources.each { res -> + def rid = res.'@id'?.toInteger() + def sid = resMan.findResourceById(rid)?.instanceId + def svcAeid = AppdefEntityID.newServiceID(sid) + svcList.add(svcAeid) + } + + appMan.setApplicationServices(user, app.id, svcList) + + } + + private updateAppGroups(app, groups) { + def groupIds = new ArrayList() + groups.each { grp -> + def gid = grp.'@id'?.toInteger() + groupIds.add(gid) + } + + def appRes = resMan.findResourceById(app.resource.id) + + // find groups containing the application + def allPi = PageInfo.create(PageControl.PAGE_ALL, ResourceGroupSortField.NAME) + def containing = groupMan.findGroupsContaining(user, appRes, [], allPi) + def removeList = new ArrayList() + containing.each { contain -> + if (groupIds.contains(contain.id)) { + // remove from list of ids since it is already part of the group + def ix = groupIds.indexOf(contain.id) + if (ix >=0) { + groupIds.remove(ix) + } + } + else { + removeList.add(contain) + } + } + + // remove the application from old groups + removeList.each { group -> + groupMan.removeResources(user, group, [appRes]) + } + + // add app to new groups that don't already contain the app + groupIds.each { id -> + def group = groupMan.findResourceGroupById(id) + groupMan.addResource(user, group, appRes) + } + } + private getApplication(id) { try { return appMan.findApplicationById(user, id) diff --git a/src/org/hyperic/hq/hqapi1/test/ApplicationCreate_test.java b/src/org/hyperic/hq/hqapi1/test/ApplicationCreate_test.java index a641322a..7193d38b 100644 --- a/src/org/hyperic/hq/hqapi1/test/ApplicationCreate_test.java +++ b/src/org/hyperic/hq/hqapi1/test/ApplicationCreate_test.java @@ -1,8 +1,12 @@ package org.hyperic.hq.hqapi1.test; import org.hyperic.hq.hqapi1.ApplicationApi; -import org.hyperic.hq.hqapi1.types.Application; -import org.hyperic.hq.hqapi1.types.ApplicationResponse; +import org.hyperic.hq.hqapi1.ResourceApi; +import org.hyperic.hq.hqapi1.GroupApi; +import org.hyperic.hq.hqapi1.AgentApi; +import org.hyperic.hq.hqapi1.types.*; + +import java.util.List; public class ApplicationCreate_test extends HQApiTestBase { @@ -23,6 +27,57 @@ public void testApplicationCreate() throws Exception { a.setBizContact("the Businessman"); a.setOpsContact("the Ops Man"); + Agent agent = getRunningAgent(); + + ResourceApi resourceApi = getApi().getResourceApi(); + AgentApi agentApi = getApi().getAgentApi(); + GroupApi groupApi = getApi().getGroupApi(); + + GroupsResponse grpResponse = groupApi.getMixedGroups(); + List groups = a.getGroup(); + for (Group g : grpResponse.getGroup()) { + System.out.println("GROUP: " + g.getDescription() + " :: " + g.getResource()); + // TODO: need a better way of finding App groups - maybe create one? + if (g.getDescription().contains("Application")) { + System.out.println("+> " + g.getId() + " " + g.getName()); + groups.add(g); + } + } + + ResourcePrototypeResponse rp = resourceApi.getResourcePrototype(""); + ResourcesResponse findResponse = resourceApi.getResources(agent, true, true); + hqAssertSuccess(findResponse); + + assertTrue("Found 0 platform resources for agent " + agent.getId(), + findResponse.getResource().size() > 0); + + Resource toAdd1 = null; + Resource toAdd2 = null; + for (Resource r : findResponse.getResource().get(0).getResource()) { + boolean done = false; + System.out.println("RESOURCE: " + r.getDescription() + " :: " + r.getResourcePrototype().getName()); + for (Resource r2 : r.getResource()) { + System.out.println("+> " + r2.getId() + " " + r2.getName()); + if (toAdd1 == null) { + toAdd1 = r2; + } + else { + toAdd2 = r2; + break; + } + } + if (toAdd2 != null) { + break; + } + } + assertNotNull("Found 0 services to add", toAdd1); + + List resources = a.getResource(); + resources.add(toAdd1); + if (toAdd2 != null) { + resources.add(toAdd2); + } + ApplicationResponse response = api.createApplication(a); hqAssertSuccess(response); diff --git a/src/org/hyperic/hq/hqapi1/tools/ApplicationCommand.java b/src/org/hyperic/hq/hqapi1/tools/ApplicationCommand.java index 19fbde05..e305825d 100644 --- a/src/org/hyperic/hq/hqapi1/tools/ApplicationCommand.java +++ b/src/org/hyperic/hq/hqapi1/tools/ApplicationCommand.java @@ -83,6 +83,6 @@ private void delete(String[] args) throws Exception { StatusResponse response = groupApi.deleteApplication(id); checkSuccess(response); - System.out.println("Successfully deleted group id " + id); + System.out.println("Successfully deleted application id " + id); } } \ No newline at end of file From 11adffbd5c010764d5fc44d968706e6dfb27facb Mon Sep 17 00:00:00 2001 From: Ryan Morgan Date: Thu, 24 Sep 2009 11:29:10 -0700 Subject: [PATCH 11/20] Test refactoring, pull common utilities into test base class. --- .../hqapi1/test/ApplicationCreate_test.java | 86 +++--------- .../hqapi1/test/ApplicationDelete_test.java | 17 +-- .../hq/hqapi1/test/ApplicationList_test.java | 42 ++++-- .../hq/hqapi1/test/ApplicationTestBase.java | 131 ++++++++++++++++++ .../hqapi1/test/ApplicationUpdate_test.java | 41 +++--- 5 files changed, 210 insertions(+), 107 deletions(-) create mode 100644 src/org/hyperic/hq/hqapi1/test/ApplicationTestBase.java diff --git a/src/org/hyperic/hq/hqapi1/test/ApplicationCreate_test.java b/src/org/hyperic/hq/hqapi1/test/ApplicationCreate_test.java index 7193d38b..787d17a7 100644 --- a/src/org/hyperic/hq/hqapi1/test/ApplicationCreate_test.java +++ b/src/org/hyperic/hq/hqapi1/test/ApplicationCreate_test.java @@ -1,87 +1,45 @@ package org.hyperic.hq.hqapi1.test; import org.hyperic.hq.hqapi1.ApplicationApi; +import org.hyperic.hq.hqapi1.HQApi; import org.hyperic.hq.hqapi1.ResourceApi; import org.hyperic.hq.hqapi1.GroupApi; -import org.hyperic.hq.hqapi1.AgentApi; import org.hyperic.hq.hqapi1.types.*; import java.util.List; +import java.util.ArrayList; -public class ApplicationCreate_test extends HQApiTestBase { +public class ApplicationCreate_test extends ApplicationTestBase { public ApplicationCreate_test(String name) { super(name); } - // TODO: Stub - public void testApplicationCreate() throws Exception { - + public void testApplicationCreateNoServices() throws Exception { ApplicationApi api = getApi().getApplicationApi(); + Application a = createTestApplication(null, null); - Application a = new Application(); - a.setName("A new app"); - a.setLocation("Tahiti"); - a.setDescription("A test app created using the API"); - a.setEngContact("the Engineer"); - a.setBizContact("the Businessman"); - a.setOpsContact("the Ops Man"); - - Agent agent = getRunningAgent(); - - ResourceApi resourceApi = getApi().getResourceApi(); - AgentApi agentApi = getApi().getAgentApi(); - GroupApi groupApi = getApi().getGroupApi(); - - GroupsResponse grpResponse = groupApi.getMixedGroups(); - List groups = a.getGroup(); - for (Group g : grpResponse.getGroup()) { - System.out.println("GROUP: " + g.getDescription() + " :: " + g.getResource()); - // TODO: need a better way of finding App groups - maybe create one? - if (g.getDescription().contains("Application")) { - System.out.println("+> " + g.getId() + " " + g.getName()); - groups.add(g); - } - } - - ResourcePrototypeResponse rp = resourceApi.getResourcePrototype(""); - ResourcesResponse findResponse = resourceApi.getResources(agent, true, true); - hqAssertSuccess(findResponse); - - assertTrue("Found 0 platform resources for agent " + agent.getId(), - findResponse.getResource().size() > 0); + StatusResponse response = api.deleteApplication(a.getId()); + hqAssertSuccess(response); + } - Resource toAdd1 = null; - Resource toAdd2 = null; - for (Resource r : findResponse.getResource().get(0).getResource()) { - boolean done = false; - System.out.println("RESOURCE: " + r.getDescription() + " :: " + r.getResourcePrototype().getName()); - for (Resource r2 : r.getResource()) { - System.out.println("+> " + r2.getId() + " " + r2.getName()); - if (toAdd1 == null) { - toAdd1 = r2; - } - else { - toAdd2 = r2; - break; - } - } - if (toAdd2 != null) { - break; - } - } - assertNotNull("Found 0 services to add", toAdd1); + public void testApplicationCreateWithServices() throws Exception { + HQApi api = getApi(); + ResourceApi rApi = api.getResourceApi(); + ApplicationApi appApi = api.getApplicationApi(); - List resources = a.getResource(); - resources.add(toAdd1); - if (toAdd2 != null) { - resources.add(toAdd2); - } + ResourcePrototypeResponse protoResponse = + rApi.getResourcePrototype("CPU"); + hqAssertSuccess(protoResponse); - ApplicationResponse response = api.createApplication(a); + ResourcesResponse cpusResponse = + rApi.getResources(protoResponse.getResourcePrototype(), + false, false); + hqAssertSuccess(cpusResponse); - hqAssertSuccess(response); + Application a = createTestApplication(cpusResponse.getResource(), null); - api.deleteApplication(response.getApplication().getId()); + StatusResponse deleteResponse = appApi.deleteApplication(a.getId()); + hqAssertSuccess(deleteResponse); } } diff --git a/src/org/hyperic/hq/hqapi1/test/ApplicationDelete_test.java b/src/org/hyperic/hq/hqapi1/test/ApplicationDelete_test.java index 650f171c..b955fbb8 100644 --- a/src/org/hyperic/hq/hqapi1/test/ApplicationDelete_test.java +++ b/src/org/hyperic/hq/hqapi1/test/ApplicationDelete_test.java @@ -3,9 +3,8 @@ import org.hyperic.hq.hqapi1.ApplicationApi; import org.hyperic.hq.hqapi1.types.StatusResponse; import org.hyperic.hq.hqapi1.types.Application; -import org.hyperic.hq.hqapi1.types.ApplicationResponse; -public class ApplicationDelete_test extends HQApiTestBase { +public class ApplicationDelete_test extends ApplicationTestBase { public ApplicationDelete_test(String name) { super(name); @@ -13,19 +12,10 @@ public ApplicationDelete_test(String name) { public void testDeleteExistingApp() throws Exception { ApplicationApi api = getApi().getApplicationApi(); + Application a = createTestApplication(null, null); - Application a = new Application(); - a.setName("App to delete"); - a.setLocation("Hawaii"); - a.setDescription("A test app created using the API"); - a.setEngContact("the Engineer"); - a.setBizContact("the Businessman"); - a.setOpsContact("the Ops Man"); - ApplicationResponse appResponse = api.createApplication(a); - - StatusResponse response = api.deleteApplication(appResponse.getApplication().getId()); + StatusResponse response = api.deleteApplication(a.getId()); hqAssertSuccess(response); - } public void testDeleteNonExistingApp() throws Exception { @@ -33,6 +23,5 @@ public void testDeleteNonExistingApp() throws Exception { StatusResponse response = api.deleteApplication(Integer.MAX_VALUE); hqAssertFailureObjectNotFound(response); - } } diff --git a/src/org/hyperic/hq/hqapi1/test/ApplicationList_test.java b/src/org/hyperic/hq/hqapi1/test/ApplicationList_test.java index 35c5421e..06f7b04a 100644 --- a/src/org/hyperic/hq/hqapi1/test/ApplicationList_test.java +++ b/src/org/hyperic/hq/hqapi1/test/ApplicationList_test.java @@ -5,28 +5,46 @@ import org.hyperic.hq.hqapi1.types.Resource; import org.hyperic.hq.hqapi1.types.Application; -public class ApplicationList_test extends HQApiTestBase { +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.HashMap; + +public class ApplicationList_test extends ApplicationTestBase { public ApplicationList_test(String name) { super(name); } - // TODO: Stub public void testList() throws Exception { ApplicationApi api = getApi().getApplicationApi(); ApplicationsResponse response = api.listApplications(); - // TODO: remove debugging lines - System.out.println("TEST " + response); - System.out.println("TEST - Apps: " + response.getApplication().size()); - for (Application app : response.getApplication()) { - Integer id = app.getId(); - System.out.println("APP " + id + " name: " + app.getName()); - System.out.println(" location: " + app.getLocation()); - System.out.println(" desription: " + app.getDescription()); - System.out.println(" Resources: " + app.getResource().size()); - System.out.println(" Groups: " + app.getGroup().size()); + hqAssertSuccess(response); + } + + public void testListWithApplications() throws Exception { + ApplicationApi api = getApi().getApplicationApi(); + + int num = 5; + Map apps = new HashMap(); + for (int i = 0; i < num; i++) { + Application a = createTestApplication(null, null); + apps.put(a.getId(), a); } + + ApplicationsResponse response = api.listApplications(); hqAssertSuccess(response); + + for (Application a : response.getApplication()) { + apps.remove(a.getId()); + } + + assertTrue("Not all created applications were found during listing!", + apps.isEmpty()); + + for (Application a : response.getApplication()) { + api.deleteApplication(a.getId()); + } } } diff --git a/src/org/hyperic/hq/hqapi1/test/ApplicationTestBase.java b/src/org/hyperic/hq/hqapi1/test/ApplicationTestBase.java new file mode 100644 index 00000000..cd0e5372 --- /dev/null +++ b/src/org/hyperic/hq/hqapi1/test/ApplicationTestBase.java @@ -0,0 +1,131 @@ +package org.hyperic.hq.hqapi1.test; + +import org.hyperic.hq.hqapi1.types.Application; +import org.hyperic.hq.hqapi1.types.ApplicationResponse; +import org.hyperic.hq.hqapi1.types.Resource; +import org.hyperic.hq.hqapi1.types.Group; +import org.hyperic.hq.hqapi1.types.ResourcePrototypeResponse; +import org.hyperic.hq.hqapi1.types.ResourcesResponse; +import org.hyperic.hq.hqapi1.types.GroupResponse; +import org.hyperic.hq.hqapi1.ApplicationApi; +import org.hyperic.hq.hqapi1.HQApi; +import org.hyperic.hq.hqapi1.GroupApi; +import org.hyperic.hq.hqapi1.ResourceApi; + +import java.util.Random; +import java.util.List; + +public abstract class ApplicationTestBase extends HQApiTestBase { + + protected static final String APP_NAME = "Test Application"; + protected static final String APP_LOCATION = "SFO"; + protected static final String APP_DESC = "Test Application Description"; + protected static final String APP_ENG_CONTACT = "415-555-5555"; + protected static final String APP_BIZ_CONTACT = "212-555-5555"; + protected static final String APP_OPS_CONTACT = "510-555-5555"; + + static final String GROUP_NAME = "API Test Group"; + static final String GROUP_LOCATION = "API Test Group Location"; + static final String GROUP_DESCRIPTION = "API Test Group Description"; + + public ApplicationTestBase(String name) { + super(name); + } + + /** + * Create an Application with no groups or services associated. + * + * @param services A list of Resources to add to the Application or null + * if no Resources should be added. + * @param groups A list of Groups to add to the Application or null if no + * Groups should be added. + * @return The created Application + * @throws Exception If an error occurs. + */ + protected Application createTestApplication(List services, + List groups) throws Exception { + ApplicationApi api = getApi().getApplicationApi(); + + Random r = new Random(); + Application a = new Application(); + + String name = APP_NAME + r.nextInt(); + a.setName(name); + a.setLocation(APP_LOCATION); + a.setDescription(APP_DESC); + a.setEngContact(APP_ENG_CONTACT); + a.setBizContact(APP_BIZ_CONTACT); + a.setOpsContact(APP_OPS_CONTACT); + + if (services != null) { + a.getResource().addAll(services); + } + + if (groups != null) { + a.getGroup().addAll(groups); + } + + ApplicationResponse response = api.createApplication(a); + hqAssertSuccess(response); + + Application createdApp = response.getApplication(); + + assertNotNull("Application id was null!", createdApp.getId()); + assertEquals(createdApp.getName(), name); + assertEquals(createdApp.getLocation(), APP_LOCATION); + assertEquals(createdApp.getDescription(), APP_DESC); + assertEquals(createdApp.getEngContact(), APP_ENG_CONTACT); + assertEquals(createdApp.getBizContact(), APP_BIZ_CONTACT); + assertEquals(createdApp.getOpsContact(), APP_OPS_CONTACT); + + if (services != null) { + assertEquals(createdApp.getResource().size(), services.size()); + } + + if (groups != null) { + assertEquals(createdApp.getGroup().size(), groups.size()); + } + + return response.getApplication(); + } + + /** + * Create a compabile group of the given prototype that includes all + * resources of that type in the inventory. + * + * @param prototype The type of compatible group to create. + * @return The created Group + * @throws Exception If an error occurs creating the group + */ + protected Group createTestCompatibleGroup(String prototype) throws Exception { + HQApi api = getApi(); + GroupApi groupApi = api.getGroupApi(); + ResourceApi rApi = api.getResourceApi(); + + ResourcePrototypeResponse protoResponse = + rApi.getResourcePrototype(prototype); + hqAssertSuccess(protoResponse); + + ResourcesResponse resourcesResponse = + rApi.getResources(protoResponse.getResourcePrototype(), + false, false); + hqAssertSuccess(resourcesResponse); + + assertTrue("No resources of type " + prototype + " found!", + resourcesResponse.getResource().size() > 0); + + Group g = new Group(); + Random r = new Random(); + + g.setName(GROUP_NAME + r.nextInt()); + g.setDescription(GROUP_DESCRIPTION); + g.setLocation(GROUP_LOCATION); + g.setResourcePrototype(protoResponse.getResourcePrototype()); + g.getResource().addAll(resourcesResponse.getResource()); + + GroupResponse groupResponse = groupApi.createGroup(g); + hqAssertSuccess(groupResponse); + + return groupResponse.getGroup(); + } +} diff --git a/src/org/hyperic/hq/hqapi1/test/ApplicationUpdate_test.java b/src/org/hyperic/hq/hqapi1/test/ApplicationUpdate_test.java index 99ffd26c..18592ac4 100644 --- a/src/org/hyperic/hq/hqapi1/test/ApplicationUpdate_test.java +++ b/src/org/hyperic/hq/hqapi1/test/ApplicationUpdate_test.java @@ -3,34 +3,41 @@ import org.hyperic.hq.hqapi1.ApplicationApi; import org.hyperic.hq.hqapi1.types.ApplicationResponse; import org.hyperic.hq.hqapi1.types.Application; +import org.hyperic.hq.hqapi1.types.StatusResponse; -public class ApplicationUpdate_test extends HQApiTestBase { +public class ApplicationUpdate_test extends ApplicationTestBase { + + private static final String UPDATE_PREFIX = "UPDATED-"; public ApplicationUpdate_test(String name) { super(name); } - // TODO: Stub - public void testUpdate() throws Exception { + public void testUpdateNoServices() throws Exception { ApplicationApi api = getApi().getApplicationApi(); - Application a = new Application(); - a.setName("A new app"); - a.setLocation("Tahiti"); - a.setDescription("A test app created using the API"); - a.setEngContact("the Engineer"); - a.setBizContact("the Businessman"); - a.setOpsContact("the Ops Man"); + Application a = createTestApplication(null, null); + + a.setName(UPDATE_PREFIX + a.getName()); + a.setDescription(UPDATE_PREFIX + a.getDescription()); + a.setLocation(UPDATE_PREFIX + a.getLocation()); + a.setOpsContact(UPDATE_PREFIX + a.getOpsContact()); + a.setBizContact(UPDATE_PREFIX + a.getBizContact()); + a.setEngContact(UPDATE_PREFIX + a.getEngContact()); - ApplicationResponse newResponse = api.createApplication(a); + ApplicationResponse updateResponse = api.updateApplication(a); + hqAssertSuccess(updateResponse); - Application a2 = newResponse.getApplication(); - a2.setBizContact("new biz contact"); + Application updatedApp = updateResponse.getApplication(); - ApplicationResponse response = api.updateApplication(a2); - hqAssertSuccess(response); - assertEquals("new biz contact", newResponse.getApplication().getBizContact()); + assertEquals(a.getName(), updatedApp.getName()); + assertEquals(a.getDescription(), updatedApp.getDescription()); + assertEquals(a.getLocation(), updatedApp.getLocation()); + assertEquals(a.getOpsContact(), updatedApp.getOpsContact()); + assertEquals(a.getBizContact(), updatedApp.getBizContact()); + assertEquals(a.getEngContact(), updatedApp.getEngContact()); - api.deleteApplication(newResponse.getApplication().getId()); + StatusResponse deleteResponse = api.deleteApplication(updatedApp.getId()); + hqAssertSuccess(deleteResponse); } } From 28d3f7a29100a871e9451b2cd8af22027c27a6d9 Mon Sep 17 00:00:00 2001 From: Ryan Morgan Date: Thu, 24 Sep 2009 11:40:45 -0700 Subject: [PATCH 12/20] Fix adding groups to applications --- hqu/hqapi1/app/ApplicationController.groovy | 35 +++---------------- .../hqapi1/test/ApplicationCreate_test.java | 23 ++++++++++++ 2 files changed, 27 insertions(+), 31 deletions(-) diff --git a/hqu/hqapi1/app/ApplicationController.groovy b/hqu/hqapi1/app/ApplicationController.groovy index dfd18484..36c0c0b7 100644 --- a/hqu/hqapi1/app/ApplicationController.groovy +++ b/hqu/hqapi1/app/ApplicationController.groovy @@ -275,41 +275,14 @@ class ApplicationController extends ApiController { } private updateAppGroups(app, groups) { - def groupIds = new ArrayList() + def grpList = new ArrayList() groups.each { grp -> def gid = grp.'@id'?.toInteger() - groupIds.add(gid) + def groupAeid = AppdefEntityID.newGroupID(gid) + grpList.add(groupAeid) } - def appRes = resMan.findResourceById(app.resource.id) - - // find groups containing the application - def allPi = PageInfo.create(PageControl.PAGE_ALL, ResourceGroupSortField.NAME) - def containing = groupMan.findGroupsContaining(user, appRes, [], allPi) - def removeList = new ArrayList() - containing.each { contain -> - if (groupIds.contains(contain.id)) { - // remove from list of ids since it is already part of the group - def ix = groupIds.indexOf(contain.id) - if (ix >=0) { - groupIds.remove(ix) - } - } - else { - removeList.add(contain) - } - } - - // remove the application from old groups - removeList.each { group -> - groupMan.removeResources(user, group, [appRes]) - } - - // add app to new groups that don't already contain the app - groupIds.each { id -> - def group = groupMan.findResourceGroupById(id) - groupMan.addResource(user, group, appRes) - } + appMan.setApplicationServices(user, app.id, grpList) } private getApplication(id) { diff --git a/src/org/hyperic/hq/hqapi1/test/ApplicationCreate_test.java b/src/org/hyperic/hq/hqapi1/test/ApplicationCreate_test.java index 787d17a7..6a470b5b 100644 --- a/src/org/hyperic/hq/hqapi1/test/ApplicationCreate_test.java +++ b/src/org/hyperic/hq/hqapi1/test/ApplicationCreate_test.java @@ -42,4 +42,27 @@ public void testApplicationCreateWithServices() throws Exception { StatusResponse deleteResponse = appApi.deleteApplication(a.getId()); hqAssertSuccess(deleteResponse); } + + public void testApplicationCreateWithServiceGroups() throws Exception { + HQApi api = getApi(); + ApplicationApi appApi = api.getApplicationApi(); + GroupApi groupApi = api.getGroupApi(); + + Group compatibleServiceGroup = createTestCompatibleGroup("CPU"); + Group compatibleFileMountGroup = createTestCompatibleGroup("FileServer Mount"); + + List serviceGroups = new ArrayList(); + serviceGroups.add(compatibleServiceGroup); + serviceGroups.add(compatibleFileMountGroup); + + Application a = createTestApplication(null, serviceGroups); + + for (Group g : serviceGroups) { + StatusResponse deleteResponse = groupApi.deleteGroup(g.getId()); + hqAssertSuccess(deleteResponse); + } + + StatusResponse deleteResponse = appApi.deleteApplication(a.getId()); + hqAssertSuccess(deleteResponse); + } } From 77eec6997e51b985f906ee21d831546ab9320d65 Mon Sep 17 00:00:00 2001 From: Thomas Risberg Date: Thu, 24 Sep 2009 17:53:51 -0400 Subject: [PATCH 13/20] improved the create application test --- .../hq/hqapi1/test/ApplicationCreate_test.java | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/org/hyperic/hq/hqapi1/test/ApplicationCreate_test.java b/src/org/hyperic/hq/hqapi1/test/ApplicationCreate_test.java index 7193d38b..bc9a7590 100644 --- a/src/org/hyperic/hq/hqapi1/test/ApplicationCreate_test.java +++ b/src/org/hyperic/hq/hqapi1/test/ApplicationCreate_test.java @@ -14,7 +14,6 @@ public ApplicationCreate_test(String name) { super(name); } - // TODO: Stub public void testApplicationCreate() throws Exception { ApplicationApi api = getApi().getApplicationApi(); @@ -30,21 +29,26 @@ public void testApplicationCreate() throws Exception { Agent agent = getRunningAgent(); ResourceApi resourceApi = getApi().getResourceApi(); - AgentApi agentApi = getApi().getAgentApi(); GroupApi groupApi = getApi().getGroupApi(); + // TODO: This actually creates a mixed group for "Platforms, Servers & Service resource types." + // It still works for adding "Application resource types." using the API but not the GUI + Group group = new Group(); + group.setName("An Application Group"); + group.setDescription("Application Test Group"); + group.setLocation("Anywhere"); + GroupResponse newGroup = groupApi.createGroup(group); + GroupsResponse grpResponse = groupApi.getMixedGroups(); List groups = a.getGroup(); for (Group g : grpResponse.getGroup()) { - System.out.println("GROUP: " + g.getDescription() + " :: " + g.getResource()); - // TODO: need a better way of finding App groups - maybe create one? + System.out.println("GROUP: " + g.getDescription() + " :: " + g.getResourcePrototype()); if (g.getDescription().contains("Application")) { System.out.println("+> " + g.getId() + " " + g.getName()); groups.add(g); } } - ResourcePrototypeResponse rp = resourceApi.getResourcePrototype(""); ResourcesResponse findResponse = resourceApi.getResources(agent, true, true); hqAssertSuccess(findResponse); @@ -54,7 +58,6 @@ public void testApplicationCreate() throws Exception { Resource toAdd1 = null; Resource toAdd2 = null; for (Resource r : findResponse.getResource().get(0).getResource()) { - boolean done = false; System.out.println("RESOURCE: " + r.getDescription() + " :: " + r.getResourcePrototype().getName()); for (Resource r2 : r.getResource()) { System.out.println("+> " + r2.getId() + " " + r2.getName()); @@ -83,5 +86,6 @@ public void testApplicationCreate() throws Exception { hqAssertSuccess(response); api.deleteApplication(response.getApplication().getId()); + groupApi.deleteGroup(newGroup.getGroup().getId().intValue()); } } From 5f642a49a01a006699528e27520bf83132f17deb Mon Sep 17 00:00:00 2001 From: Ryan Morgan Date: Thu, 24 Sep 2009 15:26:58 -0700 Subject: [PATCH 14/20] Add tests for add/remove application services. --- hqu/hqapi1/app/ApplicationController.groovy | 88 ++++++------- .../hqapi1/test/ApplicationUpdate_test.java | 124 ++++++++++++++++++ 2 files changed, 163 insertions(+), 49 deletions(-) diff --git a/hqu/hqapi1/app/ApplicationController.groovy b/hqu/hqapi1/app/ApplicationController.groovy index 36c0c0b7..7cdc5110 100644 --- a/hqu/hqapi1/app/ApplicationController.groovy +++ b/hqu/hqapi1/app/ApplicationController.groovy @@ -98,15 +98,12 @@ class ApplicationController extends ApiController { applicationValue.opsContact = appOps applicationValue.businessContact = appBiz - Application newApp = null; + def newApp; try { applicationValue.applicationType = appMan.findApplicationType(1) - newApp = appMan.createApplication( user, applicationValue, new ArrayList()) - //def sessionId = SessionManager.instance.put(user) - //def newAppValue = aBoss.createApplication( sessionId, applicationValue, new ArrayList(), new ConfigResponse()) - //newApp = appMan.findApplicationById(user, newAppValue.id) - // had to initialize appServices to avoid NPE + newApp = appMan.createApplication( user, applicationValue, new ArrayList()) + // Initialize appServices to avoid NPE newApp.appServices = new ArrayList() } catch (Exception e) { renderXml() { @@ -118,14 +115,9 @@ class ApplicationController extends ApiController { return } - def resource = xmlApplication['Resource'] - if (resource) { - updateAppServices(newApp, resource) - } - def group = xmlApplication['Group'] - if (group) { - updateAppGroups(newApp, group) - } + def resources = xmlApplication['Resource'] + def groups = xmlApplication['Group'] + updateAppServices(newApp, resources, groups) renderXml() { ApplicationResponse() { @@ -165,11 +157,11 @@ class ApplicationController extends ApiController { def appOps = xmlApplication[0].'@opsContact' def appBiz = xmlApplication[0].'@bizContact' - def updateApp = null + def updateApp try { updateApp = appMan.findApplicationById(user, appId) } catch (Exception e) { - log.error("ERROR: " + e) + log.error("Error finding application" + e) renderXml() { ApplicationResponse() { out << getFailureXML(ErrorCode.OBJECT_NOT_FOUND) @@ -179,7 +171,6 @@ class ApplicationController extends ApiController { } def applicationValue = updateApp.getApplicationValue() - applicationValue.name = appName applicationValue.location = appLoc applicationValue.description = appDesc @@ -187,10 +178,8 @@ class ApplicationController extends ApiController { applicationValue.opsContact = appOps applicationValue.businessContact = appBiz - def retAppValue = null; - try { - retAppValue = appMan.updateApplication(user, applicationValue) + appMan.updateApplication(user, applicationValue) } catch (Exception e) { renderXml() { log.error("Error updating application", e) @@ -201,19 +190,15 @@ class ApplicationController extends ApiController { return } - def resource = xmlApplication['Resource'] - if (resource) { - updateAppServices(updateApp, resource) - } - def group = xmlApplication['Group'] - if (group) { - updateAppGroups(updateApp, group) - } + def resources = xmlApplication['Resource'] + def groups = xmlApplication['Group'] + updateAppServices(updateApp, resources, groups) renderXml() { ApplicationResponse() { out << getSuccessXML() - out << getApplicationXML(retAppValue) + // Must relookup the app to get updated services + out << getApplicationXML(applicationValue) } } } @@ -261,35 +246,40 @@ class ApplicationController extends ApiController { } } - private updateAppServices(app, resources) { - def svcList = new ArrayList() - resources.each { res -> - def rid = res.'@id'?.toInteger() - def sid = resMan.findResourceById(rid)?.instanceId - def svcAeid = AppdefEntityID.newServiceID(sid) - svcList.add(svcAeid) + private updateAppServices(app, resources, groups) { + def svcList = [] + + if (resources) { + resources.each { res -> + def rid = res.'@id'?.toInteger() + def sid = resMan.findResourceById(rid)?.instanceId + def svcAeid = AppdefEntityID.newServiceID(sid) + svcList.add(svcAeid) + } } - - appMan.setApplicationServices(user, app.id, svcList) - - } - private updateAppGroups(app, groups) { - def grpList = new ArrayList() - groups.each { grp -> - def gid = grp.'@id'?.toInteger() - def groupAeid = AppdefEntityID.newGroupID(gid) - grpList.add(groupAeid) + if (groups) { + groups.each { grp -> + def gid = grp.'@id'?.toInteger() + def groupAeid = AppdefEntityID.newGroupID(gid) + svcList.add(groupAeid) + } } - appMan.setApplicationServices(user, app.id, grpList) + log.info("Updating " + app.name + " to have " + svcList.size() + " app services!") + + appMan.setApplicationServices(user, app.id, svcList) + + // Setting the application services does not remove any app services + // that may have been removed from the application. We must look up + // the tree and remove one-by-one. + // TODO: Fix me } private getApplication(id) { try { return appMan.findApplicationById(user, id) - } - catch (Exception e) { + } catch (Exception e) { return null } } diff --git a/src/org/hyperic/hq/hqapi1/test/ApplicationUpdate_test.java b/src/org/hyperic/hq/hqapi1/test/ApplicationUpdate_test.java index 18592ac4..3bfca19f 100644 --- a/src/org/hyperic/hq/hqapi1/test/ApplicationUpdate_test.java +++ b/src/org/hyperic/hq/hqapi1/test/ApplicationUpdate_test.java @@ -1,9 +1,18 @@ package org.hyperic.hq.hqapi1.test; import org.hyperic.hq.hqapi1.ApplicationApi; +import org.hyperic.hq.hqapi1.HQApi; +import org.hyperic.hq.hqapi1.ResourceApi; +import org.hyperic.hq.hqapi1.GroupApi; import org.hyperic.hq.hqapi1.types.ApplicationResponse; import org.hyperic.hq.hqapi1.types.Application; import org.hyperic.hq.hqapi1.types.StatusResponse; +import org.hyperic.hq.hqapi1.types.ResourcePrototypeResponse; +import org.hyperic.hq.hqapi1.types.ResourcesResponse; +import org.hyperic.hq.hqapi1.types.Group; + +import java.util.ArrayList; +import java.util.List; public class ApplicationUpdate_test extends ApplicationTestBase { @@ -40,4 +49,119 @@ public void testUpdateNoServices() throws Exception { StatusResponse deleteResponse = api.deleteApplication(updatedApp.getId()); hqAssertSuccess(deleteResponse); } + + public void testUpdateAddServices() throws Exception { + HQApi api = getApi(); + ResourceApi rApi = api.getResourceApi(); + ApplicationApi appApi = api.getApplicationApi(); + + ResourcePrototypeResponse protoResponse = + rApi.getResourcePrototype("CPU"); + hqAssertSuccess(protoResponse); + + ResourcesResponse cpusResponse = + rApi.getResources(protoResponse.getResourcePrototype(), + false, false); + hqAssertSuccess(cpusResponse); + + Application a = createTestApplication(null, null); + + a.getResource().addAll(cpusResponse.getResource()); + + ApplicationResponse updateResponse = appApi.updateApplication(a); + hqAssertSuccess(updateResponse); + + Application updatedApplication = updateResponse.getApplication(); + + assertEquals(cpusResponse.getResource().size(), + updatedApplication.getResource().size()); + + StatusResponse deleteResponse = + appApi.deleteApplication(updatedApplication.getId()); + hqAssertSuccess(deleteResponse); + } + + public void testUpdateRemoveServices() throws Exception { + HQApi api = getApi(); + ResourceApi rApi = api.getResourceApi(); + ApplicationApi appApi = api.getApplicationApi(); + + ResourcePrototypeResponse protoResponse = + rApi.getResourcePrototype("CPU"); + hqAssertSuccess(protoResponse); + + ResourcesResponse cpusResponse = + rApi.getResources(protoResponse.getResourcePrototype(), + false, false); + hqAssertSuccess(cpusResponse); + + Application a = createTestApplication(cpusResponse.getResource(), null); + + a.getResource().clear(); + + ApplicationResponse updateResponse = appApi.updateApplication(a); + hqAssertSuccess(updateResponse); + + Application updatedApplication = updateResponse.getApplication(); + + assertEquals(0, updatedApplication.getResource().size()); + + StatusResponse deleteResponse = + appApi.deleteApplication(updatedApplication.getId()); + hqAssertSuccess(deleteResponse); + } + + public void testUpdateAddGroups() throws Exception { + HQApi api = getApi(); + GroupApi gApi = api.getGroupApi(); + ApplicationApi appApi = api.getApplicationApi(); + + Application a = createTestApplication(null, null); + + Group g = createTestCompatibleGroup("CPU"); + List groups = new ArrayList(); + groups.add(g); + + a.getGroup().addAll(groups); + + ApplicationResponse updateResponse = appApi.updateApplication(a); + hqAssertSuccess(updateResponse); + + Application updatedApplication = updateResponse.getApplication(); + + assertEquals(groups.size(), updatedApplication.getGroup().size()); + + StatusResponse deleteResponse = gApi.deleteGroup(g.getId()); + hqAssertSuccess(deleteResponse); + + deleteResponse = appApi.deleteApplication(updatedApplication.getId()); + hqAssertSuccess(deleteResponse); + } + + public void testUpdateRemoveGroups() throws Exception { + HQApi api = getApi(); + GroupApi gApi = api.getGroupApi(); + ApplicationApi appApi = api.getApplicationApi(); + + Group g = createTestCompatibleGroup("CPU"); + List groups = new ArrayList(); + groups.add(g); + + Application a = createTestApplication(null, groups); + + a.getGroup().clear(); + + ApplicationResponse updateResponse = appApi.updateApplication(a); + hqAssertSuccess(updateResponse); + + Application updatedApplication = updateResponse.getApplication(); + + assertEquals(0, updatedApplication.getGroup().size()); + + StatusResponse deleteResponse = gApi.deleteGroup(g.getId()); + hqAssertSuccess(deleteResponse); + + deleteResponse = appApi.deleteApplication(updatedApplication.getId()); + hqAssertSuccess(deleteResponse); + } } From 0c12fd97ca4fc09b73cffed3ca67834c0c6b9b10 Mon Sep 17 00:00:00 2001 From: Ryan Morgan Date: Thu, 24 Sep 2009 15:43:07 -0700 Subject: [PATCH 15/20] Remove support for Groups in Applications. Compatible groups are not supported in HQ as service clusters. --- hqu/hqapi1/app/ApplicationController.groovy | 23 +------ .../hqapi1/test/ApplicationCreate_test.java | 27 +-------- .../hqapi1/test/ApplicationDelete_test.java | 2 +- .../hq/hqapi1/test/ApplicationList_test.java | 2 +- .../hq/hqapi1/test/ApplicationTestBase.java | 14 +---- .../hqapi1/test/ApplicationUpdate_test.java | 60 +------------------ xsd/HQApi1.xsd | 1 - 7 files changed, 13 insertions(+), 116 deletions(-) diff --git a/hqu/hqapi1/app/ApplicationController.groovy b/hqu/hqapi1/app/ApplicationController.groovy index 7cdc5110..f953c41e 100644 --- a/hqu/hqapi1/app/ApplicationController.groovy +++ b/hqu/hqapi1/app/ApplicationController.groovy @@ -4,7 +4,6 @@ import org.hyperic.hq.appdef.server.session.ApplicationType import org.hyperic.hq.appdef.server.session.ApplicationManagerEJBImpl as AppMan import org.hyperic.hq.bizapp.server.session.AppdefBossEJBImpl as ABoss import org.hyperic.hq.authz.server.session.ResourceManagerEJBImpl as ResMan -import org.hyperic.hq.authz.server.session.ResourceGroupManagerEJBImpl as GroupMan import org.hyperic.util.pager.PageControl import org.hyperic.util.config.ConfigResponse import org.hyperic.hq.auth.shared.SessionManager @@ -24,7 +23,6 @@ class ApplicationController extends ApiController { def appMan = AppMan.one def aBoss = ABoss.one def resMan = ResMan.one - def groupMan = GroupMan.one private Closure getApplicationXML(a) { { doc -> @@ -42,11 +40,6 @@ class ApplicationController extends ApiController { Resource(id : resource.id, name : resource.name, description : resource.description) - } else { - Group(id : appService.id, - name : appService.name, - description : appService.description, - location : appService.location) } } } @@ -116,8 +109,7 @@ class ApplicationController extends ApiController { } def resources = xmlApplication['Resource'] - def groups = xmlApplication['Group'] - updateAppServices(newApp, resources, groups) + updateAppServices(newApp, resources) renderXml() { ApplicationResponse() { @@ -191,8 +183,7 @@ class ApplicationController extends ApiController { } def resources = xmlApplication['Resource'] - def groups = xmlApplication['Group'] - updateAppServices(updateApp, resources, groups) + updateAppServices(updateApp, resources) renderXml() { ApplicationResponse() { @@ -246,7 +237,7 @@ class ApplicationController extends ApiController { } } - private updateAppServices(app, resources, groups) { + private updateAppServices(app, resources) { def svcList = [] if (resources) { @@ -258,14 +249,6 @@ class ApplicationController extends ApiController { } } - if (groups) { - groups.each { grp -> - def gid = grp.'@id'?.toInteger() - def groupAeid = AppdefEntityID.newGroupID(gid) - svcList.add(groupAeid) - } - } - log.info("Updating " + app.name + " to have " + svcList.size() + " app services!") appMan.setApplicationServices(user, app.id, svcList) diff --git a/src/org/hyperic/hq/hqapi1/test/ApplicationCreate_test.java b/src/org/hyperic/hq/hqapi1/test/ApplicationCreate_test.java index 6a470b5b..2da630df 100644 --- a/src/org/hyperic/hq/hqapi1/test/ApplicationCreate_test.java +++ b/src/org/hyperic/hq/hqapi1/test/ApplicationCreate_test.java @@ -17,7 +17,7 @@ public ApplicationCreate_test(String name) { public void testApplicationCreateNoServices() throws Exception { ApplicationApi api = getApi().getApplicationApi(); - Application a = createTestApplication(null, null); + Application a = createTestApplication(null); StatusResponse response = api.deleteApplication(a.getId()); hqAssertSuccess(response); @@ -37,30 +37,7 @@ public void testApplicationCreateWithServices() throws Exception { false, false); hqAssertSuccess(cpusResponse); - Application a = createTestApplication(cpusResponse.getResource(), null); - - StatusResponse deleteResponse = appApi.deleteApplication(a.getId()); - hqAssertSuccess(deleteResponse); - } - - public void testApplicationCreateWithServiceGroups() throws Exception { - HQApi api = getApi(); - ApplicationApi appApi = api.getApplicationApi(); - GroupApi groupApi = api.getGroupApi(); - - Group compatibleServiceGroup = createTestCompatibleGroup("CPU"); - Group compatibleFileMountGroup = createTestCompatibleGroup("FileServer Mount"); - - List serviceGroups = new ArrayList(); - serviceGroups.add(compatibleServiceGroup); - serviceGroups.add(compatibleFileMountGroup); - - Application a = createTestApplication(null, serviceGroups); - - for (Group g : serviceGroups) { - StatusResponse deleteResponse = groupApi.deleteGroup(g.getId()); - hqAssertSuccess(deleteResponse); - } + Application a = createTestApplication(cpusResponse.getResource()); StatusResponse deleteResponse = appApi.deleteApplication(a.getId()); hqAssertSuccess(deleteResponse); diff --git a/src/org/hyperic/hq/hqapi1/test/ApplicationDelete_test.java b/src/org/hyperic/hq/hqapi1/test/ApplicationDelete_test.java index b955fbb8..b24538fb 100644 --- a/src/org/hyperic/hq/hqapi1/test/ApplicationDelete_test.java +++ b/src/org/hyperic/hq/hqapi1/test/ApplicationDelete_test.java @@ -12,7 +12,7 @@ public ApplicationDelete_test(String name) { public void testDeleteExistingApp() throws Exception { ApplicationApi api = getApi().getApplicationApi(); - Application a = createTestApplication(null, null); + Application a = createTestApplication(null); StatusResponse response = api.deleteApplication(a.getId()); hqAssertSuccess(response); diff --git a/src/org/hyperic/hq/hqapi1/test/ApplicationList_test.java b/src/org/hyperic/hq/hqapi1/test/ApplicationList_test.java index 06f7b04a..34529575 100644 --- a/src/org/hyperic/hq/hqapi1/test/ApplicationList_test.java +++ b/src/org/hyperic/hq/hqapi1/test/ApplicationList_test.java @@ -29,7 +29,7 @@ public void testListWithApplications() throws Exception { int num = 5; Map apps = new HashMap(); for (int i = 0; i < num; i++) { - Application a = createTestApplication(null, null); + Application a = createTestApplication(null); apps.put(a.getId(), a); } diff --git a/src/org/hyperic/hq/hqapi1/test/ApplicationTestBase.java b/src/org/hyperic/hq/hqapi1/test/ApplicationTestBase.java index cd0e5372..9aa3693a 100644 --- a/src/org/hyperic/hq/hqapi1/test/ApplicationTestBase.java +++ b/src/org/hyperic/hq/hqapi1/test/ApplicationTestBase.java @@ -37,13 +37,13 @@ public ApplicationTestBase(String name) { * * @param services A list of Resources to add to the Application or null * if no Resources should be added. - * @param groups A list of Groups to add to the Application or null if no * Groups should be added. * @return The created Application * @throws Exception If an error occurs. */ - protected Application createTestApplication(List services, - List groups) throws Exception { + protected Application createTestApplication(List services) + throws Exception + { ApplicationApi api = getApi().getApplicationApi(); Random r = new Random(); @@ -61,10 +61,6 @@ protected Application createTestApplication(List services, a.getResource().addAll(services); } - if (groups != null) { - a.getGroup().addAll(groups); - } - ApplicationResponse response = api.createApplication(a); hqAssertSuccess(response); @@ -82,10 +78,6 @@ protected Application createTestApplication(List services, assertEquals(createdApp.getResource().size(), services.size()); } - if (groups != null) { - assertEquals(createdApp.getGroup().size(), groups.size()); - } - return response.getApplication(); } diff --git a/src/org/hyperic/hq/hqapi1/test/ApplicationUpdate_test.java b/src/org/hyperic/hq/hqapi1/test/ApplicationUpdate_test.java index 3bfca19f..0968a0e6 100644 --- a/src/org/hyperic/hq/hqapi1/test/ApplicationUpdate_test.java +++ b/src/org/hyperic/hq/hqapi1/test/ApplicationUpdate_test.java @@ -25,7 +25,7 @@ public ApplicationUpdate_test(String name) { public void testUpdateNoServices() throws Exception { ApplicationApi api = getApi().getApplicationApi(); - Application a = createTestApplication(null, null); + Application a = createTestApplication(null); a.setName(UPDATE_PREFIX + a.getName()); a.setDescription(UPDATE_PREFIX + a.getDescription()); @@ -64,7 +64,7 @@ public void testUpdateAddServices() throws Exception { false, false); hqAssertSuccess(cpusResponse); - Application a = createTestApplication(null, null); + Application a = createTestApplication(null); a.getResource().addAll(cpusResponse.getResource()); @@ -95,7 +95,7 @@ public void testUpdateRemoveServices() throws Exception { false, false); hqAssertSuccess(cpusResponse); - Application a = createTestApplication(cpusResponse.getResource(), null); + Application a = createTestApplication(cpusResponse.getResource()); a.getResource().clear(); @@ -110,58 +110,4 @@ public void testUpdateRemoveServices() throws Exception { appApi.deleteApplication(updatedApplication.getId()); hqAssertSuccess(deleteResponse); } - - public void testUpdateAddGroups() throws Exception { - HQApi api = getApi(); - GroupApi gApi = api.getGroupApi(); - ApplicationApi appApi = api.getApplicationApi(); - - Application a = createTestApplication(null, null); - - Group g = createTestCompatibleGroup("CPU"); - List groups = new ArrayList(); - groups.add(g); - - a.getGroup().addAll(groups); - - ApplicationResponse updateResponse = appApi.updateApplication(a); - hqAssertSuccess(updateResponse); - - Application updatedApplication = updateResponse.getApplication(); - - assertEquals(groups.size(), updatedApplication.getGroup().size()); - - StatusResponse deleteResponse = gApi.deleteGroup(g.getId()); - hqAssertSuccess(deleteResponse); - - deleteResponse = appApi.deleteApplication(updatedApplication.getId()); - hqAssertSuccess(deleteResponse); - } - - public void testUpdateRemoveGroups() throws Exception { - HQApi api = getApi(); - GroupApi gApi = api.getGroupApi(); - ApplicationApi appApi = api.getApplicationApi(); - - Group g = createTestCompatibleGroup("CPU"); - List groups = new ArrayList(); - groups.add(g); - - Application a = createTestApplication(null, groups); - - a.getGroup().clear(); - - ApplicationResponse updateResponse = appApi.updateApplication(a); - hqAssertSuccess(updateResponse); - - Application updatedApplication = updateResponse.getApplication(); - - assertEquals(0, updatedApplication.getGroup().size()); - - StatusResponse deleteResponse = gApi.deleteGroup(g.getId()); - hqAssertSuccess(deleteResponse); - - deleteResponse = appApi.deleteApplication(updatedApplication.getId()); - hqAssertSuccess(deleteResponse); - } } diff --git a/xsd/HQApi1.xsd b/xsd/HQApi1.xsd index 46cb0299..83f299c4 100644 --- a/xsd/HQApi1.xsd +++ b/xsd/HQApi1.xsd @@ -17,7 +17,6 @@ - From dadf754aa74798084d6d67cc6dd816542890460f Mon Sep 17 00:00:00 2001 From: Ryan Morgan Date: Thu, 24 Sep 2009 15:52:14 -0700 Subject: [PATCH 16/20] Import cleanups. --- src/org/hyperic/hq/hqapi1/test/ApplicationCreate_test.java | 4 ---- src/org/hyperic/hq/hqapi1/test/ApplicationList_test.java | 3 --- 2 files changed, 7 deletions(-) diff --git a/src/org/hyperic/hq/hqapi1/test/ApplicationCreate_test.java b/src/org/hyperic/hq/hqapi1/test/ApplicationCreate_test.java index 2da630df..7ba5f899 100644 --- a/src/org/hyperic/hq/hqapi1/test/ApplicationCreate_test.java +++ b/src/org/hyperic/hq/hqapi1/test/ApplicationCreate_test.java @@ -3,12 +3,8 @@ import org.hyperic.hq.hqapi1.ApplicationApi; import org.hyperic.hq.hqapi1.HQApi; import org.hyperic.hq.hqapi1.ResourceApi; -import org.hyperic.hq.hqapi1.GroupApi; import org.hyperic.hq.hqapi1.types.*; -import java.util.List; -import java.util.ArrayList; - public class ApplicationCreate_test extends ApplicationTestBase { public ApplicationCreate_test(String name) { diff --git a/src/org/hyperic/hq/hqapi1/test/ApplicationList_test.java b/src/org/hyperic/hq/hqapi1/test/ApplicationList_test.java index 34529575..3e9e94de 100644 --- a/src/org/hyperic/hq/hqapi1/test/ApplicationList_test.java +++ b/src/org/hyperic/hq/hqapi1/test/ApplicationList_test.java @@ -2,11 +2,8 @@ import org.hyperic.hq.hqapi1.ApplicationApi; import org.hyperic.hq.hqapi1.types.ApplicationsResponse; -import org.hyperic.hq.hqapi1.types.Resource; import org.hyperic.hq.hqapi1.types.Application; -import java.util.ArrayList; -import java.util.List; import java.util.Map; import java.util.HashMap; From ebae45d852c45951c058289cf7416ac580c99c64 Mon Sep 17 00:00:00 2001 From: Ryan Morgan Date: Thu, 24 Sep 2009 16:22:14 -0700 Subject: [PATCH 17/20] Remove AppService's that have been removed from the Application. --- hqu/hqapi1/app/ApplicationController.groovy | 27 +++++++++++---------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/hqu/hqapi1/app/ApplicationController.groovy b/hqu/hqapi1/app/ApplicationController.groovy index f953c41e..089715ee 100644 --- a/hqu/hqapi1/app/ApplicationController.groovy +++ b/hqu/hqapi1/app/ApplicationController.groovy @@ -1,22 +1,14 @@ import org.hyperic.hq.hqapi1.ErrorCode -import org.hyperic.hq.appdef.server.session.Application -import org.hyperic.hq.appdef.server.session.ApplicationType import org.hyperic.hq.appdef.server.session.ApplicationManagerEJBImpl as AppMan import org.hyperic.hq.bizapp.server.session.AppdefBossEJBImpl as ABoss import org.hyperic.hq.authz.server.session.ResourceManagerEJBImpl as ResMan import org.hyperic.util.pager.PageControl -import org.hyperic.util.config.ConfigResponse import org.hyperic.hq.auth.shared.SessionManager -import org.hyperic.hq.appdef.shared.AppdefGroupValue -import org.hyperic.hq.appdef.shared.AppdefEntityConstants import org.hyperic.hq.appdef.shared.AppdefEntityID -import org.hyperic.hq.appdef.shared.AppdefResourceValue import org.hyperic.hq.appdef.shared.ApplicationValue -import org.hyperic.hq.appdef.shared.AppServiceValue -import org.hyperic.hq.appdef.shared.DependencyTree import org.hyperic.hq.appdef.shared.ServiceValue -import org.hyperic.hibernate.PageInfo -import org.hyperic.hq.authz.server.session.ResourceGroupSortField +import org.hyperic.dao.DAOFactory +import org.hyperic.hq.appdef.server.session.AppServiceDAO class ApplicationController extends ApiController { @@ -249,14 +241,23 @@ class ApplicationController extends ApiController { } } - log.info("Updating " + app.name + " to have " + svcList.size() + " app services!") - appMan.setApplicationServices(user, app.id, svcList) // Setting the application services does not remove any app services // that may have been removed from the application. We must look up // the tree and remove one-by-one. - // TODO: Fix me + // TODO: Fix me - Need manager APIs for Service -> AppService mappings + def sessionId = SessionManager.instance.put(user) + def dao = new AppServiceDAO(DAOFactory.getDAOFactory()); + for (appService in aBoss.findServiceInventoryByApplication(sessionId, app.id, PageControl.PAGE_ALL)) { + if (appService instanceof ServiceValue) { + def entId = AppdefEntityID.newServiceID(appService.id) + if (!svcList.contains(entId)) { + def appSvc = dao.findByAppAndService(app.id, appService.id) + appMan.removeAppService(user, app.id, appSvc.id) + } + } + } } private getApplication(id) { From 63481796730ccdcb471518d6635786f574d44159 Mon Sep 17 00:00:00 2001 From: Ryan Morgan Date: Thu, 24 Sep 2009 16:39:54 -0700 Subject: [PATCH 18/20] Validate that resources added to an Application are Services. --- hqu/hqapi1/app/ApplicationController.groovy | 16 ++++++++ .../hqapi1/test/ApplicationCreate_test.java | 37 +++++++++++++++++++ .../hq/hqapi1/test/ApplicationTestBase.java | 29 ++++++++++----- 3 files changed, 72 insertions(+), 10 deletions(-) diff --git a/hqu/hqapi1/app/ApplicationController.groovy b/hqu/hqapi1/app/ApplicationController.groovy index 089715ee..19303374 100644 --- a/hqu/hqapi1/app/ApplicationController.groovy +++ b/hqu/hqapi1/app/ApplicationController.groovy @@ -68,6 +68,22 @@ class ApplicationController extends ApiController { return } + // 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, " + + r.name + " is not a service") + } + } + return + } + } + def appName = xmlApplication[0].'@name' def appLoc = xmlApplication[0].'@location' def appDesc = xmlApplication[0].'@description' diff --git a/src/org/hyperic/hq/hqapi1/test/ApplicationCreate_test.java b/src/org/hyperic/hq/hqapi1/test/ApplicationCreate_test.java index 7ba5f899..a889d254 100644 --- a/src/org/hyperic/hq/hqapi1/test/ApplicationCreate_test.java +++ b/src/org/hyperic/hq/hqapi1/test/ApplicationCreate_test.java @@ -38,4 +38,41 @@ public void testApplicationCreateWithServices() throws Exception { StatusResponse deleteResponse = appApi.deleteApplication(a.getId()); hqAssertSuccess(deleteResponse); } + + public void testApplicationCreateWithServers() throws Exception { + HQApi api = getApi(); + ResourceApi rApi = api.getResourceApi(); + ApplicationApi appApi = api.getApplicationApi(); + + ResourcePrototypeResponse protoResponse = + rApi.getResourcePrototype("HQ Agent"); + hqAssertSuccess(protoResponse); + + ResourcesResponse agentsResponse = + rApi.getResources(protoResponse.getResourcePrototype(), + false, false); + hqAssertSuccess(agentsResponse); + + assertTrue("No HQ Agent resources found in the inventory!", + agentsResponse.getResource().size() > 0); + + Application a = generateTestApplication(); + a.getResource().addAll(agentsResponse.getResource()); + + ApplicationResponse response = appApi.createApplication(a); + hqAssertFailureInvalidParameters(response); // Invalid - cannot have servers + } + + public void testApplicationCreateWithPlatforms() throws Exception { + HQApi api = getApi(); + ApplicationApi appApi = api.getApplicationApi(); + + Resource platform = getLocalPlatformResource(false, false); + + Application a = generateTestApplication(); + a.getResource().add(platform); + + ApplicationResponse response = appApi.createApplication(a); + hqAssertFailureInvalidParameters(response); // Invalid - cannot have servers + } } diff --git a/src/org/hyperic/hq/hqapi1/test/ApplicationTestBase.java b/src/org/hyperic/hq/hqapi1/test/ApplicationTestBase.java index 9aa3693a..581a6542 100644 --- a/src/org/hyperic/hq/hqapi1/test/ApplicationTestBase.java +++ b/src/org/hyperic/hq/hqapi1/test/ApplicationTestBase.java @@ -32,6 +32,23 @@ public ApplicationTestBase(String name) { super(name); } + protected Application generateTestApplication() + throws Exception { + + Random r = new Random(); + Application a = new Application(); + + String name = APP_NAME + r.nextInt(); + a.setName(name); + a.setLocation(APP_LOCATION); + a.setDescription(APP_DESC); + a.setEngContact(APP_ENG_CONTACT); + a.setBizContact(APP_BIZ_CONTACT); + a.setOpsContact(APP_OPS_CONTACT); + + return a; + } + /** * Create an Application with no groups or services associated. * @@ -47,15 +64,7 @@ protected Application createTestApplication(List services) ApplicationApi api = getApi().getApplicationApi(); Random r = new Random(); - Application a = new Application(); - - String name = APP_NAME + r.nextInt(); - a.setName(name); - a.setLocation(APP_LOCATION); - a.setDescription(APP_DESC); - a.setEngContact(APP_ENG_CONTACT); - a.setBizContact(APP_BIZ_CONTACT); - a.setOpsContact(APP_OPS_CONTACT); + Application a = generateTestApplication(); if (services != null) { a.getResource().addAll(services); @@ -67,7 +76,7 @@ protected Application createTestApplication(List services) Application createdApp = response.getApplication(); assertNotNull("Application id was null!", createdApp.getId()); - assertEquals(createdApp.getName(), name); + assertEquals(createdApp.getName(), a.getName()); assertEquals(createdApp.getLocation(), APP_LOCATION); assertEquals(createdApp.getDescription(), APP_DESC); assertEquals(createdApp.getEngContact(), APP_ENG_CONTACT); From 260e79ffb8c98156d97e70476a08e97f42d365f6 Mon Sep 17 00:00:00 2001 From: Ryan Morgan Date: Thu, 24 Sep 2009 16:47:34 -0700 Subject: [PATCH 19/20] Test updating Applications with invalid Resource types. --- hqu/hqapi1/app/ApplicationController.groovy | 16 +++++++ .../hqapi1/test/ApplicationUpdate_test.java | 47 +++++++++++++++++++ 2 files changed, 63 insertions(+) diff --git a/hqu/hqapi1/app/ApplicationController.groovy b/hqu/hqapi1/app/ApplicationController.groovy index 19303374..af96e9ec 100644 --- a/hqu/hqapi1/app/ApplicationController.groovy +++ b/hqu/hqapi1/app/ApplicationController.groovy @@ -150,6 +150,22 @@ class ApplicationController extends ApiController { return } + // 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, " + + r.name + " is not a service") + } + } + return + } + } + def appName = xmlApplication[0].'@name' def appLoc = xmlApplication[0].'@location' def appDesc = xmlApplication[0].'@description' diff --git a/src/org/hyperic/hq/hqapi1/test/ApplicationUpdate_test.java b/src/org/hyperic/hq/hqapi1/test/ApplicationUpdate_test.java index 0968a0e6..c67e4ece 100644 --- a/src/org/hyperic/hq/hqapi1/test/ApplicationUpdate_test.java +++ b/src/org/hyperic/hq/hqapi1/test/ApplicationUpdate_test.java @@ -10,6 +10,7 @@ import org.hyperic.hq.hqapi1.types.ResourcePrototypeResponse; import org.hyperic.hq.hqapi1.types.ResourcesResponse; import org.hyperic.hq.hqapi1.types.Group; +import org.hyperic.hq.hqapi1.types.Resource; import java.util.ArrayList; import java.util.List; @@ -110,4 +111,50 @@ public void testUpdateRemoveServices() throws Exception { appApi.deleteApplication(updatedApplication.getId()); hqAssertSuccess(deleteResponse); } + + public void testUpdateAddServers() throws Exception { + HQApi api = getApi(); + ResourceApi rApi = api.getResourceApi(); + ApplicationApi appApi = api.getApplicationApi(); + + ResourcePrototypeResponse protoResponse = + rApi.getResourcePrototype("HQ Agent"); + hqAssertSuccess(protoResponse); + + ResourcesResponse agentsResponse = + rApi.getResources(protoResponse.getResourcePrototype(), + false, false); + hqAssertSuccess(agentsResponse); + + assertTrue("No HQ Agent resources found in the inventory!", + agentsResponse.getResource().size() > 0); + + Application a = createTestApplication(null); + + a.getResource().addAll(agentsResponse.getResource()); + + ApplicationResponse updateResponse = appApi.updateApplication(a); + hqAssertFailureInvalidParameters(updateResponse); + + StatusResponse deleteResponse = appApi.deleteApplication(a.getId()); + hqAssertSuccess(deleteResponse); + } + + public void testUpdateAddPlatform() throws Exception { + HQApi api = getApi(); + ResourceApi rApi = api.getResourceApi(); + ApplicationApi appApi = api.getApplicationApi(); + + Resource platform = getLocalPlatformResource(false, false); + + Application a = createTestApplication(null); + + a.getResource().add(platform); + + ApplicationResponse updateResponse = appApi.updateApplication(a); + hqAssertFailureInvalidParameters(updateResponse); + + StatusResponse deleteResponse = appApi.deleteApplication(a.getId()); + hqAssertSuccess(deleteResponse); + } } From 3960fc7f6e4c1537dd222e302d00e211398cd49b Mon Sep 17 00:00:00 2001 From: Ryan Morgan Date: Thu, 24 Sep 2009 16:50:36 -0700 Subject: [PATCH 20/20] Test update on a non-persisted Application. --- hqu/hqapi1/app/ApplicationController.groovy | 3 ++- .../hyperic/hq/hqapi1/test/ApplicationUpdate_test.java | 10 +++++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/hqu/hqapi1/app/ApplicationController.groovy b/hqu/hqapi1/app/ApplicationController.groovy index af96e9ec..db2856d2 100644 --- a/hqu/hqapi1/app/ApplicationController.groovy +++ b/hqu/hqapi1/app/ApplicationController.groovy @@ -144,7 +144,8 @@ class ApplicationController extends ApiController { if (!appId) { renderXml() { ApplicationResponse() { - out << getFailureXML(ErrorCode.INVALID_PARAMETERS) + out << getFailureXML(ErrorCode.INVALID_PARAMETERS, + "No application id found") } } return diff --git a/src/org/hyperic/hq/hqapi1/test/ApplicationUpdate_test.java b/src/org/hyperic/hq/hqapi1/test/ApplicationUpdate_test.java index c67e4ece..4fe04ed6 100644 --- a/src/org/hyperic/hq/hqapi1/test/ApplicationUpdate_test.java +++ b/src/org/hyperic/hq/hqapi1/test/ApplicationUpdate_test.java @@ -142,7 +142,6 @@ public void testUpdateAddServers() throws Exception { public void testUpdateAddPlatform() throws Exception { HQApi api = getApi(); - ResourceApi rApi = api.getResourceApi(); ApplicationApi appApi = api.getApplicationApi(); Resource platform = getLocalPlatformResource(false, false); @@ -157,4 +156,13 @@ public void testUpdateAddPlatform() throws Exception { StatusResponse deleteResponse = appApi.deleteApplication(a.getId()); hqAssertSuccess(deleteResponse); } + + public void testUpdateNonPersistedApplication() throws Exception { + ApplicationApi appApi = getApi().getApplicationApi(); + + Application a = generateTestApplication(); + + ApplicationResponse response = appApi.updateApplication(a); + hqAssertFailureInvalidParameters(response); + } }