Search in sources :

Example 1 with ConstraintRequiredParameterException

use of org.alien4cloud.tosca.exceptions.ConstraintRequiredParameterException in project alien4cloud by alien4cloud.

the class RuntimeController method executeOperation.

@ApiOperation(value = "Trigger a custom command on a specific node template of a topology .", authorizations = { @Authorization("APPLICATION_MANAGER") }, notes = "Returns a response with no errors and the command response as data in success case. Application role required [ APPLICATION_MANAGER ]")
@RequestMapping(value = "/{applicationId:.+?}/operations", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
@PreAuthorize("isAuthenticated()")
@Audit(bodyHiddenFields = { "secretProviderCredentials" })
public DeferredResult<RestResponse<Object>> executeOperation(@PathVariable String applicationId, @RequestBody @Valid OperationExecRequest operationRequest) {
    final DeferredResult<RestResponse<Object>> result = new DeferredResult<>(15L * 60L * 1000L);
    Application application = applicationService.getOrFail(applicationId);
    ApplicationEnvironment environment = applicationEnvironmentService.getEnvironmentByIdOrDefault(applicationId, operationRequest.getApplicationEnvironmentId());
    AuthorizationUtil.checkAuthorizationForEnvironment(application, environment);
    Topology topology = deploymentRuntimeStateService.getRuntimeTopologyFromEnvironment(operationRequest.getApplicationEnvironmentId());
    // validate the operation request
    try {
        validateCommand(operationRequest, topology);
    } catch (ConstraintViolationException e) {
        result.setErrorResult(RestResponseBuilder.<Object>builder().data(e.getConstraintInformation()).error(new RestError(RestErrorCode.PROPERTY_CONSTRAINT_VIOLATION_ERROR.getCode(), e.getMessage())).build());
        return result;
    } catch (ConstraintValueDoNotMatchPropertyTypeException e) {
        result.setErrorResult(RestResponseBuilder.<Object>builder().data(e.getConstraintInformation()).error(new RestError(RestErrorCode.PROPERTY_TYPE_VIOLATION_ERROR.getCode(), e.getMessage())).build());
        return result;
    } catch (ConstraintRequiredParameterException e) {
        result.setErrorResult(RestResponseBuilder.<Object>builder().data(e.getConstraintInformation()).error(new RestError(RestErrorCode.PROPERTY_REQUIRED_VIOLATION_ERROR.getCode(), e.getMessage())).build());
        return result;
    } catch (ConstraintFunctionalException e) {
        result.setErrorResult(RestResponseBuilder.<Object>builder().data(e.getConstraintInformation()).error(new RestError(RestErrorCode.PROPERTY_UNKNOWN_VIOLATION_ERROR.getCode(), e.getMessage())).build());
        return result;
    }
    // try to trigger the execution of the operation
    try {
        deploymentRuntimeService.triggerOperationExecution(operationRequest, new IPaaSCallback<Map<String, String>>() {

            @Override
            public void onSuccess(Map<String, String> data) {
                result.setResult(RestResponseBuilder.<Object>builder().data(data).build());
            }

            @Override
            public void onFailure(Throwable throwable) {
                result.setErrorResult(RestResponseBuilder.<Object>builder().error(new RestError(RestErrorCode.NODE_OPERATION_EXECUTION_ERROR.getCode(), throwable.getMessage())).build());
            }
        });
    } catch (OperationExecutionException e) {
        result.setErrorResult(RestResponseBuilder.<Object>builder().error(new RestError(RestErrorCode.NODE_OPERATION_EXECUTION_ERROR.getCode(), e.getMessage())).build());
    } catch (OrchestratorDisabledException e) {
        result.setErrorResult(RestResponseBuilder.<Object>builder().error(new RestError(RestErrorCode.CLOUD_DISABLED_ERROR.getCode(), e.getMessage())).build());
    }
    return result;
}
Also used : ConstraintValueDoNotMatchPropertyTypeException(org.alien4cloud.tosca.exceptions.ConstraintValueDoNotMatchPropertyTypeException) RestResponse(alien4cloud.rest.model.RestResponse) DeploymentTopology(alien4cloud.model.deployment.DeploymentTopology) Topology(org.alien4cloud.tosca.model.templates.Topology) ApplicationEnvironment(alien4cloud.model.application.ApplicationEnvironment) ConstraintFunctionalException(org.alien4cloud.tosca.exceptions.ConstraintFunctionalException) OrchestratorDisabledException(alien4cloud.paas.exception.OrchestratorDisabledException) RestError(alien4cloud.rest.model.RestError) OperationExecutionException(alien4cloud.paas.exception.OperationExecutionException) ConstraintViolationException(org.alien4cloud.tosca.exceptions.ConstraintViolationException) Application(alien4cloud.model.application.Application) Map(java.util.Map) ConstraintRequiredParameterException(org.alien4cloud.tosca.exceptions.ConstraintRequiredParameterException) DeferredResult(org.springframework.web.context.request.async.DeferredResult) Audit(alien4cloud.audit.annotation.Audit) ApiOperation(io.swagger.annotations.ApiOperation) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 2 with ConstraintRequiredParameterException

use of org.alien4cloud.tosca.exceptions.ConstraintRequiredParameterException in project alien4cloud by alien4cloud.

the class RuntimeController method validateParameters.

private void validateParameters(Interface interfass, OperationExecRequest operationRequest, Set<CSARDependency> dependencies) throws ConstraintViolationException, ConstraintValueDoNotMatchPropertyTypeException, ConstraintRequiredParameterException {
    try {
        if (dependencies != null) {
            ToscaContext.init(dependencies);
        }
        ArrayList<String> missingParams = Lists.newArrayList();
        Operation operation = interfass.getOperations().get(operationRequest.getOperationName());
        if (operation.getInputParameters() != null) {
            for (Entry<String, IValue> inputParameter : operation.getInputParameters().entrySet()) {
                if (inputParameter.getValue().isDefinition()) {
                    Object requestInputParameter = operationRequest.getParameters() == null ? null : operationRequest.getParameters().get(inputParameter.getKey());
                    PropertyDefinition currentOperationParameter = (PropertyDefinition) inputParameter.getValue();
                    if (requestInputParameter != null) {
                        if (!(requestInputParameter instanceof Map) || !FunctionEvaluator.containGetSecretFunction(PropertyService.asFunctionPropertyValue(requestInputParameter))) {
                            // recover the good property definition for the current parameter
                            ConstraintPropertyService.checkPropertyConstraint(inputParameter.getKey(), requestInputParameter, currentOperationParameter);
                        }
                    } else if (currentOperationParameter.isRequired()) {
                        // input param not in the request, id required this is a missing parameter...
                        missingParams.add(inputParameter.getKey());
                    } else {
                        // set the value to null
                        operation.getInputParameters().put(inputParameter.getKey(), null);
                    }
                }
            }
        }
        // check required input issue
        if (!missingParams.isEmpty()) {
            log.error("Missing required parameter", missingParams);
            ConstraintInformation constraintInformation = new ConstraintInformation(null, null, missingParams.toString(), "required");
            throw new ConstraintRequiredParameterException("Missing required parameters", null, constraintInformation);
        }
    } finally {
        if (ToscaContext.get() != null) {
            ToscaContext.destroy();
        }
    }
}
Also used : IValue(org.alien4cloud.tosca.model.definitions.IValue) ConstraintInformation(alien4cloud.tosca.properties.constraints.ConstraintUtil.ConstraintInformation) ApiOperation(io.swagger.annotations.ApiOperation) Operation(org.alien4cloud.tosca.model.definitions.Operation) PropertyDefinition(org.alien4cloud.tosca.model.definitions.PropertyDefinition) Map(java.util.Map) ConstraintRequiredParameterException(org.alien4cloud.tosca.exceptions.ConstraintRequiredParameterException)

Aggregations

ApiOperation (io.swagger.annotations.ApiOperation)2 Map (java.util.Map)2 ConstraintRequiredParameterException (org.alien4cloud.tosca.exceptions.ConstraintRequiredParameterException)2 Audit (alien4cloud.audit.annotation.Audit)1 Application (alien4cloud.model.application.Application)1 ApplicationEnvironment (alien4cloud.model.application.ApplicationEnvironment)1 DeploymentTopology (alien4cloud.model.deployment.DeploymentTopology)1 OperationExecutionException (alien4cloud.paas.exception.OperationExecutionException)1 OrchestratorDisabledException (alien4cloud.paas.exception.OrchestratorDisabledException)1 RestError (alien4cloud.rest.model.RestError)1 RestResponse (alien4cloud.rest.model.RestResponse)1 ConstraintInformation (alien4cloud.tosca.properties.constraints.ConstraintUtil.ConstraintInformation)1 ConstraintFunctionalException (org.alien4cloud.tosca.exceptions.ConstraintFunctionalException)1 ConstraintValueDoNotMatchPropertyTypeException (org.alien4cloud.tosca.exceptions.ConstraintValueDoNotMatchPropertyTypeException)1 ConstraintViolationException (org.alien4cloud.tosca.exceptions.ConstraintViolationException)1 IValue (org.alien4cloud.tosca.model.definitions.IValue)1 Operation (org.alien4cloud.tosca.model.definitions.Operation)1 PropertyDefinition (org.alien4cloud.tosca.model.definitions.PropertyDefinition)1 Topology (org.alien4cloud.tosca.model.templates.Topology)1 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)1