Search in sources :

Example 1 with NiFiClientException

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;
}
Also used : RegistryClientIDResult(org.apache.nifi.toolkit.cli.impl.result.RegistryClientIDResult) StringResult(org.apache.nifi.toolkit.cli.impl.result.StringResult) Properties(java.util.Properties) NiFiClientException(org.apache.nifi.toolkit.cli.impl.client.nifi.NiFiClientException) IOException(java.io.IOException) MissingOptionException(org.apache.commons.cli.MissingOptionException) NiFiRegistryException(org.apache.nifi.registry.client.NiFiRegistryException) ParseException(org.apache.commons.cli.ParseException) Date(java.util.Date)

Example 2 with NiFiClientException

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());
}
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 3 with NiFiClientException

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);
}
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 4 with NiFiClientException

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();
}
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 5 with NiFiClientException

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();
}
Also used : NiFiClientException(org.apache.nifi.toolkit.cli.impl.client.nifi.NiFiClientException) VersionControlInformationEntity(org.apache.nifi.web.api.entity.VersionControlInformationEntity) VersionsClient(org.apache.nifi.toolkit.cli.impl.client.nifi.VersionsClient) VersionedFlowUpdateRequestEntity(org.apache.nifi.web.api.entity.VersionedFlowUpdateRequestEntity) VersionControlInformationDTO(org.apache.nifi.web.api.dto.VersionControlInformationDTO)

Aggregations

NiFiClientException (org.apache.nifi.toolkit.cli.impl.client.nifi.NiFiClientException)10 FlowClient (org.apache.nifi.toolkit.cli.impl.client.nifi.FlowClient)5 IOException (java.io.IOException)4 Properties (java.util.Properties)3 StringUtils (org.apache.commons.lang3.StringUtils)3 VersionControlInformationDTO (org.apache.nifi.web.api.dto.VersionControlInformationDTO)3 VersionControlInformationEntity (org.apache.nifi.web.api.entity.VersionControlInformationEntity)3 VersionedFlowSnapshotMetadataSetEntity (org.apache.nifi.web.api.entity.VersionedFlowSnapshotMetadataSetEntity)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 MissingOptionException (org.apache.commons.cli.MissingOptionException)2 Context (org.apache.nifi.toolkit.cli.api.Context)2 NiFiClient (org.apache.nifi.toolkit.cli.impl.client.nifi.NiFiClient)2 ProcessGroupBox (org.apache.nifi.toolkit.cli.impl.client.nifi.ProcessGroupBox)2 ProcessGroupClient (org.apache.nifi.toolkit.cli.impl.client.nifi.ProcessGroupClient)2 VersionsClient (org.apache.nifi.toolkit.cli.impl.client.nifi.VersionsClient)2 CommandOption (org.apache.nifi.toolkit.cli.impl.command.CommandOption)2 AbstractNiFiCommand (org.apache.nifi.toolkit.cli.impl.command.nifi.AbstractNiFiCommand)2 RegistryClientIDResult (org.apache.nifi.toolkit.cli.impl.result.RegistryClientIDResult)2 StringResult (org.apache.nifi.toolkit.cli.impl.result.StringResult)2