Search in sources :

Example 1 with ConstraintValueDoNotMatchPropertyTypeException

use of org.alien4cloud.tosca.exceptions.ConstraintValueDoNotMatchPropertyTypeException 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 ConstraintValueDoNotMatchPropertyTypeException

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

the class LocationMetaPropertiesController method upsertMetaProperty.

/**
 * Update or create a property for an orchestrator
 *
 * @param orchestratorId id of the orchestrator the location belongs to.
 * @param locationId id of the location to update
 * @param propertyRequest property request
 * @return information on the constraint
 */
@RequestMapping(method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
@PreAuthorize("hasAuthority('ADMIN')")
@Audit
public RestResponse<ConstraintInformation> upsertMetaProperty(@ApiParam(value = "Id of the orchestrator for which the location is defined.") @PathVariable String orchestratorId, @ApiParam(value = "Id of the location to get", required = true) @PathVariable String locationId, @ApiParam(value = "Id of the location to get", required = true) @RequestBody PropertyRequest propertyRequest) throws ConstraintViolationException, ConstraintValueDoNotMatchPropertyTypeException {
    AuthorizationUtil.hasOneRoleIn(Role.ADMIN);
    Location location = locationService.getOrFail(locationId);
    try {
        metaPropertiesService.upsertMetaProperty(location, propertyRequest.getDefinitionId(), propertyRequest.getValue());
    } catch (ConstraintViolationException e) {
        log.error("Constraint violation error for property <" + propertyRequest.getDefinitionId() + "> with value <" + propertyRequest.getValue() + ">", e);
        return RestResponseBuilder.<ConstraintInformation>builder().data(e.getConstraintInformation()).error(RestErrorBuilder.builder(RestErrorCode.PROPERTY_CONSTRAINT_VIOLATION_ERROR).message(e.getMessage()).build()).build();
    } catch (ConstraintValueDoNotMatchPropertyTypeException e) {
        log.error("Constraint value violation error for property <" + e.getConstraintInformation().getName() + "> with value <" + e.getConstraintInformation().getValue() + "> and type <" + e.getConstraintInformation().getType() + ">", e);
        return RestResponseBuilder.<ConstraintInformation>builder().data(e.getConstraintInformation()).error(RestErrorBuilder.builder(RestErrorCode.PROPERTY_TYPE_VIOLATION_ERROR).message(e.getMessage()).build()).build();
    }
    return RestResponseBuilder.<ConstraintInformation>builder().data(null).error(null).build();
}
Also used : ConstraintValueDoNotMatchPropertyTypeException(org.alien4cloud.tosca.exceptions.ConstraintValueDoNotMatchPropertyTypeException) ConstraintViolationException(org.alien4cloud.tosca.exceptions.ConstraintViolationException) ConstraintInformation(alien4cloud.tosca.properties.constraints.ConstraintUtil.ConstraintInformation) Location(alien4cloud.model.orchestrators.locations.Location) Audit(alien4cloud.audit.annotation.Audit) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 3 with ConstraintValueDoNotMatchPropertyTypeException

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

the class ApplicationMetaPropertyController method upsertProperty.

/**
 * Update or create a property for an application
 *
 * @param applicationId id of the application
 * @param propertyRequest property request
 * @return information on the constraint
 * @throws ConstraintValueDoNotMatchPropertyTypeException
 * @throws ConstraintViolationException
 */
@RequestMapping(method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
@PreAuthorize("isAuthenticated()")
@Audit
public RestResponse<ConstraintUtil.ConstraintInformation> upsertProperty(@PathVariable String applicationId, @RequestBody PropertyRequest propertyRequest) throws ConstraintViolationException, ConstraintValueDoNotMatchPropertyTypeException {
    Application application = alienDAO.findById(Application.class, applicationId);
    AuthorizationUtil.checkAuthorizationForApplication(application, ApplicationRole.APPLICATION_MANAGER);
    try {
        metaPropertiesService.upsertMetaProperty(application, propertyRequest.getDefinitionId(), propertyRequest.getValue());
    } catch (ConstraintViolationException e) {
        log.error("Constraint violation error for property <" + propertyRequest.getDefinitionId() + "> with value <" + propertyRequest.getValue() + ">", e);
        return RestResponseBuilder.<ConstraintUtil.ConstraintInformation>builder().data(e.getConstraintInformation()).error(RestErrorBuilder.builder(RestErrorCode.PROPERTY_CONSTRAINT_VIOLATION_ERROR).message(e.getMessage()).build()).build();
    } catch (ConstraintValueDoNotMatchPropertyTypeException e) {
        log.error("Constraint value violation error for property <" + e.getConstraintInformation().getName() + "> with value <" + e.getConstraintInformation().getValue() + "> and type <" + e.getConstraintInformation().getType() + ">", e);
        return RestResponseBuilder.<ConstraintUtil.ConstraintInformation>builder().data(e.getConstraintInformation()).error(RestErrorBuilder.builder(RestErrorCode.PROPERTY_TYPE_VIOLATION_ERROR).message(e.getMessage()).build()).build();
    }
    return RestResponseBuilder.<ConstraintUtil.ConstraintInformation>builder().data(null).error(null).build();
}
Also used : ConstraintUtil(alien4cloud.tosca.properties.constraints.ConstraintUtil) ConstraintValueDoNotMatchPropertyTypeException(org.alien4cloud.tosca.exceptions.ConstraintValueDoNotMatchPropertyTypeException) ConstraintViolationException(org.alien4cloud.tosca.exceptions.ConstraintViolationException) Application(alien4cloud.model.application.Application) Audit(alien4cloud.audit.annotation.Audit) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 4 with ConstraintValueDoNotMatchPropertyTypeException

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

the class ConstraintPropertyService method checkMapPropertyConstraint.

private static void checkMapPropertyConstraint(String propertyName, Map<String, Object> mapPropertyValue, PropertyDefinition propertyDefinition, Consumer<String> missingPropertyConsumer) throws ConstraintValueDoNotMatchPropertyTypeException, ConstraintViolationException {
    if (!ToscaTypes.MAP.equals(propertyDefinition.getType())) {
        throwConstraintValueDoNotMatchPropertyTypeException("The property definition should be a map list but we found " + propertyDefinition.getType(), propertyName, ToscaTypes.MAP, null);
    }
    PropertyDefinition entrySchema = propertyDefinition.getEntrySchema();
    if (entrySchema == null) {
        throw new ConstraintValueDoNotMatchPropertyTypeException("value is a map but type actually is <" + propertyDefinition.getType() + ">");
    }
    checkLengthConstraints(propertyDefinition.getConstraints(), mapPropertyValue);
    for (Map.Entry<String, Object> complexPropertyValueEntry : mapPropertyValue.entrySet()) {
        checkPropertyConstraint(propertyName + "." + complexPropertyValueEntry.getKey(), complexPropertyValueEntry.getValue(), entrySchema, missingPropertyConsumer);
    }
}
Also used : ConstraintValueDoNotMatchPropertyTypeException(org.alien4cloud.tosca.exceptions.ConstraintValueDoNotMatchPropertyTypeException) PropertyDefinition(org.alien4cloud.tosca.model.definitions.PropertyDefinition) Map(java.util.Map)

Example 5 with ConstraintValueDoNotMatchPropertyTypeException

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

the class ConstraintPropertyService method checkBasicType.

/**
 * Check that a given value is matching the native type defined on the property definition.
 *
 * @param propertyName The name of the property under validation
 * @param primitiveType The primitive type to check the value against.
 * @param propertyValue The value to check.
 * @throws ConstraintValueDoNotMatchPropertyTypeException in case the value does not match the primitive type.
 */
private static void checkBasicType(final String propertyName, final String primitiveType, final String propertyValue) throws ConstraintValueDoNotMatchPropertyTypeException {
    // "string" (basic case, no exception), "float", "integer", "version"
    try {
        IPropertyType<?> propertyType = ToscaTypes.fromYamlTypeName(primitiveType);
        propertyType.parse(propertyValue);
    } catch (InvalidPropertyValueException e) {
        log.debug("The property value for property {} is not of type {}: {}", propertyName, primitiveType, propertyValue, e);
        ConstraintInformation consInformation = new ConstraintInformation(propertyName, null, propertyValue, primitiveType);
        throw new ConstraintValueDoNotMatchPropertyTypeException(e.getMessage(), e, consInformation);
    }
}
Also used : ConstraintValueDoNotMatchPropertyTypeException(org.alien4cloud.tosca.exceptions.ConstraintValueDoNotMatchPropertyTypeException) InvalidPropertyValueException(org.alien4cloud.tosca.exceptions.InvalidPropertyValueException) ConstraintInformation(alien4cloud.tosca.properties.constraints.ConstraintUtil.ConstraintInformation)

Aggregations

ConstraintValueDoNotMatchPropertyTypeException (org.alien4cloud.tosca.exceptions.ConstraintValueDoNotMatchPropertyTypeException)17 ConstraintViolationException (org.alien4cloud.tosca.exceptions.ConstraintViolationException)11 Map (java.util.Map)7 ConstraintTechnicalException (org.alien4cloud.tosca.exceptions.ConstraintTechnicalException)4 Audit (alien4cloud.audit.annotation.Audit)3 NotFoundException (alien4cloud.exception.NotFoundException)3 Application (alien4cloud.model.application.Application)3 ApplicationEnvironment (alien4cloud.model.application.ApplicationEnvironment)3 Location (alien4cloud.model.orchestrators.locations.Location)3 ConstraintInformation (alien4cloud.tosca.properties.constraints.ConstraintUtil.ConstraintInformation)3 AbstractPropertyValue (org.alien4cloud.tosca.model.definitions.AbstractPropertyValue)3 PropertyDefinition (org.alien4cloud.tosca.model.definitions.PropertyDefinition)3 ScalarPropertyValue (org.alien4cloud.tosca.model.definitions.ScalarPropertyValue)3 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)3 ConstraintUtil (alien4cloud.tosca.properties.constraints.ConstraintUtil)2 DeploymentInputs (org.alien4cloud.alm.deployment.configuration.model.DeploymentInputs)2 FunctionPropertyValue (org.alien4cloud.tosca.model.definitions.FunctionPropertyValue)2 PropertyConstraint (org.alien4cloud.tosca.model.definitions.PropertyConstraint)2 Topology (org.alien4cloud.tosca.model.templates.Topology)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2