brooklyn-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aleds...@apache.org
Subject [2/3] incubator-brooklyn git commit: Adds getConfig method to server REST API to allow client code to read properties
Date Wed, 18 Feb 2015 10:45:49 GMT
Adds getConfig method to server REST API to allow client code to read properties


Project: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/commit/5ee01b8a
Tree: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/tree/5ee01b8a
Diff: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/diff/5ee01b8a

Branch: refs/heads/master
Commit: 5ee01b8a774c8dd0f09bdef5dfe55d8efde88df5
Parents: e0cc1a6
Author: Martin Harris <github@nakomis.com>
Authored: Mon Feb 16 11:13:03 2015 +0000
Committer: Aled Sage <aled.sage@gmail.com>
Committed: Wed Feb 18 10:45:37 2015 +0000

----------------------------------------------------------------------
 .../main/java/brooklyn/rest/api/ServerApi.java  | 15 ++++++++
 .../brooklyn/rest/resources/ServerResource.java | 11 ++++++
 .../rest/resources/ServerResourceTest.java      | 40 +++++++++++++++++++-
 3 files changed, 65 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/5ee01b8a/usage/rest-api/src/main/java/brooklyn/rest/api/ServerApi.java
----------------------------------------------------------------------
diff --git a/usage/rest-api/src/main/java/brooklyn/rest/api/ServerApi.java b/usage/rest-api/src/main/java/brooklyn/rest/api/ServerApi.java
index 6621ce1..624c9db 100644
--- a/usage/rest-api/src/main/java/brooklyn/rest/api/ServerApi.java
+++ b/usage/rest-api/src/main/java/brooklyn/rest/api/ServerApi.java
@@ -26,6 +26,7 @@ import javax.ws.rs.FormParam;
 import javax.ws.rs.GET;
 import javax.ws.rs.POST;
 import javax.ws.rs.Path;
+import javax.ws.rs.PathParam;
 import javax.ws.rs.Produces;
 import javax.ws.rs.QueryParam;
 import javax.ws.rs.core.MediaType;
@@ -38,6 +39,8 @@ import brooklyn.rest.domain.HighAvailabilitySummary;
 import brooklyn.rest.domain.VersionSummary;
 
 import com.google.common.annotations.Beta;
+import com.wordnik.swagger.core.ApiError;
+import com.wordnik.swagger.core.ApiErrors;
 import com.wordnik.swagger.core.ApiOperation;
 import com.wordnik.swagger.core.ApiParam;
 
@@ -87,6 +90,18 @@ public interface ServerApi {
             multiValueResponse = false)
     public String getStatus();
 
+    @GET
+    @Path("/config/{configKey}")
+    @ApiOperation(value = "Get the value of the specified config key from brooklyn properties")
+    @ApiErrors(value = {
+            // TODO: This should probably return a 404 if the key is not present, and should
return a predictable
+            // value if the value is not set. Behaviour should be consistent with EntityConfigApi.get()
+            @ApiError(code = 204, reason = "Could not find config key")
+    })
+    public String getConfig(
+            @ApiParam(value = "Config key ID", required = true)
+            @PathParam("configKey") String configKey);
+
     @Deprecated /** @deprecated since 0.7.0 use /ha/states */
     @GET
     @Path("/highAvailability")

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/5ee01b8a/usage/rest-server/src/main/java/brooklyn/rest/resources/ServerResource.java
----------------------------------------------------------------------
diff --git a/usage/rest-server/src/main/java/brooklyn/rest/resources/ServerResource.java b/usage/rest-server/src/main/java/brooklyn/rest/resources/ServerResource.java
index caacc29..6f8465e 100644
--- a/usage/rest-server/src/main/java/brooklyn/rest/resources/ServerResource.java
+++ b/usage/rest-server/src/main/java/brooklyn/rest/resources/ServerResource.java
@@ -33,6 +33,8 @@ import java.util.concurrent.atomic.AtomicBoolean;
 import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.Response;
 
+import brooklyn.config.ConfigKey;
+import brooklyn.entity.basic.ConfigKeys;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -253,6 +255,15 @@ public class ServerResource extends AbstractBrooklynRestResource implements
Serv
         return getHighAvailabilityNodeState().toString();
     }
 
+    @Override
+    public String getConfig(String configKey) {
+        if (!Entitlements.isEntitled(mgmt().getEntitlementManager(), Entitlements.SEE_ALL_SERVER_INFO,
null)) {
+            throw WebResourceUtils.unauthorized("User '%s' is not authorized for this operation",
Entitlements.getEntitlementContext().user());
+        }
+        ConfigKey<String> config = ConfigKeys.newStringConfigKey(configKey);
+        return mgmt().getConfig().getConfig(config);
+    }
+
     @Deprecated
     @Override
     public HighAvailabilitySummary getHighAvailability() {

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/5ee01b8a/usage/rest-server/src/test/java/brooklyn/rest/resources/ServerResourceTest.java
----------------------------------------------------------------------
diff --git a/usage/rest-server/src/test/java/brooklyn/rest/resources/ServerResourceTest.java
b/usage/rest-server/src/test/java/brooklyn/rest/resources/ServerResourceTest.java
index d7411c3..26491b3 100644
--- a/usage/rest-server/src/test/java/brooklyn/rest/resources/ServerResourceTest.java
+++ b/usage/rest-server/src/test/java/brooklyn/rest/resources/ServerResourceTest.java
@@ -21,10 +21,16 @@ package brooklyn.rest.resources;
 import static org.testng.Assert.assertEquals;
 import static org.testng.Assert.assertFalse;
 import static org.testng.Assert.assertNotNull;
+import static org.testng.Assert.assertNull;
 import static org.testng.Assert.assertTrue;
 
 import java.util.concurrent.atomic.AtomicInteger;
 
+import brooklyn.config.BrooklynProperties;
+import brooklyn.management.internal.ManagementContextInternal;
+import com.google.common.collect.ImmutableMap;
+import com.sun.jersey.api.client.UniformInterfaceException;
+import com.sun.jersey.api.client.WebResource;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.testng.annotations.Test;
@@ -97,5 +103,37 @@ public class ServerResourceTest extends BrooklynRestResourceTest {
                 assertFalse(getManagementContext().isRunning());
             }});
     }
-    
+
+    @Test
+    void testGetConfig() throws Exception {
+        ((ManagementContextInternal)getManagementContext()).getBrooklynProperties().put("foo.bar.baz",
"quux");
+        try {
+            assertEquals(client().resource("/v1/server/config/foo.bar.baz").get(String.class),
"quux");
+        } finally {
+            ((ManagementContextInternal)getManagementContext()).getBrooklynProperties().remove("foo.bar.baz");
+        }
+    }
+
+    @Test
+    void testGetMissingConfigThrowsException() throws Exception {
+        final String key = "foo.bar.baz";
+        BrooklynProperties properties = ((ManagementContextInternal)getManagementContext()).getBrooklynProperties();
+        Object existingValue = null;
+        boolean keyAlreadyPresent = false;
+        String response = null;
+        if (properties.containsKey(key)) {
+            existingValue = properties.remove(key);
+            keyAlreadyPresent = true;
+        }
+        try {
+            response = client().resource("/v1/server/config/" + key).get(String.class);
+            Asserts.fail("Expected call to /v1/server/config/" + key + " to fail with status
404, instead server returned " + response);
+        } catch (UniformInterfaceException e) {
+            assertEquals(e.getResponse().getStatus(), 204);
+        } finally {
+            if (keyAlreadyPresent) {
+                properties.put(key, existingValue);
+            }
+        }
+    }
 }


Mime
View raw message