Search in sources :

Example 96 with ArtifactRange

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

the class ArtifactRepositoryTest method testPluginMetadata.

@Test
public void testPluginMetadata() throws Exception {
    // Create a plugin jar. It contains two plugins, TestPlugin and TestPlugin2 inside.
    Id.Artifact artifact1Id = Id.Artifact.from(Id.Namespace.DEFAULT, "myPlugin", "1.0");
    Manifest manifest = createManifest(ManifestFields.EXPORT_PACKAGE, TestPlugin.class.getPackage().getName());
    File jarFile = createPluginJar(TestPlugin.class, new File(tmpDir, "myPlugin-1.0.jar"), manifest);
    // Build up the plugin repository.
    Set<ArtifactRange> parents = ImmutableSet.of(new ArtifactRange(APP_ARTIFACT_ID.getNamespace().getId(), APP_ARTIFACT_ID.getName(), new ArtifactVersion("1.0.0"), new ArtifactVersion("2.0.0")));
    artifactRepository.addArtifact(artifact1Id, jarFile, parents, null);
    PluginId testPlugin1 = new PluginId("default", "myPlugin", "1.0", "TestPlugin", "plugin");
    Metadata expected = new Metadata(MetadataScope.SYSTEM, ImmutableSet.of("tag1", "tag2", "tag3"), ImmutableMap.of("k1", "v1", "k2", "v2"));
    Assert.assertEquals(expected, metadataAdmin.getMetadata(testPlugin1.toMetadataEntity()));
    PluginId testPlugin2 = new PluginId("default", "myPlugin", "1.0", "TestPlugin2", "plugin");
    expected = new Metadata(MetadataScope.SYSTEM, ImmutableSet.of("test-tag1", "test-tag2", "test-tag3"), ImmutableMap.of("key1", "val1", "key2", "val2"));
    Assert.assertEquals(expected, metadataAdmin.getMetadata(testPlugin2.toMetadataEntity()));
    // test metadata is cleaned up when the artifact gets deleted
    artifactRepository.deleteArtifact(artifact1Id);
    Assert.assertEquals(Metadata.EMPTY, metadataAdmin.getMetadata(testPlugin1.toMetadataEntity()));
    Assert.assertEquals(Metadata.EMPTY, metadataAdmin.getMetadata(testPlugin2.toMetadataEntity()));
}
Also used : ArtifactVersion(io.cdap.cdap.api.artifact.ArtifactVersion) ArtifactRange(io.cdap.cdap.api.artifact.ArtifactRange) Metadata(io.cdap.cdap.spi.metadata.Metadata) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) PluginId(io.cdap.cdap.proto.id.PluginId) Id(io.cdap.cdap.common.id.Id) ArtifactId(io.cdap.cdap.api.artifact.ArtifactId) Manifest(java.util.jar.Manifest) PluginId(io.cdap.cdap.proto.id.PluginId) File(java.io.File) Test(org.junit.Test)

Example 97 with ArtifactRange

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

the class ArtifactRepositoryTest method testSelfExtendingArtifact.

@Test(expected = InvalidArtifactException.class)
public void testSelfExtendingArtifact() throws InvalidArtifactException {
    Id.Artifact child = Id.Artifact.from(Id.Namespace.SYSTEM, "abc", "1.0.0");
    DefaultArtifactRepository.validateParentSet(child, ImmutableSet.of(new ArtifactRange(NamespaceId.SYSTEM.getNamespace(), "abc", new ArtifactVersion("1.0.0"), new ArtifactVersion("2.0.0"))));
}
Also used : ArtifactVersion(io.cdap.cdap.api.artifact.ArtifactVersion) ArtifactRange(io.cdap.cdap.api.artifact.ArtifactRange) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) PluginId(io.cdap.cdap.proto.id.PluginId) Id(io.cdap.cdap.common.id.Id) ArtifactId(io.cdap.cdap.api.artifact.ArtifactId) Test(org.junit.Test)

Example 98 with ArtifactRange

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

the class ArtifactHttpHandler method getArtifactProperties.

@POST
@Path("/namespaces/{namespace-id}/artifactproperties")
public void getArtifactProperties(FullHttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId, @QueryParam("order") @DefaultValue("DESC") String order) throws Exception {
    NamespaceId namespace = validateAndGetNamespace(namespaceId);
    ArtifactSortOrder sortOrder = ArtifactSortOrder.valueOf(order);
    List<ArtifactPropertiesRequest> propertyRequests;
    try (Reader reader = new InputStreamReader(new ByteBufInputStream(request.content()), StandardCharsets.UTF_8)) {
        propertyRequests = GSON.fromJson(reader, BATCH_ARTIFACT_PROPERTIES_REQUEST);
    } catch (JsonSyntaxException e) {
        throw new BadRequestException("Unable to parse request: " + e.getMessage(), e);
    }
    List<ArtifactSummaryProperties> result = new ArrayList<>(propertyRequests.size());
    for (ArtifactPropertiesRequest propertiesRequest : propertyRequests) {
        NamespaceId requestNamespace = propertiesRequest.getScope() == ArtifactScope.SYSTEM ? NamespaceId.SYSTEM : namespace;
        ArtifactRange range = new ArtifactRange(requestNamespace.getNamespace(), propertiesRequest.getName(), ArtifactVersionRange.parse(propertiesRequest.getVersion()));
        List<ArtifactDetail> artifactDetails = artifactRepository.getArtifactDetails(range, 1, sortOrder);
        for (ArtifactDetail artifactDetail : artifactDetails) {
            Map<String, String> properties = artifactDetail.getMeta().getProperties();
            Map<String, String> filteredProperties = new HashMap<>(propertiesRequest.getProperties().size());
            for (String propertyKey : propertiesRequest.getProperties()) {
                if (properties.containsKey(propertyKey)) {
                    filteredProperties.put(propertyKey, properties.get(propertyKey));
                }
            }
            String artifactVersion = artifactDetail.getDescriptor().getArtifactId().getVersion().getVersion();
            result.add(new ArtifactSummaryProperties(propertiesRequest.getName(), artifactVersion, propertiesRequest.getScope(), filteredProperties));
        }
    }
    responder.sendJson(HttpResponseStatus.OK, GSON.toJson(result, BATCH_ARTIFACT_PROPERTIES_RESPONSE));
}
Also used : InputStreamReader(java.io.InputStreamReader) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ArtifactRange(io.cdap.cdap.api.artifact.ArtifactRange) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) CapabilityReader(io.cdap.cdap.internal.capability.CapabilityReader) ByteBufInputStream(io.netty.buffer.ByteBufInputStream) ArtifactSortOrder(io.cdap.cdap.proto.artifact.ArtifactSortOrder) JsonSyntaxException(com.google.gson.JsonSyntaxException) ArtifactPropertiesRequest(io.cdap.cdap.proto.artifact.ArtifactPropertiesRequest) BadRequestException(io.cdap.cdap.common.BadRequestException) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) ArtifactSummaryProperties(io.cdap.cdap.proto.artifact.ArtifactSummaryProperties) ArtifactDetail(io.cdap.cdap.internal.app.runtime.artifact.ArtifactDetail) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST)

Example 99 with ArtifactRange

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

the class ArtifactHttpHandler method parseExtendsHeader.

// find out if this artifact extends other artifacts. If so, there will be a header like
// 'Artifact-Extends: <name>[<lowerversion>,<upperversion>]/<name>[<lowerversion>,<upperversion>]:
// for example: 'Artifact-Extends: etl-batch[1.0.0,2.0.0]/etl-realtime[1.0.0:3.0.0]
private Set<ArtifactRange> parseExtendsHeader(NamespaceId namespace, String extendsHeader) throws BadRequestException {
    Set<ArtifactRange> parentArtifacts = Sets.newHashSet();
    if (extendsHeader != null && !extendsHeader.isEmpty()) {
        for (String parent : Splitter.on('/').split(extendsHeader)) {
            parent = parent.trim();
            ArtifactRange range;
            // try parsing it as a namespaced range like system:etl-batch[1.0.0,2.0.0)
            try {
                range = ArtifactRanges.parseArtifactRange(parent);
                // only support extending an artifact that is in the same namespace, or system namespace
                if (!NamespaceId.SYSTEM.getNamespace().equals(range.getNamespace()) && !namespace.getNamespace().equals(range.getNamespace())) {
                    throw new BadRequestException(String.format("Parent artifact %s must be in the same namespace or a system artifact.", parent));
                }
            } catch (InvalidArtifactRangeException e) {
                // if this failed, try parsing as a non-namespaced range like etl-batch[1.0.0,2.0.0)
                try {
                    range = ArtifactRanges.parseArtifactRange(namespace.getNamespace(), parent);
                } catch (InvalidArtifactRangeException e1) {
                    throw new BadRequestException(String.format("Invalid artifact range %s: %s", parent, e1.getMessage()));
                }
            }
            parentArtifacts.add(range);
        }
    }
    return parentArtifacts;
}
Also used : ArtifactRange(io.cdap.cdap.api.artifact.ArtifactRange) InvalidArtifactRangeException(io.cdap.cdap.api.artifact.InvalidArtifactRangeException) BadRequestException(io.cdap.cdap.common.BadRequestException)

Example 100 with ArtifactRange

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

the class ArtifactHttpHandlerInternal method getArtifactDetailForVersions.

@GET
@Path("/namespaces/{namespace-id}/artifacts/{artifact-name}/versions")
public void getArtifactDetailForVersions(HttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespace, @PathParam("artifact-name") String artifactName, @QueryParam("lower") String lower, @QueryParam("upper") String upper, @QueryParam("limit") @DefaultValue("1") int limit, @QueryParam("order") String order, @QueryParam("scope") @DefaultValue("user") String scope) throws Exception {
    NamespaceId namespaceId = new NamespaceId(namespace);
    if (!namespaceId.equals(NamespaceId.SYSTEM)) {
        if (!namespaceQueryAdmin.exists(namespaceId)) {
            throw new NamespaceNotFoundException(namespaceId);
        }
    }
    ArtifactRange range = new ArtifactRange(namespaceId.getNamespace(), artifactName, new ArtifactVersionRange(new ArtifactVersion(lower), true, new ArtifactVersion(upper), true));
    ArtifactSortOrder sortOrder = ArtifactSortOrder.valueOf(order);
    List<ArtifactDetail> artifactDetailList = artifactRepository.getArtifactDetails(range, limit, sortOrder);
    responder.sendJson(HttpResponseStatus.OK, GSON.toJson(artifactDetailList, ARTIFACT_DETAIL_LIST_TYPE));
}
Also used : ArtifactVersion(io.cdap.cdap.api.artifact.ArtifactVersion) ArtifactVersionRange(io.cdap.cdap.api.artifact.ArtifactVersionRange) ArtifactRange(io.cdap.cdap.api.artifact.ArtifactRange) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) NamespaceNotFoundException(io.cdap.cdap.common.NamespaceNotFoundException) ArtifactSortOrder(io.cdap.cdap.proto.artifact.ArtifactSortOrder) ArtifactDetail(io.cdap.cdap.internal.app.runtime.artifact.ArtifactDetail) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Aggregations

ArtifactRange (io.cdap.cdap.api.artifact.ArtifactRange)107 ArtifactVersion (io.cdap.cdap.api.artifact.ArtifactVersion)77 Test (org.junit.Test)68 NamespaceId (io.cdap.cdap.proto.id.NamespaceId)54 PluginClass (io.cdap.cdap.api.plugin.PluginClass)42 Id (io.cdap.cdap.common.id.Id)40 ArtifactId (io.cdap.cdap.proto.id.ArtifactId)36 File (java.io.File)28 HashSet (java.util.HashSet)21 ImmutableSet (com.google.common.collect.ImmutableSet)20 ArtifactNotFoundException (io.cdap.cdap.common.ArtifactNotFoundException)20 Set (java.util.Set)20 Manifest (java.util.jar.Manifest)20 ArtifactId (io.cdap.cdap.api.artifact.ArtifactId)16 PluginNotExistsException (io.cdap.cdap.internal.app.runtime.plugin.PluginNotExistsException)14 PluginId (io.cdap.cdap.proto.id.PluginId)14 ApplicationClass (io.cdap.cdap.api.artifact.ApplicationClass)12 PluginPropertyField (io.cdap.cdap.api.plugin.PluginPropertyField)12 IOException (java.io.IOException)12 ArtifactDetail (io.cdap.cdap.internal.app.runtime.artifact.ArtifactDetail)10