Github user aledsage commented on a diff in the pull request:

    https://github.com/apache/brooklyn-server/pull/810#discussion_r140311355
  
    --- Diff: 
rest/rest-resources/src/main/java/org/apache/brooklyn/rest/resources/BundleResource.java
 ---
    @@ -0,0 +1,186 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License.  You may obtain a copy of the License at
    + *
    + *     http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing,
    + * software distributed under the License is distributed on an
    + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    + * KIND, either express or implied.  See the License for the
    + * specific language governing permissions and limitations
    + * under the License.
    + */
    +package org.apache.brooklyn.rest.resources;
    +
    +import java.io.ByteArrayInputStream;
    +import java.io.InputStreamReader;
    +import java.util.List;
    +import java.util.Map;
    +import java.util.TreeMap;
    +
    +import javax.ws.rs.core.Response;
    +import javax.ws.rs.core.Response.Status;
    +
    +import org.apache.brooklyn.api.typereg.ManagedBundle;
    +import org.apache.brooklyn.core.catalog.internal.BasicBrooklynCatalog;
    +import org.apache.brooklyn.core.mgmt.entitlement.Entitlements;
    +import org.apache.brooklyn.core.mgmt.ha.OsgiBundleInstallationResult;
    +import org.apache.brooklyn.core.mgmt.internal.ManagementContextInternal;
    +import org.apache.brooklyn.rest.api.BundleApi;
    +import org.apache.brooklyn.rest.domain.ApiError;
    +import org.apache.brooklyn.rest.domain.BundleInstallationRestResult;
    +import org.apache.brooklyn.rest.domain.BundleSummary;
    +import org.apache.brooklyn.rest.filter.HaHotStateRequired;
    +import org.apache.brooklyn.rest.transform.TypeTransformer;
    +import org.apache.brooklyn.rest.util.WebResourceUtils;
    +import org.apache.brooklyn.util.collections.MutableList;
    +import org.apache.brooklyn.util.exceptions.Exceptions;
    +import org.apache.brooklyn.util.exceptions.ReferenceWithError;
    +import org.apache.brooklyn.util.osgi.VersionedName;
    +import org.apache.brooklyn.util.osgi.VersionedName.VersionedNameComparator;
    +import org.apache.brooklyn.util.yaml.Yamls;
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +
    +import com.google.common.base.Predicate;
    +import com.google.common.base.Predicates;
    +
    +@HaHotStateRequired
    +public class BundleResource extends AbstractBrooklynRestResource 
implements BundleApi {
    +
    +    private static final Logger log = 
LoggerFactory.getLogger(BundleResource.class);
    +    private static final String LATEST = "latest";
    +
    +    @Override
    +    public List<BundleSummary> list(String versions) {
    +        return list(TypeResource.isLatestOnly(versions, true), 
Predicates.alwaysTrue());
    +    }
    +    
    +    private List<BundleSummary> list(boolean onlyLatest, Predicate<String> 
symbolicNameFilter) {
    +        
    +        Map<VersionedName,ManagedBundle> bundles = new 
TreeMap<>(VersionedNameComparator.INSTANCE);
    +        for (ManagedBundle b: 
((ManagementContextInternal)mgmt()).getOsgiManager().get().getManagedBundles().values())
 {
    +            if (symbolicNameFilter.apply(b.getSymbolicName())) {
    +                // TODO entitlements for bundles
    +                VersionedName key = onlyLatest ? new 
VersionedName(b.getSymbolicName(), LATEST) : b.getVersionedName();
    +                ManagedBundle oldBundle = bundles.get(key);
    +                if (oldBundle==null || 
oldBundle.getVersionedName().compareTo(b.getVersionedName()) > 0) {
    +                    bundles.put(key, b);
    +                }
    +            }
    +        }
    +        return toBundleSummary(bundles.values());
    +    }
    +
    +    private List<BundleSummary> toBundleSummary(Iterable<ManagedBundle> 
sortedItems) {
    +        List<BundleSummary> result = MutableList.of();
    +        for (ManagedBundle t: sortedItems) {
    +            result.add(TypeTransformer.bundleSummary(brooklyn(), t, 
ui.getBaseUriBuilder(), mgmt()));
    +        }
    +        return result;
    +    }
    +
    +    @Override
    +    public List<BundleSummary> listVersions(String symbolicName) {
    +        return list(false, Predicates.equalTo(symbolicName));
    +    }
    +
    +    @Override
    +    public BundleSummary detail(String symbolicName, String version) {
    +        ManagedBundle b = lookup(symbolicName, version);
    +        return TypeTransformer.bundleDetails(brooklyn(), b, 
ui.getBaseUriBuilder(), mgmt());
    +    }
    +
    +    protected ManagedBundle lookup(String symbolicName, String version) {
    +        // TODO entitlements for bundles
    --- End diff --
    
    Perhaps we could just use the `catalog.see` entitlement for now?
    ```
            if (!Entitlements.isEntitled(mgmt().getEntitlementManager(), 
Entitlements.SEE_CATALOG_ITEM, 
symbolicName+(Strings.isBlank(version)?"":":"+version))) {
                throw WebResourceUtils.forbidden("User '%s' is not authorized 
to see catalog bundle",
                    Entitlements.getEntitlementContext().user());
            }
    ```


---

Reply via email to