use of org.apache.nifi.toolkit.cli.impl.client.nifi.ProcessGroupClient 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.ProcessGroupClient 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.ProcessGroupClient in project nifi by apache.
the class PGGetVars method doExecute.
@Override
public VariableRegistryResult doExecute(final NiFiClient client, final Properties properties) throws NiFiClientException, IOException, MissingOptionException, CommandException {
final String pgId = getRequiredArg(properties, CommandOption.PG_ID);
final ProcessGroupClient pgClient = client.getProcessGroupClient();
final VariableRegistryEntity varEntity = pgClient.getVariables(pgId);
return new VariableRegistryResult(getResultType(properties), varEntity);
}
Aggregations