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();
}
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;
}
}
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);
}
}
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());
}
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());
}
Aggregations