use of co.cask.cdap.api.dataset.DatasetSpecification 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;
}
}
use of co.cask.cdap.api.dataset.DatasetSpecification in project cdap by caskdata.
the class DatasetInstanceService method dropAll.
/**
* Drops all datasets in the given namespace. If authorization is turned on, only datasets that the current
* principal that has {@link Action#ADMIN} privilege will be deleted
*
* @param namespaceId namespace to operate on
* @throws Exception if it fails to delete dataset
*/
void dropAll(NamespaceId namespaceId) throws Exception {
ensureNamespaceExists(namespaceId);
Principal principal = authenticationContext.getPrincipal();
Map<DatasetId, DatasetSpecification> datasets = new HashMap<>();
for (DatasetSpecification spec : instanceManager.getAll(namespaceId)) {
DatasetId datasetId = namespaceId.dataset(spec.getName());
if (!DatasetsUtil.isSystemDatasetInUserNamespace(datasetId)) {
authorizationEnforcer.enforce(datasetId, principal, Action.ADMIN);
}
datasets.put(datasetId, spec);
}
// auth check passed, we can start deleting the datasets
for (DatasetId datasetId : datasets.keySet()) {
dropDataset(datasetId, datasets.get(datasetId));
}
}
use of co.cask.cdap.api.dataset.DatasetSpecification in project cdap by caskdata.
the class DatasetInstanceService method getFromMds.
/**
* Read the dataset meta data (instance and type) from MDS.
*
* Note this method cannot be called to create dataset instance, since it does not have enforcement on the dataset
* type.
*/
private DatasetMeta getFromMds(DatasetId instance) throws Exception {
// TODO: CDAP-3901 add back namespace existence check
DatasetSpecification spec = instanceManager.get(instance);
if (spec == null) {
throw new NotFoundException(instance);
}
spec = DatasetsUtil.fixOriginalProperties(spec);
DatasetTypeId datasetTypeId = instance.getParent().datasetType(spec.getType());
// 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) {
// TODO: This shouldn't happen unless CDAP is in an invalid state - maybe give different error
throw new NotFoundException(datasetTypeId);
}
// for system dataset do not look up owner information in store as we know that it will be null.
// Also, this is required for CDAP to start, because initially we don't want to look up owner admin
// (causing its own lookup) as the SystemDatasetInitiator.getDataset is called when CDAP starts
String ownerPrincipal = null;
if (!NamespaceId.SYSTEM.equals(instance.getNamespaceId())) {
ownerPrincipal = ownerAdmin.getOwnerPrincipal(instance);
}
return new DatasetMeta(spec, typeMeta, null, ownerPrincipal);
}
use of co.cask.cdap.api.dataset.DatasetSpecification in project cdap by caskdata.
the class RemoteDatasetOpExecutor method update.
@Override
public DatasetSpecification update(DatasetId datasetInstanceId, DatasetTypeMeta typeMeta, DatasetProperties props, DatasetSpecification existing) throws Exception {
InternalDatasetCreationParams updateParams = new InternalDatasetUpdateParams(typeMeta, existing, props);
HttpResponse response = doRequest(datasetInstanceId, "update", GSON.toJson(updateParams));
return ObjectResponse.fromJsonBody(response, DatasetSpecification.class).getResponseObject();
}
use of co.cask.cdap.api.dataset.DatasetSpecification in project cdap by caskdata.
the class ConversionHelpers method spec2Summary.
static Collection<DatasetSpecificationSummary> spec2Summary(Collection<DatasetSpecification> specs) {
List<DatasetSpecificationSummary> datasetSummaries = Lists.newArrayList();
for (DatasetSpecification spec : specs) {
// HBaseQueueAdmin through DatasetFramework.
if (QueueConstants.STATE_STORE_NAME.equals(spec.getName())) {
continue;
}
spec = DatasetsUtil.fixOriginalProperties(spec);
datasetSummaries.add(new DatasetSpecificationSummary(spec.getName(), spec.getType(), spec.getDescription(), spec.getOriginalProperties()));
}
return datasetSummaries;
}
Aggregations