use of org.apache.nifi.registry.flow.VersionedFlowSnapshotMetadata in project nifi by apache.
the class DeleteFlow method doExecute.
@Override
public OkResult doExecute(final NiFiRegistryClient client, final Properties properties) throws IOException, NiFiRegistryException, ParseException {
final String flowId = getRequiredArg(properties, CommandOption.FLOW_ID);
final boolean forceDelete = properties.containsKey(CommandOption.FORCE.getLongName());
final String bucketId = getBucketId(client, flowId);
final FlowSnapshotClient flowSnapshotClient = client.getFlowSnapshotClient();
final List<VersionedFlowSnapshotMetadata> snapshotMetadata = flowSnapshotClient.getSnapshotMetadata(bucketId, flowId);
if (snapshotMetadata != null && snapshotMetadata.size() > 0 && !forceDelete) {
throw new NiFiRegistryException("Flow has versions, use --" + CommandOption.FORCE.getLongName() + " to delete");
} else {
final FlowClient flowClient = client.getFlowClient();
flowClient.delete(bucketId, flowId);
return new OkResult(getContext().isInteractive());
}
}
use of org.apache.nifi.registry.flow.VersionedFlowSnapshotMetadata in project nifi by apache.
the class ImportFlowVersion method doExecute.
@Override
public StringResult doExecute(final NiFiRegistryClient client, final Properties properties) throws ParseException, IOException, NiFiRegistryException {
final String flowId = getRequiredArg(properties, CommandOption.FLOW_ID);
final String inputFile = getRequiredArg(properties, CommandOption.INPUT_SOURCE);
String contents;
try {
// try a public resource URL
URL url = new URL(inputFile);
contents = IOUtils.toString(url, StandardCharsets.UTF_8);
} catch (MalformedURLException e) {
// assume a local file then
URI uri = Paths.get(inputFile).toAbsolutePath().toUri();
contents = IOUtils.toString(uri, StandardCharsets.UTF_8);
}
final FlowSnapshotClient snapshotClient = client.getFlowSnapshotClient();
final ObjectMapper objectMapper = JacksonUtils.getObjectMapper();
final VersionedFlowSnapshot deserializedSnapshot = objectMapper.readValue(contents, VersionedFlowSnapshot.class);
if (deserializedSnapshot == null) {
throw new IOException("Unable to deserialize flow version from " + inputFile);
}
// determine the bucket for the provided flow id
final String bucketId = getBucketId(client, flowId);
// determine the latest existing version in the destination system
Integer version;
try {
final VersionedFlowSnapshotMetadata latestMetadata = snapshotClient.getLatestMetadata(bucketId, flowId);
version = latestMetadata.getVersion() + 1;
} catch (NiFiRegistryException e) {
// when there are no versions it produces a 404 not found
version = 1;
}
// create new metadata using the passed in bucket and flow in the target registry, keep comments
final VersionedFlowSnapshotMetadata metadata = new VersionedFlowSnapshotMetadata();
metadata.setBucketIdentifier(bucketId);
metadata.setFlowIdentifier(flowId);
metadata.setVersion(version);
metadata.setComments(deserializedSnapshot.getSnapshotMetadata().getComments());
// create a new snapshot using the new metadata and the contents from the deserialized snapshot
final VersionedFlowSnapshot snapshot = new VersionedFlowSnapshot();
snapshot.setSnapshotMetadata(metadata);
snapshot.setFlowContents(deserializedSnapshot.getFlowContents());
final VersionedFlowSnapshot createdSnapshot = snapshotClient.create(snapshot);
final VersionedFlowSnapshotMetadata createdMetadata = createdSnapshot.getSnapshotMetadata();
return new StringResult(String.valueOf(createdMetadata.getVersion()), getContext().isInteractive());
}
use of org.apache.nifi.registry.flow.VersionedFlowSnapshotMetadata in project nifi by apache.
the class ListFlowVersions method doExecute.
@Override
public VersionedFlowSnapshotMetadataResult doExecute(final NiFiRegistryClient client, final Properties properties) throws ParseException, IOException, NiFiRegistryException {
final String flow = getRequiredArg(properties, CommandOption.FLOW_ID);
final String bucket = getBucketId(client, flow);
final FlowSnapshotClient snapshotClient = client.getFlowSnapshotClient();
final List<VersionedFlowSnapshotMetadata> snapshotMetadata = snapshotClient.getSnapshotMetadata(bucket, flow);
return new VersionedFlowSnapshotMetadataResult(getResultType(properties), snapshotMetadata);
}
use of org.apache.nifi.registry.flow.VersionedFlowSnapshotMetadata in project nifi by apache.
the class TransferFlowVersion method doExecute.
@Override
public StringResult doExecute(final NiFiRegistryClient client, final Properties properties) throws IOException, NiFiRegistryException, ParseException {
final String srcPropsValue = getArg(properties, CommandOption.SRC_PROPS);
final String srcFlowId = getRequiredArg(properties, CommandOption.SRC_FLOW_ID);
final Integer srcFlowVersion = getIntArg(properties, CommandOption.SRC_FLOW_VERSION);
final String destFlowId = getRequiredArg(properties, CommandOption.FLOW_ID);
final NiFiRegistryClient srcClient = getSourceClient(client, srcPropsValue);
// determine the bucket ids of the source and dest flows
final String srcBucketId = getBucketId(srcClient, srcFlowId);
final String destBucketId = getBucketId(client, destFlowId);
// get the snapshot of the source flow, either the version specified or the latest
final VersionedFlowSnapshot srcSnapshot;
if (srcFlowVersion == null) {
srcSnapshot = srcClient.getFlowSnapshotClient().getLatest(srcBucketId, srcFlowId);
} else {
srcSnapshot = srcClient.getFlowSnapshotClient().get(srcBucketId, srcFlowId, srcFlowVersion);
}
// determine the next version number for the destination flow
final List<Integer> destVersions = getVersions(client, destBucketId, destFlowId);
final Integer destFlowVersion = destVersions.isEmpty() ? 1 : destVersions.get(0) + 1;
// create the new metadata for the destination snapshot
final VersionedFlowSnapshotMetadata destMetadata = new VersionedFlowSnapshotMetadata();
destMetadata.setBucketIdentifier(destBucketId);
destMetadata.setFlowIdentifier(destFlowId);
destMetadata.setVersion(destFlowVersion);
destMetadata.setComments(srcSnapshot.getSnapshotMetadata().getComments());
// update the source snapshot with the destination metadata
srcSnapshot.setFlow(null);
srcSnapshot.setBucket(null);
srcSnapshot.setSnapshotMetadata(destMetadata);
// create the destination snapshot
client.getFlowSnapshotClient().create(srcSnapshot);
if (getContext().isInteractive()) {
println();
println("Transferred version " + srcSnapshot.getSnapshotMetadata().getVersion() + " of source flow to version " + destFlowVersion + " of destination flow");
}
return new OkResult(getContext().isInteractive());
}
use of org.apache.nifi.registry.flow.VersionedFlowSnapshotMetadata in project nifi by apache.
the class TestVersionedFlowSnapshotMetadataResult method testWriteSimpleVersionedFlowSnapshotResult.
@Test
public void testWriteSimpleVersionedFlowSnapshotResult() throws ParseException, IOException {
final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
final VersionedFlowSnapshotMetadata vfs1 = new VersionedFlowSnapshotMetadata();
vfs1.setVersion(1);
vfs1.setAuthor("user1");
vfs1.setTimestamp(dateFormat.parse("2018-02-14T12:00:00").getTime());
vfs1.setComments("This is a long comment, longer than the display limit for comments");
final VersionedFlowSnapshotMetadata vfs2 = new VersionedFlowSnapshotMetadata();
vfs2.setVersion(2);
vfs2.setAuthor("user2");
vfs2.setTimestamp(dateFormat.parse("2018-02-14T12:30:00").getTime());
vfs2.setComments("This is v2");
final List<VersionedFlowSnapshotMetadata> versions = new ArrayList<>();
versions.add(vfs1);
versions.add(vfs2);
final VersionedFlowSnapshotMetadataResult result = new VersionedFlowSnapshotMetadataResult(ResultType.SIMPLE, versions);
result.write(printStream);
final String resultOut = new String(outputStream.toByteArray(), StandardCharsets.UTF_8);
// System.out.println(resultOut);
// can't get the time zone to line up on travis, so ignore this for now
final String expected = "\n" + "Ver Date Author Message \n" + // +
"--- -------------------------- ------ ---------------------------------------- \n";
// "1 Wed, Feb 14 2018 12:00 EST user1 This is a long comment, longer than t... \n" +
// "2 Wed, Feb 14 2018 12:30 EST user2 This is v2 \n" +
// "\n";
Assert.assertTrue(resultOut.startsWith(expected));
}
Aggregations