Search in sources :

Example 1 with ConstraintFunctionalException

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

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

the class DeploymentTopologyController method updateSubstitutionProperty.

@ApiOperation(value = "Update node substitution's property.", authorizations = { @Authorization("ADMIN") })
@RequestMapping(value = "/substitutions/{nodeId}/properties", method = RequestMethod.POST)
@PreAuthorize("isAuthenticated()")
@Audit
public RestResponse<?> updateSubstitutionProperty(@PathVariable String appId, @PathVariable String environmentId, @PathVariable String nodeId, @RequestBody UpdatePropertyRequest updateRequest) {
    try {
        Application application = applicationService.getOrFail(appId);
        ApplicationEnvironment environment = appEnvironmentService.getOrFail(environmentId);
        AuthorizationUtil.checkAuthorizationForEnvironment(application, environment);
        ApplicationTopologyVersion topologyVersion = applicationVersionService.getOrFail(Csar.createId(environment.getApplicationId(), environment.getVersion()), environment.getTopologyVersion());
        Topology topology = topologyServiceCore.getOrFail(topologyVersion.getArchiveId());
        DeploymentTopologyDTO dto = deploymentTopologyDTOBuilder.prepareDeployment(topology, () -> matchedNodePropertiesConfigService.updateProperty(application, environment, topology, nodeId, Optional.empty(), updateRequest.getPropertyName(), updateRequest.getPropertyValue()));
        return RestResponseBuilder.<DeploymentTopologyDTO>builder().data(dto).build();
    } catch (ConstraintTechnicalException e) {
        if (e.getCause() instanceof ConstraintFunctionalException) {
            return RestConstraintValidator.fromException((ConstraintFunctionalException) e.getCause(), updateRequest.getPropertyName(), updateRequest.getPropertyValue());
        }
        throw e;
    }
}
Also used : ConstraintFunctionalException(org.alien4cloud.tosca.exceptions.ConstraintFunctionalException) DeploymentTopology(alien4cloud.model.deployment.DeploymentTopology) Topology(org.alien4cloud.tosca.model.templates.Topology) Application(alien4cloud.model.application.Application) ApplicationEnvironment(alien4cloud.model.application.ApplicationEnvironment) DeploymentTopologyDTO(alien4cloud.deployment.DeploymentTopologyDTO) ConstraintTechnicalException(org.alien4cloud.tosca.exceptions.ConstraintTechnicalException) ApplicationTopologyVersion(alien4cloud.model.application.ApplicationTopologyVersion) Audit(alien4cloud.audit.annotation.Audit) ApiOperation(io.swagger.annotations.ApiOperation) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 3 with ConstraintFunctionalException

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

the class DeploymentTopologyController method updateSubstitutionCapabilityProperty.

@ApiOperation(value = "Update substitution's capability property.", authorizations = { @Authorization("ADMIN") })
@RequestMapping(value = "/substitutions/{nodeId}/capabilities/{capabilityName}/properties", method = RequestMethod.POST)
@PreAuthorize("isAuthenticated()")
@Audit
public RestResponse<?> updateSubstitutionCapabilityProperty(@PathVariable String appId, @PathVariable String environmentId, @PathVariable String nodeId, @PathVariable String capabilityName, @RequestBody UpdatePropertyRequest updateRequest) {
    try {
        Application application = applicationService.getOrFail(appId);
        ApplicationEnvironment environment = appEnvironmentService.getOrFail(environmentId);
        AuthorizationUtil.checkAuthorizationForEnvironment(application, environment);
        ApplicationTopologyVersion topologyVersion = applicationVersionService.getOrFail(Csar.createId(environment.getApplicationId(), environment.getVersion()), environment.getTopologyVersion());
        Topology topology = topologyServiceCore.getOrFail(topologyVersion.getArchiveId());
        DeploymentTopologyDTO dto = deploymentTopologyDTOBuilder.prepareDeployment(topology, () -> matchedNodePropertiesConfigService.updateProperty(application, environment, topology, nodeId, Optional.of(capabilityName), updateRequest.getPropertyName(), updateRequest.getPropertyValue()));
        return RestResponseBuilder.<DeploymentTopologyDTO>builder().data(dto).build();
    } catch (ConstraintTechnicalException e) {
        if (e.getCause() instanceof ConstraintFunctionalException) {
            return RestConstraintValidator.fromException((ConstraintFunctionalException) e.getCause(), updateRequest.getPropertyName(), updateRequest.getPropertyValue());
        }
        throw e;
    }
}
Also used : ConstraintFunctionalException(org.alien4cloud.tosca.exceptions.ConstraintFunctionalException) DeploymentTopology(alien4cloud.model.deployment.DeploymentTopology) Topology(org.alien4cloud.tosca.model.templates.Topology) Application(alien4cloud.model.application.Application) ApplicationEnvironment(alien4cloud.model.application.ApplicationEnvironment) DeploymentTopologyDTO(alien4cloud.deployment.DeploymentTopologyDTO) ConstraintTechnicalException(org.alien4cloud.tosca.exceptions.ConstraintTechnicalException) ApplicationTopologyVersion(alien4cloud.model.application.ApplicationTopologyVersion) Audit(alien4cloud.audit.annotation.Audit) ApiOperation(io.swagger.annotations.ApiOperation) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 4 with ConstraintFunctionalException

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

the class TemplateBuilder method fillProperties.

public static void fillProperties(Map<String, AbstractPropertyValue> properties, Map<String, PropertyDefinition> propertiesDefinitions, Map<String, AbstractPropertyValue> originalProperties, boolean adaptToType) {
    if (propertiesDefinitions == null || properties == null) {
        return;
    }
    for (Map.Entry<String, PropertyDefinition> entry : propertiesDefinitions.entrySet()) {
        AbstractPropertyValue originalValue = MapUtils.getObject(originalProperties, entry.getKey());
        if (originalValue == null) {
            AbstractPropertyValue pv = PropertyUtil.getDefaultPropertyValueFromPropertyDefinition(entry.getValue());
            properties.put(entry.getKey(), pv);
        } else if (originalValue instanceof FunctionPropertyValue || originalValue instanceof ConcatPropertyValue) {
            properties.put(entry.getKey(), originalValue);
        } else {
            // we check the property type before accepting it
            try {
                ConstraintPropertyService.checkPropertyConstraint(entry.getKey(), originalValue, entry.getValue());
                properties.put(entry.getKey(), originalValue);
            } catch (ConstraintFunctionalException e) {
                log.debug("Not able to merge property <" + entry.getKey() + "> value due to a type check exception", e);
                if (adaptToType) {
                    AbstractPropertyValue pv = PropertyUtil.getDefaultPropertyValueFromPropertyDefinition(entry.getValue());
                    properties.put(entry.getKey(), pv);
                }
            }
        }
    }
    if (!adaptToType) {
        // maybe we could put validations here actually.
        for (Map.Entry<String, AbstractPropertyValue> originalProperty : safe(originalProperties).entrySet()) {
            if (!properties.containsKey(originalProperty.getKey())) {
                properties.put(originalProperty.getKey(), originalProperty.getValue());
            }
        }
    }
}
Also used : ConstraintFunctionalException(org.alien4cloud.tosca.exceptions.ConstraintFunctionalException) FunctionPropertyValue(org.alien4cloud.tosca.model.definitions.FunctionPropertyValue) Map(java.util.Map) PropertyDefinition(org.alien4cloud.tosca.model.definitions.PropertyDefinition) AbstractPropertyValue(org.alien4cloud.tosca.model.definitions.AbstractPropertyValue) ConcatPropertyValue(org.alien4cloud.tosca.model.definitions.ConcatPropertyValue)

Example 5 with ConstraintFunctionalException

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

the class UpdateCapabilityPropertyValueProcessor method process.

@Override
@SneakyThrows
public void process(Csar csar, Topology topology, UpdateCapabilityPropertyValueOperation operation) {
    String propertyName = operation.getPropertyName();
    Object propertyValue = operation.getPropertyValue();
    Map<String, NodeTemplate> nodeTemplates = TopologyUtils.getNodeTemplates(topology);
    NodeTemplate nodeTemplate = TopologyUtils.getNodeTemplate(topology.getId(), operation.getNodeName(), nodeTemplates);
    Capability capability = nodeTemplate.getCapabilities().get(operation.getCapabilityName());
    CapabilityType capabilityType = ToscaContext.get(CapabilityType.class, capability.getType());
    if (!capabilityType.getProperties().containsKey(propertyName)) {
        throw new NotFoundException("Property <" + propertyName + "> doesn't exists for node <" + operation.getNodeName() + "> of type <" + capabilityType + ">");
    }
    log.debug("Updating property [ {} ] of the capability [ {} ] for the Node template [ {} ] from the topology [ {} ]: changing value from [{}] to [{}].", propertyName, capability.getType(), operation.getNodeName(), topology.getId(), capabilityType.getProperties().get(propertyName), propertyValue);
    try {
        propertyService.setCapabilityPropertyValue(capability, capabilityType.getProperties().get(propertyName), propertyName, propertyValue);
    } catch (ConstraintFunctionalException e) {
        throw new PropertyValueException("Error when setting node " + operation.getNodeName() + " property.", e, propertyName, propertyValue);
    }
}
Also used : ConstraintFunctionalException(org.alien4cloud.tosca.exceptions.ConstraintFunctionalException) CapabilityType(org.alien4cloud.tosca.model.types.CapabilityType) NodeTemplate(org.alien4cloud.tosca.model.templates.NodeTemplate) Capability(org.alien4cloud.tosca.model.templates.Capability) NotFoundException(alien4cloud.exception.NotFoundException) PropertyValueException(org.alien4cloud.tosca.editor.exception.PropertyValueException) SneakyThrows(lombok.SneakyThrows)

Aggregations

ConstraintFunctionalException (org.alien4cloud.tosca.exceptions.ConstraintFunctionalException)10 Audit (alien4cloud.audit.annotation.Audit)5 ApiOperation (io.swagger.annotations.ApiOperation)5 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)5 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)5 DeploymentTopologyDTO (alien4cloud.deployment.DeploymentTopologyDTO)4 Application (alien4cloud.model.application.Application)4 ApplicationEnvironment (alien4cloud.model.application.ApplicationEnvironment)4 DeploymentTopology (alien4cloud.model.deployment.DeploymentTopology)4 PropertyValueException (org.alien4cloud.tosca.editor.exception.PropertyValueException)4 ConstraintTechnicalException (org.alien4cloud.tosca.exceptions.ConstraintTechnicalException)4 PropertyDefinition (org.alien4cloud.tosca.model.definitions.PropertyDefinition)4 Topology (org.alien4cloud.tosca.model.templates.Topology)4 ApplicationTopologyVersion (alien4cloud.model.application.ApplicationTopologyVersion)3 NodeTemplate (org.alien4cloud.tosca.model.templates.NodeTemplate)3 NotFoundException (alien4cloud.exception.NotFoundException)2 Map (java.util.Map)2 OperationExecutionException (alien4cloud.paas.exception.OperationExecutionException)1 OrchestratorDisabledException (alien4cloud.paas.exception.OrchestratorDisabledException)1 RestError (alien4cloud.rest.model.RestError)1