Search in sources :

Example 71 with ArtifactRange

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

the class ArtifactClientTestRun method testAddSelfExtendingThrowsBadRequest.

@Test
public void testAddSelfExtendingThrowsBadRequest() throws Exception {
    try {
        artifactClient.add(NamespaceId.DEFAULT, "abc", DUMMY_SUPPLIER, "1.0.0", Sets.newHashSet(new ArtifactRange(NamespaceId.DEFAULT.getNamespace(), "abc", new ArtifactVersion("1.0.0"), new ArtifactVersion("2.0.0"))));
        Assert.fail();
    } catch (BadRequestException e) {
    // expected
    }
}
Also used : ArtifactVersion(io.cdap.cdap.api.artifact.ArtifactVersion) ArtifactRange(io.cdap.cdap.api.artifact.ArtifactRange) BadRequestException(io.cdap.cdap.common.BadRequestException) Test(org.junit.Test)

Example 72 with ArtifactRange

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

the class ArtifactHttpHandler method getArtifactVersions.

@GET
@Path("/namespaces/{namespace-id}/artifacts/{artifact-name}")
public void getArtifactVersions(HttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId, @PathParam("artifact-name") String artifactName, @QueryParam("scope") @DefaultValue("user") String scope, @QueryParam("artifactVersion") String versionRange, @QueryParam("limit") @DefaultValue("2147483647") String limit, @QueryParam("order") @DefaultValue("UNORDERED") String order) throws Exception {
    NamespaceId namespace = validateAndGetScopedNamespace(Ids.namespace(namespaceId), scope);
    ArtifactRange range = versionRange == null ? null : new ArtifactRange(namespaceId, artifactName, ArtifactVersionRange.parse(versionRange));
    int limitNumber = Integer.parseInt(limit);
    limitNumber = limitNumber <= 0 ? Integer.MAX_VALUE : limitNumber;
    ArtifactSortOrder sortOrder = ArtifactSortOrder.valueOf(order);
    try {
        if (range == null) {
            responder.sendJson(HttpResponseStatus.OK, GSON.toJson(artifactRepository.getArtifactSummaries(namespace, artifactName, limitNumber, sortOrder)));
        } else {
            responder.sendJson(HttpResponseStatus.OK, GSON.toJson(artifactRepository.getArtifactSummaries(range, limitNumber, sortOrder)));
        }
    } catch (IOException e) {
        LOG.error("Exception reading artifacts named {} for namespace {} from the store.", artifactName, namespaceId, e);
        responder.sendString(HttpResponseStatus.INTERNAL_SERVER_ERROR, "Error reading artifact metadata from the store.");
    }
}
Also used : ArtifactRange(io.cdap.cdap.api.artifact.ArtifactRange) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) IOException(java.io.IOException) ArtifactSortOrder(io.cdap.cdap.proto.artifact.ArtifactSortOrder) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 73 with ArtifactRange

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

the class ArtifactConfigReaderTest method testInvalidParentNamespace.

@Test(expected = InvalidArtifactException.class)
public void testInvalidParentNamespace() throws IOException, InvalidArtifactException {
    ArtifactConfig badConfig = new ArtifactConfig(ImmutableSet.of(new ArtifactRange(NamespaceId.DEFAULT.getNamespace(), "b", new ArtifactVersion("1.0.0"), new ArtifactVersion("2.0.0"))), ImmutableSet.<PluginClass>of(), ImmutableMap.<String, String>of());
    File configFile = new File(tmpFolder.newFolder(), "r1-1.0.0.json");
    try (BufferedWriter writer = Files.newWriter(configFile, Charsets.UTF_8)) {
        writer.write(badConfig.toString());
    }
    configReader.read(Id.Namespace.SYSTEM, configFile);
}
Also used : ArtifactVersion(io.cdap.cdap.api.artifact.ArtifactVersion) ArtifactRange(io.cdap.cdap.api.artifact.ArtifactRange) File(java.io.File) BufferedWriter(java.io.BufferedWriter) Test(org.junit.Test)

Example 74 with ArtifactRange

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

the class HubPackage method installPlugin.

/**
 * Downloads the plugin from the given URL and installs it in the artifact repository
 *
 * @param url URL that points to the plugin directory on the hub
 * @param artifactRepository {@link ArtifactRepository} in which the plugin will be installed
 * @param tmpDir temporary directory where plugin jar is downloaded from the hub
 */
public void installPlugin(URL url, ArtifactRepository artifactRepository, File tmpDir) throws Exception {
    // Deserialize spec.json
    URL specURL = new URL(url.getProtocol(), url.getHost(), url.getPort(), url.getPath() + "/packages/" + name + "/" + version + "/spec.json");
    Spec spec = GSON.fromJson(HttpClients.doGetAsString(specURL), Spec.class);
    for (Spec.Action action : spec.getActions()) {
        // See https://cdap.atlassian.net/wiki/spaces/DOCS/pages/554401840/Hub+API?src=search#one_step_deploy_plugin
        if (!action.getType().equals("one_step_deploy_plugin")) {
            continue;
        }
        String configFilename = action.getConfigFilename();
        if (configFilename == null) {
            LOG.warn("Ignoring plugin {} due to missing config", name);
            continue;
        }
        URL configURL = new URL(url.getProtocol(), url.getHost(), url.getPort(), url.getPath() + Joiner.on("/").join(Arrays.asList("/packages", name, version, configFilename)));
        // Download plugin json from hub
        JsonObject jsonObj = GSON.fromJson(HttpClients.doGetAsString(configURL), JsonObject.class);
        List<String> parents = GSON.fromJson(jsonObj.get("parents"), new TypeToken<List<String>>() {
        }.getType());
        String jarName = action.getJarName();
        if (jarName == null) {
            LOG.warn("Ignoring plugin {} due to missing jar", name);
            continue;
        }
        // Download plugin jar from hub
        File destination = File.createTempFile("artifact-", ".jar", tmpDir);
        FileChannel channel = new FileOutputStream(destination, false).getChannel();
        URL jarURL = new URL(url.getProtocol(), url.getHost(), url.getPort(), url.getPath() + Joiner.on("/").join(Arrays.asList("/packages", name, version, jarName)));
        HttpRequest request = HttpRequest.get(jarURL).withContentConsumer(new HttpContentConsumer() {

            @Override
            public boolean onReceived(ByteBuffer buffer) {
                try {
                    channel.write(buffer);
                } catch (IOException e) {
                    LOG.error("Failed write to file {}", destination);
                    return false;
                }
                return true;
            }

            @Override
            public void onFinished() {
                Closeables.closeQuietly(channel);
            }
        }).build();
        HttpClients.executeStreamingRequest(request);
        Set<ArtifactRange> parentArtifacts = new HashSet<>();
        for (String parent : parents) {
            try {
                // try parsing it as a namespaced range like system:cdap-data-pipeline[6.3 1.1,7.0.0)
                parentArtifacts.add(ArtifactRanges.parseArtifactRange(parent));
            } catch (InvalidArtifactRangeException e) {
                // if this failed, try parsing as a non-namespaced range like cdap-data-pipeline[6.3 1.1,7.0.0)
                parentArtifacts.add(ArtifactRanges.parseArtifactRange(NamespaceId.DEFAULT.getNamespace(), parent));
            }
        }
        // add the artifact to the repo
        io.cdap.cdap.proto.id.ArtifactId artifactId = NamespaceId.DEFAULT.artifact(name, version);
        Id.Artifact artifact = Id.Artifact.fromEntityId(artifactId);
        try {
            artifactRepository.addArtifact(artifact, destination, parentArtifacts, ImmutableSet.of());
        } catch (ArtifactAlreadyExistsException e) {
            LOG.debug("Artifact artifact {}-{} already exists", name, version);
        }
        Map<String, String> properties = GSON.fromJson(jsonObj.get("properties"), new TypeToken<Map<String, String>>() {
        }.getType());
        artifactRepository.writeArtifactProperties(Id.Artifact.fromEntityId(artifactId), properties);
        if (!java.nio.file.Files.deleteIfExists(Paths.get(destination.getPath()))) {
            LOG.warn("Failed to cleanup file {}", destination);
        }
    }
}
Also used : HttpContentConsumer(io.cdap.common.http.HttpContentConsumer) ArtifactRange(io.cdap.cdap.api.artifact.ArtifactRange) JsonObject(com.google.gson.JsonObject) URL(java.net.URL) ArtifactAlreadyExistsException(io.cdap.cdap.common.ArtifactAlreadyExistsException) HashSet(java.util.HashSet) HttpRequest(io.cdap.common.http.HttpRequest) FileChannel(java.nio.channels.FileChannel) InvalidArtifactRangeException(io.cdap.cdap.api.artifact.InvalidArtifactRangeException) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) TypeToken(io.cdap.cdap.internal.guava.reflect.TypeToken) FileOutputStream(java.io.FileOutputStream) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) Id(io.cdap.cdap.common.id.Id) File(java.io.File)

Example 75 with ArtifactRange

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

the class AbstractTestManager method toRange.

private Set<ArtifactRange> toRange(ArtifactId parent) {
    Set<ArtifactRange> parents = new HashSet<>();
    parents.add(new ArtifactRange(parent.getParent().getNamespace(), parent.getArtifact(), new ArtifactVersion(parent.getVersion()), true, new ArtifactVersion(parent.getVersion()), true));
    return parents;
}
Also used : ArtifactVersion(io.cdap.cdap.api.artifact.ArtifactVersion) ArtifactRange(io.cdap.cdap.api.artifact.ArtifactRange) HashSet(java.util.HashSet)

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