Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ALIEN-2975] [ALIEN-2979] add ability to define meta properties for c…
…omponents and services, add a filter to only search for concerned objects when removing a metaproperty
- Loading branch information
Showing
15 changed files
with
279 additions
and
46 deletions.
There are no files selected for viewing
22 changes: 22 additions & 0 deletions
22
alien4cloud-common/src/main/java/alien4cloud/model/common/IMetaProperties.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package alien4cloud.model.common; | ||
|
||
import java.util.Map; | ||
|
||
/** | ||
* Interface to be implemented by resources that supports meta-properties management. | ||
*/ | ||
public interface IMetaProperties { | ||
/** | ||
* Set the map of meta-properties on the resource. | ||
* | ||
* @param metaProperties the map of meta-properties. | ||
*/ | ||
void setMetaProperties(Map<String, String> metaProperties); | ||
|
||
/** | ||
* Get the map of meta-properties on the resource. | ||
* | ||
* @return the map of meta-properties. | ||
*/ | ||
Map<String, String> getMetaProperties(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
...4cloud-rest-api/src/main/java/alien4cloud/rest/common/AbstractMetaPropertyController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package alien4cloud.rest.common; | ||
|
||
import alien4cloud.common.MetaPropertiesService; | ||
import alien4cloud.dao.IGenericSearchDAO; | ||
import alien4cloud.model.common.IMetaProperties; | ||
import alien4cloud.rest.internal.model.PropertyRequest; | ||
import alien4cloud.rest.model.RestErrorBuilder; | ||
import alien4cloud.rest.model.RestErrorCode; | ||
import alien4cloud.rest.model.RestResponse; | ||
import alien4cloud.rest.model.RestResponseBuilder; | ||
import alien4cloud.security.AuthorizationUtil; | ||
import alien4cloud.security.model.ApplicationRole; | ||
import alien4cloud.tosca.properties.constraints.ConstraintUtil; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.alien4cloud.tosca.exceptions.ConstraintValueDoNotMatchPropertyTypeException; | ||
import org.alien4cloud.tosca.exceptions.ConstraintViolationException; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
|
||
import javax.annotation.Resource; | ||
import javax.inject.Inject; | ||
|
||
@Slf4j | ||
public abstract class AbstractMetaPropertyController<T extends IMetaProperties> { | ||
@Resource(name = "alien-es-dao") | ||
protected IGenericSearchDAO alienDAO; | ||
@Inject | ||
private MetaPropertiesService metaPropertiesService; | ||
|
||
/** | ||
* Update or create a property for an object that handle meta-properties. | ||
* | ||
* @param id id of the target | ||
* @param propertyRequest property request | ||
* @return information on the constraint | ||
* @throws ConstraintValueDoNotMatchPropertyTypeException | ||
* @throws ConstraintViolationException | ||
*/ | ||
protected RestResponse<ConstraintUtil.ConstraintInformation> upsertProperty(String id, | ||
@RequestBody PropertyRequest propertyRequest) | ||
throws ConstraintViolationException, ConstraintValueDoNotMatchPropertyTypeException { | ||
|
||
IMetaProperties target = getTarget(id); | ||
try { | ||
metaPropertiesService.upsertMetaProperty(target, 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(); | ||
} | ||
|
||
protected abstract T getTarget(String id); | ||
|
||
} |
59 changes: 59 additions & 0 deletions
59
...ud-rest-api/src/main/java/alien4cloud/rest/component/ComponentMetaPropertyController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package alien4cloud.rest.component; | ||
|
||
import alien4cloud.audit.annotation.Audit; | ||
import alien4cloud.common.MetaPropertiesService; | ||
import alien4cloud.dao.IGenericSearchDAO; | ||
import alien4cloud.model.application.Application; | ||
import alien4cloud.model.common.IMetaProperties; | ||
import alien4cloud.rest.common.AbstractMetaPropertyController; | ||
import alien4cloud.rest.internal.model.PropertyRequest; | ||
import alien4cloud.rest.model.RestErrorBuilder; | ||
import alien4cloud.rest.model.RestErrorCode; | ||
import alien4cloud.rest.model.RestResponse; | ||
import alien4cloud.rest.model.RestResponseBuilder; | ||
import alien4cloud.security.AuthorizationUtil; | ||
import alien4cloud.security.model.ApplicationRole; | ||
import alien4cloud.security.model.Role; | ||
import alien4cloud.tosca.properties.constraints.ConstraintUtil; | ||
import io.swagger.annotations.Api; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.alien4cloud.tosca.exceptions.ConstraintValueDoNotMatchPropertyTypeException; | ||
import org.alien4cloud.tosca.exceptions.ConstraintViolationException; | ||
import org.alien4cloud.tosca.model.types.NodeType; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.security.access.prepost.PreAuthorize; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
import javax.annotation.Resource; | ||
import javax.inject.Inject; | ||
|
||
@Slf4j | ||
@RestController | ||
@RequestMapping({"/rest/components/{id:.+}/properties", "/rest/v1/components/{id:.+}/properties", "/rest/latest/components/{id:.+}/properties"}) | ||
@Api(value = "", description = "Operations on Component's meta-properties") | ||
public class ComponentMetaPropertyController extends AbstractMetaPropertyController { | ||
|
||
/** | ||
* Update or create a property for an component. | ||
* | ||
* @param id id of the component | ||
* @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 id, | ||
@RequestBody PropertyRequest propertyRequest) | ||
throws ConstraintViolationException, ConstraintValueDoNotMatchPropertyTypeException { | ||
AuthorizationUtil.hasOneRoleIn(Role.ADMIN, Role.COMPONENTS_MANAGER); | ||
return super.upsertProperty(id, propertyRequest); | ||
} | ||
|
||
@Override | ||
protected IMetaProperties getTarget(String id) { | ||
return alienDAO.findById(NodeType.class, id); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.