Search in sources :

Example 11 with ArtifactNotFoundException

use of io.cdap.cdap.common.ArtifactNotFoundException in project cdap by caskdata.

the class ArtifactClient method deleteProperties.

/**
 * Delete all properties for an artifact. If no properties exist, this will be a no-op.
 *
 * @param artifactId the artifact to delete properties from
 * @throws BadRequestException if the request is invalid. For example, if the artifact name or version is invalid
 * @throws UnauthenticatedException if the request is not authorized successfully in the gateway server
 * @throws ArtifactNotFoundException if the artifact does not exist
 * @throws IOException if a network error occurred
 */
public void deleteProperties(ArtifactId artifactId) throws IOException, UnauthenticatedException, ArtifactNotFoundException, BadRequestException, UnauthorizedException {
    String path = String.format("artifacts/%s/versions/%s/properties", artifactId.getArtifact(), artifactId.getVersion());
    URL url = config.resolveNamespacedURLV3(artifactId.getParent(), path);
    HttpRequest.Builder requestBuilder = HttpRequest.delete(url);
    HttpRequest request = requestBuilder.build();
    HttpResponse response = restClient.execute(request, config.getAccessToken(), HttpURLConnection.HTTP_BAD_REQUEST, HttpURLConnection.HTTP_NOT_FOUND);
    int responseCode = response.getResponseCode();
    if (responseCode == HttpURLConnection.HTTP_NOT_FOUND) {
        throw new ArtifactNotFoundException(artifactId);
    } else if (responseCode == HttpURLConnection.HTTP_BAD_REQUEST) {
        throw new BadRequestException(response.getResponseBodyAsString());
    }
}
Also used : HttpRequest(io.cdap.common.http.HttpRequest) HttpResponse(io.cdap.common.http.HttpResponse) BadRequestException(io.cdap.cdap.common.BadRequestException) ArtifactNotFoundException(io.cdap.cdap.common.ArtifactNotFoundException) URL(java.net.URL)

Example 12 with ArtifactNotFoundException

use of io.cdap.cdap.common.ArtifactNotFoundException in project cdap by caskdata.

the class ArtifactClient method writeProperties.

/**
 * Write properties for an artifact. Any existing properties will be overwritten.
 *
 * @param artifactId the artifact to add properties to
 * @param properties the properties to add
 * @throws BadRequestException if the request is invalid. For example, if the artifact name or version is invalid
 * @throws UnauthenticatedException if the request is not authorized successfully in the gateway server
 * @throws ArtifactNotFoundException if the artifact does not exist
 * @throws IOException if a network error occurred
 */
public void writeProperties(ArtifactId artifactId, Map<String, String> properties) throws IOException, UnauthenticatedException, ArtifactNotFoundException, BadRequestException, UnauthorizedException {
    String path = String.format("artifacts/%s/versions/%s/properties", artifactId.getArtifact(), artifactId.getVersion());
    URL url = config.resolveNamespacedURLV3(artifactId.getParent(), path);
    HttpRequest.Builder requestBuilder = HttpRequest.put(url);
    HttpRequest request = requestBuilder.withBody(GSON.toJson(properties)).build();
    HttpResponse response = restClient.execute(request, config.getAccessToken(), HttpURLConnection.HTTP_BAD_REQUEST, HttpURLConnection.HTTP_NOT_FOUND);
    int responseCode = response.getResponseCode();
    if (responseCode == HttpURLConnection.HTTP_NOT_FOUND) {
        throw new ArtifactNotFoundException(artifactId);
    } else if (responseCode == HttpURLConnection.HTTP_BAD_REQUEST) {
        throw new BadRequestException(response.getResponseBodyAsString());
    }
}
Also used : HttpRequest(io.cdap.common.http.HttpRequest) HttpResponse(io.cdap.common.http.HttpResponse) BadRequestException(io.cdap.cdap.common.BadRequestException) ArtifactNotFoundException(io.cdap.cdap.common.ArtifactNotFoundException) URL(java.net.URL)

Example 13 with ArtifactNotFoundException

use of io.cdap.cdap.common.ArtifactNotFoundException in project cdap by caskdata.

the class ArtifactClient method getArtifactInfo.

/**
 * Gets information about a specific artifact version.
 *
 * @param artifactId the id of the artifact to get
 * @param scope the scope of the artifact
 * @return information about the given artifact
 * @throws IOException if a network error occurred
 * @throws UnauthenticatedException if the request is not authorized successfully in the gateway server
 * @throws ArtifactNotFoundException if the given artifact does not exist
 */
public ArtifactInfo getArtifactInfo(ArtifactId artifactId, ArtifactScope scope) throws IOException, UnauthenticatedException, ArtifactNotFoundException, UnauthorizedException {
    String path = String.format("artifacts/%s/versions/%s?scope=%s", artifactId.getArtifact(), artifactId.getVersion(), scope.name());
    URL url = config.resolveNamespacedURLV3(artifactId.getParent(), path);
    HttpResponse response = restClient.execute(HttpMethod.GET, url, config.getAccessToken(), HttpURLConnection.HTTP_NOT_FOUND);
    if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
        throw new ArtifactNotFoundException(artifactId);
    }
    return ObjectResponse.fromJsonBody(response, ArtifactInfo.class, GSON).getResponseObject();
}
Also used : ArtifactInfo(io.cdap.cdap.api.artifact.ArtifactInfo) HttpResponse(io.cdap.common.http.HttpResponse) ArtifactNotFoundException(io.cdap.cdap.common.ArtifactNotFoundException) URL(java.net.URL)

Example 14 with ArtifactNotFoundException

use of io.cdap.cdap.common.ArtifactNotFoundException in project cdap by caskdata.

the class ArtifactClient method getPluginTypes.

/**
 * Gets all the plugin types available to a specific artifact.
 *
 * @param artifactId the id of the artifact to get
 * @param scope the scope of the artifact
 * @return list of plugin types available to the given artifact.
 * @throws ArtifactNotFoundException if the given artifact does not exist
 * @throws IOException if a network error occurred
 * @throws UnauthenticatedException if the request is not authorized successfully in the gateway server
 */
public List<String> getPluginTypes(ArtifactId artifactId, ArtifactScope scope) throws IOException, UnauthenticatedException, ArtifactNotFoundException, UnauthorizedException {
    String path = String.format("artifacts/%s/versions/%s/extensions?scope=%s", artifactId.getArtifact(), artifactId.getVersion(), scope.name());
    URL url = config.resolveNamespacedURLV3(artifactId.getParent(), path);
    HttpResponse response = restClient.execute(HttpMethod.GET, url, config.getAccessToken(), HttpURLConnection.HTTP_NOT_FOUND);
    if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
        throw new ArtifactNotFoundException(artifactId);
    }
    return ObjectResponse.<List<String>>fromJsonBody(response, EXTENSIONS_TYPE).getResponseObject();
}
Also used : HttpResponse(io.cdap.common.http.HttpResponse) ArtifactNotFoundException(io.cdap.cdap.common.ArtifactNotFoundException) URL(java.net.URL)

Example 15 with ArtifactNotFoundException

use of io.cdap.cdap.common.ArtifactNotFoundException in project cdap by caskdata.

the class ArtifactStoreTest method testDelete.

@Test
public void testDelete() throws Exception {
    // write an artifact with an app
    Id.Artifact parentId = Id.Artifact.from(Id.Namespace.DEFAULT, "parent", "1.0.0");
    ApplicationClass appClass = new ApplicationClass(InspectionApp.class.getName(), "", new ReflectionSchemaGenerator().generate(InspectionApp.AConfig.class));
    ArtifactMeta artifactMeta = new ArtifactMeta(ArtifactClasses.builder().addApp(appClass).build());
    writeArtifact(parentId, artifactMeta, "parent contents");
    // write a child artifact that extends the parent with some plugins
    Id.Artifact childId = Id.Artifact.from(Id.Namespace.DEFAULT, "myplugins", "1.0.0");
    Id.Artifact anotherId = Id.Artifact.from(Id.Namespace.SYSTEM, "myplugins", "1.0.0");
    List<PluginClass> plugins = ImmutableList.of(PluginClass.builder().setName("plugin1").setType("atype").setDescription("").setClassName("c.c.c.plugin1").setConfigFieldName("cfg").setProperties(ImmutableMap.of()).build(), PluginClass.builder().setName("plugin2").setType("atype").setDescription("").setClassName("c.c.c.plugin2").setConfigFieldName("cfg").setProperties(ImmutableMap.of()).build());
    Set<ArtifactRange> parents = ImmutableSet.of(new ArtifactRange(parentId.getNamespace().getId(), parentId.getName(), new ArtifactVersion("0.1.0"), new ArtifactVersion("2.0.0")));
    artifactMeta = new ArtifactMeta(ArtifactClasses.builder().addPlugins(plugins).build(), parents);
    writeArtifact(childId, artifactMeta, "child contents");
    writeArtifact(anotherId, artifactMeta, "child contents");
    // check parent has plugins from the child
    Assert.assertFalse(artifactStore.getPluginClasses(NamespaceId.DEFAULT, parentId).isEmpty());
    // delete the child artifact
    artifactStore.delete(childId);
    // check that the other artifact is not getting deleted and the plugin classes are not getting deleted
    artifactStore.getArtifact(anotherId);
    Assert.assertFalse(artifactStore.getPluginClasses(NamespaceId.SYSTEM, parentId).isEmpty());
    // shouldn't be able to get artifact detail
    try {
        artifactStore.getArtifact(childId);
        Assert.fail();
    } catch (ArtifactNotFoundException e) {
    // expected
    }
    // shouldn't see it in the list
    List<ArtifactDetail> artifactList = artifactStore.getArtifacts(parentId.getNamespace().toEntityId());
    Assert.assertEquals(1, artifactList.size());
    Assert.assertEquals(parentId.getName(), artifactList.get(0).getDescriptor().getArtifactId().getName());
    // delete the one in system scope
    artifactStore.delete(anotherId);
    // shouldn't be able to get artifact detail
    try {
        artifactStore.getArtifact(anotherId);
        Assert.fail();
    } catch (ArtifactNotFoundException e) {
    // expected
    }
    Assert.assertTrue(artifactStore.getPluginClasses(NamespaceId.SYSTEM, parentId).isEmpty());
    // shouldn't see any more plugins for parent
    Assert.assertTrue(artifactStore.getPluginClasses(NamespaceId.DEFAULT, parentId).isEmpty());
    // delete parent
    artifactStore.delete(parentId);
    // nothing should be in the list
    Assert.assertTrue(artifactStore.getArtifacts(parentId.getNamespace().toEntityId()).isEmpty());
    // shouldn't be able to see app class either
    Assert.assertTrue(artifactStore.getApplicationClasses(NamespaceId.DEFAULT, appClass.getClassName()).isEmpty());
}
Also used : ArtifactRange(io.cdap.cdap.api.artifact.ArtifactRange) ApplicationClass(io.cdap.cdap.api.artifact.ApplicationClass) ReflectionSchemaGenerator(io.cdap.cdap.internal.io.ReflectionSchemaGenerator) ArtifactVersion(io.cdap.cdap.api.artifact.ArtifactVersion) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) Id(io.cdap.cdap.common.id.Id) ArtifactId(io.cdap.cdap.proto.id.ArtifactId) PluginClass(io.cdap.cdap.api.plugin.PluginClass) ArtifactNotFoundException(io.cdap.cdap.common.ArtifactNotFoundException) InspectionApp(io.cdap.cdap.internal.app.runtime.artifact.app.inspection.InspectionApp) Test(org.junit.Test)

Aggregations

ArtifactNotFoundException (io.cdap.cdap.common.ArtifactNotFoundException)62 HttpResponse (io.cdap.common.http.HttpResponse)24 URL (java.net.URL)20 IOException (java.io.IOException)18 ArtifactRange (io.cdap.cdap.api.artifact.ArtifactRange)16 ArtifactId (io.cdap.cdap.proto.id.ArtifactId)16 NamespaceId (io.cdap.cdap.proto.id.NamespaceId)16 ArtifactVersion (io.cdap.cdap.api.artifact.ArtifactVersion)14 HttpRequest (io.cdap.common.http.HttpRequest)14 ArtifactSummary (io.cdap.cdap.api.artifact.ArtifactSummary)12 PluginClass (io.cdap.cdap.api.plugin.PluginClass)12 StructuredRow (io.cdap.cdap.spi.data.StructuredRow)12 Location (org.apache.twill.filesystem.Location)12 BadRequestException (io.cdap.cdap.common.BadRequestException)10 File (java.io.File)10 Test (org.junit.Test)10 StructuredTable (io.cdap.cdap.spi.data.StructuredTable)9 ApplicationClass (io.cdap.cdap.api.artifact.ApplicationClass)6 ApplicationNotFoundException (io.cdap.cdap.common.ApplicationNotFoundException)6 Id (io.cdap.cdap.common.id.Id)6