use of org.alien4cloud.tosca.exceptions.ConstraintViolationException 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;
}
use of org.alien4cloud.tosca.exceptions.ConstraintViolationException 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();
}
use of org.alien4cloud.tosca.exceptions.ConstraintViolationException 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();
}
use of org.alien4cloud.tosca.exceptions.ConstraintViolationException in project alien4cloud by alien4cloud.
the class ConstraintPropertyService method checkSimplePropertyConstraint.
/**
* Check constraints defined on a property for a specified value
*
* @param propertyName Property name (mainly used to create a comprehensive error message)
* @param stringValue Tested property value
* @param propertyDefinition Full property definition with type, constraints, default value,...
* @throws ConstraintViolationException
* @throws ConstraintValueDoNotMatchPropertyTypeException
*/
private static void checkSimplePropertyConstraint(final String propertyName, final String stringValue, final PropertyDefinition propertyDefinition) throws ConstraintViolationException, ConstraintValueDoNotMatchPropertyTypeException {
ConstraintInformation consInformation = null;
// check any property definition without constraints (type/value)
checkBasicType(propertyName, propertyDefinition.getType(), stringValue);
if (propertyDefinition.getConstraints() != null && !propertyDefinition.getConstraints().isEmpty()) {
IPropertyType<?> toscaType = ToscaTypes.fromYamlTypeName(propertyDefinition.getType());
for (PropertyConstraint constraint : propertyDefinition.getConstraints()) {
try {
consInformation = ConstraintUtil.getConstraintInformation(constraint);
consInformation.setPath(propertyName + ".constraints[" + consInformation.getName() + "]");
constraint.initialize(toscaType);
constraint.validate(toscaType, stringValue);
} catch (ConstraintViolationException e) {
throw new ConstraintViolationException(e.getMessage(), e, consInformation);
} catch (IntrospectionException e) {
// ConstraintValueDoNotMatchPropertyTypeException is not supposed to be raised here (only in constraint definition validation)
log.info("Constraint introspection error for property <" + propertyName + "> value <" + stringValue + ">", e);
throw new ConstraintTechnicalException("Constraint introspection error for property <" + propertyName + "> value <" + stringValue + ">", e);
}
}
}
}
use of org.alien4cloud.tosca.exceptions.ConstraintViolationException in project alien4cloud by alien4cloud.
the class ConstraintPropertyService method checkConstraints.
private static void checkConstraints(final String propertyName, final String stringValue, final String typeName, List<PropertyConstraint> constraints) throws ConstraintViolationException, ConstraintValueDoNotMatchPropertyTypeException {
ConstraintInformation consInformation = null;
for (PropertyConstraint constraint : constraints) {
IPropertyType<?> toscaType = ToscaTypes.fromYamlTypeName(typeName);
try {
consInformation = ConstraintUtil.getConstraintInformation(constraint);
consInformation.setPath(propertyName + ".constraints[" + consInformation.getName() + "]");
constraint.initialize(toscaType);
constraint.validate(toscaType, stringValue);
} catch (ConstraintViolationException e) {
throw new ConstraintViolationException(e.getMessage(), e, consInformation);
} catch (IntrospectionException e) {
// ConstraintValueDoNotMatchPropertyTypeException is not supposed to be raised here (only in constraint definition validation)
log.info("Constraint introspection error for property <" + propertyName + "> value <" + stringValue + ">", e);
throw new ConstraintTechnicalException("Constraint introspection error for property <" + propertyName + "> value <" + stringValue + ">", e);
}
}
}
Aggregations