use of org.apache.nifi.toolkit.cli.impl.result.StringResult 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());
}
Aggregations