use of org.apache.nifi.registry.flow.VersionedFlowSnapshot 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();
});
}
use of org.apache.nifi.registry.flow.VersionedFlowSnapshot 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());
}
use of org.apache.nifi.registry.flow.VersionedFlowSnapshot 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;
}
use of org.apache.nifi.registry.flow.VersionedFlowSnapshot in project nifi-registry by apache.
the class RegistryService method getFlowSnapshot.
public VersionedFlowSnapshot getFlowSnapshot(final String bucketIdentifier, final String flowIdentifier, final Integer version) {
if (StringUtils.isBlank(bucketIdentifier)) {
throw new IllegalArgumentException("Bucket identifier cannot be null or blank");
}
if (StringUtils.isBlank(flowIdentifier)) {
throw new IllegalArgumentException("Flow identifier cannot be null or blank");
}
if (version == null) {
throw new IllegalArgumentException("Version cannot be null or blank");
}
readLock.lock();
try {
final BucketEntity existingBucket = metadataService.getBucketById(bucketIdentifier);
if (existingBucket == null) {
LOGGER.warn("The specified bucket id [{}] does not exist.", bucketIdentifier);
throw new ResourceNotFoundException("The specified bucket ID does not exist in this registry.");
}
// we need to populate the version count here so we have to do this retrieval instead of snapshotEntity.getFlow()
final FlowEntity flowEntityWithCount = metadataService.getFlowByIdWithSnapshotCounts(flowIdentifier);
if (flowEntityWithCount == null) {
LOGGER.warn("The specified flow id [{}] does not exist.", flowIdentifier);
throw new ResourceNotFoundException("The specified flow ID does not exist in this bucket.");
}
if (!existingBucket.getId().equals(flowEntityWithCount.getBucketId())) {
throw new IllegalStateException("The requested flow is not located in the given bucket");
}
// ensure the snapshot exists
final FlowSnapshotEntity snapshotEntity = metadataService.getFlowSnapshot(flowIdentifier, version);
if (snapshotEntity == null) {
LOGGER.warn("The specified flow snapshot id [{}] does not exist for version [{}].", flowIdentifier, version);
throw new ResourceNotFoundException("The specified versioned flow snapshot does not exist for this flow.");
}
// get the serialized bytes of the snapshot
final byte[] serializedSnapshot = flowPersistenceProvider.getFlowContent(bucketIdentifier, flowIdentifier, version);
if (serializedSnapshot == null || serializedSnapshot.length == 0) {
throw new IllegalStateException("No serialized content found for snapshot with flow identifier " + flowIdentifier + " and version " + version);
}
// deserialize the contents
final InputStream input = new ByteArrayInputStream(serializedSnapshot);
final VersionedProcessGroup flowContents = processGroupSerializer.deserialize(input);
// map entities to data model
final Bucket bucket = DataModelMapper.map(existingBucket);
final VersionedFlow versionedFlow = DataModelMapper.map(existingBucket, flowEntityWithCount);
final VersionedFlowSnapshotMetadata snapshotMetadata = DataModelMapper.map(existingBucket, snapshotEntity);
// create the snapshot to return
final VersionedFlowSnapshot snapshot = new VersionedFlowSnapshot();
snapshot.setFlowContents(flowContents);
snapshot.setSnapshotMetadata(snapshotMetadata);
snapshot.setFlow(versionedFlow);
snapshot.setBucket(bucket);
return snapshot;
} finally {
readLock.unlock();
}
}
use of org.apache.nifi.registry.flow.VersionedFlowSnapshot in project nifi-registry by apache.
the class TestRegistryService method testCreateSnapshotBucketDoesNotExist.
@Test(expected = ResourceNotFoundException.class)
public void testCreateSnapshotBucketDoesNotExist() {
when(metadataService.getBucketById(any(String.class))).thenReturn(null);
final VersionedFlowSnapshot snapshot = createSnapshot();
registryService.createFlowSnapshot(snapshot);
}
Aggregations