From e2d22dd39c9486afc8aa29f408e516bd1a853224 Mon Sep 17 00:00:00 2001 From: pnguyen Date: Thu, 24 Jun 2010 01:46:38 -0700 Subject: [PATCH] Get resource by fqdn. --- hqu/hqapi1/app/ResourceController.groovy | 21 ++++++- src/org/hyperic/hq/hqapi1/ResourceApi.java | 29 ++++++++- .../hq/hqapi1/test/ResourceGet_test.java | 61 ++++++++++++++++++- 3 files changed, 107 insertions(+), 4 deletions(-) diff --git a/hqu/hqapi1/app/ResourceController.groovy b/hqu/hqapi1/app/ResourceController.groovy index c68bf332..936e4d74 100644 --- a/hqu/hqapi1/app/ResourceController.groovy +++ b/hqu/hqapi1/app/ResourceController.groovy @@ -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) { @@ -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) + } } } @@ -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) + } } } diff --git a/src/org/hyperic/hq/hqapi1/ResourceApi.java b/src/org/hyperic/hq/hqapi1/ResourceApi.java index 1945f241..cc62f0bd 100644 --- a/src/org/hyperic/hq/hqapi1/ResourceApi.java +++ b/src/org/hyperic/hq/hqapi1/ResourceApi.java @@ -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 @@ -299,6 +299,33 @@ public ResourceResponse getPlatformResource(String name, boolean verbose, new XmlResponseHandler(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 params = new HashMap(); + 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.class)); + } + /** * Find the platform {@link Resource}s serviced by the given * {@link org.hyperic.hq.hqapi1.types.Agent}. diff --git a/src/org/hyperic/hq/hqapi1/test/ResourceGet_test.java b/src/org/hyperic/hq/hqapi1/test/ResourceGet_test.java index c8ca9eb0..c5ec2827 100644 --- a/src/org/hyperic/hq/hqapi1/test/ResourceGet_test.java +++ b/src/org/hyperic/hq/hqapi1/test/ResourceGet_test.java @@ -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 @@ -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 { @@ -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 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; + } } \ No newline at end of file