Skip to content

Commit

Permalink
started the sync feature; needs more work
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Risberg authored and Thomas Risberg committed Sep 28, 2009
1 parent 02ae450 commit 18b7c82
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 21 deletions.
39 changes: 39 additions & 0 deletions hqu/hqapi1/app/ApplicationController.groovy
Expand Up @@ -236,6 +236,45 @@ class ApplicationController extends ApiController {
}
}

def sync(params) {
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()
if (!appId) {
print "CREATING: " + xmlApp.'@name'
}
else {
print "UPDATING: " + appId + " " + xmlApp.'@name'
}
}

// TODO: This needs some work
renderXml() {
ApplicationResponse() {
out << getSuccessXML()
out << Application(
applications.each { app ->
getApplicationXML(app)
})
}
}
}

def delete(params) {
def id = params.getOne('id')?.toInteger()

Expand Down
25 changes: 20 additions & 5 deletions src/org/hyperic/hq/hqapi1/ApplicationApi.java
@@ -1,14 +1,11 @@
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 org.hyperic.hq.hqapi1.types.*;

import java.io.IOException;
import java.util.Map;
import java.util.HashMap;
import java.util.List;

/**
* The Hyperic HQ Application API.
Expand Down Expand Up @@ -98,4 +95,22 @@ public StatusResponse deleteApplication(int id)
params.put("id", new String[] { Integer.toString(id)});
return doGet("application/delete.hqu", params, StatusResponse.class);
}

/**
* Sync a list of {@link org.hyperic.hq.hqapi1.types.Application}s.
*
* @param applications The list of applications to sync.
*
* @return {@link org.hyperic.hq.hqapi1.types.ResponseStatus#SUCCESS} if
* all the applications were successfully syced.
*
* @throws IOException If a network error occurs while making the request.
*/
public ApplicationsResponse syncApplications(List<Application> applications)
throws IOException {

ApplicationsRequest applicationsRequest = new ApplicationsRequest();
applicationsRequest.getApplication().addAll(applications);
return doPost("application/sync.hqu", applicationsRequest, ApplicationsResponse.class);
}
}
57 changes: 55 additions & 2 deletions src/org/hyperic/hq/hqapi1/tools/ApplicationCommand.java
Expand Up @@ -8,18 +8,21 @@
import org.hyperic.hq.hqapi1.types.*;

import java.util.Arrays;
import java.util.List;
import java.io.InputStream;

public class ApplicationCommand extends Command {

private static String CMD_LIST = "list";
private static String CMD_SYNC = "sync";
private static String CMD_DELETE = "delete";

private static String[] COMMANDS = { CMD_LIST, CMD_DELETE };
private static String[] COMMANDS = { CMD_LIST, CMD_SYNC, 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_NAME = "name";
// private static String OPT_PROTOTYPE = "prototype";
// private static String OPT_REGEX = "regex";
// private static String OPT_DELETEMISSING = "deleteMissing";
Expand All @@ -37,6 +40,8 @@ protected void handleCommand(String[] args) throws Exception {

if (args[0].equals(CMD_LIST)) {
list(trim(args));
} else if (args[0].equals(CMD_SYNC)) {
sync(trim(args));
} else if (args[0].equals(CMD_DELETE)) {
delete(trim(args));
} else {
Expand All @@ -61,6 +66,54 @@ private void list(String[] args) throws Exception {
XmlUtil.serialize(applications, System.out, Boolean.TRUE);
}

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);

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
20 changes: 6 additions & 14 deletions xsd/HQApi1.xsd
Expand Up @@ -31,25 +31,17 @@

<xs:element name="ApplicationRequest">
<xs:complexType>
<xs:complexContent>
<xs:extension base="Response">
<xs:sequence>
<xs:element name="Application" type="Application" minOccurs="0" maxOccurs="1"/>
</xs:sequence>
</xs:extension>
</xs:complexContent>
<xs:sequence>
<xs:element name="Application" type="Application" minOccurs="0" maxOccurs="1"/>
</xs:sequence>
</xs:complexType>
</xs:element>

<xs:element name="ApplicationsRequest">
<xs:complexType>
<xs:complexContent>
<xs:extension base="Response">
<xs:sequence>
<xs:element name="Application" type="Application" minOccurs="0" maxOccurs="1"/>
</xs:sequence>
</xs:extension>
</xs:complexContent>
<xs:sequence>
<xs:element name="Application" type="Application" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>

Expand Down

0 comments on commit 18b7c82

Please sign in to comment.