Navigation Menu

Skip to content

Commit

Permalink
Get resource by fqdn.
Browse files Browse the repository at this point in the history
  • Loading branch information
pnguyen committed Jun 24, 2010
1 parent 29d9848 commit e2d22dd
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 4 deletions.
21 changes: 19 additions & 2 deletions hqu/hqapi1/app/ResourceController.groovy
Expand Up @@ -358,12 +358,13 @@ class ResourceController extends ApiController {
def get(params) {
def id = params.getOne("id")?.toInteger()
def platformName = params.getOne("platformName")
def fqdn = params.getOne("fqdn")
boolean children = params.getOne("children", "false").toBoolean()
boolean verbose = params.getOne("verbose", "false").toBoolean()

def resource = null
def failureXml
if (!id && !platformName) {
if (!id && !platformName && !fqdn) {
failureXml = getFailureXML(ErrorCode.INVALID_PARAMETERS)
} else {
if (id) {
Expand All @@ -380,6 +381,17 @@ class ResourceController extends ApiController {
"Platform '" + platformName +
"' not found")
}
} else if (fqdn) {
try {
resource = resourceHelper.find('byFqdn':fqdn)
if (!resource) {
failureXml = getFailureXML(ErrorCode.OBJECT_NOT_FOUND,
"Platform fqdn='" + fqdn +
"' not found")
}
} catch (PermissionException pe) {
failureXml = getFailureXML(ErrorCode.PERMISSION_DENIED)
}
}
}

Expand Down Expand Up @@ -611,7 +623,12 @@ class ResourceController extends ApiController {
}
} else {
// Assume platform
resource = resourceHelper.find('platform':name)
def fqdn = xmlResource['ResourceInfo'].find { it.'@key' == PROP_FQDN }
if (fqdn) {
resource = resourceHelper.find('byFqdn':fqdn.'@value')
} else {
resource = resourceHelper.find('platform':name)
}
}
}

Expand Down
29 changes: 28 additions & 1 deletion src/org/hyperic/hq/hqapi1/ResourceApi.java
Expand Up @@ -7,7 +7,7 @@
* normal use of the program, and does *not* fall under the heading of
* "derived work".
*
* Copyright (C) [2008, 2009], Hyperic, Inc.
* Copyright (C) [2008-2010], Hyperic, Inc.
* This file is part of HQ.
*
* HQ is free software; you can redistribute it and/or modify
Expand Down Expand Up @@ -299,6 +299,33 @@ public ResourceResponse getPlatformResource(String name, boolean verbose,
new XmlResponseHandler<ResourceResponse>(ResourceResponse.class));
}

/**
* Get a {@link Resource} by it's platform fqdn.
*
* @param fqdn The platform fqdn to look up.
* @param verbose Flag to indicate whether {@link org.hyperic.hq.hqapi1.types.ResourceConfig}
* and {@link org.hyperic.hq.hqapi1.types.ResourceProperty} information will
* be included.
* @param children Flag to control whether child resources of this resource
* will be included.
* @return On {@link org.hyperic.hq.hqapi1.types.ResponseStatus#SUCCESS},
* the Resource is returned via
* {@link org.hyperic.hq.hqapi1.types.ResourceResponse#getResource()}.
*
* @throws java.io.IOException If a network error occurs while making the request.
*/
public ResourceResponse getPlatformResourceByFqdn(String fqdn, boolean verbose,
boolean children)
throws IOException
{
Map<String, String[]> params = new HashMap<String, String[]>();
params.put("fqdn", new String[] { fqdn });
params.put("verbose", new String[] { Boolean.toString(verbose) });
params.put("children", new String[] { Boolean.toString(children)});
return doGet("resource/get.hqu", params,
new XmlResponseHandler<ResourceResponse>(ResourceResponse.class));
}

/**
* Find the platform {@link Resource}s serviced by the given
* {@link org.hyperic.hq.hqapi1.types.Agent}.
Expand Down
61 changes: 60 additions & 1 deletion src/org/hyperic/hq/hqapi1/test/ResourceGet_test.java
Expand Up @@ -7,7 +7,7 @@
* normal use of the program, and does *not* fall under the heading of
* "derived work".
*
* Copyright (C) [2008, 2009], Hyperic, Inc.
* Copyright (C) [2008-2010], Hyperic, Inc.
* This file is part of HQ.
*
* HQ is free software; you can redistribute it and/or modify
Expand All @@ -27,9 +27,14 @@

package org.hyperic.hq.hqapi1.test;

import java.util.List;

import org.hyperic.hq.hqapi1.HQApi;
import org.hyperic.hq.hqapi1.ResourceApi;
import org.hyperic.hq.hqapi1.types.Resource;
import org.hyperic.hq.hqapi1.types.ResourceInfo;
import org.hyperic.hq.hqapi1.types.ResourceResponse;
import org.hyperic.hq.hqapi1.types.User;

public class ResourceGet_test extends ResourceTestBase {

Expand Down Expand Up @@ -150,4 +155,58 @@ public void testGetInvalidPlatformResource() throws Exception {
false, false);
hqAssertFailureObjectNotFound(getResponse);
}

public void testGetPlatformResourceByFqdn() throws Exception {

Resource r = getLocalPlatformResource(false, false);

String fqdn = getFqdn(r);
assertNotNull("Platform has no fqdn", fqdn);

ResourceResponse getResponse =
getApi().getResourceApi().getPlatformResourceByFqdn(fqdn, false, false);

hqAssertSuccess(getResponse);
Resource resource = getResponse.getResource();
validateResource(resource);
}

public void testGetPlatformResourceByFqdnNoPermission() throws Exception {

Resource r = getLocalPlatformResource(false, false);

String fqdn = getFqdn(r);
assertNotNull("Platform has no fqdn", fqdn);

List<User> users = createTestUsers(1);
User user = users.get(0);
HQApi apiUnpriv = getApi(user.getName(), TESTUSER_PASSWORD);

ResourceResponse getResponse =
apiUnpriv.getResourceApi().getPlatformResourceByFqdn(fqdn, false, false);

hqAssertFailurePermissionDenied(getResponse);

deleteTestUsers(users);
}

public void testGetInvalidPlatformResourceByFqdn() throws Exception {

ResourceApi api = getApi().getResourceApi();

ResourceResponse getResponse = api.getPlatformResourceByFqdn("Invalid platform fqdn",
false, false);
hqAssertFailureObjectNotFound(getResponse);
}

private String getFqdn(Resource r) {
String fqdn = null;
for (ResourceInfo ri : r.getResourceInfo()) {
if ("fqdn".equals(ri.getKey())) {
fqdn = ri.getValue();
break;
}
}
return fqdn;
}
}

0 comments on commit e2d22dd

Please sign in to comment.