Search in sources :

Example 6 with FlowSnapshotClient

use of org.apache.nifi.registry.client.FlowSnapshotClient 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());
    }
}
Also used : OkResult(org.apache.nifi.toolkit.cli.impl.result.OkResult) FlowClient(org.apache.nifi.registry.client.FlowClient) FlowSnapshotClient(org.apache.nifi.registry.client.FlowSnapshotClient) NiFiRegistryException(org.apache.nifi.registry.client.NiFiRegistryException) VersionedFlowSnapshotMetadata(org.apache.nifi.registry.flow.VersionedFlowSnapshotMetadata)

Example 7 with FlowSnapshotClient

use of org.apache.nifi.registry.client.FlowSnapshotClient 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());
}
Also used : MalformedURLException(java.net.MalformedURLException) VersionedFlowSnapshot(org.apache.nifi.registry.flow.VersionedFlowSnapshot) FlowSnapshotClient(org.apache.nifi.registry.client.FlowSnapshotClient) IOException(java.io.IOException) StringResult(org.apache.nifi.toolkit.cli.impl.result.StringResult) NiFiRegistryException(org.apache.nifi.registry.client.NiFiRegistryException) URI(java.net.URI) URL(java.net.URL) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) VersionedFlowSnapshotMetadata(org.apache.nifi.registry.flow.VersionedFlowSnapshotMetadata)

Example 8 with FlowSnapshotClient

use of org.apache.nifi.registry.client.FlowSnapshotClient 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);
}
Also used : FlowSnapshotClient(org.apache.nifi.registry.client.FlowSnapshotClient) VersionedFlowSnapshotMetadata(org.apache.nifi.registry.flow.VersionedFlowSnapshotMetadata) VersionedFlowSnapshotMetadataResult(org.apache.nifi.toolkit.cli.impl.result.VersionedFlowSnapshotMetadataResult)

Example 9 with FlowSnapshotClient

use of org.apache.nifi.registry.client.FlowSnapshotClient 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)

Aggregations

FlowSnapshotClient (org.apache.nifi.registry.client.FlowSnapshotClient)9 VersionedFlowSnapshotMetadata (org.apache.nifi.registry.flow.VersionedFlowSnapshotMetadata)6 NiFiRegistryException (org.apache.nifi.registry.client.NiFiRegistryException)4 FlowClient (org.apache.nifi.registry.client.FlowClient)3 VersionedFlowSnapshot (org.apache.nifi.registry.flow.VersionedFlowSnapshot)3 IOException (java.io.IOException)2 Bucket (org.apache.nifi.registry.bucket.Bucket)2 BucketItem (org.apache.nifi.registry.bucket.BucketItem)2 BucketClient (org.apache.nifi.registry.client.BucketClient)2 NiFiRegistryClient (org.apache.nifi.registry.client.NiFiRegistryClient)2 VersionedFlow (org.apache.nifi.registry.flow.VersionedFlow)2 VersionedProcessGroup (org.apache.nifi.registry.flow.VersionedProcessGroup)2 Test (org.junit.Test)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)1 MalformedURLException (java.net.MalformedURLException)1 URI (java.net.URI)1 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1