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);
}
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);
}
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));
}
}
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());
}
}
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);
}
Aggregations