Skip to content

Commit 1954ee2

Browse files
committed
better detail in REST API response when posting ZIP
preserves backwards compatibility, adding a detail=true flag which gives richer info. see extended comment in code. also fixes bug where the added/changed items weren't actually listed.
1 parent e1b0be1 commit 1954ee2

3 files changed

Lines changed: 23 additions & 12 deletions

File tree

core/src/main/java/org/apache/brooklyn/core/mgmt/ha/OsgiManager.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -372,12 +372,11 @@ public synchronized Bundle registerBundle(CatalogBundle bundleMetadata) {
372372
// possibly remove that other capability, so that bundles with BOMs _have_ to be installed via this method.
373373
// (load order gets confusing with auto-scanning...)
374374
public List<? extends CatalogItem<?,?>> loadCatalogBom(Bundle bundle) {
375-
List<? extends CatalogItem<?, ?>> catalogItems = MutableList.of();
376-
loadCatalogBom(mgmt, bundle, catalogItems);
377-
return catalogItems;
375+
return MutableList.copyOf(loadCatalogBom(mgmt, bundle));
378376
}
379377

380-
private static Iterable<? extends CatalogItem<?, ?>> loadCatalogBom(ManagementContext mgmt, Bundle bundle, Iterable<? extends CatalogItem<?, ?>> catalogItems) {
378+
private static Iterable<? extends CatalogItem<?, ?>> loadCatalogBom(ManagementContext mgmt, Bundle bundle) {
379+
Iterable<? extends CatalogItem<?, ?>> catalogItems = MutableList.of();
381380
if (!BrooklynFeatureEnablement.isEnabled(BrooklynFeatureEnablement.FEATURE_LOAD_BUNDLE_CATALOG_BOM)) {
382381
// if the above feature is not enabled, let's do it manually (as a contract of this method)
383382
try {

rest/rest-api/src/main/java/org/apache/brooklyn/rest/api/CatalogApi.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,22 @@ public Response createFromYaml(
8888
@Valid String yaml);
8989

9090
@Beta
91+
/* TODO the polymorphic return type dependent on 'detail' is ugly,
92+
* but we're stuck in this API because backwards compatibility expects the types map
93+
* whereas typical usage wants more feedback. we should introduce a
94+
* /registry and/or /types and/or /bundles endpoint that always provides details
95+
* (and an approach to handling types more aligned with BrooklynTypeRegistry and OSGi bundling).
96+
* Not too concerned here as this method is beta and the above switch will probably naturally happen soon.
97+
* The main client who cares about this is the Go CLI. */
9198
@POST
9299
@Consumes({"application/x-zip", "application/x-jar"})
93100
@ApiOperation(
94101
value = "Add a catalog items (e.g. new type of entity, policy or location) by uploading a ZIP/JAR archive.",
95102
notes = "Accepts either an OSGi bundle JAR, or ZIP which will be turned into bundle JAR. Bother format must "
96103
+ "contain a catalog.bom at the root of the archive, which must contain the bundle and version key."
97-
+ "Return value is map of ID to CatalogItemSummary.",
104+
+ "Return value is map of ID to CatalogItemSummary unless detail=true is passed as a parameter in which "
105+
+ "case the return value is a BundleInstallationRestResult map containing the types map in types along "
106+
+ "with a message, bundle, and code.",
98107
response = String.class,
99108
hidden = true)
100109
@ApiResponses(value = {
@@ -106,7 +115,10 @@ public Response createFromArchive(
106115
name = "archive",
107116
value = "Bundle to install, in ZIP or JAR format, requiring catalog.bom containing bundle name and version",
108117
required = true)
109-
byte[] archive);
118+
byte[] archive,
119+
@ApiParam(name="detail", value="Provide a wrapping details map", required=false)
120+
@QueryParam("detail") @DefaultValue("false")
121+
boolean detail);
110122

111123
@Beta
112124
@POST

rest/rest-resources/src/main/java/org/apache/brooklyn/rest/resources/CatalogResource.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@
4545
import org.apache.brooklyn.api.policy.PolicySpec;
4646
import org.apache.brooklyn.api.sensor.Enricher;
4747
import org.apache.brooklyn.api.sensor.EnricherSpec;
48-
import org.apache.brooklyn.api.typereg.ManagedBundle;
4948
import org.apache.brooklyn.api.typereg.RegisteredType;
5049
import org.apache.brooklyn.core.catalog.CatalogPredicates;
5150
import org.apache.brooklyn.core.catalog.internal.CatalogItemComparator;
@@ -130,7 +129,7 @@ public Response createFromUpload(byte[] item) {
130129
return createFromYaml(new String(item));
131130
}
132131

133-
return createFromArchive(item);
132+
return createFromArchive(item, false);
134133
}
135134

136135
@Override
@@ -159,7 +158,7 @@ public static class BundleInstallationRestResult {
159158
// as Osgi result, but without bundle, and with maps of catalog items installed
160159

161160
String message;
162-
ManagedBundle metadata;
161+
String bundle;
163162
OsgiBundleInstallationResult.ResultCode code;
164163

165164
Map<String,Object> types;
@@ -172,7 +171,7 @@ public String getMessage() {
172171
public static BundleInstallationRestResult of(OsgiBundleInstallationResult in, ManagementContext mgmt, BrooklynRestResourceUtils brooklynU, UriInfo ui) {
173172
BundleInstallationRestResult result = new BundleInstallationRestResult();
174173
result.message = in.getMessage();
175-
result.metadata = in.getMetadata();
174+
result.bundle = in.getMetadata().getVersionedName().toString();
176175
result.code = in.getCode();
177176
if (in.getCatalogItemsInstalled()!=null) {
178177
result.types = MutableMap.of();
@@ -191,7 +190,7 @@ public static BundleInstallationRestResult of(OsgiBundleInstallationResult in, M
191190

192191
@Override
193192
@Beta
194-
public Response createFromArchive(byte[] zipInput) {
193+
public Response createFromArchive(byte[] zipInput, boolean detail) {
195194
if (!Entitlements.isEntitled(mgmt().getEntitlementManager(), Entitlements.ROOT, null)) {
196195
throw WebResourceUtils.forbidden("User '%s' is not authorized to add catalog item",
197196
Entitlements.getEntitlementContext().user());
@@ -205,7 +204,8 @@ public Response createFromArchive(byte[] zipInput) {
205204
.data(BundleInstallationRestResult.of(result.getWithoutError(), mgmt(), brooklyn(), ui)).build().asJsonResponse();
206205
}
207206

208-
return Response.status(Status.CREATED).entity( BundleInstallationRestResult.of(result.get(), mgmt(), brooklyn(), ui) ).build();
207+
BundleInstallationRestResult resultR = BundleInstallationRestResult.of(result.get(), mgmt(), brooklyn(), ui);
208+
return Response.status(Status.CREATED).entity( detail ? resultR : resultR.types ).build();
209209
}
210210

211211
private Response buildCreateResponse(Iterable<? extends CatalogItem<?, ?>> catalogItems) {

0 commit comments

Comments
 (0)