Skip to content

Commit

Permalink
Implement Application sync().
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan Morgan committed Sep 28, 2009
1 parent 6570b7d commit cb6792a
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 103 deletions.
111 changes: 52 additions & 59 deletions hqu/hqapi1/app/ApplicationController.groovy
Expand Up @@ -79,22 +79,16 @@ class ApplicationController extends ApiController {
* the cause.
*/
private createApplication(xmlApplication) {

if (!xmlApplication || xmlApplication.size() != 1) {
failureXml = getFailureXML(ErrorCode.INVALID_PARAMETERS)
return null
}

if (!validateApplicationServices(xmlApplication)) {
return null
}

def appName = xmlApplication[0].'@name'
def appLoc = xmlApplication[0].'@location'
def appDesc = xmlApplication[0].'@description'
def appEng = xmlApplication[0].'@engContact'
def appOps = xmlApplication[0].'@opsContact'
def appBiz = xmlApplication[0].'@bizContact'
def appName = xmlApplication.'@name'
def appLoc = xmlApplication.'@location'
def appDesc = xmlApplication.'@description'
def appEng = xmlApplication.'@engContact'
def appOps = xmlApplication.'@opsContact'
def appBiz = xmlApplication.'@bizContact'

def applicationValue = new ApplicationValue()
applicationValue.name = appName
Expand All @@ -104,8 +98,7 @@ class ApplicationController extends ApiController {
applicationValue.opsContact = appOps
applicationValue.businessContact = appBiz

def newApp = null

def newApp
try {
applicationValue.applicationType = appMan.findApplicationType(1)
newApp = appMan.createApplication(user, applicationValue, new ArrayList())
Expand All @@ -132,7 +125,13 @@ class ApplicationController extends ApiController {
def createRequest = new XmlParser().parseText(getUpload('postdata'))
def xmlApplication = createRequest['Application']

def newApp = createApplication(xmlApplication)
def newApp
if (!xmlApplication || xmlApplication.size() != 1) {
failureXml = getFailureXML(ErrorCode.INVALID_PARAMETERS,
"Wrong number of Applications")
} else {
newApp = createApplication(xmlApplication[0])
}

renderXml() {
ApplicationResponse() {
Expand All @@ -154,12 +153,7 @@ class ApplicationController extends ApiController {
* the cause.
*/
private updateApplication(xmlApplication) {
if (!xmlApplication || xmlApplication.size() != 1) {
failureXml = getFailureXML(ErrorCode.INVALID_PARAMETERS)
return null
}

def appId = xmlApplication[0].'@id'?.toInteger()
def appId = xmlApplication.'@id'?.toInteger()
if (!appId) {
failureXml = getFailureXML(ErrorCode.INVALID_PARAMETERS,
"No application id found")
Expand All @@ -170,14 +164,14 @@ class ApplicationController extends ApiController {
return null
}

def appName = xmlApplication[0].'@name'
def appLoc = xmlApplication[0].'@location'
def appDesc = xmlApplication[0].'@description'
def appEng = xmlApplication[0].'@engContact'
def appOps = xmlApplication[0].'@opsContact'
def appBiz = xmlApplication[0].'@bizContact'
def appName = xmlApplication.'@name'
def appLoc = xmlApplication.'@location'
def appDesc = xmlApplication.'@description'
def appEng = xmlApplication.'@engContact'
def appOps = xmlApplication.'@opsContact'
def appBiz = xmlApplication.'@bizContact'

def updateApp = null
def updateApp
try {
updateApp = appMan.findApplicationById(user, appId)
} catch (Exception e) {
Expand All @@ -189,11 +183,11 @@ class ApplicationController extends ApiController {
}

def applicationValue = updateApp.getApplicationValue()
applicationValue.name = appName
applicationValue.location = appLoc
applicationValue.description = appDesc
applicationValue.engContact = appEng
applicationValue.opsContact = appOps
applicationValue.name = appName
applicationValue.location = appLoc
applicationValue.description = appDesc
applicationValue.engContact = appEng
applicationValue.opsContact = appOps
applicationValue.businessContact = appBiz

try {
Expand All @@ -218,7 +212,13 @@ class ApplicationController extends ApiController {
def updateRequest = new XmlParser().parseText(getUpload('postdata'))
def xmlApplication = updateRequest['Application']

def updatedApp = updateApplication(xmlApplication)
def updatedApp
if (!xmlApplication || xmlApplication.size() != 1) {
failureXml = getFailureXML(ErrorCode.INVALID_PARAMETERS,
"Wrong number of Applications")
} else {
updatedApp = updateApplication(xmlApplication[0])
}

renderXml() {
ApplicationResponse() {
Expand All @@ -236,37 +236,30 @@ class ApplicationController extends ApiController {
def syncRequest = new XmlParser().parseText(getUpload('postdata'))

def applications = []

def xmlApplications = syncRequest['Application']

if (!xmlApplications || xmlApplications.size() < 1) {
renderXml() {
ApplicationResponse() {
out << getFailureXML(ErrorCode.INVALID_PARAMETERS)
}
}
return
}

xmlApplications.each { xmlApp ->
// TODO: This needs some work
def appId = xmlApp.'@id'?.toInteger()
for (xmlApplication in syncRequest['Application']) {
def appId = xmlApplication.'@id'?.toInteger()
if (!appId) {
print "CREATING: " + xmlApp.'@name'
applications << createApplication(xmlApplication)
} else {
applications << updateApplication(xmlApplication)
}
else {
print "UPDATING: " + appId + " " + xmlApp.'@name'

if (failureXml) {
// Break out early on errors.
break
}
}

// TODO: This needs some work
renderXml() {
ApplicationResponse() {
out << getSuccessXML()
out << Application(
applications.each { app ->
getApplicationXML(app)
})
ApplicationsResponse() {
if (failureXml) {
out << failureXml
} else {
out << getSuccessXML()
for (app in applications) {
out << getApplicationXML(app)
}
}
}
}
}
Expand Down
7 changes: 4 additions & 3 deletions src/org/hyperic/hq/hqapi1/ApplicationApi.java
Expand Up @@ -99,10 +99,11 @@ public StatusResponse deleteApplication(int id)
/**
* Sync a list of {@link org.hyperic.hq.hqapi1.types.Application}s.
*
* @param applications The list of applications to sync.
* @param applications The list of Applications to sync.
*
* @return {@link org.hyperic.hq.hqapi1.types.ResponseStatus#SUCCESS} if
* all the applications were successfully syced.
* @return On {@link org.hyperic.hq.hqapi1.types.ResponseStatus#SUCCESS},
* the synced list of Application's are returned via
* {@link org.hyperic.hq.hqapi1.types.ApplicationsResponse#getApplication()}.
*
* @throws IOException If a network error occurs while making the request.
*/
Expand Down
4 changes: 2 additions & 2 deletions src/org/hyperic/hq/hqapi1/test/ApplicationTestBase.java
Expand Up @@ -10,6 +10,8 @@

public abstract class ApplicationTestBase extends HQApiTestBase {

static final String UPDATE_PREFIX = "UPDATED-";

protected static final String APP_NAME = "Test Application";
protected static final String APP_LOCATION = "SFO";
protected static final String APP_DESC = "Test Application Description";
Expand Down Expand Up @@ -55,8 +57,6 @@ protected Application createTestApplication(List<Resource> services)
throws Exception
{
ApplicationApi api = getApi().getApplicationApi();

Random r = new Random();
Application a = generateTestApplication();

if (services != null) {
Expand Down
2 changes: 0 additions & 2 deletions src/org/hyperic/hq/hqapi1/test/ApplicationUpdate_test.java
Expand Up @@ -17,8 +17,6 @@

public class ApplicationUpdate_test extends ApplicationTestBase {

private static final String UPDATE_PREFIX = "UPDATED-";

public ApplicationUpdate_test(String name) {
super(name);
}
Expand Down
39 changes: 2 additions & 37 deletions src/org/hyperic/hq/hqapi1/tools/ApplicationCommand.java
Expand Up @@ -21,13 +21,6 @@ public class ApplicationCommand extends Command {

private static String OPT_ID = "id";

// Additional sync commands when syncing via command line options.
private static String OPT_NAME = "name";
// private static String OPT_PROTOTYPE = "prototype";
// private static String OPT_REGEX = "regex";
// private static String OPT_DELETEMISSING = "deleteMissing";
// private static String OPT_DESC = "description";

private void printUsage() {
System.err.println("One of " + Arrays.toString(COMMANDS) + " required");
}
Expand Down Expand Up @@ -70,50 +63,22 @@ private void sync(String[] args) throws Exception {

OptionParser p = getOptionParser();

p.accepts(OPT_NAME, "The application name to sync").
withRequiredArg().ofType(String.class);
// p.accepts(OPT_PROTOTYPE, "The resource type to query for group membership").
// withRequiredArg().ofType(String.class);
// p.accepts(OPT_REGEX, "The regular expression to apply to the " + OPT_PROTOTYPE +
// " flag").withRequiredArg().ofType(String.class);
// p.accepts(OPT_DELETEMISSING, "Remove resources in the group not included in " +
// "the " + OPT_PROTOTYPE + " and " + OPT_REGEX);
// p.accepts(OPT_COMPAT, "If specified, attempt to make the group compatible");
// p.accepts(OPT_DESC, "If specified, set the description for the group").
// withRequiredArg().ofType(String.class);

OptionSet options = getOptions(p, args);

if (options.hasArgument(OPT_NAME)) {
syncViaCommandLineArgs(options);
return;
}

HQApi api = getApi(options);
System.out.println("api: " + api);

ApplicationApi applicationApi = api.getApplicationApi();
System.out.println("applicationApi: " + applicationApi);
ApplicationApi applicationApi = getApi(options).getApplicationApi();

InputStream is = getInputStream(options);
System.out.println("is: " + is);

ApplicationsResponse resp = XmlUtil.deserialize(ApplicationsResponse.class, is);
System.out.println("resp: " + resp);

List<Application> applications = resp.getApplication();
System.out.println("->" + applications);

ApplicationsResponse syncResponse = applicationApi.syncApplications(applications);
checkSuccess(syncResponse);

System.out.println("Successfully synced " + applications.size() + " applications.");
}

private void syncViaCommandLineArgs(OptionSet s) throws Exception
{
System.out.println("Feature not implemented.");
}

private void delete(String[] args) throws Exception {

OptionParser p = getOptionParser();
Expand Down

0 comments on commit cb6792a

Please sign in to comment.