Search in sources :

Example 1 with DatasetNotFoundException

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

the class DatasetInstanceService method executeAdmin.

/**
 * Executes an admin operation on a dataset.
 *
 * @param datasetId the datasetId to execute the admin operation on
 * @param method the type of admin operation to execute
 * @return the {@link DatasetAdminOpResponse} from the HTTP handler
 * @throws NamespaceNotFoundException if the requested namespace was not found
 * @throws IOException if there was a problem in checking if the namespace exists over HTTP
 * @throws UnauthorizedException if perimeter security and authorization are enabled, and the current user does not
 *  have -
 *  <ol>
 *    <li>{@link StandardPermission#DELETE} privileges on the dataset for "truncate" </li>
 *    <li>{@link StandardPermission#UPDATE} privileges on the dataset for "upgrade" </li>
 *    <li>read privileges on the dataset for "exists"</li>
 *  <ol>
 */
DatasetAdminOpResponse executeAdmin(DatasetId datasetId, String method) throws Exception {
    ensureNamespaceExists(datasetId.getParent());
    Object result = null;
    // NOTE: one cannot directly call create and drop, instead this should be called thru
    // POST/DELETE @ /data/datasets/{datasetId-id}. Because we must create/drop metadata for these at same time
    Principal principal = authenticationContext.getPrincipal();
    switch(method) {
        case "exists":
            // ensure the user has some privilege on the dataset datasetId if it is not system dataset
            if (!DatasetsUtil.isSystemDatasetInUserNamespace(datasetId)) {
                accessEnforcer.enforce(datasetId, principal, StandardPermission.GET);
            }
            result = opExecutorClient.exists(datasetId);
            break;
        case "truncate":
            if (!DatasetsUtil.isSystemDatasetInUserNamespace(datasetId)) {
                accessEnforcer.enforce(datasetId, principal, StandardPermission.DELETE);
            }
            if (instanceManager.get(datasetId) == null) {
                throw new DatasetNotFoundException(datasetId);
            }
            opExecutorClient.truncate(datasetId);
            publishAudit(datasetId, AuditType.TRUNCATE);
            break;
        case "upgrade":
            if (!DatasetsUtil.isSystemDatasetInUserNamespace(datasetId)) {
                accessEnforcer.enforce(datasetId, principal, StandardPermission.UPDATE);
            }
            if (instanceManager.get(datasetId) == null) {
                throw new DatasetNotFoundException(datasetId);
            }
            opExecutorClient.upgrade(datasetId);
            publishAudit(datasetId, AuditType.UPDATE);
            break;
        default:
            throw new HandlerException(HttpResponseStatus.NOT_FOUND, "Invalid admin operation: " + method);
    }
    return new DatasetAdminOpResponse(result, null);
}
Also used : HandlerException(io.cdap.cdap.common.HandlerException) DatasetNotFoundException(io.cdap.cdap.common.DatasetNotFoundException) DatasetAdminOpResponse(io.cdap.cdap.data2.datafabric.dataset.service.executor.DatasetAdminOpResponse) Principal(io.cdap.cdap.proto.security.Principal)

Example 2 with DatasetNotFoundException

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

the class DatasetInstanceService method drop.

/**
 * Drops the specified dataset.
 *
 * @param datasetId the {@link DatasetId} to drop
 * @throws NamespaceNotFoundException if the namespace was not found
 * @throws DatasetNotFoundException if the dataset datasetId was not found
 * @throws IOException if there was a problem in checking if the namespace exists over HTTP
 * @throws UnauthorizedException if perimeter security and authorization are enabled, and the current user does not
 *  have {@link StandardPermission#DELETE} privileges on the dataset
 */
void drop(DatasetId datasetId) throws Exception {
    Principal requestingUser = authenticationContext.getPrincipal();
    if (!DatasetsUtil.isSystemDatasetInUserNamespace(datasetId)) {
        accessEnforcer.enforce(datasetId, requestingUser, StandardPermission.DELETE);
    }
    ensureNamespaceExists(datasetId.getParent());
    DatasetSpecification spec = instanceManager.get(datasetId);
    if (spec == null) {
        throw new DatasetNotFoundException(datasetId);
    }
    dropDataset(datasetId, spec);
}
Also used : DatasetSpecification(io.cdap.cdap.api.dataset.DatasetSpecification) DatasetNotFoundException(io.cdap.cdap.common.DatasetNotFoundException) Principal(io.cdap.cdap.proto.security.Principal)

Example 3 with DatasetNotFoundException

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

the class DatasetInstanceService method update.

/**
 * Updates an existing Dataset specification properties.
 * {@link DatasetInstanceConfiguration} is constructed based on request and the Dataset instance is updated.
 *
 * @param instance the dataset instance
 * @param properties the dataset properties to be used
 * @throws NamespaceNotFoundException if the specified namespace was not found
 * @throws DatasetNotFoundException if the dataset was not found
 * @throws DatasetTypeNotFoundException if the type of the existing dataset was not found
 * @throws UnauthorizedException if perimeter security and authorization are enabled, and the current user does not
 *  have {@link StandardPermission#UPDATE} privilege on the #instance
 */
void update(DatasetId instance, Map<String, String> properties) throws Exception {
    ensureNamespaceExists(instance.getParent());
    Principal requestingUser = authenticationContext.getPrincipal();
    if (!DatasetsUtil.isSystemDatasetInUserNamespace(instance)) {
        accessEnforcer.enforce(instance, requestingUser, StandardPermission.UPDATE);
    }
    DatasetSpecification existing = instanceManager.get(instance);
    if (existing == null) {
        throw new DatasetNotFoundException(instance);
    }
    LOG.info("Update dataset {}, properties: {}", instance.getEntityName(), ConversionHelpers.toJson(properties));
    // by pass the auth check for dataset type when the operation is not creation
    DatasetTypeMeta typeMeta = getTypeInfo(instance.getParent(), existing.getType(), true);
    if (typeMeta == null) {
        // Type not found in the instance's namespace and the system namespace. Bail out.
        throw new DatasetTypeNotFoundException(ConversionHelpers.toDatasetTypeId(instance.getParent(), existing.getType()));
    }
    // Note how we execute configure() via opExecutorClient (outside of ds service) to isolate running user code
    DatasetProperties datasetProperties = DatasetProperties.of(properties);
    DatasetCreationResponse response = opExecutorClient.update(instance, typeMeta, datasetProperties, existing);
    DatasetSpecification spec = response.getSpec();
    instanceManager.add(instance.getParent(), spec);
    metaCache.invalidate(instance);
    updateExplore(instance, datasetProperties, existing, spec);
    publishAudit(instance, AuditType.UPDATE);
    publishMetadata(instance, response.getMetadata());
}
Also used : DatasetProperties(io.cdap.cdap.api.dataset.DatasetProperties) DatasetSpecification(io.cdap.cdap.api.dataset.DatasetSpecification) DatasetTypeMeta(io.cdap.cdap.proto.DatasetTypeMeta) DatasetCreationResponse(io.cdap.cdap.data2.datafabric.dataset.service.executor.DatasetCreationResponse) DatasetNotFoundException(io.cdap.cdap.common.DatasetNotFoundException) DatasetTypeNotFoundException(io.cdap.cdap.common.DatasetTypeNotFoundException) Principal(io.cdap.cdap.proto.security.Principal)

Example 4 with DatasetNotFoundException

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

the class DatasetInstanceService method dropDataset.

/**
 * Drops a dataset.
 *
 * @param spec specification of dataset to be dropped.
 * @throws Exception on error.
 */
private void dropDataset(DatasetId instance, DatasetSpecification spec) throws Exception {
    LOG.info("Deleting dataset {}.{}", instance.getNamespace(), instance.getEntityName());
    disableExplore(instance, spec);
    if (!instanceManager.delete(instance)) {
        throw new DatasetNotFoundException(instance);
    }
    metaCache.invalidate(instance);
    // by pass the auth check for dataset type when the operation is not creation
    DatasetTypeMeta typeMeta = getTypeInfo(instance.getParent(), spec.getType(), true);
    if (typeMeta == null) {
        throw new DatasetNotFoundException(instance);
    }
    opExecutorClient.drop(instance, typeMeta, spec);
    // Remove metadata for the dataset
    LOG.trace("Removing metadata for dataset {}", instance);
    metadataServiceClient.drop(new MetadataMutation.Drop(instance.toMetadataEntity()));
    LOG.trace("Removed metadata for dataset {}", instance);
    publishAudit(instance, AuditType.DELETE);
    // deletes the owner principal for the entity if one was stored during creation
    ownerAdmin.delete(instance);
}
Also used : MetadataMutation(io.cdap.cdap.spi.metadata.MetadataMutation) DatasetTypeMeta(io.cdap.cdap.proto.DatasetTypeMeta) DatasetNotFoundException(io.cdap.cdap.common.DatasetNotFoundException)

Example 5 with DatasetNotFoundException

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

the class DatasetClient method delete.

/**
 * Deletes a dataset.
 *
 * @param instance the dataset to delete
 * @throws DatasetNotFoundException if the dataset with the specified name could not be found
 * @throws IOException if a network error occurred
 * @throws UnauthenticatedException if the request is not authorized successfully in the gateway server
 */
public void delete(DatasetId instance) throws DatasetNotFoundException, IOException, UnauthenticatedException, UnauthorizedException {
    URL url = config.resolveNamespacedURLV3(instance.getParent(), String.format("data/datasets/%s", instance.getDataset()));
    HttpResponse response = restClient.execute(HttpMethod.DELETE, url, config.getAccessToken(), HttpURLConnection.HTTP_NOT_FOUND);
    if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
        throw new DatasetNotFoundException(instance);
    }
}
Also used : HttpResponse(io.cdap.common.http.HttpResponse) DatasetNotFoundException(io.cdap.cdap.common.DatasetNotFoundException) URL(java.net.URL)

Aggregations

DatasetNotFoundException (io.cdap.cdap.common.DatasetNotFoundException)5 Principal (io.cdap.cdap.proto.security.Principal)3 DatasetSpecification (io.cdap.cdap.api.dataset.DatasetSpecification)2 DatasetTypeMeta (io.cdap.cdap.proto.DatasetTypeMeta)2 DatasetProperties (io.cdap.cdap.api.dataset.DatasetProperties)1 DatasetTypeNotFoundException (io.cdap.cdap.common.DatasetTypeNotFoundException)1 HandlerException (io.cdap.cdap.common.HandlerException)1 DatasetAdminOpResponse (io.cdap.cdap.data2.datafabric.dataset.service.executor.DatasetAdminOpResponse)1 DatasetCreationResponse (io.cdap.cdap.data2.datafabric.dataset.service.executor.DatasetCreationResponse)1 MetadataMutation (io.cdap.cdap.spi.metadata.MetadataMutation)1 HttpResponse (io.cdap.common.http.HttpResponse)1 URL (java.net.URL)1