Search in sources :

Example 1 with DatasetAdminOpResponse

use of co.cask.cdap.data2.datafabric.dataset.service.executor.DatasetAdminOpResponse in project cdap by caskdata.

the class DatasetInstanceHandler method executeAdmin.

/**
   * Executes an admin operation on a dataset instance.
   *
   * @param namespaceId namespace of the dataset instance
   * @param name name of the dataset instance
   * @param method the admin operation to execute (e.g. "exists", "truncate", "upgrade")
   * @throws Exception
   */
@POST
@Path("/data/datasets/{name}/admin/{method}")
public void executeAdmin(HttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId, @PathParam("name") String name, @PathParam("method") String method) throws Exception {
    DatasetId instance = ConversionHelpers.toDatasetInstanceId(namespaceId, name);
    try {
        DatasetAdminOpResponse response = instanceService.executeAdmin(instance, method);
        responder.sendJson(HttpResponseStatus.OK, response);
    } catch (HandlerException e) {
        responder.sendStatus(e.getFailureStatus());
    }
}
Also used : HandlerException(co.cask.cdap.common.HandlerException) DatasetAdminOpResponse(co.cask.cdap.data2.datafabric.dataset.service.executor.DatasetAdminOpResponse) DatasetId(co.cask.cdap.proto.id.DatasetId) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST)

Example 2 with DatasetAdminOpResponse

use of co.cask.cdap.data2.datafabric.dataset.service.executor.DatasetAdminOpResponse in project cdap by caskdata.

the class DatasetInstanceService method executeAdmin.

/**
   * Executes an admin operation on a dataset instance.
   *
   * @param instance the instance 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 Action#ADMIN} privileges on the #instance (for "drop" or "truncate") </li>
   *    <li>any privileges on the #instance (for "exists")</li>
   *  <ol>
   */
DatasetAdminOpResponse executeAdmin(DatasetId instance, String method) throws Exception {
    ensureNamespaceExists(instance.getParent());
    Object result = null;
    // NOTE: one cannot directly call create and drop, instead this should be called thru
    //       POST/DELETE @ /data/datasets/{instance-id}. Because we must create/drop metadata for these at same time
    Principal principal = authenticationContext.getPrincipal();
    switch(method) {
        case "exists":
            ensureAccess(instance);
            result = opExecutorClient.exists(instance);
            break;
        case "truncate":
            if (instanceManager.get(instance) == null) {
                throw new DatasetNotFoundException(instance);
            }
            authorizationEnforcer.enforce(instance, principal, Action.ADMIN);
            opExecutorClient.truncate(instance);
            publishAudit(instance, AuditType.TRUNCATE);
            break;
        case "upgrade":
            if (instanceManager.get(instance) == null) {
                throw new DatasetNotFoundException(instance);
            }
            authorizationEnforcer.enforce(instance, principal, Action.ADMIN);
            opExecutorClient.upgrade(instance);
            publishAudit(instance, AuditType.UPDATE);
            break;
        default:
            throw new HandlerException(HttpResponseStatus.NOT_FOUND, "Invalid admin operation: " + method);
    }
    return new DatasetAdminOpResponse(result, null);
}
Also used : HandlerException(co.cask.cdap.common.HandlerException) DatasetNotFoundException(co.cask.cdap.common.DatasetNotFoundException) DatasetAdminOpResponse(co.cask.cdap.data2.datafabric.dataset.service.executor.DatasetAdminOpResponse) Principal(co.cask.cdap.proto.security.Principal)

Aggregations

HandlerException (co.cask.cdap.common.HandlerException)2 DatasetAdminOpResponse (co.cask.cdap.data2.datafabric.dataset.service.executor.DatasetAdminOpResponse)2 DatasetNotFoundException (co.cask.cdap.common.DatasetNotFoundException)1 DatasetId (co.cask.cdap.proto.id.DatasetId)1 Principal (co.cask.cdap.proto.security.Principal)1 POST (javax.ws.rs.POST)1 Path (javax.ws.rs.Path)1