Search in sources :

Example 11 with NiFiRegistryException

use of org.apache.nifi.registry.client.NiFiRegistryException in project nifi by apache.

the class DeleteBucket method doExecute.

@Override
public OkResult doExecute(final NiFiRegistryClient client, final Properties properties) throws IOException, NiFiRegistryException, ParseException {
    final String bucketId = getRequiredArg(properties, CommandOption.BUCKET_ID);
    final boolean forceDelete = properties.containsKey(CommandOption.FORCE.getLongName());
    final FlowClient flowClient = client.getFlowClient();
    final List<VersionedFlow> flowsInBucket = flowClient.getByBucket(bucketId);
    if (flowsInBucket != null && flowsInBucket.size() > 0 && !forceDelete) {
        throw new NiFiRegistryException("Bucket is not empty, use --" + CommandOption.FORCE.getLongName() + " to delete");
    } else {
        final BucketClient bucketClient = client.getBucketClient();
        bucketClient.delete(bucketId);
        return new OkResult(getContext().isInteractive());
    }
}
Also used : BucketClient(org.apache.nifi.registry.client.BucketClient) OkResult(org.apache.nifi.toolkit.cli.impl.result.OkResult) VersionedFlow(org.apache.nifi.registry.flow.VersionedFlow) FlowClient(org.apache.nifi.registry.client.FlowClient) NiFiRegistryException(org.apache.nifi.registry.client.NiFiRegistryException)

Example 12 with NiFiRegistryException

use of org.apache.nifi.registry.client.NiFiRegistryException 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 13 with NiFiRegistryException

use of org.apache.nifi.registry.client.NiFiRegistryException 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 14 with NiFiRegistryException

use of org.apache.nifi.registry.client.NiFiRegistryException in project nifi by apache.

the class FlowRegistryDAO method getFlowsForUser.

@Override
public Set<VersionedFlow> getFlowsForUser(String registryId, String bucketId, 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<VersionedFlow> flows = flowRegistry.getFlows(bucketId, user);
        final Set<VersionedFlow> sortedFlows = new TreeSet<>((f1, f2) -> f1.getName().compareTo(f2.getName()));
        sortedFlows.addAll(flows);
        return sortedFlows;
    } catch (final IOException | NiFiRegistryException ioe) {
        throw new NiFiCoreException("Unable to obtain listing of flows for bucket with ID " + bucketId + ": " + ioe, ioe);
    }
}
Also used : NiFiCoreException(org.apache.nifi.web.NiFiCoreException) TreeSet(java.util.TreeSet) FlowRegistry(org.apache.nifi.registry.flow.FlowRegistry) VersionedFlow(org.apache.nifi.registry.flow.VersionedFlow) IOException(java.io.IOException) NiFiRegistryException(org.apache.nifi.registry.client.NiFiRegistryException)

Aggregations

NiFiRegistryException (org.apache.nifi.registry.client.NiFiRegistryException)14 IOException (java.io.IOException)10 VersionedFlowSnapshot (org.apache.nifi.registry.flow.VersionedFlowSnapshot)7 FlowRegistry (org.apache.nifi.registry.flow.FlowRegistry)6 VersionedFlowSnapshotMetadata (org.apache.nifi.registry.flow.VersionedFlowSnapshotMetadata)6 VersionedFlow (org.apache.nifi.registry.flow.VersionedFlow)5 Bucket (org.apache.nifi.registry.bucket.Bucket)4 FlowSnapshotClient (org.apache.nifi.registry.client.FlowSnapshotClient)4 VersionedProcessGroup (org.apache.nifi.registry.flow.VersionedProcessGroup)4 TreeSet (java.util.TreeSet)3 BucketClient (org.apache.nifi.registry.client.BucketClient)3 FlowClient (org.apache.nifi.registry.client.FlowClient)3 NiFiCoreException (org.apache.nifi.web.NiFiCoreException)3 ProcessGroup (org.apache.nifi.groups.ProcessGroup)2 RemoteProcessGroup (org.apache.nifi.groups.RemoteProcessGroup)2 BucketItem (org.apache.nifi.registry.bucket.BucketItem)2 NiFiRegistryClient (org.apache.nifi.registry.client.NiFiRegistryClient)2 VersionControlInformation (org.apache.nifi.registry.flow.VersionControlInformation)2 InstantiatedVersionedProcessGroup (org.apache.nifi.registry.flow.mapping.InstantiatedVersionedProcessGroup)2 OkResult (org.apache.nifi.toolkit.cli.impl.result.OkResult)2