You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@brooklyn.apache.org by ge...@apache.org on 2017/03/27 10:35:22 UTC

[03/12] brooklyn-server git commit: Using the new CatalogBundleLoader and make sure the REST API returns a 400 + uninstall the bundle if an error occur

Using the new CatalogBundleLoader and make sure the REST API returns a 400 + uninstall the bundle if an error occur


Project: http://git-wip-us.apache.org/repos/asf/brooklyn-server/repo
Commit: http://git-wip-us.apache.org/repos/asf/brooklyn-server/commit/5977702a
Tree: http://git-wip-us.apache.org/repos/asf/brooklyn-server/tree/5977702a
Diff: http://git-wip-us.apache.org/repos/asf/brooklyn-server/diff/5977702a

Branch: refs/heads/master
Commit: 5977702aef29c73c7cc38a4119ca7be39dd590ca
Parents: 84c1b69
Author: Thomas Bouron <th...@cloudsoftcorp.com>
Authored: Thu Mar 23 16:15:24 2017 +0000
Committer: Thomas Bouron <th...@cloudsoftcorp.com>
Committed: Thu Mar 23 17:21:02 2017 +0000

----------------------------------------------------------------------
 .../apache/brooklyn/rest/api/CatalogApi.java    |  4 +-
 .../rest/resources/CatalogResource.java         | 41 ++++++++++++--------
 2 files changed, 27 insertions(+), 18 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/5977702a/rest/rest-api/src/main/java/org/apache/brooklyn/rest/api/CatalogApi.java
----------------------------------------------------------------------
diff --git a/rest/rest-api/src/main/java/org/apache/brooklyn/rest/api/CatalogApi.java b/rest/rest-api/src/main/java/org/apache/brooklyn/rest/api/CatalogApi.java
index 79b6186..66db391 100644
--- a/rest/rest-api/src/main/java/org/apache/brooklyn/rest/api/CatalogApi.java
+++ b/rest/rest-api/src/main/java/org/apache/brooklyn/rest/api/CatalogApi.java
@@ -71,13 +71,13 @@ public interface CatalogApi {
     @ApiOperation(value = "Add items to the catalog, either YAML or JAR/ZIP, format autodetected. "
             + "Specify a content-type header to skip auto-detection and invoke one of the more specific methods. "
             + "Return value is 201 CREATED if bundle could be added.", response = String.class)
-    public Response createPoly(
+    public Response createFromUpload(
             @ApiParam(
                     name = "item",
                     value = "Item to install, as JAR/ZIP or Catalog YAML (autodetected)",
                     required = true)
             byte[] item);
-    
+
     @POST
     @Beta
     @Consumes({"application/x-zip", "application/x-jar"})

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/5977702a/rest/rest-resources/src/main/java/org/apache/brooklyn/rest/resources/CatalogResource.java
----------------------------------------------------------------------
diff --git a/rest/rest-resources/src/main/java/org/apache/brooklyn/rest/resources/CatalogResource.java b/rest/rest-resources/src/main/java/org/apache/brooklyn/rest/resources/CatalogResource.java
index 9fdf977..8cf4d01 100644
--- a/rest/rest-resources/src/main/java/org/apache/brooklyn/rest/resources/CatalogResource.java
+++ b/rest/rest-resources/src/main/java/org/apache/brooklyn/rest/resources/CatalogResource.java
@@ -85,10 +85,12 @@ import org.apache.brooklyn.util.yaml.Yamls;
 import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
 import org.apache.commons.compress.archivers.zip.ZipFile;
 import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleException;
 import org.osgi.framework.Constants;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import com.google.common.annotations.Beta;
 import com.google.common.base.Function;
 import com.google.common.base.Predicate;
 import com.google.common.base.Predicates;
@@ -115,7 +117,8 @@ public class CatalogResource extends AbstractBrooklynRestResource implements Cat
     static Set<String> missingIcons = MutableSet.of();
 
     @Override
-    public Response createPoly(byte[] item) {
+    @Beta
+    public Response createFromUpload(byte[] item) {
         Throwable yamlException = null;
         try {
             MutableList.copyOf( Yamls.parseAll(new InputStreamReader(new ByteArrayInputStream(item))) );
@@ -129,14 +132,11 @@ public class CatalogResource extends AbstractBrooklynRestResource implements Cat
             return createFromYaml(new String(item));
         }
         
-        try {
-            return createFromArchive(item);
-        } catch (Exception e) {
-            throw Exceptions.propagate("Unable to handle input: not YAML or compatible ZIP. Specify Content-Type to clarify.", e);
-        }
+        return createFromArchive(item);
     }
     
-    @Override @Deprecated
+    @Override
+    @Deprecated
     public Response create(String yaml) {
         return createFromYaml(yaml);
     }
@@ -177,6 +177,7 @@ public class CatalogResource extends AbstractBrooklynRestResource implements Cat
     }
 
     @Override
+    @Beta
     public Response createFromArchive(byte[] zipInput) {
         if (!Entitlements.isEntitled(mgmt().getEntitlementManager(), Entitlements.ROOT, null)) {
             throw WebResourceUtils.forbidden("User '%s' is not authorized to add catalog item",
@@ -197,20 +198,20 @@ public class CatalogResource extends AbstractBrooklynRestResource implements Cat
             try {
                 zf = new ZipFile(f);
             } catch (IOException e) {
-                throw WebResourceUtils.badRequest("Invalid ZIP/JAR archive: "+e);
+                throw new IllegalArgumentException("Invalid ZIP/JAR archive: "+e);
             }
             ZipArchiveEntry bom = zf.getEntry("catalog.bom");
             if (bom==null) {
                 bom = zf.getEntry("/catalog.bom");
             }
             if (bom==null) {
-                throw WebResourceUtils.badRequest("Archive must contain a catalog.bom file in the root");
+                throw new IllegalArgumentException("Archive must contain a catalog.bom file in the root");
             }
             String bomS;
             try {
                 bomS = Streams.readFullyString(zf.getInputStream(bom));
             } catch (IOException e) {
-                throw WebResourceUtils.badRequest("Error reading catalog.bom from ZIP/JAR archive: "+e);
+                throw new IllegalArgumentException("Error reading catalog.bom from ZIP/JAR archive: "+e);
             }
             VersionedName vn = BasicBrooklynCatalog.getVersionedName( BasicBrooklynCatalog.getCatalogMetadata(bomS) );
             
@@ -221,7 +222,7 @@ public class CatalogResource extends AbstractBrooklynRestResource implements Cat
             String bundleNameInMF = mf.getMainAttributes().getValue(Constants.BUNDLE_SYMBOLICNAME);
             if (Strings.isNonBlank(bundleNameInMF)) {
                 if (!bundleNameInMF.equals(vn.getSymbolicName())) {
-                    throw new IllegalStateException("JAR MANIFEST symbolic-name '"+bundleNameInMF+"' does not match '"+vn.getSymbolicName()+"' defined in BOM");
+                    throw new IllegalArgumentException("JAR MANIFEST symbolic-name '"+bundleNameInMF+"' does not match '"+vn.getSymbolicName()+"' defined in BOM");
                 }
             } else {
                 mf.getMainAttributes().putValue(Constants.BUNDLE_SYMBOLICNAME, vn.getSymbolicName());
@@ -230,7 +231,7 @@ public class CatalogResource extends AbstractBrooklynRestResource implements Cat
             String bundleVersionInMF = mf.getMainAttributes().getValue(Constants.BUNDLE_VERSION);
             if (Strings.isNonBlank(bundleVersionInMF)) {
                 if (!bundleVersionInMF.equals(vn.getVersion().toString())) {
-                    throw new IllegalStateException("JAR MANIFEST version '"+bundleVersionInMF+"' does not match '"+vn.getVersion()+"' defined in BOM");
+                    throw new IllegalArgumentException("JAR MANIFEST version '"+bundleVersionInMF+"' does not match '"+vn.getVersion()+"' defined in BOM");
                 }
             } else {
                 mf.getMainAttributes().putValue(Constants.BUNDLE_VERSION, vn.getVersion().toString());
@@ -245,13 +246,21 @@ public class CatalogResource extends AbstractBrooklynRestResource implements Cat
             
             if (!BrooklynFeatureEnablement.isEnabled(BrooklynFeatureEnablement.FEATURE_LOAD_BUNDLE_CATALOG_BOM)) {
                 // if the above feature is not enabled, let's do it manually (as a contract of this method)
-                new CatalogBomScanner().new CatalogPopulator(
-                        ((LocalManagementContext) mgmt()).getOsgiManager().get().getFramework().getBundleContext(),
-                        mgmt()).addingBundle(bundle, null);
+                try {
+                    new CatalogBomScanner().new CatalogBundleLoader(mgmt()).scanForCatalog(bundle);
+                } catch (RuntimeException ex) {
+                    try {
+                        bundle.uninstall();
+                    } catch (BundleException e) {
+                        log.error("Cannot uninstall bundle " + bundle.getSymbolicName() + ":" + bundle.getVersion(), e);
+                    }
+                    throw new IllegalArgumentException("Error installing catalog items", ex);
+                }
             }
             
             return Response.status(Status.CREATED).build();
-            
+        } catch (RuntimeException ex) {
+            throw WebResourceUtils.badRequest(ex);
         } finally {
             if (f!=null) f.delete();
             if (f2!=null) f2.delete();