Search in sources :

Example 1 with DatasetInstanceConfiguration

use of io.cdap.cdap.proto.DatasetInstanceConfiguration in project cdap by caskdata.

the class DatasetTypeHandlerTest method testBasics.

@Test
public void testBasics() throws Exception {
    // nothing has been deployed, modules and types list is empty
    verifyAll(NO_MODULES, NO_DEPENDENCIES);
    // deploy module and verify that it can be retrieved in all ways
    Assert.assertEquals(HttpStatus.SC_OK, deployModule("module1", TestModule1x.class).getResponseCode());
    verifyAll(ONLY_MODULE1X, ONLY_1X_DEPENDENCIES);
    // cannot deploy other module with same types
    Assert.assertEquals(HttpStatus.SC_CONFLICT, deployModule("not-module1", TestModule1.class).getResponseCode());
    verifyAll(ONLY_MODULE1X, ONLY_1X_DEPENDENCIES);
    // can deploy same module again
    Assert.assertEquals(HttpStatus.SC_OK, deployModule("module1", TestModule1x.class).getResponseCode());
    verifyAll(ONLY_MODULE1X, ONLY_1X_DEPENDENCIES);
    // create a dataset instance, verify that we cannot redeploy the module with fewer types, even with force option
    instanceService.create(NamespaceId.DEFAULT.getNamespace(), "instance1x", new DatasetInstanceConfiguration("datasetType1x", new HashMap<>()));
    Assert.assertEquals(HttpStatus.SC_CONFLICT, deployModule("module1", TestModule1.class).getResponseCode());
    Assert.assertEquals(HttpStatus.SC_CONFLICT, deployModule("module1", TestModule1.class, true).getResponseCode());
    verifyAll(ONLY_MODULE1X, ONLY_1X_DEPENDENCIES);
    // drop the instance, now we should be able to redeploy, dropping type1x
    instanceService.drop(NamespaceId.DEFAULT.dataset("instance1x"));
    Assert.assertEquals(HttpStatus.SC_OK, deployModule("module1", TestModule1.class).getResponseCode());
    verifyAll(ONLY_MODULE1, ONLY_1_DEPENDENCIES);
    // redeploy module with more types and validate
    Assert.assertEquals(HttpStatus.SC_OK, deployModule("module1", TestModule1x.class).getResponseCode());
    verifyAll(ONLY_MODULE1X, ONLY_1X_DEPENDENCIES);
    // deploy another module which depends on the first one
    Assert.assertEquals(HttpStatus.SC_OK, deployModule("module2", TestModule2.class).getResponseCode());
    verifyAll(MODULES_1X_AND_2, BOTH_1X_2_DEPENDENCIES);
    // cannot delete non-existent module
    Assert.assertEquals(HttpStatus.SC_NOT_FOUND, deleteModule("non-existing-module").getResponseCode());
    // cannot delete module1 since module2 depends on it, verify that nothing has been deleted
    Assert.assertEquals(HttpStatus.SC_CONFLICT, deleteModule("module1").getResponseCode());
    verifyAll(MODULES_1X_AND_2, BOTH_1X_2_DEPENDENCIES);
    // cannot deploy same module again with fewer types (because module2 depends on it)
    Assert.assertEquals(HttpStatus.SC_CONFLICT, deployModule("module1", TestModule1.class).getResponseCode());
    verifyAll(MODULES_1X_AND_2, BOTH_1X_2_DEPENDENCIES);
    // create dataset instances, try force deploy of same module again with fewer types - should fail
    instanceService.create(NamespaceId.DEFAULT.getNamespace(), "instance1", new DatasetInstanceConfiguration("datasetType1", new HashMap<>()));
    instanceService.create(NamespaceId.DEFAULT.getNamespace(), "instance1x", new DatasetInstanceConfiguration("datasetType1x", new HashMap<>()));
    Assert.assertEquals(HttpStatus.SC_CONFLICT, deployModule("module1", TestModule1.class, true).getResponseCode());
    verifyAll(MODULES_1X_AND_2, BOTH_1X_2_DEPENDENCIES);
    // drop the instance of type1x, now forced deploy should work
    instanceService.drop(NamespaceId.DEFAULT.dataset("instance1x"));
    Assert.assertEquals(HttpStatus.SC_OK, deployModule("module1", TestModule1.class, true).getResponseCode());
    verifyAll(MODULES_1_AND_2, BOTH_1_2_DEPENDENCIES);
    // delete module2, should be removed from usedBy list everywhere and all its types should no longer be available
    Assert.assertEquals(HttpStatus.SC_OK, deleteModule("module2").getResponseCode());
    Assert.assertEquals(HttpStatus.SC_NOT_FOUND, getMissingType("datasetType2").getResponseCode());
    verifyAll(ONLY_MODULE1, ONLY_1_DEPENDENCIES);
    // cannot delete module2 again
    Assert.assertEquals(HttpStatus.SC_NOT_FOUND, deleteModule("module2").getResponseCode());
    // cannot delete module 1 becuse there is an instance
    Assert.assertEquals(HttpStatus.SC_CONFLICT, deleteModules().getResponseCode());
    verifyAll(ONLY_MODULE1, ONLY_1_DEPENDENCIES);
    // drop the instance of type1, now delete of module1 should work
    instanceService.drop(NamespaceId.DEFAULT.dataset("instance1"));
    Assert.assertEquals(HttpStatus.SC_OK, deleteModules().getResponseCode());
    Assert.assertEquals(HttpStatus.SC_NOT_FOUND, getMissingType("datasetType1").getResponseCode());
    verifyAll(NO_MODULES, NO_DEPENDENCIES);
}
Also used : HashMap(java.util.HashMap) DatasetInstanceConfiguration(io.cdap.cdap.proto.DatasetInstanceConfiguration) Test(org.junit.Test)

Example 2 with DatasetInstanceConfiguration

use of io.cdap.cdap.proto.DatasetInstanceConfiguration in project cdap by caskdata.

the class DatasetInstanceHandlerTest method createInstance.

private HttpResponse createInstance(DatasetId instance, String typeName, @Nullable String description, @Nullable DatasetProperties props, @Nullable String ownerPrincipal) throws IOException {
    DatasetInstanceConfiguration creationProperties;
    if (props != null) {
        creationProperties = new DatasetInstanceConfiguration(typeName, props.getProperties(), description, ownerPrincipal);
    } else {
        creationProperties = new DatasetInstanceConfiguration(typeName, null, description, ownerPrincipal);
    }
    HttpRequest request = HttpRequest.put(getUrl(instance.getNamespace(), "/data/datasets/" + instance.getEntityName())).withBody(GSON.toJson(creationProperties)).build();
    return HttpRequests.execute(request, REQUEST_CONFIG);
}
Also used : HttpRequest(io.cdap.common.http.HttpRequest) DatasetInstanceConfiguration(io.cdap.cdap.proto.DatasetInstanceConfiguration)

Example 3 with DatasetInstanceConfiguration

use of io.cdap.cdap.proto.DatasetInstanceConfiguration in project cdap by caskdata.

the class DatasetServiceClient method addInstance.

public void addInstance(String datasetInstanceName, String datasetType, DatasetProperties props, @Nullable KerberosPrincipalId owner) throws DatasetManagementException, UnauthorizedException {
    String ownerPrincipal = owner == null ? null : owner.getPrincipal();
    DatasetInstanceConfiguration creationProperties = new DatasetInstanceConfiguration(datasetType, props.getProperties(), props.getDescription(), ownerPrincipal);
    HttpResponse response = doPut("datasets/" + datasetInstanceName, GSON.toJson(creationProperties));
    if (HttpResponseStatus.CONFLICT.code() == response.getResponseCode()) {
        throw new InstanceConflictException(String.format("Failed to add instance %s due to conflict, details: %s", datasetInstanceName, response));
    }
    if (HttpResponseStatus.FORBIDDEN.code() == response.getResponseCode()) {
        throw new DatasetManagementException(String.format("Failed to add instance %s, details: %s", datasetInstanceName, response), new UnauthorizedException(response.getResponseBodyAsString()));
    }
    if (HttpResponseStatus.OK.code() != response.getResponseCode()) {
        throw new DatasetManagementException(String.format("Failed to add instance %s, details: %s", datasetInstanceName, response));
    }
}
Also used : DatasetManagementException(io.cdap.cdap.api.dataset.DatasetManagementException) InstanceConflictException(io.cdap.cdap.api.dataset.InstanceConflictException) UnauthorizedException(io.cdap.cdap.security.spi.authorization.UnauthorizedException) HttpResponse(io.cdap.common.http.HttpResponse) DatasetInstanceConfiguration(io.cdap.cdap.proto.DatasetInstanceConfiguration)

Example 4 with DatasetInstanceConfiguration

use of io.cdap.cdap.proto.DatasetInstanceConfiguration in project cdap by caskdata.

the class ConversionHelpers method getInstanceConfiguration.

static DatasetInstanceConfiguration getInstanceConfiguration(FullHttpRequest request) throws BadRequestException {
    Reader reader = new InputStreamReader(new ByteBufInputStream(request.content()), StandardCharsets.UTF_8);
    try {
        DatasetInstanceConfiguration config = GSON.fromJson(reader, DatasetInstanceConfiguration.class);
        Preconditions.checkNotNull(config.getTypeName(), "The typeName must be specified.");
        return config;
    } catch (JsonSyntaxException | NullPointerException e) {
        throw new BadRequestException(e.getMessage());
    }
}
Also used : JsonSyntaxException(com.google.gson.JsonSyntaxException) InputStreamReader(java.io.InputStreamReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) BadRequestException(io.cdap.cdap.common.BadRequestException) ByteBufInputStream(io.netty.buffer.ByteBufInputStream) DatasetInstanceConfiguration(io.cdap.cdap.proto.DatasetInstanceConfiguration)

Example 5 with DatasetInstanceConfiguration

use of io.cdap.cdap.proto.DatasetInstanceConfiguration in project cdap by caskdata.

the class DatasetInstanceHandler method create.

/**
 * Creates a new dataset instance.
 *
 * @param namespaceId namespace of the new dataset instance
 * @param name name of the new dataset instance
 */
@PUT
@Path("/data/datasets/{name}")
@AuditPolicy(AuditDetail.REQUEST_BODY)
public void create(FullHttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId, @PathParam("name") String name) throws Exception {
    logCallReceived(request);
    DatasetInstanceConfiguration creationProperties = ConversionHelpers.getInstanceConfiguration(request);
    try {
        instanceService.create(namespaceId, name, creationProperties);
        responder.sendStatus(HttpResponseStatus.OK);
    } catch (DatasetAlreadyExistsException e) {
        responder.sendString(HttpResponseStatus.CONFLICT, e.getMessage());
    } catch (IllegalArgumentException e) {
        responder.sendString(HttpResponseStatus.BAD_REQUEST, e.getMessage());
    } catch (DatasetTypeNotFoundException e) {
        responder.sendString(HttpResponseStatus.NOT_FOUND, e.getMessage());
    } catch (HandlerException e) {
        responder.sendString(e.getFailureStatus(), e.getMessage());
    }
    logCallResponded(request);
}
Also used : HandlerException(io.cdap.cdap.common.HandlerException) DatasetAlreadyExistsException(io.cdap.cdap.common.DatasetAlreadyExistsException) DatasetInstanceConfiguration(io.cdap.cdap.proto.DatasetInstanceConfiguration) DatasetTypeNotFoundException(io.cdap.cdap.common.DatasetTypeNotFoundException) Path(javax.ws.rs.Path) AuditPolicy(io.cdap.cdap.common.security.AuditPolicy) PUT(javax.ws.rs.PUT)

Aggregations

DatasetInstanceConfiguration (io.cdap.cdap.proto.DatasetInstanceConfiguration)12 Test (org.junit.Test)6 DatasetId (io.cdap.cdap.proto.id.DatasetId)4 NamespaceMeta (io.cdap.cdap.proto.NamespaceMeta)3 NamespaceId (io.cdap.cdap.proto.id.NamespaceId)3 KeyValueTable (io.cdap.cdap.api.dataset.lib.KeyValueTable)2 Table (io.cdap.cdap.api.dataset.table.Table)2 DatasetTypeNotFoundException (io.cdap.cdap.common.DatasetTypeNotFoundException)2 DatasetMeta (io.cdap.cdap.proto.DatasetMeta)2 MetadataSearchResponse (io.cdap.cdap.proto.metadata.MetadataSearchResponse)2 HashMap (java.util.HashMap)2 JsonSyntaxException (com.google.gson.JsonSyntaxException)1 DatasetManagementException (io.cdap.cdap.api.dataset.DatasetManagementException)1 InstanceConflictException (io.cdap.cdap.api.dataset.InstanceConflictException)1 StandaloneDataset (io.cdap.cdap.client.app.StandaloneDataset)1 StandaloneDatasetModule (io.cdap.cdap.client.app.StandaloneDatasetModule)1 BadRequestException (io.cdap.cdap.common.BadRequestException)1 DatasetAlreadyExistsException (io.cdap.cdap.common.DatasetAlreadyExistsException)1 DatasetModuleNotFoundException (io.cdap.cdap.common.DatasetModuleNotFoundException)1 HandlerException (io.cdap.cdap.common.HandlerException)1