Search in sources :

Example 21 with VersionedFlowSnapshotMetadata

use of org.apache.nifi.registry.flow.VersionedFlowSnapshotMetadata in project nifi by apache.

the class VersionsResource method updateFlowVersion.

@PUT
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Path("process-groups/{id}")
@ApiOperation(value = "Update the version of a Process Group with the given ID", response = VersionControlInformationEntity.class, notes = "For a Process Group that is already under Version Control, this will update the version of the flow to a different version. This endpoint expects " + "that the given snapshot will not modify any Processor that is currently running or any Controller Service that is enabled. " + NON_GUARANTEED_ENDPOINT, authorizations = { @Authorization(value = "Read - /process-groups/{uuid}"), @Authorization(value = "Write - /process-groups/{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 updateFlowVersion(@ApiParam("The process group id.") @PathParam("id") final String groupId, @ApiParam(value = "The controller service configuration details.", required = true) final VersionedFlowSnapshotEntity requestEntity) {
    // Verify the request
    final RevisionDTO revisionDto = requestEntity.getProcessGroupRevision();
    if (revisionDto == null) {
        throw new IllegalArgumentException("Process Group Revision must be specified.");
    }
    final VersionedFlowSnapshot requestFlowSnapshot = requestEntity.getVersionedFlowSnapshot();
    if (requestFlowSnapshot == null) {
        throw new IllegalArgumentException("Versioned Flow Snapshot must be supplied.");
    }
    final VersionedFlowSnapshotMetadata requestSnapshotMetadata = requestFlowSnapshot.getSnapshotMetadata();
    if (requestSnapshotMetadata == null) {
        throw new IllegalArgumentException("Snapshot Metadata must be supplied.");
    }
    if (requestSnapshotMetadata.getBucketIdentifier() == null) {
        throw new IllegalArgumentException("The Bucket ID must be supplied.");
    }
    if (requestSnapshotMetadata.getFlowIdentifier() == null) {
        throw new IllegalArgumentException("The Flow ID must be supplied.");
    }
    // Perform the request
    if (isReplicateRequest()) {
        return replicate(HttpMethod.PUT, requestEntity);
    }
    final Revision requestRevision = getRevision(requestEntity.getProcessGroupRevision(), groupId);
    return withWriteLock(serviceFacade, requestEntity, requestRevision, lookup -> {
        final ProcessGroupAuthorizable groupAuthorizable = lookup.getProcessGroup(groupId);
        final Authorizable processGroup = groupAuthorizable.getAuthorizable();
        processGroup.authorize(authorizer, RequestAction.READ, NiFiUserUtils.getNiFiUser());
        processGroup.authorize(authorizer, RequestAction.WRITE, NiFiUserUtils.getNiFiUser());
    }, () -> {
        // We do not enforce that the Process Group is 'not dirty' because at this point,
        // the client has explicitly indicated the dataflow that the Process Group should
        // provide and provided the Revision to ensure that they have the most up-to-date
        // view of the Process Group.
        serviceFacade.verifyCanUpdate(groupId, requestFlowSnapshot, true, false);
    }, (rev, entity) -> {
        final VersionedFlowSnapshot flowSnapshot = entity.getVersionedFlowSnapshot();
        final VersionedFlowSnapshotMetadata snapshotMetadata = flowSnapshot.getSnapshotMetadata();
        final Bucket bucket = flowSnapshot.getBucket();
        final VersionedFlow flow = flowSnapshot.getFlow();
        // Update the Process Group to match the proposed flow snapshot
        final VersionControlInformationDTO versionControlInfoDto = new VersionControlInformationDTO();
        versionControlInfoDto.setBucketId(snapshotMetadata.getBucketIdentifier());
        versionControlInfoDto.setBucketName(bucket.getName());
        versionControlInfoDto.setFlowId(snapshotMetadata.getFlowIdentifier());
        versionControlInfoDto.setFlowName(flow.getName());
        versionControlInfoDto.setFlowDescription(flow.getDescription());
        versionControlInfoDto.setGroupId(groupId);
        versionControlInfoDto.setVersion(snapshotMetadata.getVersion());
        versionControlInfoDto.setRegistryId(entity.getRegistryId());
        versionControlInfoDto.setRegistryName(serviceFacade.getFlowRegistryName(entity.getRegistryId()));
        final VersionedFlowState flowState = snapshotMetadata.getVersion() == flow.getVersionCount() ? VersionedFlowState.UP_TO_DATE : VersionedFlowState.STALE;
        versionControlInfoDto.setState(flowState.name());
        final NiFiUser user = NiFiUserUtils.getNiFiUser();
        final ProcessGroupEntity updatedGroup = serviceFacade.updateProcessGroupContents(user, rev, groupId, versionControlInfoDto, flowSnapshot, getIdGenerationSeed().orElse(null), false, true, entity.getUpdateDescendantVersionedFlows());
        final VersionControlInformationDTO updatedVci = updatedGroup.getComponent().getVersionControlInformation();
        final VersionControlInformationEntity responseEntity = new VersionControlInformationEntity();
        responseEntity.setProcessGroupRevision(updatedGroup.getRevision());
        responseEntity.setVersionControlInformation(updatedVci);
        return generateOkResponse(responseEntity).build();
    });
}
Also used : ProcessGroupEntity(org.apache.nifi.web.api.entity.ProcessGroupEntity) NiFiUser(org.apache.nifi.authorization.user.NiFiUser) VersionedFlow(org.apache.nifi.registry.flow.VersionedFlow) RevisionDTO(org.apache.nifi.web.api.dto.RevisionDTO) VersionedFlowSnapshotMetadata(org.apache.nifi.registry.flow.VersionedFlowSnapshotMetadata) VersionControlInformationDTO(org.apache.nifi.web.api.dto.VersionControlInformationDTO) ProcessGroupAuthorizable(org.apache.nifi.authorization.ProcessGroupAuthorizable) VersionControlInformationEntity(org.apache.nifi.web.api.entity.VersionControlInformationEntity) Revision(org.apache.nifi.web.Revision) Bucket(org.apache.nifi.registry.bucket.Bucket) VersionedFlowSnapshot(org.apache.nifi.registry.flow.VersionedFlowSnapshot) VersionedFlowState(org.apache.nifi.registry.flow.VersionedFlowState) ComponentAuthorizable(org.apache.nifi.authorization.ComponentAuthorizable) Authorizable(org.apache.nifi.authorization.resource.Authorizable) ProcessGroupAuthorizable(org.apache.nifi.authorization.ProcessGroupAuthorizable) 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 22 with VersionedFlowSnapshotMetadata

use of org.apache.nifi.registry.flow.VersionedFlowSnapshotMetadata in project nifi-registry by apache.

the class SecureNiFiRegistryClientIT method testCrudOperations.

@Test
public void testCrudOperations() throws IOException, NiFiRegistryException {
    final Bucket bucket = new Bucket();
    bucket.setName("Bucket 1");
    bucket.setDescription("This is bucket 1");
    final BucketClient bucketClient = client.getBucketClient();
    final Bucket createdBucket = bucketClient.create(bucket);
    Assert.assertNotNull(createdBucket);
    Assert.assertNotNull(createdBucket.getIdentifier());
    final List<Bucket> buckets = bucketClient.getAll();
    Assert.assertEquals(1, buckets.size());
    final VersionedFlow flow = new VersionedFlow();
    flow.setBucketIdentifier(createdBucket.getIdentifier());
    flow.setName("Flow 1");
    final FlowClient flowClient = client.getFlowClient();
    final VersionedFlow createdFlow = flowClient.create(flow);
    Assert.assertNotNull(createdFlow);
    Assert.assertNotNull(createdFlow.getIdentifier());
    final VersionedFlowSnapshotMetadata snapshotMetadata = new VersionedFlowSnapshotMetadata();
    snapshotMetadata.setBucketIdentifier(createdFlow.getBucketIdentifier());
    snapshotMetadata.setFlowIdentifier(createdFlow.getIdentifier());
    snapshotMetadata.setVersion(1);
    snapshotMetadata.setComments("This is snapshot #1");
    final VersionedProcessGroup rootProcessGroup = new VersionedProcessGroup();
    rootProcessGroup.setIdentifier("root-pg");
    rootProcessGroup.setName("Root Process Group");
    final VersionedFlowSnapshot snapshot = new VersionedFlowSnapshot();
    snapshot.setSnapshotMetadata(snapshotMetadata);
    snapshot.setFlowContents(rootProcessGroup);
    final FlowSnapshotClient snapshotClient = client.getFlowSnapshotClient();
    final VersionedFlowSnapshot createdSnapshot = snapshotClient.create(snapshot);
    Assert.assertNotNull(createdSnapshot);
    Assert.assertEquals("CN=user1, OU=nifi", createdSnapshot.getSnapshotMetadata().getAuthor());
}
Also used : BucketClient(org.apache.nifi.registry.client.BucketClient) Bucket(org.apache.nifi.registry.bucket.Bucket) VersionedFlow(org.apache.nifi.registry.flow.VersionedFlow) VersionedProcessGroup(org.apache.nifi.registry.flow.VersionedProcessGroup) VersionedFlowSnapshot(org.apache.nifi.registry.flow.VersionedFlowSnapshot) FlowClient(org.apache.nifi.registry.client.FlowClient) FlowSnapshotClient(org.apache.nifi.registry.client.FlowSnapshotClient) VersionedFlowSnapshotMetadata(org.apache.nifi.registry.flow.VersionedFlowSnapshotMetadata) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 23 with VersionedFlowSnapshotMetadata

use of org.apache.nifi.registry.flow.VersionedFlowSnapshotMetadata in project nifi-registry by apache.

the class UnsecuredNiFiRegistryClientIT method buildSnapshot.

private static VersionedFlowSnapshot buildSnapshot(VersionedFlow flow, int num) {
    final VersionedFlowSnapshotMetadata snapshotMetadata = new VersionedFlowSnapshotMetadata();
    snapshotMetadata.setBucketIdentifier(flow.getBucketIdentifier());
    snapshotMetadata.setFlowIdentifier(flow.getIdentifier());
    snapshotMetadata.setVersion(num);
    snapshotMetadata.setComments("This is snapshot #" + num);
    final VersionedProcessGroup rootProcessGroup = new VersionedProcessGroup();
    rootProcessGroup.setIdentifier("root-pg");
    rootProcessGroup.setName("Root Process Group");
    final VersionedProcessGroup subProcessGroup = new VersionedProcessGroup();
    subProcessGroup.setIdentifier("sub-pg");
    subProcessGroup.setName("Sub Process Group");
    rootProcessGroup.getProcessGroups().add(subProcessGroup);
    final Map<String, String> processorProperties = new HashMap<>();
    processorProperties.put("Prop 1", "Val 1");
    processorProperties.put("Prop 2", "Val 2");
    final Map<String, VersionedPropertyDescriptor> propertyDescriptors = new HashMap<>();
    final VersionedProcessor processor1 = new VersionedProcessor();
    processor1.setIdentifier("p1");
    processor1.setName("Processor 1");
    processor1.setProperties(processorProperties);
    processor1.setPropertyDescriptors(propertyDescriptors);
    final VersionedProcessor processor2 = new VersionedProcessor();
    processor2.setIdentifier("p2");
    processor2.setName("Processor 2");
    processor2.setProperties(processorProperties);
    processor2.setPropertyDescriptors(propertyDescriptors);
    subProcessGroup.getProcessors().add(processor1);
    subProcessGroup.getProcessors().add(processor2);
    final VersionedFlowSnapshot snapshot = new VersionedFlowSnapshot();
    snapshot.setSnapshotMetadata(snapshotMetadata);
    snapshot.setFlowContents(rootProcessGroup);
    return snapshot;
}
Also used : HashMap(java.util.HashMap) VersionedProcessGroup(org.apache.nifi.registry.flow.VersionedProcessGroup) VersionedFlowSnapshot(org.apache.nifi.registry.flow.VersionedFlowSnapshot) VersionedFlowSnapshotMetadata(org.apache.nifi.registry.flow.VersionedFlowSnapshotMetadata) VersionedPropertyDescriptor(org.apache.nifi.registry.flow.VersionedPropertyDescriptor) VersionedProcessor(org.apache.nifi.registry.flow.VersionedProcessor)

Example 24 with VersionedFlowSnapshotMetadata

use of org.apache.nifi.registry.flow.VersionedFlowSnapshotMetadata in project nifi-registry by apache.

the class DataModelMapper method map.

public static VersionedFlowSnapshotMetadata map(final BucketEntity bucketEntity, final FlowSnapshotEntity flowSnapshotEntity) {
    final VersionedFlowSnapshotMetadata metadata = new VersionedFlowSnapshotMetadata();
    metadata.setFlowIdentifier(flowSnapshotEntity.getFlowId());
    metadata.setVersion(flowSnapshotEntity.getVersion());
    metadata.setComments(flowSnapshotEntity.getComments());
    metadata.setTimestamp(flowSnapshotEntity.getCreated().getTime());
    metadata.setAuthor(flowSnapshotEntity.getCreatedBy());
    if (bucketEntity != null) {
        metadata.setBucketIdentifier(bucketEntity.getId());
    }
    return metadata;
}
Also used : VersionedFlowSnapshotMetadata(org.apache.nifi.registry.flow.VersionedFlowSnapshotMetadata)

Example 25 with VersionedFlowSnapshotMetadata

use of org.apache.nifi.registry.flow.VersionedFlowSnapshotMetadata in project nifi-registry by apache.

the class RegistryService method createFlowSnapshot.

// ---------------------- VersionedFlowSnapshot methods ---------------------------------------------
public VersionedFlowSnapshot createFlowSnapshot(final VersionedFlowSnapshot flowSnapshot) {
    if (flowSnapshot == null) {
        throw new IllegalArgumentException("Versioned flow snapshot cannot be null");
    }
    // validation will ensure that the metadata and contents are not null
    if (flowSnapshot.getSnapshotMetadata() != null) {
        flowSnapshot.getSnapshotMetadata().setTimestamp(System.currentTimeMillis());
    }
    // these fields aren't used for creation
    flowSnapshot.setFlow(null);
    flowSnapshot.setBucket(null);
    validate(flowSnapshot, "Cannot create versioned flow snapshot");
    writeLock.lock();
    try {
        final VersionedFlowSnapshotMetadata snapshotMetadata = flowSnapshot.getSnapshotMetadata();
        // ensure the bucket exists
        final BucketEntity existingBucket = metadataService.getBucketById(snapshotMetadata.getBucketIdentifier());
        if (existingBucket == null) {
            LOGGER.warn("The specified bucket id [{}] does not exist.", snapshotMetadata.getBucketIdentifier());
            throw new ResourceNotFoundException("The specified bucket ID does not exist in this registry.");
        }
        // ensure the flow exists
        final FlowEntity existingFlow = metadataService.getFlowById(snapshotMetadata.getFlowIdentifier());
        if (existingFlow == null) {
            LOGGER.warn("The specified flow id [{}] does not exist.", snapshotMetadata.getFlowIdentifier());
            throw new ResourceNotFoundException("The specified flow ID does not exist in this bucket.");
        }
        if (!existingBucket.getId().equals(existingFlow.getBucketId())) {
            throw new IllegalStateException("The requested flow is not located in the given bucket");
        }
        // convert the set of FlowSnapshotEntity to set of VersionedFlowSnapshotMetadata
        final SortedSet<VersionedFlowSnapshotMetadata> sortedSnapshots = new TreeSet<>();
        final List<FlowSnapshotEntity> existingFlowSnapshots = metadataService.getSnapshots(existingFlow.getId());
        if (existingFlowSnapshots != null) {
            existingFlowSnapshots.stream().forEach(s -> sortedSnapshots.add(DataModelMapper.map(existingBucket, s)));
        }
        // if we already have snapshots we need to verify the new one has the correct version
        if (sortedSnapshots != null && sortedSnapshots.size() > 0) {
            final VersionedFlowSnapshotMetadata lastSnapshot = sortedSnapshots.last();
            if (snapshotMetadata.getVersion() <= lastSnapshot.getVersion()) {
                throw new IllegalStateException("A Versioned flow snapshot with the same version already exists: " + snapshotMetadata.getVersion());
            }
            if (snapshotMetadata.getVersion() > (lastSnapshot.getVersion() + 1)) {
                throw new IllegalStateException("Version must be a one-up number, last version was " + lastSnapshot.getVersion() + " and version for this snapshot was " + snapshotMetadata.getVersion());
            }
        } else if (snapshotMetadata.getVersion() != 1) {
            throw new IllegalStateException("Version of first snapshot must be 1");
        }
        // serialize the snapshot
        final ByteArrayOutputStream out = new ByteArrayOutputStream();
        processGroupSerializer.serialize(flowSnapshot.getFlowContents(), out);
        // save the serialized snapshot to the persistence provider
        final Bucket bucket = DataModelMapper.map(existingBucket);
        final VersionedFlow versionedFlow = DataModelMapper.map(existingBucket, existingFlow);
        final FlowSnapshotContext context = new StandardFlowSnapshotContext.Builder(bucket, versionedFlow, snapshotMetadata).build();
        flowPersistenceProvider.saveFlowContent(context, out.toByteArray());
        // create snapshot in the metadata provider
        metadataService.createFlowSnapshot(DataModelMapper.map(snapshotMetadata));
        // update the modified date on the flow
        metadataService.updateFlow(existingFlow);
        // get the updated flow, we need to use "with counts" here so we can return this is a part of the response
        final FlowEntity updatedFlow = metadataService.getFlowByIdWithSnapshotCounts(snapshotMetadata.getFlowIdentifier());
        if (updatedFlow == null) {
            throw new ResourceNotFoundException("Versioned flow does not exist for identifier " + snapshotMetadata.getFlowIdentifier());
        }
        final VersionedFlow updatedVersionedFlow = DataModelMapper.map(existingBucket, updatedFlow);
        flowSnapshot.setBucket(bucket);
        flowSnapshot.setFlow(updatedVersionedFlow);
        return flowSnapshot;
    } finally {
        writeLock.unlock();
    }
}
Also used : FlowSnapshotContext(org.apache.nifi.registry.flow.FlowSnapshotContext) StandardFlowSnapshotContext(org.apache.nifi.registry.provider.flow.StandardFlowSnapshotContext) VersionedFlow(org.apache.nifi.registry.flow.VersionedFlow) ByteArrayOutputStream(java.io.ByteArrayOutputStream) VersionedFlowSnapshotMetadata(org.apache.nifi.registry.flow.VersionedFlowSnapshotMetadata) FlowEntity(org.apache.nifi.registry.db.entity.FlowEntity) BucketEntity(org.apache.nifi.registry.db.entity.BucketEntity) FlowSnapshotEntity(org.apache.nifi.registry.db.entity.FlowSnapshotEntity) Bucket(org.apache.nifi.registry.bucket.Bucket) TreeSet(java.util.TreeSet) StandardFlowSnapshotContext(org.apache.nifi.registry.provider.flow.StandardFlowSnapshotContext) ResourceNotFoundException(org.apache.nifi.registry.exception.ResourceNotFoundException)

Aggregations

VersionedFlowSnapshotMetadata (org.apache.nifi.registry.flow.VersionedFlowSnapshotMetadata)30 VersionedFlowSnapshot (org.apache.nifi.registry.flow.VersionedFlowSnapshot)14 VersionedFlow (org.apache.nifi.registry.flow.VersionedFlow)9 Test (org.junit.Test)9 Bucket (org.apache.nifi.registry.bucket.Bucket)8 BucketEntity (org.apache.nifi.registry.db.entity.BucketEntity)8 FlowEntity (org.apache.nifi.registry.db.entity.FlowEntity)8 FlowSnapshotEntity (org.apache.nifi.registry.db.entity.FlowSnapshotEntity)8 VersionedProcessGroup (org.apache.nifi.registry.flow.VersionedProcessGroup)8 FlowSnapshotClient (org.apache.nifi.registry.client.FlowSnapshotClient)6 NiFiRegistryException (org.apache.nifi.registry.client.NiFiRegistryException)6 Date (java.util.Date)5 ApiOperation (io.swagger.annotations.ApiOperation)4 ApiResponses (io.swagger.annotations.ApiResponses)4 IOException (java.io.IOException)4 Consumes (javax.ws.rs.Consumes)4 Path (javax.ws.rs.Path)4 Produces (javax.ws.rs.Produces)4 InputStream (java.io.InputStream)3 ArrayList (java.util.ArrayList)3