Search in sources :

Example 1 with DatasetTypeNotFoundException

use of co.cask.cdap.common.DatasetTypeNotFoundException in project cdap by caskdata.

the class DatasetTypeService method getType.

/**
   * Returns details of the specified {@link DatasetTypeId dataset type}.
   */
DatasetTypeMeta getType(DatasetTypeId datasetTypeId) throws Exception {
    ensureNamespaceExists(datasetTypeId.getParent());
    DatasetTypeMeta typeMeta = typeManager.getTypeInfo(datasetTypeId);
    if (typeMeta == null) {
        throw new DatasetTypeNotFoundException(datasetTypeId);
    }
    // TODO: Test if this can be removed
    if (NamespaceId.SYSTEM.equals(datasetTypeId.getParent())) {
        return typeMeta;
    }
    // only return the type if the user has some privileges on it
    Principal principal = authenticationContext.getPrincipal();
    Predicate<EntityId> authFilter = authorizationEnforcer.createFilter(principal);
    if (!authFilter.apply(datasetTypeId)) {
        throw new UnauthorizedException(principal, datasetTypeId);
    }
    return typeMeta;
}
Also used : EntityId(co.cask.cdap.proto.id.EntityId) DatasetTypeMeta(co.cask.cdap.proto.DatasetTypeMeta) UnauthorizedException(co.cask.cdap.security.spi.authorization.UnauthorizedException) DatasetTypeNotFoundException(co.cask.cdap.common.DatasetTypeNotFoundException) Principal(co.cask.cdap.proto.security.Principal)

Example 2 with DatasetTypeNotFoundException

use of co.cask.cdap.common.DatasetTypeNotFoundException in project cdap by caskdata.

the class DatasetInstanceService method create.

/**
 * Creates a dataset instance.
 *
 * @param namespaceId the namespace to create the dataset instance in
 * @param name the name of the new dataset instance
 * @param props the properties for the new dataset instance
 * @throws NamespaceNotFoundException if the specified namespace was not found
 * @throws DatasetAlreadyExistsException if a dataset with the same name already exists
 * @throws DatasetTypeNotFoundException if the dataset type was not found
 * @throws UnauthorizedException if perimeter security and authorization are enabled, and the current user does not
 *  have {@link Action#WRITE} privilege on the #instance's namespace
 */
void create(String namespaceId, String name, DatasetInstanceConfiguration props) throws Exception {
    NamespaceId namespace = ConversionHelpers.toNamespaceId(namespaceId);
    DatasetId datasetId = ConversionHelpers.toDatasetInstanceId(namespaceId, name);
    Principal requestingUser = authenticationContext.getPrincipal();
    String ownerPrincipal = props.getOwnerPrincipal();
    // need to enforce on the principal id if impersonation is involved
    KerberosPrincipalId effectiveOwner = SecurityUtil.getEffectiveOwner(ownerAdmin, namespace, ownerPrincipal);
    if (!DatasetsUtil.isSystemDatasetInUserNamespace(datasetId)) {
        if (effectiveOwner != null) {
            authorizationEnforcer.enforce(effectiveOwner, requestingUser, Action.ADMIN);
        }
        authorizationEnforcer.enforce(datasetId, requestingUser, Action.ADMIN);
    }
    ensureNamespaceExists(namespace);
    DatasetSpecification existing = instanceManager.get(datasetId);
    if (existing != null) {
        throw new DatasetAlreadyExistsException(datasetId);
    }
    // for creation, we need enforcement for dataset type for user dataset, but bypass for system datasets
    DatasetTypeMeta typeMeta = getTypeInfo(namespace, props.getTypeName(), DatasetsUtil.isSystemDatasetInUserNamespace(datasetId));
    if (typeMeta == null) {
        // Type not found in the instance's namespace and the system namespace. Bail out.
        throw new DatasetTypeNotFoundException(ConversionHelpers.toDatasetTypeId(namespace, props.getTypeName()));
    }
    LOG.info("Creating dataset {}.{}, type name: {}, properties: {}", namespaceId, name, props.getTypeName(), props.getProperties());
    // exists or not
    if (ownerPrincipal != null) {
        KerberosPrincipalId owner = new KerberosPrincipalId(ownerPrincipal);
        ownerAdmin.add(datasetId, owner);
    }
    try {
        DatasetSpecification spec = opExecutorClient.create(datasetId, typeMeta, DatasetProperties.builder().addAll(props.getProperties()).setDescription(props.getDescription()).build());
        instanceManager.add(namespace, spec);
        metaCache.invalidate(datasetId);
        publishAudit(datasetId, AuditType.CREATE);
        // Enable explore
        enableExplore(datasetId, spec, props);
    } catch (Exception e) {
        // there was a problem in creating the dataset instance so delete the owner if it got added earlier
        // safe to call for entities which does not have an owner too
        ownerAdmin.delete(datasetId);
        throw e;
    }
}
Also used : DatasetSpecification(co.cask.cdap.api.dataset.DatasetSpecification) DatasetTypeMeta(co.cask.cdap.proto.DatasetTypeMeta) DatasetAlreadyExistsException(co.cask.cdap.common.DatasetAlreadyExistsException) NamespaceId(co.cask.cdap.proto.id.NamespaceId) DatasetTypeNotFoundException(co.cask.cdap.common.DatasetTypeNotFoundException) KerberosPrincipalId(co.cask.cdap.proto.id.KerberosPrincipalId) Principal(co.cask.cdap.proto.security.Principal) NamespaceNotFoundException(co.cask.cdap.common.NamespaceNotFoundException) DatasetNotFoundException(co.cask.cdap.common.DatasetNotFoundException) DatasetTypeNotFoundException(co.cask.cdap.common.DatasetTypeNotFoundException) UnauthorizedException(co.cask.cdap.security.spi.authorization.UnauthorizedException) HandlerException(co.cask.cdap.common.HandlerException) DatasetAlreadyExistsException(co.cask.cdap.common.DatasetAlreadyExistsException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) NotFoundException(co.cask.cdap.common.NotFoundException) DatasetId(co.cask.cdap.proto.id.DatasetId)

Example 3 with DatasetTypeNotFoundException

use of co.cask.cdap.common.DatasetTypeNotFoundException in project cdap by caskdata.

the class DatasetClient method create.

/**
 * Creates a dataset.
 *
 * @param instance ID of the dataset instance
 * @param properties properties of the dataset to create
 * @throws DatasetTypeNotFoundException if the desired dataset type was not found
 * @throws DatasetAlreadyExistsException if a dataset by the same name already exists
 * @throws IOException if a network error occurred
 * @throws UnauthenticatedException if the request is not authorized successfully in the gateway server
 */
public void create(DatasetId instance, DatasetInstanceConfiguration properties) throws DatasetTypeNotFoundException, DatasetAlreadyExistsException, IOException, UnauthenticatedException, UnauthorizedException {
    URL url = config.resolveNamespacedURLV3(instance.getParent(), String.format("data/datasets/%s", instance.getDataset()));
    HttpRequest request = HttpRequest.put(url).withBody(GSON.toJson(properties)).build();
    HttpResponse response = restClient.execute(request, config.getAccessToken(), HttpURLConnection.HTTP_NOT_FOUND, HttpURLConnection.HTTP_CONFLICT);
    if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
        throw new DatasetTypeNotFoundException(instance.getParent().datasetType(properties.getTypeName()));
    } else if (response.getResponseCode() == HttpURLConnection.HTTP_CONFLICT) {
        throw new DatasetAlreadyExistsException(instance);
    }
}
Also used : HttpRequest(co.cask.common.http.HttpRequest) HttpResponse(co.cask.common.http.HttpResponse) DatasetAlreadyExistsException(co.cask.cdap.common.DatasetAlreadyExistsException) DatasetTypeNotFoundException(co.cask.cdap.common.DatasetTypeNotFoundException) URL(java.net.URL)

Example 4 with DatasetTypeNotFoundException

use of co.cask.cdap.common.DatasetTypeNotFoundException in project cdap by caskdata.

the class CLIMainTest method testDataset.

@Test
public void testDataset() throws Exception {
    String datasetName = PREFIX + "sdf123lkj";
    String ownedDatasetName = PREFIX + "owned";
    DatasetTypeClient datasetTypeClient = new DatasetTypeClient(cliConfig.getClientConfig());
    DatasetTypeMeta datasetType = datasetTypeClient.list(NamespaceId.DEFAULT).get(0);
    testCommandOutputContains(cli, "create dataset instance " + datasetType.getName() + " " + datasetName + " \"a=1\"", "Successfully created dataset");
    testCommandOutputContains(cli, "list dataset instances", FakeDataset.class.getSimpleName());
    testCommandOutputContains(cli, "get dataset instance properties " + datasetName, "a,1");
    // test dataset creation with owner
    String commandOutput = getCommandOutput(cli, "create dataset instance " + datasetType.getName() + " " + ownedDatasetName + " \"a=1\"" + " " + "someDescription " + ArgumentName.PRINCIPAL + " alice/somehost.net@somekdc.net");
    Assert.assertTrue(commandOutput.contains("Successfully created dataset"));
    Assert.assertTrue(commandOutput.contains("alice/somehost.net@somekdc.net"));
    // test describing the table returns the given owner information
    testCommandOutputContains(cli, "describe dataset instance " + ownedDatasetName, "alice/somehost.net@somekdc.net");
    NamespaceClient namespaceClient = new NamespaceClient(cliConfig.getClientConfig());
    NamespaceId barspace = new NamespaceId("bar");
    namespaceClient.create(new NamespaceMeta.Builder().setName(barspace).build());
    cliConfig.setNamespace(barspace);
    // list of dataset instances is different in 'foo' namespace
    testCommandOutputNotContains(cli, "list dataset instances", FakeDataset.class.getSimpleName());
    // also can not create dataset instances if the type it depends on exists only in a different namespace.
    DatasetTypeId datasetType1 = barspace.datasetType(datasetType.getName());
    testCommandOutputContains(cli, "create dataset instance " + datasetType.getName() + " " + datasetName, new DatasetTypeNotFoundException(datasetType1).getMessage());
    testCommandOutputContains(cli, "use namespace default", "Now using namespace 'default'");
    try {
        testCommandOutputContains(cli, "truncate dataset instance " + datasetName, "Successfully truncated");
    } finally {
        testCommandOutputContains(cli, "delete dataset instance " + datasetName, "Successfully deleted");
    }
    String datasetName2 = PREFIX + "asoijm39485";
    String description = "test-description-for-" + datasetName2;
    testCommandOutputContains(cli, "create dataset instance " + datasetType.getName() + " " + datasetName2 + " \"a=1\"" + " " + description, "Successfully created dataset");
    testCommandOutputContains(cli, "list dataset instances", description);
    testCommandOutputContains(cli, "delete dataset instance " + datasetName2, "Successfully deleted");
    testCommandOutputContains(cli, "delete dataset instance " + ownedDatasetName, "Successfully deleted");
}
Also used : DatasetTypeClient(co.cask.cdap.client.DatasetTypeClient) DatasetTypeId(co.cask.cdap.proto.id.DatasetTypeId) NamespaceMeta(co.cask.cdap.proto.NamespaceMeta) NamespaceClient(co.cask.cdap.client.NamespaceClient) DatasetTypeMeta(co.cask.cdap.proto.DatasetTypeMeta) NamespaceId(co.cask.cdap.proto.id.NamespaceId) DatasetTypeNotFoundException(co.cask.cdap.common.DatasetTypeNotFoundException) FakeDataset(co.cask.cdap.client.app.FakeDataset) Test(org.junit.Test)

Example 5 with DatasetTypeNotFoundException

use of co.cask.cdap.common.DatasetTypeNotFoundException 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());
    }
}
Also used : HandlerException(co.cask.cdap.common.HandlerException) DatasetAlreadyExistsException(co.cask.cdap.common.DatasetAlreadyExistsException) DatasetInstanceConfiguration(co.cask.cdap.proto.DatasetInstanceConfiguration) DatasetTypeNotFoundException(co.cask.cdap.common.DatasetTypeNotFoundException) Path(javax.ws.rs.Path) AuditPolicy(co.cask.cdap.common.security.AuditPolicy) PUT(javax.ws.rs.PUT)

Aggregations

DatasetTypeNotFoundException (co.cask.cdap.common.DatasetTypeNotFoundException)10 DatasetTypeMeta (co.cask.cdap.proto.DatasetTypeMeta)7 DatasetAlreadyExistsException (co.cask.cdap.common.DatasetAlreadyExistsException)4 HandlerException (co.cask.cdap.common.HandlerException)3 DatasetInstanceConfiguration (co.cask.cdap.proto.DatasetInstanceConfiguration)3 DatasetSpecification (co.cask.cdap.api.dataset.DatasetSpecification)2 DatasetNotFoundException (co.cask.cdap.common.DatasetNotFoundException)2 AuditPolicy (co.cask.cdap.common.security.AuditPolicy)2 DatasetId (co.cask.cdap.proto.id.DatasetId)2 DatasetTypeId (co.cask.cdap.proto.id.DatasetTypeId)2 NamespaceId (co.cask.cdap.proto.id.NamespaceId)2 Principal (co.cask.cdap.proto.security.Principal)2 UnauthorizedException (co.cask.cdap.security.spi.authorization.UnauthorizedException)2 HttpResponse (co.cask.common.http.HttpResponse)2 URL (java.net.URL)2 PUT (javax.ws.rs.PUT)2 Path (javax.ws.rs.Path)2 Test (org.junit.Test)2 DatasetProperties (co.cask.cdap.api.dataset.DatasetProperties)1 DatasetTypeClient (co.cask.cdap.client.DatasetTypeClient)1