use of org.apache.nifi.toolkit.cli.impl.client.nifi.NiFiClientException in project nifi by apache.
the class QuickImport method getRegistryClientId.
private String getRegistryClientId(final NiFiClient nifiClient, final String registryClientBaseUrl, final boolean isInteractive) throws NiFiClientException, IOException, MissingOptionException {
final Properties getRegClientProps = new Properties();
getRegClientProps.setProperty(CommandOption.REGISTRY_CLIENT_URL.getLongName(), registryClientBaseUrl);
String registryClientId;
try {
final RegistryClientIDResult registryClientResult = getRegistryClientId.doExecute(nifiClient, getRegClientProps);
registryClientId = registryClientResult.getResult().getId();
if (isInteractive) {
println();
println("Found existing registry client '" + registryClientResult.getResult().getName() + "'...");
}
} catch (Exception e) {
registryClientId = null;
}
if (registryClientId == null) {
final Properties createRegClientProps = new Properties();
createRegClientProps.setProperty(CommandOption.REGISTRY_CLIENT_NAME.getLongName(), REG_CLIENT_NAME);
createRegClientProps.setProperty(CommandOption.REGISTRY_CLIENT_DESC.getLongName(), REG_CLIENT_DESC + new Date().toString());
createRegClientProps.setProperty(CommandOption.REGISTRY_CLIENT_URL.getLongName(), registryClientBaseUrl);
final StringResult createdRegClient = createRegistryClient.doExecute(nifiClient, createRegClientProps);
registryClientId = createdRegClient.getResult();
if (isInteractive) {
println();
println("Created new registry client '" + REG_CLIENT_NAME + "'...");
}
}
return registryClientId;
}
use of org.apache.nifi.toolkit.cli.impl.client.nifi.NiFiClientException in project nifi by apache.
the class PGImport method doExecute.
@Override
public StringResult doExecute(final NiFiClient client, final Properties properties) throws NiFiClientException, IOException, MissingOptionException {
final String bucketId = getRequiredArg(properties, CommandOption.BUCKET_ID);
final String flowId = getRequiredArg(properties, CommandOption.FLOW_ID);
final Integer flowVersion = getRequiredIntArg(properties, CommandOption.FLOW_VERSION);
// if a registry client is specified use it, otherwise see if there is only one available and use that,
// if more than one is available then throw an exception because we don't know which one to use
String registryId = getArg(properties, CommandOption.REGISTRY_CLIENT_ID);
if (StringUtils.isBlank(registryId)) {
final RegistryClientsEntity registries = client.getControllerClient().getRegistryClients();
final Set<RegistryClientEntity> entities = registries.getRegistries();
if (entities == null || entities.isEmpty()) {
throw new NiFiClientException("No registry clients available");
}
if (entities.size() == 1) {
registryId = entities.stream().findFirst().get().getId();
} else {
throw new MissingOptionException(CommandOption.REGISTRY_CLIENT_ID.getLongName() + " must be provided when there is more than one available");
}
}
// 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)) {
final FlowClient flowClient = client.getFlowClient();
parentPgId = flowClient.getRootGroupId();
}
final VersionControlInformationDTO versionControlInfo = new VersionControlInformationDTO();
versionControlInfo.setRegistryId(registryId);
versionControlInfo.setBucketId(bucketId);
versionControlInfo.setFlowId(flowId);
versionControlInfo.setVersion(flowVersion);
final ProcessGroupBox pgBox = client.getFlowClient().getSuggestedProcessGroupCoordinates(parentPgId);
final PositionDTO posDto = new PositionDTO();
posDto.setX(Integer.valueOf(pgBox.getX()).doubleValue());
posDto.setY(Integer.valueOf(pgBox.getY()).doubleValue());
final ProcessGroupDTO pgDto = new ProcessGroupDTO();
pgDto.setVersionControlInformation(versionControlInfo);
pgDto.setPosition(posDto);
final ProcessGroupEntity pgEntity = new ProcessGroupEntity();
pgEntity.setComponent(pgDto);
pgEntity.setRevision(getInitialRevisionDTO());
final ProcessGroupClient pgClient = client.getProcessGroupClient();
final ProcessGroupEntity createdEntity = pgClient.createProcessGroup(parentPgId, pgEntity);
return new StringResult(createdEntity.getId(), getContext().isInteractive());
}
use of org.apache.nifi.toolkit.cli.impl.client.nifi.NiFiClientException 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);
}
use of org.apache.nifi.toolkit.cli.impl.client.nifi.NiFiClientException in project nifi by apache.
the class PGSetVar method doExecute.
@Override
public VoidResult doExecute(final NiFiClient client, final Properties properties) throws NiFiClientException, IOException, MissingOptionException, CommandException {
final String pgId = getRequiredArg(properties, CommandOption.PG_ID);
final String varName = getRequiredArg(properties, CommandOption.PG_VAR_NAME);
final String varVal = getRequiredArg(properties, CommandOption.PG_VAR_VALUE);
final ProcessGroupClient pgClient = client.getProcessGroupClient();
final VariableRegistryEntity variableRegistry = pgClient.getVariables(pgId);
final VariableRegistryDTO variableRegistryDTO = variableRegistry.getVariableRegistry();
final VariableDTO variableDTO = new VariableDTO();
variableDTO.setName(varName);
variableDTO.setValue(varVal);
final VariableEntity variableEntity = new VariableEntity();
variableEntity.setVariable(variableDTO);
// take the existing DTO and set only the requested variable for this command
variableRegistryDTO.setVariables(Collections.singleton(variableEntity));
// initiate the update request by posting the updated variable registry
final VariableRegistryUpdateRequestEntity createdUpdateRequest = pgClient.updateVariableRegistry(pgId, variableRegistry);
// poll the update request for up to 30 seconds to see if it has completed
// if it doesn't complete then an exception will be thrown, but in either case the request will be deleted
final String updateRequestId = createdUpdateRequest.getRequest().getRequestId();
try {
boolean completed = false;
for (int i = 0; i < 30; i++) {
final VariableRegistryUpdateRequestEntity updateRequest = pgClient.getVariableRegistryUpdateRequest(pgId, updateRequestId);
if (updateRequest != null && updateRequest.getRequest().isComplete()) {
completed = true;
break;
} else {
try {
if (getContext().isInteractive()) {
println("Waiting for update request to complete...");
}
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
if (!completed) {
throw new NiFiClientException("Unable to update variables of process group, cancelling request");
}
} finally {
pgClient.deleteVariableRegistryUpdateRequest(pgId, updateRequestId);
}
return VoidResult.getInstance();
}
use of org.apache.nifi.toolkit.cli.impl.client.nifi.NiFiClientException in project nifi by apache.
the class PGChangeVersion method doExecute.
@Override
public VoidResult doExecute(final NiFiClient client, final Properties properties) throws NiFiClientException, IOException, MissingOptionException, CommandException {
final String pgId = getRequiredArg(properties, CommandOption.PG_ID);
final VersionsClient versionsClient = client.getVersionsClient();
final VersionControlInformationEntity existingVersionControlInfo = versionsClient.getVersionControlInfo(pgId);
final VersionControlInformationDTO existingVersionControlDTO = existingVersionControlInfo.getVersionControlInformation();
if (existingVersionControlDTO == null) {
throw new NiFiClientException("Process group is not under version control");
}
// start with the version specified in the arguments
Integer newVersion = getIntArg(properties, CommandOption.FLOW_VERSION);
// if no version was specified, automatically determine the latest and change to that
if (newVersion == null) {
newVersion = getLatestVersion(client, existingVersionControlDTO);
if (newVersion.intValue() == existingVersionControlDTO.getVersion().intValue()) {
throw new NiFiClientException("Process group already at latest version");
}
}
// update the version in the existing DTO to the new version so we can submit it back
existingVersionControlDTO.setVersion(newVersion);
// initiate the version change which creates an update request that must be checked for completion
final VersionedFlowUpdateRequestEntity initialUpdateRequest = versionsClient.updateVersionControlInfo(pgId, existingVersionControlInfo);
// poll the update request for up to 30 seconds to see if it has completed
// if it doesn't complete then an exception will be thrown, but in either case the request will be deleted
final String updateRequestId = initialUpdateRequest.getRequest().getRequestId();
try {
boolean completed = false;
for (int i = 0; i < 30; i++) {
final VersionedFlowUpdateRequestEntity updateRequest = versionsClient.getUpdateRequest(updateRequestId);
if (updateRequest != null && updateRequest.getRequest().isComplete()) {
completed = true;
break;
} else {
try {
if (getContext().isInteractive()) {
println("Waiting for update request to complete...");
}
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
if (!completed) {
throw new NiFiClientException("Unable to change version of process group, cancelling request");
}
} finally {
versionsClient.deleteUpdateRequest(updateRequestId);
}
return VoidResult.getInstance();
}
Aggregations