Search in sources :

Example 1 with ProcessGroupClient

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());
}
Also used : NiFiClientException(org.apache.nifi.toolkit.cli.impl.client.nifi.NiFiClientException) RegistryClientsEntity(org.apache.nifi.web.api.entity.RegistryClientsEntity) ProcessGroupEntity(org.apache.nifi.web.api.entity.ProcessGroupEntity) RegistryClientEntity(org.apache.nifi.web.api.entity.RegistryClientEntity) FlowClient(org.apache.nifi.toolkit.cli.impl.client.nifi.FlowClient) ProcessGroupBox(org.apache.nifi.toolkit.cli.impl.client.nifi.ProcessGroupBox) PositionDTO(org.apache.nifi.web.api.dto.PositionDTO) VersionControlInformationDTO(org.apache.nifi.web.api.dto.VersionControlInformationDTO) ProcessGroupDTO(org.apache.nifi.web.api.dto.ProcessGroupDTO) StringResult(org.apache.nifi.toolkit.cli.impl.result.StringResult) ProcessGroupClient(org.apache.nifi.toolkit.cli.impl.client.nifi.ProcessGroupClient) MissingOptionException(org.apache.commons.cli.MissingOptionException)

Example 2 with ProcessGroupClient

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();
}
Also used : NiFiClientException(org.apache.nifi.toolkit.cli.impl.client.nifi.NiFiClientException) VariableEntity(org.apache.nifi.web.api.entity.VariableEntity) VariableRegistryUpdateRequestEntity(org.apache.nifi.web.api.entity.VariableRegistryUpdateRequestEntity) VariableRegistryDTO(org.apache.nifi.web.api.dto.VariableRegistryDTO) VariableRegistryEntity(org.apache.nifi.web.api.entity.VariableRegistryEntity) VariableDTO(org.apache.nifi.web.api.dto.VariableDTO) ProcessGroupClient(org.apache.nifi.toolkit.cli.impl.client.nifi.ProcessGroupClient)

Example 3 with ProcessGroupClient

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);
}
Also used : VariableRegistryEntity(org.apache.nifi.web.api.entity.VariableRegistryEntity) ProcessGroupClient(org.apache.nifi.toolkit.cli.impl.client.nifi.ProcessGroupClient) VariableRegistryResult(org.apache.nifi.toolkit.cli.impl.result.VariableRegistryResult)

Aggregations

ProcessGroupClient (org.apache.nifi.toolkit.cli.impl.client.nifi.ProcessGroupClient)3 NiFiClientException (org.apache.nifi.toolkit.cli.impl.client.nifi.NiFiClientException)2 VariableRegistryEntity (org.apache.nifi.web.api.entity.VariableRegistryEntity)2 MissingOptionException (org.apache.commons.cli.MissingOptionException)1 FlowClient (org.apache.nifi.toolkit.cli.impl.client.nifi.FlowClient)1 ProcessGroupBox (org.apache.nifi.toolkit.cli.impl.client.nifi.ProcessGroupBox)1 StringResult (org.apache.nifi.toolkit.cli.impl.result.StringResult)1 VariableRegistryResult (org.apache.nifi.toolkit.cli.impl.result.VariableRegistryResult)1 PositionDTO (org.apache.nifi.web.api.dto.PositionDTO)1 ProcessGroupDTO (org.apache.nifi.web.api.dto.ProcessGroupDTO)1 VariableDTO (org.apache.nifi.web.api.dto.VariableDTO)1 VariableRegistryDTO (org.apache.nifi.web.api.dto.VariableRegistryDTO)1 VersionControlInformationDTO (org.apache.nifi.web.api.dto.VersionControlInformationDTO)1 ProcessGroupEntity (org.apache.nifi.web.api.entity.ProcessGroupEntity)1 RegistryClientEntity (org.apache.nifi.web.api.entity.RegistryClientEntity)1 RegistryClientsEntity (org.apache.nifi.web.api.entity.RegistryClientsEntity)1 VariableEntity (org.apache.nifi.web.api.entity.VariableEntity)1 VariableRegistryUpdateRequestEntity (org.apache.nifi.web.api.entity.VariableRegistryUpdateRequestEntity)1