Search in sources :

Example 6 with VersionControlInformationEntity

use of org.apache.nifi.web.api.entity.VersionControlInformationEntity in project nifi by apache.

the class PGChangeVersion method doExecute.

@Override
public VoidResult doExecute(final NiFiClient client, final Properties properties) throws NiFiClientException, IOException, MissingOptionException, CommandException {
    final String pgId = getRequiredArg(properties, CommandOption.PG_ID);
    final VersionsClient versionsClient = client.getVersionsClient();
    final VersionControlInformationEntity existingVersionControlInfo = versionsClient.getVersionControlInfo(pgId);
    final VersionControlInformationDTO existingVersionControlDTO = existingVersionControlInfo.getVersionControlInformation();
    if (existingVersionControlDTO == null) {
        throw new NiFiClientException("Process group is not under version control");
    }
    // start with the version specified in the arguments
    Integer newVersion = getIntArg(properties, CommandOption.FLOW_VERSION);
    // if no version was specified, automatically determine the latest and change to that
    if (newVersion == null) {
        newVersion = getLatestVersion(client, existingVersionControlDTO);
        if (newVersion.intValue() == existingVersionControlDTO.getVersion().intValue()) {
            throw new NiFiClientException("Process group already at latest version");
        }
    }
    // update the version in the existing DTO to the new version so we can submit it back
    existingVersionControlDTO.setVersion(newVersion);
    // initiate the version change which creates an update request that must be checked for completion
    final VersionedFlowUpdateRequestEntity initialUpdateRequest = versionsClient.updateVersionControlInfo(pgId, existingVersionControlInfo);
    // poll the update request for up to 30 seconds to see if it has completed
    // if it doesn't complete then an exception will be thrown, but in either case the request will be deleted
    final String updateRequestId = initialUpdateRequest.getRequest().getRequestId();
    try {
        boolean completed = false;
        for (int i = 0; i < 30; i++) {
            final VersionedFlowUpdateRequestEntity updateRequest = versionsClient.getUpdateRequest(updateRequestId);
            if (updateRequest != null && updateRequest.getRequest().isComplete()) {
                completed = true;
                break;
            } else {
                try {
                    if (getContext().isInteractive()) {
                        println("Waiting for update request to complete...");
                    }
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
        if (!completed) {
            throw new NiFiClientException("Unable to change version of process group, cancelling request");
        }
    } finally {
        versionsClient.deleteUpdateRequest(updateRequestId);
    }
    return VoidResult.getInstance();
}
Also used : NiFiClientException(org.apache.nifi.toolkit.cli.impl.client.nifi.NiFiClientException) VersionControlInformationEntity(org.apache.nifi.web.api.entity.VersionControlInformationEntity) VersionsClient(org.apache.nifi.toolkit.cli.impl.client.nifi.VersionsClient) VersionedFlowUpdateRequestEntity(org.apache.nifi.web.api.entity.VersionedFlowUpdateRequestEntity) VersionControlInformationDTO(org.apache.nifi.web.api.dto.VersionControlInformationDTO)

Example 7 with VersionControlInformationEntity

use of org.apache.nifi.web.api.entity.VersionControlInformationEntity in project nifi by apache.

the class PGGetAllVersions method doExecute.

@Override
public VersionedFlowSnapshotMetadataSetResult doExecute(final NiFiClient client, final Properties properties) throws NiFiClientException, IOException, MissingOptionException {
    final String pgId = getRequiredArg(properties, CommandOption.PG_ID);
    final VersionsClient versionsClient = client.getVersionsClient();
    final VersionControlInformationEntity existingVersionControlInfo = versionsClient.getVersionControlInfo(pgId);
    final VersionControlInformationDTO existingVersionControlDTO = existingVersionControlInfo.getVersionControlInformation();
    if (existingVersionControlDTO == null) {
        throw new NiFiClientException("Process group is not under version control");
    }
    final String registryId = existingVersionControlDTO.getRegistryId();
    final String bucketId = existingVersionControlDTO.getBucketId();
    final String flowId = existingVersionControlDTO.getFlowId();
    final FlowClient flowClient = client.getFlowClient();
    final VersionedFlowSnapshotMetadataSetEntity versions = flowClient.getVersions(registryId, bucketId, flowId);
    if (versions.getVersionedFlowSnapshotMetadataSet() == null || versions.getVersionedFlowSnapshotMetadataSet().isEmpty()) {
        throw new NiFiClientException("No versions available");
    }
    return new VersionedFlowSnapshotMetadataSetResult(getResultType(properties), versions);
}
Also used : NiFiClientException(org.apache.nifi.toolkit.cli.impl.client.nifi.NiFiClientException) VersionControlInformationEntity(org.apache.nifi.web.api.entity.VersionControlInformationEntity) VersionedFlowSnapshotMetadataSetResult(org.apache.nifi.toolkit.cli.impl.result.VersionedFlowSnapshotMetadataSetResult) VersionsClient(org.apache.nifi.toolkit.cli.impl.client.nifi.VersionsClient) FlowClient(org.apache.nifi.toolkit.cli.impl.client.nifi.FlowClient) VersionedFlowSnapshotMetadataSetEntity(org.apache.nifi.web.api.entity.VersionedFlowSnapshotMetadataSetEntity) VersionControlInformationDTO(org.apache.nifi.web.api.dto.VersionControlInformationDTO)

Example 8 with VersionControlInformationEntity

use of org.apache.nifi.web.api.entity.VersionControlInformationEntity in project nifi by apache.

the class PGGetVersion method doExecute.

@Override
public VersionControlInfoResult doExecute(final NiFiClient client, final Properties properties) throws NiFiClientException, IOException, MissingOptionException, CommandException {
    final String pgId = getRequiredArg(properties, CommandOption.PG_ID);
    final VersionControlInformationEntity entity = client.getVersionsClient().getVersionControlInfo(pgId);
    if (entity.getVersionControlInformation() == null) {
        throw new NiFiClientException("Process group is not under version control");
    }
    return new VersionControlInfoResult(getResultType(properties), entity);
}
Also used : NiFiClientException(org.apache.nifi.toolkit.cli.impl.client.nifi.NiFiClientException) VersionControlInformationEntity(org.apache.nifi.web.api.entity.VersionControlInformationEntity) VersionControlInfoResult(org.apache.nifi.toolkit.cli.impl.result.VersionControlInfoResult)

Example 9 with VersionControlInformationEntity

use of org.apache.nifi.web.api.entity.VersionControlInformationEntity in project nifi by apache.

the class VersionsResource method saveToFlowRegistry.

@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Path("process-groups/{id}")
@ApiOperation(value = "Save the Process Group with the given ID", response = VersionControlInformationEntity.class, notes = "Begins version controlling the Process Group with the given ID or commits changes to the Versioned Flow, " + "depending on if the provided VersionControlInformation includes a flowId. " + NON_GUARANTEED_ENDPOINT, authorizations = { @Authorization(value = "Read - /process-groups/{uuid}"), @Authorization(value = "Write - /process-groups/{uuid}"), @Authorization(value = "Read - /{component-type}/{uuid} - For all encapsulated components"), @Authorization(value = "Read - any referenced Controller Services by any encapsulated components - /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 saveToFlowRegistry(@ApiParam("The process group id.") @PathParam("id") final String groupId, @ApiParam(value = "The versioned flow details.", required = true) final StartVersionControlRequestEntity requestEntity) {
    // Verify the request
    final RevisionDTO revisionDto = requestEntity.getProcessGroupRevision();
    if (revisionDto == null) {
        throw new IllegalArgumentException("Process Group Revision must be specified");
    }
    final VersionedFlowDTO versionedFlowDto = requestEntity.getVersionedFlow();
    if (versionedFlowDto == null) {
        throw new IllegalArgumentException("Version Control Information must be supplied.");
    }
    if (StringUtils.isEmpty(versionedFlowDto.getBucketId())) {
        throw new IllegalArgumentException("The Bucket ID must be supplied.");
    }
    if (StringUtils.isEmpty(versionedFlowDto.getFlowName()) && StringUtils.isEmpty(versionedFlowDto.getFlowId())) {
        throw new IllegalArgumentException("The Flow Name or Flow ID must be supplied.");
    }
    if (versionedFlowDto.getFlowName() != null && versionedFlowDto.getFlowName().length() > 1000) {
        throw new IllegalArgumentException("The Flow Name cannot exceed 1,000 characters");
    }
    if (StringUtils.isEmpty(versionedFlowDto.getRegistryId())) {
        throw new IllegalArgumentException("The Registry ID must be supplied.");
    }
    if (versionedFlowDto.getDescription() != null && versionedFlowDto.getDescription().length() > 65535) {
        throw new IllegalArgumentException("Flow Description cannot exceed 65,535 characters");
    }
    if (versionedFlowDto.getComments() != null && versionedFlowDto.getComments().length() > 65535) {
        throw new IllegalArgumentException("Comments cannot exceed 65,535 characters");
    }
    // ensure we're not attempting to version the root group
    final ProcessGroupEntity root = serviceFacade.getProcessGroup(FlowController.ROOT_GROUP_ID_ALIAS);
    if (root.getId().equals(groupId)) {
        throw new IllegalArgumentException("The Root Process Group cannot be versioned.");
    }
    if (isReplicateRequest()) {
        // We first have to obtain a "lock" on all nodes in the cluster so that multiple Version Control requests
        // are not being made simultaneously. We do this by making a POST to /nifi-api/versions/active-requests.
        // The Response gives us back the Request ID.
        final URI requestUri;
        try {
            final URI originalUri = getAbsolutePath();
            final String requestId = lockVersionControl(originalUri, groupId);
            requestUri = new URI(originalUri.getScheme(), originalUri.getUserInfo(), originalUri.getHost(), originalUri.getPort(), "/nifi-api/versions/active-requests/" + requestId, null, originalUri.getFragment());
        } catch (final URISyntaxException e) {
            throw new RuntimeException(e);
        }
        // Finally, we can delete the Request.
        try {
            final VersionControlComponentMappingEntity mappingEntity = serviceFacade.registerFlowWithFlowRegistry(groupId, requestEntity);
            replicateVersionControlMapping(mappingEntity, requestEntity, requestUri, groupId);
            final VersionControlInformationEntity responseEntity = serviceFacade.getVersionControlInformation(groupId);
            return generateOkResponse(responseEntity).build();
        } finally {
            unlockVersionControl(requestUri, groupId);
        }
    }
    // Perform local task. If running in a cluster environment, we will never get to this point. This is because
    // in the above block, we check if (isReplicate()) and if true, we implement the 'cluster logic', but this
    // does not involve replicating the actual request, because we only want a single node to handle the logic of
    // creating the flow in the Registry.
    final Revision groupRevision = new Revision(revisionDto.getVersion(), revisionDto.getClientId(), groupId);
    return withWriteLock(serviceFacade, requestEntity, groupRevision, lookup -> {
        final ProcessGroupAuthorizable groupAuthorizable = lookup.getProcessGroup(groupId);
        final Authorizable processGroup = groupAuthorizable.getAuthorizable();
        // require write to this group
        processGroup.authorize(authorizer, RequestAction.WRITE, NiFiUserUtils.getNiFiUser());
        // require read to this group and all descendants
        authorizeProcessGroup(groupAuthorizable, authorizer, lookup, RequestAction.READ, true, false, true, true);
    }, () -> {
        final VersionedFlowDTO versionedFlow = requestEntity.getVersionedFlow();
        final String registryId = versionedFlow.getRegistryId();
        final String bucketId = versionedFlow.getBucketId();
        final String flowId = versionedFlow.getFlowId();
        serviceFacade.verifyCanSaveToFlowRegistry(groupId, registryId, bucketId, flowId);
    }, (rev, flowEntity) -> {
        // Register the current flow with the Flow Registry.
        final VersionControlComponentMappingEntity mappingEntity = serviceFacade.registerFlowWithFlowRegistry(groupId, flowEntity);
        // Update the Process Group's Version Control Information
        final VersionControlInformationEntity responseEntity = serviceFacade.setVersionControlInformation(rev, groupId, mappingEntity.getVersionControlInformation(), mappingEntity.getVersionControlComponentMapping());
        return generateOkResponse(responseEntity).build();
    });
}
Also used : ProcessGroupEntity(org.apache.nifi.web.api.entity.ProcessGroupEntity) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) RevisionDTO(org.apache.nifi.web.api.dto.RevisionDTO) VersionControlInformationEntity(org.apache.nifi.web.api.entity.VersionControlInformationEntity) ProcessGroupAuthorizable(org.apache.nifi.authorization.ProcessGroupAuthorizable) VersionedFlowDTO(org.apache.nifi.web.api.dto.VersionedFlowDTO) Revision(org.apache.nifi.web.Revision) ComponentAuthorizable(org.apache.nifi.authorization.ComponentAuthorizable) Authorizable(org.apache.nifi.authorization.resource.Authorizable) ProcessGroupAuthorizable(org.apache.nifi.authorization.ProcessGroupAuthorizable) VersionControlComponentMappingEntity(org.apache.nifi.web.api.entity.VersionControlComponentMappingEntity) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 10 with VersionControlInformationEntity

use of org.apache.nifi.web.api.entity.VersionControlInformationEntity in project nifi by apache.

the class VersionsResource method deleteRequest.

private Response deleteRequest(final String requestType, final String requestId) {
    if (requestId == null) {
        throw new IllegalArgumentException("Request ID must be specified.");
    }
    final NiFiUser user = NiFiUserUtils.getNiFiUser();
    // request manager will ensure that the current is the user that submitted this request
    final AsynchronousWebRequest<VersionControlInformationEntity> asyncRequest = requestManager.removeRequest(requestType, requestId, user);
    if (asyncRequest == null) {
        throw new ResourceNotFoundException("Could not find request of type " + requestType + " with ID " + requestId);
    }
    if (!asyncRequest.isComplete()) {
        asyncRequest.cancel();
    }
    final VersionedFlowUpdateRequestDTO updateRequestDto = new VersionedFlowUpdateRequestDTO();
    updateRequestDto.setComplete(asyncRequest.isComplete());
    updateRequestDto.setFailureReason(asyncRequest.getFailureReason());
    updateRequestDto.setLastUpdated(asyncRequest.getLastUpdated());
    updateRequestDto.setProcessGroupId(asyncRequest.getProcessGroupId());
    updateRequestDto.setRequestId(requestId);
    updateRequestDto.setUri(generateResourceUri("versions", requestType, requestId));
    updateRequestDto.setPercentCompleted(asyncRequest.getPercentComplete());
    updateRequestDto.setState(asyncRequest.getState());
    if (updateRequestDto.isComplete()) {
        final VersionControlInformationEntity vciEntity = serviceFacade.getVersionControlInformation(asyncRequest.getProcessGroupId());
        updateRequestDto.setVersionControlInformation(vciEntity == null ? null : vciEntity.getVersionControlInformation());
    }
    final RevisionDTO groupRevision = serviceFacade.getProcessGroup(asyncRequest.getProcessGroupId()).getRevision();
    final VersionedFlowUpdateRequestEntity updateRequestEntity = new VersionedFlowUpdateRequestEntity();
    updateRequestEntity.setProcessGroupRevision(groupRevision);
    updateRequestEntity.setRequest(updateRequestDto);
    return generateOkResponse(updateRequestEntity).build();
}
Also used : VersionControlInformationEntity(org.apache.nifi.web.api.entity.VersionControlInformationEntity) NiFiUser(org.apache.nifi.authorization.user.NiFiUser) VersionedFlowUpdateRequestDTO(org.apache.nifi.web.api.dto.VersionedFlowUpdateRequestDTO) ResourceNotFoundException(org.apache.nifi.web.ResourceNotFoundException) RevisionDTO(org.apache.nifi.web.api.dto.RevisionDTO) VersionedFlowUpdateRequestEntity(org.apache.nifi.web.api.entity.VersionedFlowUpdateRequestEntity)

Aggregations

VersionControlInformationEntity (org.apache.nifi.web.api.entity.VersionControlInformationEntity)15 ApiOperation (io.swagger.annotations.ApiOperation)8 ApiResponses (io.swagger.annotations.ApiResponses)8 Consumes (javax.ws.rs.Consumes)8 Path (javax.ws.rs.Path)8 Produces (javax.ws.rs.Produces)8 ComponentAuthorizable (org.apache.nifi.authorization.ComponentAuthorizable)7 ProcessGroupAuthorizable (org.apache.nifi.authorization.ProcessGroupAuthorizable)7 Authorizable (org.apache.nifi.authorization.resource.Authorizable)7 Revision (org.apache.nifi.web.Revision)7 RevisionDTO (org.apache.nifi.web.api.dto.RevisionDTO)7 VersionControlInformationDTO (org.apache.nifi.web.api.dto.VersionControlInformationDTO)7 NiFiUser (org.apache.nifi.authorization.user.NiFiUser)6 ProcessGroupEntity (org.apache.nifi.web.api.entity.ProcessGroupEntity)6 PUT (javax.ws.rs.PUT)5 VersionedFlowUpdateRequestEntity (org.apache.nifi.web.api.entity.VersionedFlowUpdateRequestEntity)5 URI (java.net.URI)4 URISyntaxException (java.net.URISyntaxException)4 Map (java.util.Map)4 POST (javax.ws.rs.POST)4