Search in sources :

Example 6 with ArtifactVersionRange

use of io.cdap.cdap.api.artifact.ArtifactVersionRange in project cdap by cdapio.

the class ArtifactHttpHandler method getArtifactPlugin.

@GET
@Path("/namespaces/{namespace-id}/artifacts/{artifact-name}/" + "versions/{artifact-version}/extensions/{plugin-type}/plugins/{plugin-name}")
public void getArtifactPlugin(HttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId, @PathParam("artifact-name") String artifactName, @PathParam("artifact-version") String artifactVersion, @PathParam("plugin-type") String pluginType, @PathParam("plugin-name") String pluginName, @QueryParam("scope") @DefaultValue("user") final String scope, @QueryParam("artifactName") final String pluginArtifactName, @QueryParam("artifactVersion") String pluginVersion, @QueryParam("artifactScope") final String pluginScope, @QueryParam("limit") @DefaultValue("2147483647") String limit, @QueryParam("order") @DefaultValue("UNORDERED") String order) throws NamespaceNotFoundException, BadRequestException, ArtifactNotFoundException, InvalidArtifactRangeException {
    NamespaceId namespace = Ids.namespace(namespaceId);
    NamespaceId artifactNamespace = validateAndGetScopedNamespace(namespace, scope);
    final NamespaceId pluginArtifactNamespace = validateAndGetScopedNamespace(namespace, pluginScope);
    ArtifactId parentArtifactId = validateAndGetArtifactId(artifactNamespace, artifactName, artifactVersion);
    final ArtifactVersionRange pluginRange = pluginVersion == null ? null : ArtifactVersionRange.parse(pluginVersion);
    int limitNumber = Integer.parseInt(limit);
    limitNumber = limitNumber <= 0 ? Integer.MAX_VALUE : limitNumber;
    ArtifactSortOrder sortOrder = ArtifactSortOrder.valueOf(order);
    Predicate<ArtifactId> predicate = new Predicate<ArtifactId>() {

        @Override
        public boolean apply(ArtifactId input) {
            // by default, the scoped namespace is for USER scope
            return (((pluginScope == null && NamespaceId.SYSTEM.equals(input.getParent())) || pluginArtifactNamespace.equals(input.getParent())) && (pluginArtifactName == null || pluginArtifactName.equals(input.getArtifact())) && (pluginRange == null || pluginRange.versionIsInRange(new ArtifactVersion(input.getVersion()))));
        }
    };
    try {
        SortedMap<ArtifactDescriptor, PluginClass> plugins = artifactRepository.getPlugins(namespace, Id.Artifact.fromEntityId(parentArtifactId), pluginType, pluginName, predicate, limitNumber, sortOrder);
        List<PluginInfo> pluginInfos = Lists.newArrayList();
        // flatten the map
        for (Map.Entry<ArtifactDescriptor, PluginClass> pluginsEntry : plugins.entrySet()) {
            ArtifactDescriptor pluginArtifact = pluginsEntry.getKey();
            ArtifactSummary pluginArtifactSummary = ArtifactSummary.from(pluginArtifact.getArtifactId());
            PluginClass pluginClass = pluginsEntry.getValue();
            try {
                capabilityReader.checkAllEnabled(pluginClass.getRequirements().getCapabilities());
            } catch (CapabilityNotAvailableException e) {
                LOG.debug("Skipping plugin {} because of disabled capability", pluginClass, e);
                continue;
            }
            pluginInfos.add(new PluginInfo(pluginClass, pluginArtifactSummary));
        }
        responder.sendJson(HttpResponseStatus.OK, GSON.toJson(pluginInfos));
    } catch (PluginNotExistsException e) {
        responder.sendString(HttpResponseStatus.NOT_FOUND, e.getMessage());
    } catch (IOException e) {
        LOG.error("Exception looking up plugins for artifact {}", parentArtifactId, e);
        responder.sendString(HttpResponseStatus.INTERNAL_SERVER_ERROR, "Error reading plugins for the artifact from the store.");
    }
}
Also used : CapabilityNotAvailableException(io.cdap.cdap.internal.capability.CapabilityNotAvailableException) ArtifactId(io.cdap.cdap.proto.id.ArtifactId) ArtifactVersionRange(io.cdap.cdap.api.artifact.ArtifactVersionRange) IOException(java.io.IOException) ArtifactSortOrder(io.cdap.cdap.proto.artifact.ArtifactSortOrder) Predicate(com.google.common.base.Predicate) PluginNotExistsException(io.cdap.cdap.internal.app.runtime.plugin.PluginNotExistsException) ArtifactVersion(io.cdap.cdap.api.artifact.ArtifactVersion) ArtifactSummary(io.cdap.cdap.api.artifact.ArtifactSummary) ArtifactDescriptor(io.cdap.cdap.internal.app.runtime.artifact.ArtifactDescriptor) PluginInfo(io.cdap.cdap.proto.artifact.PluginInfo) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) PluginClass(io.cdap.cdap.api.plugin.PluginClass) Map(java.util.Map) SortedMap(java.util.SortedMap) HashMap(java.util.HashMap) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 7 with ArtifactVersionRange

use of io.cdap.cdap.api.artifact.ArtifactVersionRange in project cdap by caskdata.

the class ArtifactHttpHandler method getArtifactPlugin.

@GET
@Path("/namespaces/{namespace-id}/artifacts/{artifact-name}/" + "versions/{artifact-version}/extensions/{plugin-type}/plugins/{plugin-name}")
public void getArtifactPlugin(HttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId, @PathParam("artifact-name") String artifactName, @PathParam("artifact-version") String artifactVersion, @PathParam("plugin-type") String pluginType, @PathParam("plugin-name") String pluginName, @QueryParam("scope") @DefaultValue("user") final String scope, @QueryParam("artifactName") final String pluginArtifactName, @QueryParam("artifactVersion") String pluginVersion, @QueryParam("artifactScope") final String pluginScope, @QueryParam("limit") @DefaultValue("2147483647") String limit, @QueryParam("order") @DefaultValue("UNORDERED") String order) throws NamespaceNotFoundException, BadRequestException, ArtifactNotFoundException, InvalidArtifactRangeException {
    NamespaceId namespace = Ids.namespace(namespaceId);
    NamespaceId artifactNamespace = validateAndGetScopedNamespace(namespace, scope);
    final NamespaceId pluginArtifactNamespace = validateAndGetScopedNamespace(namespace, pluginScope);
    ArtifactId parentArtifactId = validateAndGetArtifactId(artifactNamespace, artifactName, artifactVersion);
    final ArtifactVersionRange pluginRange = pluginVersion == null ? null : ArtifactVersionRange.parse(pluginVersion);
    int limitNumber = Integer.parseInt(limit);
    limitNumber = limitNumber <= 0 ? Integer.MAX_VALUE : limitNumber;
    ArtifactSortOrder sortOrder = ArtifactSortOrder.valueOf(order);
    Predicate<ArtifactId> predicate = new Predicate<ArtifactId>() {

        @Override
        public boolean apply(ArtifactId input) {
            // by default, the scoped namespace is for USER scope
            return (((pluginScope == null && NamespaceId.SYSTEM.equals(input.getParent())) || pluginArtifactNamespace.equals(input.getParent())) && (pluginArtifactName == null || pluginArtifactName.equals(input.getArtifact())) && (pluginRange == null || pluginRange.versionIsInRange(new ArtifactVersion(input.getVersion()))));
        }
    };
    try {
        SortedMap<ArtifactDescriptor, PluginClass> plugins = artifactRepository.getPlugins(namespace, Id.Artifact.fromEntityId(parentArtifactId), pluginType, pluginName, predicate, limitNumber, sortOrder);
        List<PluginInfo> pluginInfos = Lists.newArrayList();
        // flatten the map
        for (Map.Entry<ArtifactDescriptor, PluginClass> pluginsEntry : plugins.entrySet()) {
            ArtifactDescriptor pluginArtifact = pluginsEntry.getKey();
            ArtifactSummary pluginArtifactSummary = ArtifactSummary.from(pluginArtifact.getArtifactId());
            PluginClass pluginClass = pluginsEntry.getValue();
            try {
                capabilityReader.checkAllEnabled(pluginClass.getRequirements().getCapabilities());
            } catch (CapabilityNotAvailableException e) {
                LOG.debug("Skipping plugin {} because of disabled capability", pluginClass, e);
                continue;
            }
            pluginInfos.add(new PluginInfo(pluginClass, pluginArtifactSummary));
        }
        responder.sendJson(HttpResponseStatus.OK, GSON.toJson(pluginInfos));
    } catch (PluginNotExistsException e) {
        responder.sendString(HttpResponseStatus.NOT_FOUND, e.getMessage());
    } catch (IOException e) {
        LOG.error("Exception looking up plugins for artifact {}", parentArtifactId, e);
        responder.sendString(HttpResponseStatus.INTERNAL_SERVER_ERROR, "Error reading plugins for the artifact from the store.");
    }
}
Also used : CapabilityNotAvailableException(io.cdap.cdap.internal.capability.CapabilityNotAvailableException) ArtifactId(io.cdap.cdap.proto.id.ArtifactId) ArtifactVersionRange(io.cdap.cdap.api.artifact.ArtifactVersionRange) IOException(java.io.IOException) ArtifactSortOrder(io.cdap.cdap.proto.artifact.ArtifactSortOrder) Predicate(com.google.common.base.Predicate) PluginNotExistsException(io.cdap.cdap.internal.app.runtime.plugin.PluginNotExistsException) ArtifactVersion(io.cdap.cdap.api.artifact.ArtifactVersion) ArtifactSummary(io.cdap.cdap.api.artifact.ArtifactSummary) ArtifactDescriptor(io.cdap.cdap.internal.app.runtime.artifact.ArtifactDescriptor) PluginInfo(io.cdap.cdap.proto.artifact.PluginInfo) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) PluginClass(io.cdap.cdap.api.plugin.PluginClass) Map(java.util.Map) SortedMap(java.util.SortedMap) HashMap(java.util.HashMap) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 8 with ArtifactVersionRange

use of io.cdap.cdap.api.artifact.ArtifactVersionRange in project cdap by caskdata.

the class DefaultApplicationUpdateContext method getScopedPluginArtifacts.

private List<ArtifactId> getScopedPluginArtifacts(String pluginType, String pluginName, ArtifactScope pluginScope, @Nullable ArtifactVersionRange pluginRange, int limit) throws Exception {
    List<ArtifactId> pluginArtifacts = new ArrayList<>();
    NamespaceId pluginArtifactNamespace = ArtifactScope.SYSTEM.equals(pluginScope) ? NamespaceId.SYSTEM : namespaceId;
    Predicate<io.cdap.cdap.proto.id.ArtifactId> predicate = input -> {
        // Check if it is from the scoped namespace and should check if plugin is in given range if provided.
        return (pluginArtifactNamespace.equals(input.getParent()) && (pluginRange == null || pluginRange.versionIsInRange(new ArtifactVersion(input.getVersion()))));
    };
    try {
        // TODO: Pass ArtifactSortOrder as argument for better flexibility.
        Map<ArtifactDescriptor, PluginClass> plugins = artifactRepository.getPlugins(pluginArtifactNamespace, Artifact.from(Namespace.fromEntityId(namespaceId), applicationArtifactId), pluginType, pluginName, predicate, limit, ArtifactSortOrder.ASC);
        for (Map.Entry<ArtifactDescriptor, PluginClass> pluginsEntry : plugins.entrySet()) {
            ArtifactId plugin = pluginsEntry.getKey().getArtifactId();
            // Consider if it is a non-snapshot version artifact or it is a snapshot version than allowSnapshot is true.
            if ((plugin.getVersion().isSnapshot() && allowSnapshot) || !plugin.getVersion().isSnapshot()) {
                pluginArtifacts.add(plugin);
            }
        }
    } catch (PluginNotExistsException e) {
        LOG.trace("No plugin found for plugin {} of type {} in scope {} for app {}", pluginName, pluginType, pluginScope, applicationId, e);
        return Collections.emptyList();
    } catch (Exception e) {
        throw e;
    }
    return pluginArtifacts;
}
Also used : NamespaceId(io.cdap.cdap.proto.id.NamespaceId) LoggerFactory(org.slf4j.LoggerFactory) TypeToken(com.google.common.reflect.TypeToken) GsonBuilder(com.google.gson.GsonBuilder) ArrayList(java.util.ArrayList) PluginNotExistsException(io.cdap.cdap.internal.app.runtime.plugin.PluginNotExistsException) ArtifactVersionRange(io.cdap.cdap.api.artifact.ArtifactVersionRange) Artifact(io.cdap.cdap.common.id.Id.Artifact) Gson(com.google.gson.Gson) ArtifactSortOrder(io.cdap.cdap.proto.artifact.ArtifactSortOrder) Map(java.util.Map) Nullable(javax.annotation.Nullable) ArtifactDescriptor(io.cdap.cdap.internal.app.runtime.artifact.ArtifactDescriptor) Logger(org.slf4j.Logger) JsonSyntaxException(com.google.gson.JsonSyntaxException) ApplicationUpdateContext(io.cdap.cdap.api.app.ApplicationUpdateContext) Config(io.cdap.cdap.api.Config) Set(java.util.Set) PluginClass(io.cdap.cdap.api.plugin.PluginClass) List(java.util.List) Predicate(com.google.common.base.Predicate) Type(java.lang.reflect.Type) ApplicationConfigUpdateAction(io.cdap.cdap.api.app.ApplicationConfigUpdateAction) ArtifactVersion(io.cdap.cdap.api.artifact.ArtifactVersion) CaseInsensitiveEnumTypeAdapterFactory(io.cdap.cdap.common.io.CaseInsensitiveEnumTypeAdapterFactory) ArtifactRepository(io.cdap.cdap.internal.app.runtime.artifact.ArtifactRepository) Preconditions(com.google.common.base.Preconditions) ApplicationId(io.cdap.cdap.proto.id.ApplicationId) Collections(java.util.Collections) ArtifactId(io.cdap.cdap.api.artifact.ArtifactId) ArtifactScope(io.cdap.cdap.api.artifact.ArtifactScope) Namespace(io.cdap.cdap.common.id.Id.Namespace) ArtifactId(io.cdap.cdap.api.artifact.ArtifactId) ArrayList(java.util.ArrayList) PluginNotExistsException(io.cdap.cdap.internal.app.runtime.plugin.PluginNotExistsException) JsonSyntaxException(com.google.gson.JsonSyntaxException) PluginNotExistsException(io.cdap.cdap.internal.app.runtime.plugin.PluginNotExistsException) ArtifactVersion(io.cdap.cdap.api.artifact.ArtifactVersion) ArtifactDescriptor(io.cdap.cdap.internal.app.runtime.artifact.ArtifactDescriptor) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) PluginClass(io.cdap.cdap.api.plugin.PluginClass) Map(java.util.Map)

Example 9 with ArtifactVersionRange

use of io.cdap.cdap.api.artifact.ArtifactVersionRange in project cdap by caskdata.

the class ArtifactSelectorTest method testSelection.

@SuppressWarnings("ConstantConditions")
@Test
public void testSelection() {
    SortedMap<ArtifactId, PluginClass> plugins = new TreeMap<>();
    // doesn't matter what this is, since we only select on artifact id.
    PluginClass pluginClass = PluginClass.builder().setName("name").setType("type").setDescription("desc").setClassName("com.company.class").setConfigFieldName("field").setProperties(ImmutableMap.of()).build();
    // put every combination of abc or def as name, 1.0.0 or 2.0.0 as version, and system or user as scope
    plugins.put(new ArtifactId("abc", new ArtifactVersion("1.0.0"), ArtifactScope.SYSTEM), pluginClass);
    plugins.put(new ArtifactId("abc", new ArtifactVersion("2.0.0"), ArtifactScope.SYSTEM), pluginClass);
    plugins.put(new ArtifactId("abc", new ArtifactVersion("1.0.0"), ArtifactScope.USER), pluginClass);
    plugins.put(new ArtifactId("abc", new ArtifactVersion("2.0.0"), ArtifactScope.USER), pluginClass);
    plugins.put(new ArtifactId("def", new ArtifactVersion("1.0.0"), ArtifactScope.SYSTEM), pluginClass);
    plugins.put(new ArtifactId("def", new ArtifactVersion("2.0.0"), ArtifactScope.SYSTEM), pluginClass);
    plugins.put(new ArtifactId("def", new ArtifactVersion("1.0.0"), ArtifactScope.USER), pluginClass);
    plugins.put(new ArtifactId("def", new ArtifactVersion("2.0.0"), ArtifactScope.USER), pluginClass);
    // test scope only
    ArtifactSelector selector = new ArtifactSelector(ArtifactScope.SYSTEM, null, null);
    Assert.assertEquals(new ArtifactId("def", new ArtifactVersion("2.0.0"), ArtifactScope.SYSTEM), selector.select(plugins).getKey());
    selector = new ArtifactSelector(ArtifactScope.USER, null, null);
    Assert.assertEquals(new ArtifactId("def", new ArtifactVersion("2.0.0"), ArtifactScope.USER), selector.select(plugins).getKey());
    // test name only
    selector = new ArtifactSelector(null, "abc", null);
    Assert.assertEquals(new ArtifactId("abc", new ArtifactVersion("2.0.0"), ArtifactScope.USER), selector.select(plugins).getKey());
    selector = new ArtifactSelector(null, "def", null);
    Assert.assertEquals(new ArtifactId("def", new ArtifactVersion("2.0.0"), ArtifactScope.USER), selector.select(plugins).getKey());
    selector = new ArtifactSelector(null, "xyz", null);
    Assert.assertNull(selector.select(plugins));
    // test version only
    selector = new ArtifactSelector(null, null, new ArtifactVersionRange(new ArtifactVersion("1.0.0"), true, new ArtifactVersion("1.0.0"), true));
    Assert.assertEquals(new ArtifactId("def", new ArtifactVersion("1.0.0"), ArtifactScope.USER), selector.select(plugins).getKey());
    selector = new ArtifactSelector(null, null, new ArtifactVersionRange(new ArtifactVersion("2.0.0"), true, new ArtifactVersion("2.0.0"), true));
    Assert.assertEquals(new ArtifactId("def", new ArtifactVersion("2.0.0"), ArtifactScope.USER), selector.select(plugins).getKey());
    selector = new ArtifactSelector(null, null, new ArtifactVersionRange(new ArtifactVersion("3.0.0"), true, new ArtifactVersion("3.0.0"), true));
    Assert.assertNull(selector.select(plugins));
    // test range only
    selector = new ArtifactSelector(null, null, new ArtifactVersionRange(new ArtifactVersion("1.0.0-SNAPSHOT"), true, new ArtifactVersion("2.0.0"), false));
    Assert.assertEquals(new ArtifactId("def", new ArtifactVersion("1.0.0"), ArtifactScope.USER), selector.select(plugins).getKey());
    selector = new ArtifactSelector(null, null, new ArtifactVersionRange(new ArtifactVersion("1.0.0-SNAPSHOT"), true, new ArtifactVersion("2.0.0"), true));
    Assert.assertEquals(new ArtifactId("def", new ArtifactVersion("2.0.0"), ArtifactScope.USER), selector.select(plugins).getKey());
    selector = new ArtifactSelector(null, null, new ArtifactVersionRange(new ArtifactVersion("2.0.0"), false, new ArtifactVersion("3.0.0"), true));
    Assert.assertNull(selector.select(plugins));
    // test name + version
    selector = new ArtifactSelector(null, "abc", new ArtifactVersionRange(new ArtifactVersion("1.0.0"), true, new ArtifactVersion("1.0.0"), true));
    Assert.assertEquals(new ArtifactId("abc", new ArtifactVersion("1.0.0"), ArtifactScope.USER), selector.select(plugins).getKey());
    selector = new ArtifactSelector(null, "abc", new ArtifactVersionRange(new ArtifactVersion("2.0.0"), true, new ArtifactVersion("2.0.0"), true));
    Assert.assertEquals(new ArtifactId("abc", new ArtifactVersion("2.0.0"), ArtifactScope.USER), selector.select(plugins).getKey());
    selector = new ArtifactSelector(null, "def", new ArtifactVersionRange(new ArtifactVersion("1.0.0"), true, new ArtifactVersion("1.0.0"), true));
    Assert.assertEquals(new ArtifactId("def", new ArtifactVersion("1.0.0"), ArtifactScope.USER), selector.select(plugins).getKey());
    selector = new ArtifactSelector(null, "def", new ArtifactVersionRange(new ArtifactVersion("2.0.0"), true, new ArtifactVersion("2.0.0"), true));
    Assert.assertEquals(new ArtifactId("def", new ArtifactVersion("2.0.0"), ArtifactScope.USER), selector.select(plugins).getKey());
    selector = new ArtifactSelector(null, "xyz", new ArtifactVersionRange(new ArtifactVersion("1.0.0"), true, new ArtifactVersion("1.0.0"), true));
    Assert.assertNull(selector.select(plugins));
    selector = new ArtifactSelector(null, "abc", new ArtifactVersionRange(new ArtifactVersion("3.0.0"), true, new ArtifactVersion("3.0.0"), true));
    Assert.assertNull(selector.select(plugins));
    // test name + scope
    selector = new ArtifactSelector(ArtifactScope.SYSTEM, "abc", null);
    Assert.assertEquals(new ArtifactId("abc", new ArtifactVersion("2.0.0"), ArtifactScope.SYSTEM), selector.select(plugins).getKey());
    selector = new ArtifactSelector(ArtifactScope.SYSTEM, "def", null);
    Assert.assertEquals(new ArtifactId("def", new ArtifactVersion("2.0.0"), ArtifactScope.SYSTEM), selector.select(plugins).getKey());
    selector = new ArtifactSelector(ArtifactScope.USER, "abc", null);
    Assert.assertEquals(new ArtifactId("abc", new ArtifactVersion("2.0.0"), ArtifactScope.USER), selector.select(plugins).getKey());
    selector = new ArtifactSelector(ArtifactScope.USER, "def", null);
    Assert.assertEquals(new ArtifactId("def", new ArtifactVersion("2.0.0"), ArtifactScope.USER), selector.select(plugins).getKey());
    selector = new ArtifactSelector(ArtifactScope.SYSTEM, "xyz", null);
    Assert.assertNull(selector.select(plugins));
    // test version + scope
    selector = new ArtifactSelector(ArtifactScope.SYSTEM, null, new ArtifactVersionRange(new ArtifactVersion("1.0.0"), true, new ArtifactVersion("1.0.0"), true));
    Assert.assertEquals(new ArtifactId("def", new ArtifactVersion("1.0.0"), ArtifactScope.SYSTEM), selector.select(plugins).getKey());
    selector = new ArtifactSelector(ArtifactScope.SYSTEM, null, new ArtifactVersionRange(new ArtifactVersion("2.0.0"), true, new ArtifactVersion("2.0.0"), true));
    Assert.assertEquals(new ArtifactId("def", new ArtifactVersion("2.0.0"), ArtifactScope.SYSTEM), selector.select(plugins).getKey());
    selector = new ArtifactSelector(ArtifactScope.USER, null, new ArtifactVersionRange(new ArtifactVersion("1.0.0"), true, new ArtifactVersion("1.0.0"), true));
    Assert.assertEquals(new ArtifactId("def", new ArtifactVersion("1.0.0"), ArtifactScope.USER), selector.select(plugins).getKey());
    selector = new ArtifactSelector(ArtifactScope.USER, null, new ArtifactVersionRange(new ArtifactVersion("2.0.0"), true, new ArtifactVersion("2.0.0"), true));
    Assert.assertEquals(new ArtifactId("def", new ArtifactVersion("2.0.0"), ArtifactScope.USER), selector.select(plugins).getKey());
    selector = new ArtifactSelector(ArtifactScope.SYSTEM, null, new ArtifactVersionRange(new ArtifactVersion("3.0.0"), true, new ArtifactVersion("3.0.0"), true));
    Assert.assertNull(selector.select(plugins));
    // test name + range
    selector = new ArtifactSelector(null, "abc", new ArtifactVersionRange(new ArtifactVersion("1.0.0-SNAPSHOT"), true, new ArtifactVersion("2.0.0"), false));
    Assert.assertEquals(new ArtifactId("abc", new ArtifactVersion("1.0.0"), ArtifactScope.USER), selector.select(plugins).getKey());
    selector = new ArtifactSelector(null, "abc", new ArtifactVersionRange(new ArtifactVersion("1.0.0-SNAPSHOT"), true, new ArtifactVersion("2.0.0"), true));
    Assert.assertEquals(new ArtifactId("abc", new ArtifactVersion("2.0.0"), ArtifactScope.USER), selector.select(plugins).getKey());
    selector = new ArtifactSelector(null, "def", new ArtifactVersionRange(new ArtifactVersion("2.0.0"), false, new ArtifactVersion("3.0.0"), true));
    Assert.assertNull(selector.select(plugins));
    // test scope + range
    selector = new ArtifactSelector(ArtifactScope.SYSTEM, null, new ArtifactVersionRange(new ArtifactVersion("1.0.0-SNAPSHOT"), true, new ArtifactVersion("2.0.0"), false));
    Assert.assertEquals(new ArtifactId("def", new ArtifactVersion("1.0.0"), ArtifactScope.SYSTEM), selector.select(plugins).getKey());
    selector = new ArtifactSelector(ArtifactScope.USER, null, new ArtifactVersionRange(new ArtifactVersion("1.0.0-SNAPSHOT"), true, new ArtifactVersion("2.0.0"), false));
    Assert.assertEquals(new ArtifactId("def", new ArtifactVersion("1.0.0"), ArtifactScope.USER), selector.select(plugins).getKey());
    selector = new ArtifactSelector(ArtifactScope.SYSTEM, null, new ArtifactVersionRange(new ArtifactVersion("1.0.0-SNAPSHOT"), true, new ArtifactVersion("2.0.0"), true));
    Assert.assertEquals(new ArtifactId("def", new ArtifactVersion("2.0.0"), ArtifactScope.SYSTEM), selector.select(plugins).getKey());
    selector = new ArtifactSelector(ArtifactScope.USER, null, new ArtifactVersionRange(new ArtifactVersion("1.0.0-SNAPSHOT"), true, new ArtifactVersion("2.0.0"), true));
    Assert.assertEquals(new ArtifactId("def", new ArtifactVersion("2.0.0"), ArtifactScope.USER), selector.select(plugins).getKey());
    selector = new ArtifactSelector(ArtifactScope.SYSTEM, null, new ArtifactVersionRange(new ArtifactVersion("2.0.0"), false, new ArtifactVersion("3.0.0"), true));
    Assert.assertNull(selector.select(plugins));
    // test name + version + scope
    selector = new ArtifactSelector(ArtifactScope.USER, "def", new ArtifactVersionRange(new ArtifactVersion("2.0.0"), true, new ArtifactVersion("2.0.0"), true));
    Assert.assertEquals(new ArtifactId("def", new ArtifactVersion("2.0.0"), ArtifactScope.USER), selector.select(plugins).getKey());
    selector = new ArtifactSelector(ArtifactScope.USER, "xyz", new ArtifactVersionRange(new ArtifactVersion("1.0.0"), true, new ArtifactVersion("1.0.0"), true));
    Assert.assertNull(selector.select(plugins));
    // test name + scope + range
    selector = new ArtifactSelector(ArtifactScope.SYSTEM, "abc", new ArtifactVersionRange(new ArtifactVersion("1.0.0-SNAPSHOT"), true, new ArtifactVersion("2.0.0"), false));
    Assert.assertEquals(new ArtifactId("abc", new ArtifactVersion("1.0.0"), ArtifactScope.SYSTEM), selector.select(plugins).getKey());
    selector = new ArtifactSelector(ArtifactScope.USER, "abc", new ArtifactVersionRange(new ArtifactVersion("1.0.0-SNAPSHOT"), true, new ArtifactVersion("2.0.0"), true));
    Assert.assertEquals(new ArtifactId("abc", new ArtifactVersion("2.0.0"), ArtifactScope.USER), selector.select(plugins).getKey());
    selector = new ArtifactSelector(ArtifactScope.SYSTEM, "def", new ArtifactVersionRange(new ArtifactVersion("1.0.0-SNAPSHOT"), true, new ArtifactVersion("2.0.0"), false));
    Assert.assertEquals(new ArtifactId("def", new ArtifactVersion("1.0.0"), ArtifactScope.SYSTEM), selector.select(plugins).getKey());
    selector = new ArtifactSelector(ArtifactScope.USER, "def", new ArtifactVersionRange(new ArtifactVersion("1.0.0-SNAPSHOT"), true, new ArtifactVersion("2.0.0"), true));
    Assert.assertEquals(new ArtifactId("def", new ArtifactVersion("2.0.0"), ArtifactScope.USER), selector.select(plugins).getKey());
    selector = new ArtifactSelector(ArtifactScope.SYSTEM, "abc", new ArtifactVersionRange(new ArtifactVersion("2.0.0"), false, new ArtifactVersion("3.0.0"), true));
    Assert.assertNull(selector.select(plugins));
}
Also used : ArtifactVersion(io.cdap.cdap.api.artifact.ArtifactVersion) ArtifactId(io.cdap.cdap.api.artifact.ArtifactId) ArtifactVersionRange(io.cdap.cdap.api.artifact.ArtifactVersionRange) PluginClass(io.cdap.cdap.api.plugin.PluginClass) TreeMap(java.util.TreeMap) Test(org.junit.Test)

Example 10 with ArtifactVersionRange

use of io.cdap.cdap.api.artifact.ArtifactVersionRange in project cdap by cdapio.

the class DefaultApplicationUpdateContext method getScopedPluginArtifacts.

private List<ArtifactId> getScopedPluginArtifacts(String pluginType, String pluginName, ArtifactScope pluginScope, @Nullable ArtifactVersionRange pluginRange, int limit) throws Exception {
    List<ArtifactId> pluginArtifacts = new ArrayList<>();
    NamespaceId pluginArtifactNamespace = ArtifactScope.SYSTEM.equals(pluginScope) ? NamespaceId.SYSTEM : namespaceId;
    Predicate<io.cdap.cdap.proto.id.ArtifactId> predicate = input -> {
        // Check if it is from the scoped namespace and should check if plugin is in given range if provided.
        return (pluginArtifactNamespace.equals(input.getParent()) && (pluginRange == null || pluginRange.versionIsInRange(new ArtifactVersion(input.getVersion()))));
    };
    try {
        // TODO: Pass ArtifactSortOrder as argument for better flexibility.
        Map<ArtifactDescriptor, PluginClass> plugins = artifactRepository.getPlugins(pluginArtifactNamespace, Artifact.from(Namespace.fromEntityId(namespaceId), applicationArtifactId), pluginType, pluginName, predicate, limit, ArtifactSortOrder.ASC);
        for (Map.Entry<ArtifactDescriptor, PluginClass> pluginsEntry : plugins.entrySet()) {
            ArtifactId plugin = pluginsEntry.getKey().getArtifactId();
            // Consider if it is a non-snapshot version artifact or it is a snapshot version than allowSnapshot is true.
            if ((plugin.getVersion().isSnapshot() && allowSnapshot) || !plugin.getVersion().isSnapshot()) {
                pluginArtifacts.add(plugin);
            }
        }
    } catch (PluginNotExistsException e) {
        LOG.trace("No plugin found for plugin {} of type {} in scope {} for app {}", pluginName, pluginType, pluginScope, applicationId, e);
        return Collections.emptyList();
    } catch (Exception e) {
        throw e;
    }
    return pluginArtifacts;
}
Also used : NamespaceId(io.cdap.cdap.proto.id.NamespaceId) LoggerFactory(org.slf4j.LoggerFactory) TypeToken(com.google.common.reflect.TypeToken) GsonBuilder(com.google.gson.GsonBuilder) ArrayList(java.util.ArrayList) PluginNotExistsException(io.cdap.cdap.internal.app.runtime.plugin.PluginNotExistsException) ArtifactVersionRange(io.cdap.cdap.api.artifact.ArtifactVersionRange) Artifact(io.cdap.cdap.common.id.Id.Artifact) Gson(com.google.gson.Gson) ArtifactSortOrder(io.cdap.cdap.proto.artifact.ArtifactSortOrder) Map(java.util.Map) Nullable(javax.annotation.Nullable) ArtifactDescriptor(io.cdap.cdap.internal.app.runtime.artifact.ArtifactDescriptor) Logger(org.slf4j.Logger) JsonSyntaxException(com.google.gson.JsonSyntaxException) ApplicationUpdateContext(io.cdap.cdap.api.app.ApplicationUpdateContext) Config(io.cdap.cdap.api.Config) Set(java.util.Set) PluginClass(io.cdap.cdap.api.plugin.PluginClass) List(java.util.List) Predicate(com.google.common.base.Predicate) Type(java.lang.reflect.Type) ApplicationConfigUpdateAction(io.cdap.cdap.api.app.ApplicationConfigUpdateAction) ArtifactVersion(io.cdap.cdap.api.artifact.ArtifactVersion) CaseInsensitiveEnumTypeAdapterFactory(io.cdap.cdap.common.io.CaseInsensitiveEnumTypeAdapterFactory) ArtifactRepository(io.cdap.cdap.internal.app.runtime.artifact.ArtifactRepository) Preconditions(com.google.common.base.Preconditions) ApplicationId(io.cdap.cdap.proto.id.ApplicationId) Collections(java.util.Collections) ArtifactId(io.cdap.cdap.api.artifact.ArtifactId) ArtifactScope(io.cdap.cdap.api.artifact.ArtifactScope) Namespace(io.cdap.cdap.common.id.Id.Namespace) ArtifactId(io.cdap.cdap.api.artifact.ArtifactId) ArrayList(java.util.ArrayList) PluginNotExistsException(io.cdap.cdap.internal.app.runtime.plugin.PluginNotExistsException) JsonSyntaxException(com.google.gson.JsonSyntaxException) PluginNotExistsException(io.cdap.cdap.internal.app.runtime.plugin.PluginNotExistsException) ArtifactVersion(io.cdap.cdap.api.artifact.ArtifactVersion) ArtifactDescriptor(io.cdap.cdap.internal.app.runtime.artifact.ArtifactDescriptor) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) PluginClass(io.cdap.cdap.api.plugin.PluginClass) Map(java.util.Map)

Aggregations

ArtifactVersionRange (io.cdap.cdap.api.artifact.ArtifactVersionRange)12 ArtifactVersion (io.cdap.cdap.api.artifact.ArtifactVersion)10 PluginClass (io.cdap.cdap.api.plugin.PluginClass)6 ArtifactSortOrder (io.cdap.cdap.proto.artifact.ArtifactSortOrder)6 NamespaceId (io.cdap.cdap.proto.id.NamespaceId)6 Predicate (com.google.common.base.Predicate)4 ArtifactId (io.cdap.cdap.api.artifact.ArtifactId)4 ArtifactScope (io.cdap.cdap.api.artifact.ArtifactScope)4 ArtifactDescriptor (io.cdap.cdap.internal.app.runtime.artifact.ArtifactDescriptor)4 PluginNotExistsException (io.cdap.cdap.internal.app.runtime.plugin.PluginNotExistsException)4 Map (java.util.Map)4 GET (javax.ws.rs.GET)4 Path (javax.ws.rs.Path)4 Preconditions (com.google.common.base.Preconditions)2 TypeToken (com.google.common.reflect.TypeToken)2 Gson (com.google.gson.Gson)2 GsonBuilder (com.google.gson.GsonBuilder)2 JsonSyntaxException (com.google.gson.JsonSyntaxException)2 Config (io.cdap.cdap.api.Config)2 ApplicationConfigUpdateAction (io.cdap.cdap.api.app.ApplicationConfigUpdateAction)2