use of co.cask.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 co.cask.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 {
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 co.cask.cdap.proto.DatasetInstanceConfiguration in project cdap by caskdata.
the class MetadataHttpHandlerTestRun method testSearchResultPaginationWithTargetType.
@Test
public void testSearchResultPaginationWithTargetType() throws Exception {
// note that the ordering of the entity creations and the sort param used in this test case matter, in order to
// reproduce the scenario that caused the issue CDAP-7881
NamespaceId namespace = new NamespaceId("pagination_with_target_type");
namespaceClient.create(new NamespaceMeta.Builder().setName(namespace).build());
StreamId stream1 = namespace.stream("text1");
StreamId stream2 = namespace.stream("text2");
DatasetId trackerDataset = namespace.dataset("_auditLog");
DatasetId mydataset = namespace.dataset("mydataset");
// the creation order below will determine how we see the entities in search result sorted by entity creation time
// in ascending order
datasetClient.create(trackerDataset, new DatasetInstanceConfiguration(Table.class.getName(), Collections.<String, String>emptyMap()));
datasetClient.create(mydataset, new DatasetInstanceConfiguration(Table.class.getName(), Collections.<String, String>emptyMap()));
// create entities so system metadata is annotated
streamClient.create(stream1);
streamClient.create(stream2);
// do sorting with creation time here since the testSearchResultPagination does with entity name
// the sorted result order _auditLog mydataset text2 text1 (ascending: creation from earliest time)
String sort = AbstractSystemMetadataWriter.CREATION_TIME_KEY + " " + SortInfo.SortOrder.ASC;
// offset 1, limit 2, 2 cursors, should return 2nd result, with 0 cursors since we don't have enough data
// set showHidden to true which will show the trackerDataset but will not be in search response since its not stream
MetadataSearchResponse searchResponse = searchMetadata(namespace, "*", ImmutableSet.of(EntityTypeSimpleName.STREAM), sort, 1, 2, 2, null, true);
List<MetadataSearchResultRecord> expectedResults = ImmutableList.of(new MetadataSearchResultRecord(stream2));
List<String> expectedCursors = ImmutableList.of();
Assert.assertEquals(expectedResults, new ArrayList<>(searchResponse.getResults()));
Assert.assertEquals(expectedCursors, searchResponse.getCursors());
// offset 1, limit 2, 2 cursors, should return just the dataset created above other than trackerDataset even
// though it was created before since showHidden is false and it should not affect pagination
searchResponse = searchMetadata(namespace, "*", ImmutableSet.of(EntityTypeSimpleName.DATASET), sort, 0, 2, 2, null);
expectedResults = ImmutableList.of(new MetadataSearchResultRecord(mydataset));
expectedCursors = ImmutableList.of();
Assert.assertEquals(expectedResults, new ArrayList<>(searchResponse.getResults()));
Assert.assertEquals(expectedCursors, searchResponse.getCursors());
}
use of co.cask.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(HttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId, @PathParam("name") String name) throws Exception {
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 (DatasetTypeNotFoundException e) {
responder.sendString(HttpResponseStatus.NOT_FOUND, e.getMessage());
} catch (HandlerException e) {
responder.sendString(e.getFailureStatus(), e.getMessage());
}
}
use of co.cask.cdap.proto.DatasetInstanceConfiguration in project cdap by caskdata.
the class MetadataHttpHandlerTestRun method testSearchResultSorting.
@Test
public void testSearchResultSorting() throws Exception {
NamespaceId namespace = new NamespaceId("sorting");
namespaceClient.create(new NamespaceMeta.Builder().setName(namespace).build());
StreamId stream = namespace.stream("text");
DatasetId dataset = namespace.dataset("mydataset");
StreamViewId view = stream.view("view");
// create entities so system metadata is annotated
// also ensure that they are created at least 1 ms apart
streamClient.create(stream);
TimeUnit.MILLISECONDS.sleep(1);
streamViewClient.createOrUpdate(view, new ViewSpecification(new FormatSpecification("csv", null, null)));
TimeUnit.MILLISECONDS.sleep(1);
datasetClient.create(dataset, new DatasetInstanceConfiguration(Table.class.getName(), Collections.<String, String>emptyMap()));
// search with bad sort param
EnumSet<EntityTypeSimpleName> targets = EnumSet.allOf(EntityTypeSimpleName.class);
// test ascending order of entity name
Set<MetadataSearchResultRecord> searchResults = searchMetadata(namespace, "*", targets, AbstractSystemMetadataWriter.ENTITY_NAME_KEY + " asc");
List<MetadataSearchResultRecord> expected = ImmutableList.of(new MetadataSearchResultRecord(dataset), new MetadataSearchResultRecord(stream), new MetadataSearchResultRecord(view));
Assert.assertEquals(expected, new ArrayList<>(searchResults));
// test descending order of entity name
searchResults = searchMetadata(namespace, "*", targets, AbstractSystemMetadataWriter.ENTITY_NAME_KEY + " desc");
expected = ImmutableList.of(new MetadataSearchResultRecord(view), new MetadataSearchResultRecord(stream), new MetadataSearchResultRecord(dataset));
Assert.assertEquals(expected, new ArrayList<>(searchResults));
// test ascending order of creation time
searchResults = searchMetadata(namespace, "*", targets, AbstractSystemMetadataWriter.CREATION_TIME_KEY + " asc");
expected = ImmutableList.of(new MetadataSearchResultRecord(stream), new MetadataSearchResultRecord(view), new MetadataSearchResultRecord(dataset));
Assert.assertEquals(expected, new ArrayList<>(searchResults));
// test descending order of creation time
searchResults = searchMetadata(namespace, "*", targets, AbstractSystemMetadataWriter.CREATION_TIME_KEY + " desc");
expected = ImmutableList.of(new MetadataSearchResultRecord(dataset), new MetadataSearchResultRecord(view), new MetadataSearchResultRecord(stream));
Assert.assertEquals(expected, new ArrayList<>(searchResults));
// cleanup
namespaceClient.delete(namespace);
}
Aggregations