brooklyn-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aleds...@apache.org
Subject [31/50] git commit: Support for referencing Java catalog entities in YAML
Date Wed, 09 Jul 2014 21:46:44 GMT
Support for referencing Java catalog entities in YAML


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

Branch: refs/heads/master
Commit: 6dda7b6ba9c985d5fa9619672ef0032d8459d244
Parents: c663e1e
Author: Svetoslav Neykov <svetoslav.neykov@cloudsoftcorp.com>
Authored: Tue Jul 8 10:27:33 2014 +0300
Committer: Aled Sage <aled.sage@gmail.com>
Committed: Wed Jul 9 22:34:45 2014 +0100

----------------------------------------------------------------------
 .../BrooklynAssemblyTemplateInstantiator.java   | 68 +++++++++++---------
 .../rest/resources/ApplicationResourceTest.java | 18 ++++++
 2 files changed, 55 insertions(+), 31 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/6dda7b6b/usage/camp/src/main/java/io/brooklyn/camp/brooklyn/spi/creation/BrooklynAssemblyTemplateInstantiator.java
----------------------------------------------------------------------
diff --git a/usage/camp/src/main/java/io/brooklyn/camp/brooklyn/spi/creation/BrooklynAssemblyTemplateInstantiator.java
b/usage/camp/src/main/java/io/brooklyn/camp/brooklyn/spi/creation/BrooklynAssemblyTemplateInstantiator.java
index 604d8bf..46e50ed 100644
--- a/usage/camp/src/main/java/io/brooklyn/camp/brooklyn/spi/creation/BrooklynAssemblyTemplateInstantiator.java
+++ b/usage/camp/src/main/java/io/brooklyn/camp/brooklyn/spi/creation/BrooklynAssemblyTemplateInstantiator.java
@@ -360,38 +360,10 @@ public class BrooklynAssemblyTemplateInstantiator implements AssemblyTemplateSpe
             EntitySpec<?> spec;
             
             CatalogItem<Entity, EntitySpec<?>> item = entityResolver.getCatalogItem();
-            if (item != null) {
-                String yaml = item.getPlanYaml();
-                Reader input = new StringReader(yaml);
-                
-                AssemblyTemplate at;
-                BrooklynClassLoadingContext itemLoader = item.newClassLoadingContext(mgmt);
-                BrooklynLoaderTracker.setLoader(itemLoader);
-                try {
-                    at = platform.pdp().registerDeploymentPlan(input);
-                } finally {
-                    BrooklynLoaderTracker.unsetLoader(itemLoader);
-                }
-
-                // TODO Should we check entitlements, or sufficient to do once for this being
called in the first place?
-                // TODO Is it acceptable to only allow a single  top-level entity in a catalog?
If not, we need to think
-                //      about what it would mean to subsequently call buildChildrenEntitySpecs
on the list of top-level entities!
-                try {
-                    AssemblyTemplateInstantiator ati = at.getInstantiator().newInstance();
-                    if (ati instanceof BrooklynAssemblyTemplateInstantiator) {
-                        List<EntitySpec<?>> specs = ((BrooklynAssemblyTemplateInstantiator)ati).buildTemplateServicesAsSpecs(itemLoader,
at, platform);
-                        if (specs.size() > 1) {
-                            throw new UnsupportedOperationException("Only supporting single
service in catalog item currently");
-                        }
-                        spec = specs.get(0);
-                    } else {
-                        throw new IllegalStateException("Cannot create application with instantiator:
" + ati);
-                    }
-                } catch (Exception e) {
-                    throw Exceptions.propagate(e);
-                }
-            } else {
+            if (item == null || item.getJavaType() != null) {
                 spec = entityResolver.resolveSpec();
+            } else {
+                spec = resolveCatalogYamlReferenceSpec(platform, mgmt, item);
             }
 
             BrooklynClassLoadingContext newLoader = entityResolver.loader;
@@ -402,6 +374,40 @@ public class BrooklynAssemblyTemplateInstantiator implements AssemblyTemplateSpe
         return result;
     }
 
+    private EntitySpec<?> resolveCatalogYamlReferenceSpec(CampPlatform platform,
+            ManagementContext mgmt,
+            CatalogItem<Entity, EntitySpec<?>> item) {
+        String yaml = item.getPlanYaml();
+        Reader input = new StringReader(yaml);
+        
+        AssemblyTemplate at;
+        BrooklynClassLoadingContext itemLoader = item.newClassLoadingContext(mgmt);
+        BrooklynLoaderTracker.setLoader(itemLoader);
+        try {
+            at = platform.pdp().registerDeploymentPlan(input);
+        } finally {
+            BrooklynLoaderTracker.unsetLoader(itemLoader);
+        }
+
+        // TODO Should we check entitlements, or sufficient to do once for this being called
in the first place?
+        // TODO Is it acceptable to only allow a single  top-level entity in a catalog? If
not, we need to think
+        //      about what it would mean to subsequently call buildChildrenEntitySpecs on
the list of top-level entities!
+        try {
+            AssemblyTemplateInstantiator ati = at.getInstantiator().newInstance();
+            if (ati instanceof BrooklynAssemblyTemplateInstantiator) {
+                List<EntitySpec<?>> specs = ((BrooklynAssemblyTemplateInstantiator)ati).buildTemplateServicesAsSpecs(itemLoader,
at, platform);
+                if (specs.size() > 1) {
+                    throw new UnsupportedOperationException("Only supporting single service
in catalog item currently");
+                }
+                return specs.get(0);
+            } else {
+                throw new IllegalStateException("Cannot create application with instantiator:
" + ati);
+            }
+        } catch (Exception e) {
+            throw Exceptions.propagate(e);
+        }
+    }
+
     protected void buildChildrenEntitySpecs(BrooklynClassLoadingContext loader, EntitySpec<?>
parent, List<Map<String, Object>> childConfig) {
         if (childConfig != null) {
             for (Map<String, Object> childAttrs : childConfig) {

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/6dda7b6b/usage/rest-server/src/test/java/brooklyn/rest/resources/ApplicationResourceTest.java
----------------------------------------------------------------------
diff --git a/usage/rest-server/src/test/java/brooklyn/rest/resources/ApplicationResourceTest.java
b/usage/rest-server/src/test/java/brooklyn/rest/resources/ApplicationResourceTest.java
index e48ca92..a3c9a8f 100644
--- a/usage/rest-server/src/test/java/brooklyn/rest/resources/ApplicationResourceTest.java
+++ b/usage/rest-server/src/test/java/brooklyn/rest/resources/ApplicationResourceTest.java
@@ -41,6 +41,7 @@ import org.testng.annotations.Test;
 
 import brooklyn.entity.Application;
 import brooklyn.entity.basic.BasicApplication;
+import brooklyn.entity.basic.BasicEntity;
 import brooklyn.entity.basic.EntityFunctions;
 import brooklyn.entity.basic.Lifecycle;
 import brooklyn.location.Location;
@@ -219,6 +220,23 @@ public class ApplicationResourceTest extends BrooklynRestResourceTest
{
   }
 
   @Test
+  public void testReferenceCatalogEntity() throws Exception {
+      getManagementContext().getCatalog().addItem(BasicEntity.class);
+
+      String yaml = "{ name: simple-app-yaml, location: localhost, services: [ { serviceType:
" + BasicEntity.class.getName() + " } ] }";
+      
+    ClientResponse response = client().resource("/v1/applications")
+        .entity(yaml, "application/x-yaml")
+        .post(ClientResponse.class);
+    assertTrue(response.getStatus()/100 == 2, "response is "+response);
+    
+    // Expect app to be running
+    URI appUri = response.getLocation();
+    waitForApplicationToBeRunning(response.getLocation());
+    assertEquals(client().resource(appUri).get(ApplicationSummary.class).getSpec().getName(),
"simple-app-yaml");
+  }
+
+  @Test
   public void testDeployWithInvalidEntityType() {
     try {
       clientDeploy(


Mime
View raw message