try { final VersionedFlowSnapshotMetadata latestMetadata = snapshotClient.getLatestMetadata(flowId); version = latestMetadata.getVersion() + 1; } catch (NiFiRegistryException e) { final VersionedFlowSnapshotMetadata metadata = new VersionedFlowSnapshotMetadata(); metadata.setBucketIdentifier(versionedFlow.getBucketIdentifier()); metadata.setFlowIdentifier(flowId); metadata.setVersion(version); metadata.setComments(deserializedSnapshot.getSnapshotMetadata().getComments()); final VersionedFlowSnapshotMetadata createdMetadata = createdSnapshot.getSnapshotMetadata(); return new StringResult(String.valueOf(createdMetadata.getVersion()), getContext().isInteractive());
@Override protected void writeSimpleResult(final PrintStream output) { if (versions == null || versions.isEmpty()) { return; } // date length, with locale specifics final String datePattern = "%1$ta, %<tb %<td %<tY %<tR %<tZ"; final int dateLength = String.format(datePattern, new Date()).length(); final Table table = new Table.Builder() .column("Ver", 3, 3, false) .column("Date", dateLength, dateLength, false) .column("Author", 20, 200, true) .column("Message", 8, 40, true) .build(); versions.forEach(vfs -> { table.addRow( String.valueOf(vfs.getVersion()), String.format(datePattern, new Date(vfs.getTimestamp())), vfs.getAuthor(), vfs.getComments() ); }); final TableWriter tableWriter = new DynamicTableWriter(); tableWriter.write(table, output); } }
@Override public VersionedFlowSnapshot registerVersionedFlowSnapshot(final VersionedFlow flow, final VersionedProcessGroup snapshot, final String comments, final int expectedVersion, final NiFiUser user) throws IOException, NiFiRegistryException { final FlowSnapshotClient snapshotClient = getFlowSnapshotClient(user); final VersionedFlowSnapshot versionedFlowSnapshot = new VersionedFlowSnapshot(); versionedFlowSnapshot.setFlowContents(snapshot); final VersionedFlowSnapshotMetadata metadata = new VersionedFlowSnapshotMetadata(); metadata.setBucketIdentifier(flow.getBucketIdentifier()); metadata.setFlowIdentifier(flow.getIdentifier()); metadata.setAuthor(getIdentity(user)); metadata.setTimestamp(System.currentTimeMillis()); metadata.setVersion(expectedVersion); metadata.setComments(comments); versionedFlowSnapshot.setSnapshotMetadata(metadata); return snapshotClient.create(versionedFlowSnapshot); }
public static Event flowVersionCreated(final VersionedFlowSnapshot versionedFlowSnapshot) { final String versionComments = versionedFlowSnapshot.getSnapshotMetadata().getComments() == null ? "" : versionedFlowSnapshot.getSnapshotMetadata().getComments(); return new StandardEvent.Builder() .eventType(EventType.CREATE_FLOW_VERSION) .addField(EventFieldName.BUCKET_ID, versionedFlowSnapshot.getSnapshotMetadata().getBucketIdentifier()) .addField(EventFieldName.FLOW_ID, versionedFlowSnapshot.getSnapshotMetadata().getFlowIdentifier()) .addField(EventFieldName.VERSION, String.valueOf(versionedFlowSnapshot.getSnapshotMetadata().getVersion())) .addField(EventFieldName.USER, versionedFlowSnapshot.getSnapshotMetadata().getAuthor()) .addField(EventFieldName.COMMENT, versionComments) .build(); }
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."); 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());
flowSnapshot.getSnapshotMetadata().setTimestamp(System.currentTimeMillis()); 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."); 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."); 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"); final FlowEntity updatedFlow = metadataService.getFlowByIdWithSnapshotCounts(snapshotMetadata.getFlowIdentifier()); if (updatedFlow == null) { throw new ResourceNotFoundException("Versioned flow does not exist for identifier " + snapshotMetadata.getFlowIdentifier());
@Override public String toString() { final String flowName = (flow == null ? "null" : flow.getName()); return "VersionedFlowSnapshot[flowId=" + snapshotMetadata.getFlowIdentifier() + ", flowName=" + flowName + ", version=" + snapshotMetadata.getVersion() + ", comments=" + snapshotMetadata.getComments() + "]"; } }
@Override public VersionedFlowSnapshotResult doExecute(final NiFiRegistryClient client, final Properties properties) throws ParseException, IOException, NiFiRegistryException { final String flowId = getRequiredArg(properties, CommandOption.FLOW_ID); final Integer version = getIntArg(properties, CommandOption.FLOW_VERSION); // if no version was provided then export the latest, otherwise use specific version final VersionedFlowSnapshot versionedFlowSnapshot; if (version == null) { versionedFlowSnapshot = client.getFlowSnapshotClient().getLatest(flowId); } else { versionedFlowSnapshot = client.getFlowSnapshotClient().get(flowId, version); } versionedFlowSnapshot.setFlow(null); versionedFlowSnapshot.setBucket(null); versionedFlowSnapshot.getSnapshotMetadata().setBucketIdentifier(null); versionedFlowSnapshot.getSnapshotMetadata().setFlowIdentifier(null); versionedFlowSnapshot.getSnapshotMetadata().setLink(null); // currently export doesn't use the ResultWriter concept, it always writes JSON // destination will be a file if outputFile is specified, otherwise it will be the output stream of the CLI final String outputFile; if (properties.containsKey(CommandOption.OUTPUT_FILE.getLongName())) { outputFile = properties.getProperty(CommandOption.OUTPUT_FILE.getLongName()); } else { outputFile = null; } return new VersionedFlowSnapshotResult(versionedFlowSnapshot, outputFile); }
protected List<Integer> getVersions(final NiFiRegistryClient client, final String flowId) throws NiFiRegistryException, IOException { final FlowSnapshotClient srcSnapshotClient = client.getFlowSnapshotClient(); final List<VersionedFlowSnapshotMetadata> srcVersionMetadata = srcSnapshotClient.getSnapshotMetadata(flowId); return srcVersionMetadata.stream().map(s -> s.getVersion()).collect(Collectors.toList()); }
logger.info("Updating Process Group with ID {} to version {} of the Versioned Flow", groupId, flowSnapshot.getSnapshotMetadata().getVersion()); vci.setBucketId(metadata.getBucketIdentifier()); vci.setBucketName(bucket.getName()); vci.setFlowDescription(flow.getDescription()); vci.setRegistryId(requestVci.getRegistryId()); vci.setRegistryName(serviceFacade.getFlowRegistryName(requestVci.getRegistryId())); vci.setVersion(metadata.getVersion()); vci.setState(flowSnapshot.isLatest() ? VersionedFlowState.UP_TO_DATE.name() : VersionedFlowState.STALE.name());
final VersionControlInformation versionControlInfo = getVersionControlInformation(); if (versionControlInfo != null) { if (!versionControlInfo.getFlowIdentifier().equals(updatedFlow.getSnapshotMetadata().getFlowIdentifier())) { throw new IllegalStateException(this + " is under version control but the given flow does not match the flow that this Process Group is synchronized with");
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; }
@Override public Set<VersionedFlowSnapshotMetadata> getFlowVersionsForUser(String registryId, String bucketId, String flowId, NiFiUser user) { try { final FlowRegistry flowRegistry = flowRegistryClient.getFlowRegistry(registryId); if (flowRegistry == null) { throw new IllegalArgumentException("The specified registry id is unknown to this NiFi."); } final Set<VersionedFlowSnapshotMetadata> flowVersions = flowRegistry.getFlowVersions(bucketId, flowId, user); final Set<VersionedFlowSnapshotMetadata> sortedFlowVersions = new TreeSet<>((f1, f2) -> Integer.compare(f1.getVersion(), f2.getVersion())); sortedFlowVersions.addAll(flowVersions); return sortedFlowVersions; } catch (final IOException | NiFiRegistryException ioe) { throw new NiFiCoreException("Unable to obtain listing of versions for bucket with ID " + bucketId + " and flow with ID " + flowId + ": " + ioe, ioe); } }
final Integer srcSnapshotFlowVersion = srcSnapshot.getSnapshotMetadata().getVersion(); final VersionedFlowSnapshotMetadata destMetadata = new VersionedFlowSnapshotMetadata(); destMetadata.setBucketIdentifier(destFlow.getBucketIdentifier()); destMetadata.setFlowIdentifier(destFlowId); destMetadata.setVersion(destFlowVersion); destMetadata.setComments(srcSnapshot.getSnapshotMetadata().getComments());
public static FlowSnapshotEntity map(final VersionedFlowSnapshotMetadata versionedFlowSnapshot) { final FlowSnapshotEntity flowSnapshotEntity = new FlowSnapshotEntity(); flowSnapshotEntity.setFlowId(versionedFlowSnapshot.getFlowIdentifier()); flowSnapshotEntity.setVersion(versionedFlowSnapshot.getVersion()); flowSnapshotEntity.setComments(versionedFlowSnapshot.getComments()); flowSnapshotEntity.setCreated(new Date(versionedFlowSnapshot.getTimestamp())); flowSnapshotEntity.setCreatedBy(versionedFlowSnapshot.getAuthor()); return flowSnapshotEntity; }
private int getLatestVersion(final NiFiClient client, final VersionControlInformationDTO existingVersionControlDTO) throws NiFiClientException, IOException { final FlowClient flowClient = client.getFlowClient(); final String registryId = existingVersionControlDTO.getRegistryId(); final String bucketId = existingVersionControlDTO.getBucketId(); final String flowId = existingVersionControlDTO.getFlowId(); final VersionedFlowSnapshotMetadataSetEntity versions = flowClient.getVersions(registryId, bucketId, flowId); if (versions.getVersionedFlowSnapshotMetadataSet() == null || versions.getVersionedFlowSnapshotMetadataSet().isEmpty()) { throw new NiFiClientException("No versions available"); } int latestVersion = 1; for (VersionedFlowSnapshotMetadataEntity version : versions.getVersionedFlowSnapshotMetadataSet()) { if (version.getVersionedFlowSnapshotMetadata().getVersion() > latestVersion) { latestVersion = version.getVersionedFlowSnapshotMetadata().getVersion(); } } return latestVersion; }
srcFlowSnapshot.setBucket(null); final VersionedFlowSnapshotMetadata destMetadata = new VersionedFlowSnapshotMetadata(); destMetadata.setBucketIdentifier(destFlow.getBucketIdentifier()); destMetadata.setFlowIdentifier(destFlowId); destMetadata.setVersion(srcVersion); destMetadata.setComments(srcFlowSnapshot.getSnapshotMetadata().getComments());
public Builder(final Bucket bucket, final VersionedFlow versionedFlow, final VersionedFlowSnapshotMetadata snapshotMetadata) { bucketId(bucket.getIdentifier()); bucketName(bucket.getName()); flowId(snapshotMetadata.getFlowIdentifier()); flowName(versionedFlow.getName()); version(snapshotMetadata.getVersion()); comments(snapshotMetadata.getComments()); author(snapshotMetadata.getAuthor()); snapshotTimestamp(snapshotMetadata.getTimestamp()); }
vci.setRegistryId(registryId); vci.setRegistryName(getFlowRegistryName(registryId)); vci.setVersion(registeredSnapshot.getSnapshotMetadata().getVersion()); vci.setState(VersionedFlowState.UP_TO_DATE.name());
/** * This is a convenience method that will return true when flow is populated and when the flow's versionCount * is equal to the version of this snapshot. * * @return true if flow is populated and if this snapshot is the latest version for the flow at the time of retrieval */ @XmlTransient public boolean isLatest() { return flow != null && snapshotMetadata != null && flow.getVersionCount() == getSnapshotMetadata().getVersion(); }