Search in sources :

Example 1 with NiFiClient

use of org.apache.nifi.toolkit.cli.impl.client.nifi.NiFiClient 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 NiFiClient

use of org.apache.nifi.toolkit.cli.impl.client.nifi.NiFiClient in project nifi by apache.

the class NiFiClientFactory method createClient.

@Override
public NiFiClient 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 String protocol = properties.getProperty(CommandOption.PROTOCOL.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 NiFiClientConfig.Builder clientConfigBuilder = new NiFiClientConfig.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);
        }
        if (!StringUtils.isBlank(protocol)) {
            clientConfigBuilder.protocol(protocol);
        }
    }
    final NiFiClient client = new JerseyNiFiClient.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 NiFiClientFactory.ProxiedNiFiClient(client, proxiedEntity);
    } else {
        return client;
    }
}
Also used : NiFiClient(org.apache.nifi.toolkit.cli.impl.client.nifi.NiFiClient) JerseyNiFiClient(org.apache.nifi.toolkit.cli.impl.client.nifi.impl.JerseyNiFiClient) MissingOptionException(org.apache.commons.cli.MissingOptionException) NiFiClientConfig(org.apache.nifi.toolkit.cli.impl.client.nifi.NiFiClientConfig)

Example 3 with NiFiClient

use of org.apache.nifi.toolkit.cli.impl.client.nifi.NiFiClient 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 NiFiClient

use of org.apache.nifi.toolkit.cli.impl.client.nifi.NiFiClient in project nifi by apache.

the class PGList method doExecute.

@Override
public ProcessGroupsResult doExecute(final NiFiClient client, final Properties properties) throws NiFiClientException, IOException {
    final FlowClient flowClient = client.getFlowClient();
    // get the optional id of the parent PG, otherwise fallback to the root group
    String parentPgId = getArg(properties, CommandOption.PG_ID);
    if (StringUtils.isBlank(parentPgId)) {
        parentPgId = flowClient.getRootGroupId();
    }
    final ProcessGroupFlowEntity processGroupFlowEntity = flowClient.getProcessGroup(parentPgId);
    final ProcessGroupFlowDTO processGroupFlowDTO = processGroupFlowEntity.getProcessGroupFlow();
    final FlowDTO flowDTO = processGroupFlowDTO.getFlow();
    final List<ProcessGroupDTO> processGroups = new ArrayList<>();
    if (flowDTO.getProcessGroups() != null) {
        flowDTO.getProcessGroups().stream().map(pge -> pge.getComponent()).forEach(dto -> processGroups.add(dto));
    }
    return new ProcessGroupsResult(getResultType(properties), processGroups);
}
Also used : Properties(java.util.Properties) NiFiClientException(org.apache.nifi.toolkit.cli.impl.client.nifi.NiFiClientException) CommandOption(org.apache.nifi.toolkit.cli.impl.command.CommandOption) AbstractNiFiCommand(org.apache.nifi.toolkit.cli.impl.command.nifi.AbstractNiFiCommand) IOException(java.io.IOException) StringUtils(org.apache.commons.lang3.StringUtils) Context(org.apache.nifi.toolkit.cli.api.Context) ArrayList(java.util.ArrayList) NiFiClient(org.apache.nifi.toolkit.cli.impl.client.nifi.NiFiClient) List(java.util.List) ProcessGroupDTO(org.apache.nifi.web.api.dto.ProcessGroupDTO) ProcessGroupsResult(org.apache.nifi.toolkit.cli.impl.result.ProcessGroupsResult) FlowClient(org.apache.nifi.toolkit.cli.impl.client.nifi.FlowClient) ProcessGroupFlowEntity(org.apache.nifi.web.api.entity.ProcessGroupFlowEntity) ProcessGroupFlowDTO(org.apache.nifi.web.api.dto.flow.ProcessGroupFlowDTO) FlowDTO(org.apache.nifi.web.api.dto.flow.FlowDTO) ProcessGroupsResult(org.apache.nifi.toolkit.cli.impl.result.ProcessGroupsResult) ProcessGroupFlowDTO(org.apache.nifi.web.api.dto.flow.ProcessGroupFlowDTO) FlowDTO(org.apache.nifi.web.api.dto.flow.FlowDTO) ProcessGroupFlowDTO(org.apache.nifi.web.api.dto.flow.ProcessGroupFlowDTO) ProcessGroupFlowEntity(org.apache.nifi.web.api.entity.ProcessGroupFlowEntity) ArrayList(java.util.ArrayList) ProcessGroupDTO(org.apache.nifi.web.api.dto.ProcessGroupDTO) FlowClient(org.apache.nifi.toolkit.cli.impl.client.nifi.FlowClient)

Example 5 with NiFiClient

use of org.apache.nifi.toolkit.cli.impl.client.nifi.NiFiClient in project nifi by apache.

the class NiFiCLIMainRunner method main.

public static void main(String[] args) {
    final String[] cmdArgs = ("registry list-buckets help " + "").split("[ ]");
    final Session session = new InMemorySession();
    final ClientFactory<NiFiClient> niFiClientFactory = new NiFiClientFactory();
    final ClientFactory<NiFiRegistryClient> nifiRegClientFactory = new NiFiRegistryClientFactory();
    final Context context = new StandardContext.Builder().output(System.out).session(session).nifiClientFactory(niFiClientFactory).nifiRegistryClientFactory(nifiRegClientFactory).build();
    final Map<String, Command> commands = CommandFactory.createTopLevelCommands(context);
    final Map<String, CommandGroup> commandGroups = CommandFactory.createCommandGroups(context);
    final CommandProcessor processor = new CommandProcessor(commands, commandGroups, context);
    processor.process(cmdArgs);
}
Also used : StandardContext(org.apache.nifi.toolkit.cli.impl.context.StandardContext) Context(org.apache.nifi.toolkit.cli.api.Context) 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) CommandGroup(org.apache.nifi.toolkit.cli.api.CommandGroup) NiFiRegistryClient(org.apache.nifi.registry.client.NiFiRegistryClient) Command(org.apache.nifi.toolkit.cli.api.Command) InMemorySession(org.apache.nifi.toolkit.cli.impl.session.InMemorySession) StandardContext(org.apache.nifi.toolkit.cli.impl.context.StandardContext) CommandProcessor(org.apache.nifi.toolkit.cli.impl.command.CommandProcessor) Session(org.apache.nifi.toolkit.cli.api.Session) InMemorySession(org.apache.nifi.toolkit.cli.impl.session.InMemorySession)

Aggregations

NiFiClient (org.apache.nifi.toolkit.cli.impl.client.nifi.NiFiClient)7 IOException (java.io.IOException)4 NiFiRegistryClient (org.apache.nifi.registry.client.NiFiRegistryClient)4 Context (org.apache.nifi.toolkit.cli.api.Context)4 Properties (java.util.Properties)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 NiFiClientException (org.apache.nifi.toolkit.cli.impl.client.nifi.NiFiClientException)3 InMemorySession (org.apache.nifi.toolkit.cli.impl.session.InMemorySession)3 StringUtils (org.apache.commons.lang3.StringUtils)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 CommandOption (org.apache.nifi.toolkit.cli.impl.command.CommandOption)2 AbstractNiFiCommand (org.apache.nifi.toolkit.cli.impl.command.nifi.AbstractNiFiCommand)2 StandardContext (org.apache.nifi.toolkit.cli.impl.context.StandardContext)2 File (java.io.File)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1