Search in sources :

Example 71 with Revision

use of org.apache.nifi.web.Revision in project nifi by apache.

the class ReportingTaskResource method updateReportingTask.

/**
 * Updates the specified a Reporting Task.
 *
 * @param httpServletRequest  request
 * @param id                  The id of the reporting task to update.
 * @param requestReportingTaskEntity A reportingTaskEntity.
 * @return A reportingTaskEntity.
 */
@PUT
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Path("{id}")
@ApiOperation(value = "Updates a reporting task", response = ReportingTaskEntity.class, authorizations = { @Authorization(value = "Write - /reporting-tasks/{uuid}"), @Authorization(value = "Read - any referenced Controller Services if this request changes the reference - /controller-services/{uuid}") })
@ApiResponses(value = { @ApiResponse(code = 400, message = "NiFi was unable to complete the request because it was invalid. The request should not be retried without modification."), @ApiResponse(code = 401, message = "Client could not be authenticated."), @ApiResponse(code = 403, message = "Client is not authorized to make this request."), @ApiResponse(code = 404, message = "The specified resource could not be found."), @ApiResponse(code = 409, message = "The request was valid but NiFi was not in the appropriate state to process it. Retrying the same request later may be successful.") })
public Response updateReportingTask(@Context final HttpServletRequest httpServletRequest, @ApiParam(value = "The reporting task id.", required = true) @PathParam("id") final String id, @ApiParam(value = "The reporting task configuration details.", required = true) final ReportingTaskEntity requestReportingTaskEntity) {
    if (requestReportingTaskEntity == null || requestReportingTaskEntity.getComponent() == null) {
        throw new IllegalArgumentException("Reporting task details must be specified.");
    }
    if (requestReportingTaskEntity.getRevision() == null) {
        throw new IllegalArgumentException("Revision must be specified.");
    }
    // ensure the ids are the same
    final ReportingTaskDTO requestReportingTaskDTO = requestReportingTaskEntity.getComponent();
    if (!id.equals(requestReportingTaskDTO.getId())) {
        throw new IllegalArgumentException(String.format("The reporting task id (%s) in the request body does not equal the " + "reporting task id of the requested resource (%s).", requestReportingTaskDTO.getId(), id));
    }
    if (isReplicateRequest()) {
        return replicate(HttpMethod.PUT, requestReportingTaskEntity);
    }
    // handle expects request (usually from the cluster manager)
    final Revision requestRevision = getRevision(requestReportingTaskEntity, id);
    return withWriteLock(serviceFacade, requestReportingTaskEntity, requestRevision, lookup -> {
        // authorize reporting task
        final ComponentAuthorizable authorizable = lookup.getReportingTask(id);
        authorizable.getAuthorizable().authorize(authorizer, RequestAction.WRITE, NiFiUserUtils.getNiFiUser());
        // authorize any referenced services
        AuthorizeControllerServiceReference.authorizeControllerServiceReferences(requestReportingTaskDTO.getProperties(), authorizable, authorizer, lookup);
    }, () -> serviceFacade.verifyUpdateReportingTask(requestReportingTaskDTO), (revision, reportingTaskEntity) -> {
        final ReportingTaskDTO reportingTaskDTO = reportingTaskEntity.getComponent();
        // update the reporting task
        final ReportingTaskEntity entity = serviceFacade.updateReportingTask(revision, reportingTaskDTO);
        populateRemainingReportingTaskEntityContent(entity);
        return generateOkResponse(entity).build();
    });
}
Also used : ComponentAuthorizable(org.apache.nifi.authorization.ComponentAuthorizable) Revision(org.apache.nifi.web.Revision) ReportingTaskDTO(org.apache.nifi.web.api.dto.ReportingTaskDTO) ReportingTaskEntity(org.apache.nifi.web.api.entity.ReportingTaskEntity) Path(javax.ws.rs.Path) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) ApiOperation(io.swagger.annotations.ApiOperation) PUT(javax.ws.rs.PUT) ApiResponses(io.swagger.annotations.ApiResponses)

Example 72 with Revision

use of org.apache.nifi.web.Revision in project nifi by apache.

the class NaiveRevisionManager method deleteRevision.

@Override
public <T> T deleteRevision(final RevisionClaim claim, final NiFiUser user, final DeleteRevisionTask<T> task) throws ExpiredRevisionClaimException {
    Objects.requireNonNull(user);
    logger.debug("Attempting to delete revision using {}", claim);
    final List<Revision> revisionList = new ArrayList<>(claim.getRevisions());
    revisionList.sort(new RevisionComparator());
    // Verify the provided revisions.
    String failedId = null;
    for (final Revision revision : revisionList) {
        final Revision curRevision = getRevision(revision.getComponentId());
        if (!curRevision.equals(revision)) {
            throw new ExpiredRevisionClaimException("Invalid Revision was given for component with ID '" + failedId + "'");
        }
    }
    // Perform the action provided
    final T taskResult = task.performTask();
    for (final Revision revision : revisionList) {
        revisionMap.remove(revision.getComponentId());
    }
    return taskResult;
}
Also used : Revision(org.apache.nifi.web.Revision) ArrayList(java.util.ArrayList)

Example 73 with Revision

use of org.apache.nifi.web.Revision in project nifi by apache.

the class ClusterReplicationComponentLifecycle method scheduleComponents.

@Override
public Set<AffectedComponentEntity> scheduleComponents(final URI exampleUri, final NiFiUser user, final String groupId, final Set<AffectedComponentEntity> components, final ScheduledState desiredState, final Pause pause) throws LifecycleManagementException {
    final Set<String> componentIds = components.stream().map(component -> component.getId()).collect(Collectors.toSet());
    final Map<String, AffectedComponentEntity> componentMap = components.stream().collect(Collectors.toMap(AffectedComponentEntity::getId, Function.identity()));
    final Map<String, Revision> componentRevisionMap = getRevisions(groupId, componentIds);
    final Map<String, RevisionDTO> componentRevisionDtoMap = componentRevisionMap.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, entry -> dtoFactory.createRevisionDTO(entry.getValue())));
    final ScheduleComponentsEntity scheduleProcessorsEntity = new ScheduleComponentsEntity();
    scheduleProcessorsEntity.setComponents(componentRevisionDtoMap);
    scheduleProcessorsEntity.setId(groupId);
    scheduleProcessorsEntity.setState(desiredState.name());
    URI scheduleGroupUri;
    try {
        scheduleGroupUri = new URI(exampleUri.getScheme(), exampleUri.getUserInfo(), exampleUri.getHost(), exampleUri.getPort(), "/nifi-api/flow/process-groups/" + groupId, null, exampleUri.getFragment());
    } catch (URISyntaxException e) {
        throw new RuntimeException(e);
    }
    final Map<String, String> headers = new HashMap<>();
    headers.put("content-type", MediaType.APPLICATION_JSON);
    // Determine whether we should replicate only to the cluster coordinator, or if we should replicate directly to the cluster nodes themselves.
    try {
        final NodeResponse clusterResponse;
        if (getReplicationTarget() == ReplicationTarget.CLUSTER_NODES) {
            clusterResponse = getRequestReplicator().replicate(user, HttpMethod.PUT, scheduleGroupUri, scheduleProcessorsEntity, headers).awaitMergedResponse();
        } else {
            clusterResponse = getRequestReplicator().forwardToCoordinator(getClusterCoordinatorNode(), user, HttpMethod.PUT, scheduleGroupUri, scheduleProcessorsEntity, headers).awaitMergedResponse();
        }
        final int scheduleComponentStatus = clusterResponse.getStatus();
        if (scheduleComponentStatus != Status.OK.getStatusCode()) {
            final String explanation = getResponseEntity(clusterResponse, String.class);
            throw new LifecycleManagementException("Failed to transition components to a state of " + desiredState + " due to " + explanation);
        }
        final boolean processorsTransitioned = waitForProcessorStatus(user, exampleUri, groupId, componentMap, desiredState, pause);
        if (!processorsTransitioned) {
            throw new LifecycleManagementException("Failed while waiting for components to transition to state of " + desiredState);
        }
    } catch (final InterruptedException ie) {
        Thread.currentThread().interrupt();
        throw new LifecycleManagementException("Interrupted while attempting to transition components to state of " + desiredState);
    }
    final Set<AffectedComponentEntity> updatedEntities = components.stream().map(component -> AffectedComponentUtils.updateEntity(component, serviceFacade, dtoFactory, user)).collect(Collectors.toSet());
    return updatedEntities;
}
Also used : NodeIdentifier(org.apache.nifi.cluster.protocol.NodeIdentifier) Revision(org.apache.nifi.web.Revision) ProcessorStatusDTO(org.apache.nifi.web.api.dto.status.ProcessorStatusDTO) URISyntaxException(java.net.URISyntaxException) LoggerFactory(org.slf4j.LoggerFactory) ControllerServiceEntity(org.apache.nifi.web.api.entity.ControllerServiceEntity) HashMap(java.util.HashMap) RevisionDTO(org.apache.nifi.web.api.dto.RevisionDTO) Function(java.util.function.Function) AffectedComponentDTO(org.apache.nifi.web.api.dto.AffectedComponentDTO) HttpMethod(javax.ws.rs.HttpMethod) ProcessorEntity(org.apache.nifi.web.api.entity.ProcessorEntity) MediaType(javax.ws.rs.core.MediaType) NiFiUser(org.apache.nifi.authorization.user.NiFiUser) ActivateControllerServicesEntity(org.apache.nifi.web.api.entity.ActivateControllerServicesEntity) Map(java.util.Map) ClusterCoordinator(org.apache.nifi.cluster.coordination.ClusterCoordinator) RequestReplicator(org.apache.nifi.cluster.coordination.http.replication.RequestReplicator) URI(java.net.URI) Status(javax.ws.rs.core.Response.Status) NodeResponse(org.apache.nifi.cluster.manager.NodeResponse) NiFiServiceFacade(org.apache.nifi.web.NiFiServiceFacade) NoClusterCoordinatorException(org.apache.nifi.cluster.exception.NoClusterCoordinatorException) Logger(org.slf4j.Logger) ControllerServicesEntity(org.apache.nifi.web.api.entity.ControllerServicesEntity) Set(java.util.Set) ProcessorsEntity(org.apache.nifi.web.api.entity.ProcessorsEntity) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) Collectors(java.util.stream.Collectors) ReplicationTarget(org.apache.nifi.web.api.ApplicationResource.ReplicationTarget) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) ScheduledState(org.apache.nifi.controller.ScheduledState) ControllerServiceState(org.apache.nifi.controller.service.ControllerServiceState) AffectedComponentEntity(org.apache.nifi.web.api.entity.AffectedComponentEntity) DtoFactory(org.apache.nifi.web.api.dto.DtoFactory) ScheduleComponentsEntity(org.apache.nifi.web.api.entity.ScheduleComponentsEntity) HashMap(java.util.HashMap) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) NodeResponse(org.apache.nifi.cluster.manager.NodeResponse) ScheduleComponentsEntity(org.apache.nifi.web.api.entity.ScheduleComponentsEntity) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) RevisionDTO(org.apache.nifi.web.api.dto.RevisionDTO) Revision(org.apache.nifi.web.Revision) AffectedComponentEntity(org.apache.nifi.web.api.entity.AffectedComponentEntity) HashMap(java.util.HashMap) Map(java.util.Map) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) MultivaluedMap(javax.ws.rs.core.MultivaluedMap)

Aggregations

Revision (org.apache.nifi.web.Revision)73 ApiOperation (io.swagger.annotations.ApiOperation)61 ApiResponses (io.swagger.annotations.ApiResponses)61 Consumes (javax.ws.rs.Consumes)61 Produces (javax.ws.rs.Produces)61 Path (javax.ws.rs.Path)60 Authorizable (org.apache.nifi.authorization.resource.Authorizable)51 PUT (javax.ws.rs.PUT)30 ComponentAuthorizable (org.apache.nifi.authorization.ComponentAuthorizable)30 POST (javax.ws.rs.POST)25 DELETE (javax.ws.rs.DELETE)24 ProcessGroupAuthorizable (org.apache.nifi.authorization.ProcessGroupAuthorizable)21 RevisionDTO (org.apache.nifi.web.api.dto.RevisionDTO)21 PositionDTO (org.apache.nifi.web.api.dto.PositionDTO)19 SnippetAuthorizable (org.apache.nifi.authorization.SnippetAuthorizable)17 NiFiUser (org.apache.nifi.authorization.user.NiFiUser)17 HashMap (java.util.HashMap)15 Map (java.util.Map)15 Set (java.util.Set)15 Collectors (java.util.stream.Collectors)15