Search in sources :

Example 1 with NiFiRegistryClient

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

the class CLIMain method createContext.

private static Context createContext(final PrintStream output, final boolean isInteractive) {
    Session session;
    final String userHomeValue = System.getProperty("user.home");
    final File userHome = Paths.get(userHomeValue).toFile();
    if (!userHome.exists() || !userHome.canRead() || !userHome.canWrite()) {
        session = new InMemorySession();
        if (isInteractive) {
            output.println();
            output.println("Unable to create session from " + userHomeValue + ", falling back to in-memory session");
            output.println();
        }
    } else {
        final InMemorySession inMemorySession = new InMemorySession();
        final File sessionState = new File(userHome.getAbsolutePath(), SESSION_PERSISTENCE_FILE);
        try {
            if (!sessionState.exists()) {
                sessionState.createNewFile();
            }
            final PersistentSession persistentSession = new PersistentSession(sessionState, inMemorySession);
            persistentSession.loadSession();
            session = persistentSession;
            if (isInteractive) {
                output.println();
                output.println("Session loaded from " + sessionState.getAbsolutePath());
                output.println();
            }
        } catch (Exception e) {
            session = inMemorySession;
            if (isInteractive) {
                output.println();
                output.println("Unable to load session from " + sessionState.getAbsolutePath() + ", falling back to in-memory session");
                output.println();
            }
        }
    }
    final ClientFactory<NiFiClient> niFiClientFactory = new NiFiClientFactory();
    final ClientFactory<NiFiRegistryClient> nifiRegClientFactory = new NiFiRegistryClientFactory();
    return new StandardContext.Builder().output(output).session(session).nifiClientFactory(niFiClientFactory).nifiRegistryClientFactory(nifiRegClientFactory).interactive(isInteractive).build();
}
Also used : NiFiClientFactory(org.apache.nifi.toolkit.cli.impl.client.NiFiClientFactory) NiFiClient(org.apache.nifi.toolkit.cli.impl.client.nifi.NiFiClient) NiFiRegistryClientFactory(org.apache.nifi.toolkit.cli.impl.client.NiFiRegistryClientFactory) LineReaderBuilder(org.jline.reader.LineReaderBuilder) TerminalBuilder(org.jline.terminal.TerminalBuilder) PersistentSession(org.apache.nifi.toolkit.cli.impl.session.PersistentSession) IOException(java.io.IOException) EndOfFileException(org.jline.reader.EndOfFileException) UserInterruptException(org.jline.reader.UserInterruptException) NiFiRegistryClient(org.apache.nifi.registry.client.NiFiRegistryClient) InMemorySession(org.apache.nifi.toolkit.cli.impl.session.InMemorySession) File(java.io.File) PersistentSession(org.apache.nifi.toolkit.cli.impl.session.PersistentSession) Session(org.apache.nifi.toolkit.cli.api.Session) InMemorySession(org.apache.nifi.toolkit.cli.impl.session.InMemorySession)

Example 2 with NiFiRegistryClient

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

the class NiFiRegistryClientFactory method createClient.

@Override
public NiFiRegistryClient createClient(final Properties properties) throws MissingOptionException {
    final String url = properties.getProperty(CommandOption.URL.getLongName());
    if (StringUtils.isBlank(url)) {
        throw new MissingOptionException("Missing required option '" + CommandOption.URL.getLongName() + "'");
    }
    final String keystore = properties.getProperty(CommandOption.KEYSTORE.getLongName());
    final String keystoreType = properties.getProperty(CommandOption.KEYSTORE_TYPE.getLongName());
    final String keystorePasswd = properties.getProperty(CommandOption.KEYSTORE_PASSWORD.getLongName());
    final String keyPasswd = properties.getProperty(CommandOption.KEY_PASSWORD.getLongName());
    final String truststore = properties.getProperty(CommandOption.TRUSTSTORE.getLongName());
    final String truststoreType = properties.getProperty(CommandOption.TRUSTSTORE_TYPE.getLongName());
    final String truststorePasswd = properties.getProperty(CommandOption.TRUSTSTORE_PASSWORD.getLongName());
    final String proxiedEntity = properties.getProperty(CommandOption.PROXIED_ENTITY.getLongName());
    final boolean secureUrl = url.startsWith("https");
    if (secureUrl && (StringUtils.isBlank(truststore) || StringUtils.isBlank(truststoreType) || StringUtils.isBlank(truststorePasswd))) {
        throw new MissingOptionException(CommandOption.TRUSTSTORE.getLongName() + ", " + CommandOption.TRUSTSTORE_TYPE.getLongName() + ", and " + CommandOption.TRUSTSTORE_PASSWORD.getLongName() + " are required when using an https url");
    }
    final NiFiRegistryClientConfig.Builder clientConfigBuilder = new NiFiRegistryClientConfig.Builder().baseUrl(url);
    if (secureUrl) {
        if (!StringUtils.isBlank(keystore)) {
            clientConfigBuilder.keystoreFilename(keystore);
        }
        if (!StringUtils.isBlank(keystoreType)) {
            clientConfigBuilder.keystoreType(KeystoreType.valueOf(keystoreType.toUpperCase()));
        }
        if (!StringUtils.isBlank(keystorePasswd)) {
            clientConfigBuilder.keystorePassword(keystorePasswd);
        }
        if (!StringUtils.isBlank(keyPasswd)) {
            clientConfigBuilder.keyPassword(keyPasswd);
        }
        if (!StringUtils.isBlank(truststore)) {
            clientConfigBuilder.truststoreFilename(truststore);
        }
        if (!StringUtils.isBlank(truststoreType)) {
            clientConfigBuilder.truststoreType(KeystoreType.valueOf(truststoreType.toUpperCase()));
        }
        if (!StringUtils.isBlank(truststorePasswd)) {
            clientConfigBuilder.truststorePassword(truststorePasswd);
        }
    }
    final NiFiRegistryClient client = new JerseyNiFiRegistryClient.Builder().config(clientConfigBuilder.build()).build();
    // if a proxied entity was specified then return a wrapped client, otherwise return the regular client
    if (!StringUtils.isBlank(proxiedEntity)) {
        return new ProxiedNiFiRegistryClient(client, proxiedEntity);
    } else {
        return client;
    }
}
Also used : NiFiRegistryClient(org.apache.nifi.registry.client.NiFiRegistryClient) JerseyNiFiRegistryClient(org.apache.nifi.registry.client.impl.JerseyNiFiRegistryClient) NiFiRegistryClientConfig(org.apache.nifi.registry.client.NiFiRegistryClientConfig) MissingOptionException(org.apache.commons.cli.MissingOptionException)

Example 3 with NiFiRegistryClient

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

the class AbstractCompositeCommand method execute.

@Override
public final R execute(final CommandLine cli) throws CommandException {
    try {
        final Properties nifiProperties = createProperties(cli, CommandOption.NIFI_PROPS, SessionVariable.NIFI_CLIENT_PROPS);
        if (nifiProperties == null) {
            throw new CommandException("Unable to find NiFi config, must specify --" + CommandOption.NIFI_PROPS.getLongName() + ", or setup session config");
        }
        final ClientFactory<NiFiClient> nifiClientFactory = getContext().getNiFiClientFactory();
        final NiFiClient nifiClient = nifiClientFactory.createClient(nifiProperties);
        final Properties registryProperties = createProperties(cli, CommandOption.NIFI_REG_PROPS, SessionVariable.NIFI_REGISTRY_CLIENT_PROPS);
        if (registryProperties == null) {
            throw new CommandException("Unable to find NiFi Registry config, must specify --" + CommandOption.NIFI_REG_PROPS.getLongName() + ", or setup session config");
        }
        final ClientFactory<NiFiRegistryClient> registryClientFactory = getContext().getNiFiRegistryClientFactory();
        final NiFiRegistryClient registryClient = registryClientFactory.createClient(registryProperties);
        return doExecute(cli, nifiClient, nifiProperties, registryClient, registryProperties);
    } catch (CommandException ce) {
        throw ce;
    } catch (Exception e) {
        throw new CommandException("Error executing command '" + getName() + "' : " + e.getMessage(), e);
    }
}
Also used : NiFiClient(org.apache.nifi.toolkit.cli.impl.client.nifi.NiFiClient) NiFiRegistryClient(org.apache.nifi.registry.client.NiFiRegistryClient) CommandException(org.apache.nifi.toolkit.cli.api.CommandException) Properties(java.util.Properties) NiFiClientException(org.apache.nifi.toolkit.cli.impl.client.nifi.NiFiClientException) IOException(java.io.IOException) CommandException(org.apache.nifi.toolkit.cli.api.CommandException) NiFiRegistryException(org.apache.nifi.registry.client.NiFiRegistryException) ParseException(org.apache.commons.cli.ParseException) SessionException(org.apache.nifi.toolkit.cli.api.SessionException)

Example 4 with NiFiRegistryClient

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

the class AbstractNiFiRegistryCommand method getVersions.

protected List<Integer> getVersions(final NiFiRegistryClient client, final String bucketId, final String flowId) throws NiFiRegistryException, IOException {
    final FlowSnapshotClient srcSnapshotClient = client.getFlowSnapshotClient();
    final List<VersionedFlowSnapshotMetadata> srcVersionMetadata = srcSnapshotClient.getSnapshotMetadata(bucketId, flowId);
    return srcVersionMetadata.stream().map(s -> s.getVersion()).collect(Collectors.toList());
}
Also used : Properties(java.util.Properties) AbstractPropertyCommand(org.apache.nifi.toolkit.cli.impl.command.AbstractPropertyCommand) SessionVariable(org.apache.nifi.toolkit.cli.impl.session.SessionVariable) IOException(java.io.IOException) CommandException(org.apache.nifi.toolkit.cli.api.CommandException) FileInputStream(java.io.FileInputStream) StringUtils(org.apache.commons.lang3.StringUtils) Collectors(java.util.stream.Collectors) BucketItem(org.apache.nifi.registry.bucket.BucketItem) NiFiRegistryClient(org.apache.nifi.registry.client.NiFiRegistryClient) VersionedFlowSnapshotMetadata(org.apache.nifi.registry.flow.VersionedFlowSnapshotMetadata) List(java.util.List) NiFiRegistryException(org.apache.nifi.registry.client.NiFiRegistryException) Result(org.apache.nifi.toolkit.cli.api.Result) ParseException(org.apache.commons.cli.ParseException) FlowSnapshotClient(org.apache.nifi.registry.client.FlowSnapshotClient) ClientFactory(org.apache.nifi.toolkit.cli.api.ClientFactory) Optional(java.util.Optional) InputStream(java.io.InputStream) FlowSnapshotClient(org.apache.nifi.registry.client.FlowSnapshotClient) VersionedFlowSnapshotMetadata(org.apache.nifi.registry.flow.VersionedFlowSnapshotMetadata)

Example 5 with NiFiRegistryClient

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

the class SyncFlowVersions 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 String destFlowId = getRequiredArg(properties, CommandOption.FLOW_ID);
    final NiFiRegistryClient srcClient = getSourceClient(client, srcPropsValue);
    final String srcBucketId = getBucketId(srcClient, srcFlowId);
    final String destBucketId = getBucketId(client, destFlowId);
    final List<Integer> srcVersions = getVersions(srcClient, srcBucketId, srcFlowId);
    final List<Integer> destVersions = getVersions(client, destBucketId, destFlowId);
    if (destVersions.size() > srcVersions.size()) {
        throw new NiFiRegistryException("Destination flow has more versions than source flow");
    }
    srcVersions.removeAll(destVersions);
    if (srcVersions.isEmpty()) {
        if (getContext().isInteractive()) {
            println();
            println("Source and destination already in sync");
        }
        return new OkResult(getContext().isInteractive());
    }
    // the REST API returns versions in decreasing order, but we want them in increasing order
    Collections.sort(srcVersions);
    for (final Integer srcVersion : srcVersions) {
        final VersionedFlowSnapshot srcFlowSnapshot = srcClient.getFlowSnapshotClient().get(srcBucketId, srcFlowId, srcVersion);
        srcFlowSnapshot.setFlow(null);
        srcFlowSnapshot.setBucket(null);
        final VersionedFlowSnapshotMetadata destMetadata = new VersionedFlowSnapshotMetadata();
        destMetadata.setBucketIdentifier(destBucketId);
        destMetadata.setFlowIdentifier(destFlowId);
        destMetadata.setVersion(srcVersion);
        destMetadata.setComments(srcFlowSnapshot.getSnapshotMetadata().getComments());
        srcFlowSnapshot.setSnapshotMetadata(destMetadata);
        client.getFlowSnapshotClient().create(srcFlowSnapshot);
        if (getContext().isInteractive()) {
            println();
            println("Synced version " + srcVersion);
        }
    }
    return new OkResult(getContext().isInteractive());
}
Also used : OkResult(org.apache.nifi.toolkit.cli.impl.result.OkResult) NiFiRegistryClient(org.apache.nifi.registry.client.NiFiRegistryClient) VersionedFlowSnapshot(org.apache.nifi.registry.flow.VersionedFlowSnapshot) NiFiRegistryException(org.apache.nifi.registry.client.NiFiRegistryException) VersionedFlowSnapshotMetadata(org.apache.nifi.registry.flow.VersionedFlowSnapshotMetadata)

Aggregations

NiFiRegistryClient (org.apache.nifi.registry.client.NiFiRegistryClient)14 JerseyNiFiRegistryClient (org.apache.nifi.registry.client.impl.JerseyNiFiRegistryClient)6 NiFiClient (org.apache.nifi.toolkit.cli.impl.client.nifi.NiFiClient)4 IOException (java.io.IOException)3 Properties (java.util.Properties)3 NiFiRegistryClientConfig (org.apache.nifi.registry.client.NiFiRegistryClientConfig)3 NiFiRegistryException (org.apache.nifi.registry.client.NiFiRegistryException)3 VersionedFlowSnapshotMetadata (org.apache.nifi.registry.flow.VersionedFlowSnapshotMetadata)3 Session (org.apache.nifi.toolkit.cli.api.Session)3 NiFiClientFactory (org.apache.nifi.toolkit.cli.impl.client.NiFiClientFactory)3 NiFiRegistryClientFactory (org.apache.nifi.toolkit.cli.impl.client.NiFiRegistryClientFactory)3 InMemorySession (org.apache.nifi.toolkit.cli.impl.session.InMemorySession)3 FileInputStream (java.io.FileInputStream)2 InputStream (java.io.InputStream)2 ParseException (org.apache.commons.cli.ParseException)2 FlowSnapshotClient (org.apache.nifi.registry.client.FlowSnapshotClient)2 VersionedFlowSnapshot (org.apache.nifi.registry.flow.VersionedFlowSnapshot)2 Command (org.apache.nifi.toolkit.cli.api.Command)2 CommandException (org.apache.nifi.toolkit.cli.api.CommandException)2 CommandGroup (org.apache.nifi.toolkit.cli.api.CommandGroup)2