This is an automated email from the ASF dual-hosted git repository.

heneveld pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/brooklyn-server.git

commit 3ed960501ec0492cc447571d26f3db62d2f3dfe1
Author: Alex Heneveld <[email protected]>
AuthorDate: Wed Oct 14 02:16:06 2020 +0100

    add format argument when deploying an app as well
---
 .../apache/brooklyn/rest/api/ApplicationApi.java   | 54 +++++++++++-------
 .../rest/resources/ApplicationResource.java        | 66 +++++++++++++---------
 .../rest/resources/ApplicationResourceTest.java    | 17 +++++-
 .../rest/resources/LocationResourceTest.java       |  7 +++
 .../brooklyn/rest/testing/BrooklynRestApiTest.java | 10 +++-
 5 files changed, 102 insertions(+), 52 deletions(-)

diff --git 
a/rest/rest-api/src/main/java/org/apache/brooklyn/rest/api/ApplicationApi.java 
b/rest/rest-api/src/main/java/org/apache/brooklyn/rest/api/ApplicationApi.java
index f5aab9b..acc853c 100644
--- 
a/rest/rest-api/src/main/java/org/apache/brooklyn/rest/api/ApplicationApi.java
+++ 
b/rest/rest-api/src/main/java/org/apache/brooklyn/rest/api/ApplicationApi.java
@@ -134,17 +134,11 @@ public interface ApplicationApi {
                     required = true)
             @PathParam("application") String application);
 
+    /** @deprecated since 1.1 use {@link #createWithFormat(byte[], String)} 
instead */
+    @Deprecated
     @POST
-    @Consumes({"application/x-yaml",
-            // see http://stackoverflow.com/questions/332129/yaml-mime-type
-            "text/yaml", "text/x-yaml", "application/yaml"})
-    @ApiOperation(
-            value = "Create and start a new application from YAML",
-            response = org.apache.brooklyn.rest.domain.TaskSummary.class
-    )
-    @ApiResponses(value = {
-            @ApiResponse(code = 404, message = "Undefined entity or location"),
-    })
+    @Consumes("application/deprecated-yaml-app-spec")
+    @ApiOperation(value = "(deprecated)", hidden = true)
     public Response createFromYaml(
             @ApiParam(
                     name = "applicationSpec",
@@ -152,7 +146,15 @@ public interface ApplicationApi {
                     required = true)
             String yaml);
 
-    @Beta
+    /** @deprecated since 1.1 use {@link #createFromYamlWithAppId(String, 
String, String)}  instead */
+    @Deprecated
+    @POST
+    @Consumes("application/deprecated-yaml-app-spec")
+    @ApiOperation(value = "(deprecated)", hidden = true)
+    public Response createFromYamlWithAppId(
+            @ApiParam(name = "applicationSpec", value = "App spec in CAMP YAML 
format", required = true) String yaml,
+            @ApiParam(name = "application", value = "Application id", required 
= true) @PathParam("application") String appId);
+
     @PUT
     @Path("/{application}")
     @Consumes({"application/x-yaml",
@@ -167,18 +169,15 @@ public interface ApplicationApi {
             @ApiResponse(code = 409, message = "Application already 
registered")
     })
     public Response createFromYamlWithAppId(
-            @ApiParam(name = "applicationSpec", value = "App spec in CAMP YAML 
format", required = true) String yaml,
+            @ApiParam(name = "plan", value = "Plan", required = true) String 
yaml,
+            @ApiParam(name = "format", value = "Format eg broolyn-camp", 
required = false) String format,
             @ApiParam(name = "application", value = "Application id", required 
= true) @PathParam("application") String appId);
 
+    /** @deprecated since 1.1 use {@link #createWithFormat(byte[], String)} 
instead */
+    @Deprecated
     @POST
-    @Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_OCTET_STREAM, 
MediaType.TEXT_PLAIN})
-    @ApiOperation(
-            value = "Create and start a new application from miscellaneous 
types, including JSON either new CAMP format or legacy AppSpec format",
-            response = org.apache.brooklyn.rest.domain.TaskSummary.class
-    )
-    @ApiResponses(value = {
-            @ApiResponse(code = 404, message = "Undefined entity or location")
-    })
+    @Consumes("application/deprecated-yaml-app-spec")
+    @ApiOperation(value = "(deprecated)", hidden = true)
     public Response createPoly(
             @ApiParam(
                     name = "applicationSpec",
@@ -202,6 +201,21 @@ public interface ApplicationApi {
                     required = true)
             @Valid String contents);
 
+    @Beta
+    @POST
+    @Consumes
+    @ApiOperation(
+            value = "Create and start a new application from YAML",
+            response = org.apache.brooklyn.rest.domain.TaskSummary.class
+    )
+    @ApiResponses(value = {
+            @ApiResponse(code = 404, message = "Undefined entity or location")
+    })
+    public Response createWithFormat(
+            @ApiParam(name = "plan", value = "Application plan to deploy", 
required = true) byte[] plan,
+            @ApiParam(name = "format", value = "Type plan format e.g. 
brooklyn-camp", required = false) String format);
+
+
     @DELETE
     @Path("/{application}")
     @ApiOperation(
diff --git 
a/rest/rest-resources/src/main/java/org/apache/brooklyn/rest/resources/ApplicationResource.java
 
b/rest/rest-resources/src/main/java/org/apache/brooklyn/rest/resources/ApplicationResource.java
index 1a7ef61..186b65a 100644
--- 
a/rest/rest-resources/src/main/java/org/apache/brooklyn/rest/resources/ApplicationResource.java
+++ 
b/rest/rest-resources/src/main/java/org/apache/brooklyn/rest/resources/ApplicationResource.java
@@ -387,15 +387,20 @@ public class ApplicationResource extends 
AbstractBrooklynRestResource implements
 
     @Override
     public Response createFromYaml(String yaml) {
-        return createFromYaml(yaml, Optional.absent());
+        return createFromYaml(yaml, null, Optional.absent());
     }
-    
+
     @Override
     public Response createFromYamlWithAppId(String yaml, String appId) {
-        return createFromYaml(yaml, Optional.of(appId));
+        return createFromYaml(yaml, null, Optional.of(appId));
     }
-    
-    protected Response createFromYaml(String yaml, Optional<String> appId) {
+
+    @Override
+    public Response createFromYamlWithAppId(String yaml, String format, String 
appId) {
+        return createFromYaml(yaml, format, Optional.of(appId));
+    }
+
+    protected Response createFromYaml(String yaml, String format, 
Optional<String> appId) {
         // First of all, see if it's a URL
         Preconditions.checkNotNull(yaml, "Blueprint must not be null");
         URI uri = null;
@@ -422,7 +427,7 @@ public class ApplicationResource extends 
AbstractBrooklynRestResource implements
 
         EntitySpec<? extends Application> spec;
         try {
-            spec = createEntitySpecForApplication(yaml);
+            spec = createEntitySpecForApplication(yaml, format);
         } catch (Exception e) {
             Exceptions.propagateIfFatal(e);
             log.warn("Failed REST deployment, could not create spec: "+e);
@@ -499,34 +504,47 @@ public class ApplicationResource extends 
AbstractBrooklynRestResource implements
 
     @Override
     public Response createPoly(byte[] inputToAutodetectType) {
+        return createWithFormat(inputToAutodetectType, null);
+    }
+
+    @Override
+    public Response createFromForm(String contents) {
+        log.debug("Creating app from form");
+        return createPoly(contents.getBytes());
+    }
+
+    @Override
+    public Response createWithFormat(byte[] inputToAutodetectType, String 
format) {
         log.debug("Creating app from autodetecting input");
 
         boolean looksLikeLegacy = false;
         Exception legacyFormatException = null;
-        // attempt legacy format
-        try {
-            ApplicationSpec appSpec = 
mapper().readValue(inputToAutodetectType, ApplicationSpec.class);
-            if (appSpec.getType() != null || appSpec.getEntities() != null) {
-                looksLikeLegacy = true;
+        if (Strings.isBlank(format)) {
+            // attempt legacy format
+            try {
+                ApplicationSpec appSpec = 
mapper().readValue(inputToAutodetectType, ApplicationSpec.class);
+                if (appSpec.getType() != null || appSpec.getEntities() != 
null) {
+                    looksLikeLegacy = true;
+                }
+                return createFromAppSpec(appSpec);
+            } catch (Exception e) {
+                Exceptions.propagateIfFatal(e);
+                legacyFormatException = e;
+                log.debug("Input is not legacy ApplicationSpec JSON (will try 
others)");
             }
-            return createFromAppSpec(appSpec);
-        } catch (Exception e) {
-            Exceptions.propagateIfFatal(e);
-            legacyFormatException = e;
-            log.debug("Input is not legacy ApplicationSpec JSON (will try 
others)");
         }
 
         //TODO infer encoding from request
         String potentialYaml = new String(inputToAutodetectType);
         EntitySpec<? extends Application> spec;
         try {
-            spec = createEntitySpecForApplication(potentialYaml);
+            spec = createEntitySpecForApplication(potentialYaml, format);
         } catch (Exception e) {
             Exceptions.propagateIfFatal(e);
             log.warn("Failed REST deployment, could not create spec 
(autodetecting): "+e);
-            
+
             // TODO if not yaml/json - try ZIP, etc
-            
+
             throw WebResourceUtils.badRequest(e, "Error in blueprint");
         }
 
@@ -547,12 +565,6 @@ public class ApplicationResource extends 
AbstractBrooklynRestResource implements
     }
 
     @Override
-    public Response createFromForm(String contents) {
-        log.debug("Creating app from form");
-        return createPoly(contents.getBytes());
-    }
-
-    @Override
     public Response delete(String application) {
         Application app = brooklyn().getApplication(application);
         if (!Entitlements.isEntitled(mgmt().getEntitlementManager(), 
Entitlements.INVOKE_EFFECTOR, Entitlements.EntityAndItem.of(app,
@@ -565,8 +577,8 @@ public class ApplicationResource extends 
AbstractBrooklynRestResource implements
         return status(ACCEPTED).entity(ts).build();
     }
 
-    private EntitySpec<? extends Application> 
createEntitySpecForApplication(String potentialYaml) {
-        return EntityManagementUtils.createEntitySpecForApplication(mgmt(), 
potentialYaml);
+    private EntitySpec<? extends Application> 
createEntitySpecForApplication(String potentialYaml, String format) {
+        return EntityManagementUtils.createEntitySpecForApplication(mgmt(), 
format, potentialYaml);
     }
     
     private void checkApplicationTypesAreValid(ApplicationSpec 
applicationSpec) {
diff --git 
a/rest/rest-resources/src/test/java/org/apache/brooklyn/rest/resources/ApplicationResourceTest.java
 
b/rest/rest-resources/src/test/java/org/apache/brooklyn/rest/resources/ApplicationResourceTest.java
index c38c2de..71dbcc1 100644
--- 
a/rest/rest-resources/src/test/java/org/apache/brooklyn/rest/resources/ApplicationResourceTest.java
+++ 
b/rest/rest-resources/src/test/java/org/apache/brooklyn/rest/resources/ApplicationResourceTest.java
@@ -20,6 +20,7 @@ package org.apache.brooklyn.rest.resources;
 
 import static com.google.common.base.Preconditions.checkNotNull;
 import static com.google.common.collect.Iterables.find;
+import org.apache.brooklyn.util.text.Strings;
 import static org.testng.Assert.assertEquals;
 import static org.testng.Assert.assertFalse;
 import static org.testng.Assert.assertNotNull;
@@ -126,7 +127,12 @@ public class ApplicationResourceTest extends 
BrooklynRestResourceTest {
      */
 
     private static final Logger log = 
LoggerFactory.getLogger(ApplicationResourceTest.class);
-    
+
+    @Override
+    protected boolean useOsgi() {
+        return true;
+    }
+
     private final ApplicationSpec simpleSpec = 
ApplicationSpec.builder().name("simple-app")
           .entities(ImmutableSet.of(
                   new EntitySpec("simple-ent", 
RestMockSimpleEntity.class.getName()),
@@ -238,8 +244,13 @@ public class ApplicationResourceTest extends 
BrooklynRestResourceTest {
 
     @Test
     public void testReferenceCatalogEntity() throws Exception {
-        getManagementContext().getCatalog().addItems("{ name: 
"+BasicEntity.class.getName()+", "
-            + "services: [ { type: "+BasicEntity.class.getName()+" } ] }");
+        getManagementContext().getCatalog().addItems(Strings.lines(
+                "brooklyn.catalog:",
+                "  id: test-reference",
+                "  version: 0.0.1",
+                "  item:",
+                "    name: "+BasicEntity.class.getName(),
+                "    services: [ { type: "+BasicEntity.class.getName()+" } 
]"));
 
         String yaml = "{ name: simple-app-yaml, location: localhost, services: 
[ { type: " + BasicEntity.class.getName() + " } ] }";
 
diff --git 
a/rest/rest-resources/src/test/java/org/apache/brooklyn/rest/resources/LocationResourceTest.java
 
b/rest/rest-resources/src/test/java/org/apache/brooklyn/rest/resources/LocationResourceTest.java
index 2c2acfd..0f7ef26 100644
--- 
a/rest/rest-resources/src/test/java/org/apache/brooklyn/rest/resources/LocationResourceTest.java
+++ 
b/rest/rest-resources/src/test/java/org/apache/brooklyn/rest/resources/LocationResourceTest.java
@@ -18,6 +18,8 @@
  */
 package org.apache.brooklyn.rest.resources;
 
+import org.apache.brooklyn.core.mgmt.ha.OsgiManager;
+import org.apache.brooklyn.core.mgmt.internal.ManagementContextInternal;
 import static org.testng.Assert.assertEquals;
 import static org.testng.Assert.assertTrue;
 
@@ -67,6 +69,11 @@ public class LocationResourceTest extends 
BrooklynRestResourceTest {
     private String testsDisplayName = "tests_displayName";
     private String byonHostname = "10.10.10.102";
 
+    @Override
+    protected boolean useOsgi() {
+        return true;
+    }
+
     @Test
     @Deprecated
     public void testAddLegacyLocationDefinition() {
diff --git 
a/rest/rest-resources/src/test/java/org/apache/brooklyn/rest/testing/BrooklynRestApiTest.java
 
b/rest/rest-resources/src/test/java/org/apache/brooklyn/rest/testing/BrooklynRestApiTest.java
index de9199e..446d916 100644
--- 
a/rest/rest-resources/src/test/java/org/apache/brooklyn/rest/testing/BrooklynRestApiTest.java
+++ 
b/rest/rest-resources/src/test/java/org/apache/brooklyn/rest/testing/BrooklynRestApiTest.java
@@ -131,6 +131,10 @@ public abstract class BrooklynRestApiTest {
         }
     }
 
+    protected boolean useOsgi() {
+        return useLocalScannedCatalog() || false;
+    }
+
     protected boolean useLocalScannedCatalog() {
         return false;
     }
@@ -141,14 +145,16 @@ public abstract class BrooklynRestApiTest {
 
     protected synchronized ManagementContext getManagementContext() {
         if (manager==null) {
-            if (useLocalScannedCatalog()) {
+            if (useOsgi()) {
                 manager = LocalManagementContextForTests.builder(true)
                         .enableOsgiReusable()
                         .build();
-                forceUseOfDefaultCatalogWithJavaClassPath();
             } else {
                 manager = new LocalManagementContextForTests();
             }
+            if (useLocalScannedCatalog()) {
+                forceUseOfDefaultCatalogWithJavaClassPath();
+            }
             manager.getHighAvailabilityManager().disabled(false);
             ((LocalManagementContext)manager).generateManagementPlaneId();
 

Reply via email to