Improve Target Tag REST & management API (#1880)

* added methods to unassign by multiple controller ids
* deprecated toggle assigments - too complex to undestand
* deprecated unassign (management) of single controller id - in favour of methods with controller ids

Signed-off-by: Marinov Avgustin <Avgustin.Marinov@bosch.com>
This commit is contained in:
Avgustin Marinov
2024-10-16 14:51:26 +03:00
committed by GitHub
parent cf439c78b3
commit a5b24cac68
5 changed files with 119 additions and 52 deletions

View File

@@ -52,21 +52,6 @@ import org.springframework.security.access.prepost.PreAuthorize;
*/
public interface TargetManagement {
/**
* Assign a {@link TargetTag} assignment to given {@link Target}s.
*
* @param controllerIds
* to assign for
* @param tagId
* to assign
* @return list of assigned targets
*
* @throws EntityNotFoundException
* if given tagId or at least one of the targets do not exist
*/
@PreAuthorize(SpringEvalExpressions.HAS_AUTH_READ_REPOSITORY_AND_UPDATE_TARGET)
List<Target> assignTag(@NotEmpty Collection<String> controllerIds, long tagId);
/**
* Counts number of targets with the given distribution set assigned.
*
@@ -653,14 +638,13 @@ public interface TargetManagement {
* assigned, they will be. Only if all of them have the tag already assigned
* they will be removed instead.
*
* @param controllerIds
* to toggle for
* @param tagName
* to toggle
* @deprecated since 0.6.0 - not very usable with very unclear logic
* @param controllerIds to toggle for
* @param tagName to toggle
* @return TagAssigmentResult with all metadata of the assignment outcome.
* @throws EntityNotFoundException
* if tag with given name does not exist
* @throws EntityNotFoundException if tag with given name does not exist
*/
@Deprecated
@PreAuthorize(SpringEvalExpressions.HAS_AUTH_UPDATE_TARGET)
TargetTagAssignmentResult toggleTagAssignment(@NotEmpty Collection<String> controllerIds, @NotEmpty String tagName);
@@ -695,26 +679,49 @@ public interface TargetManagement {
@PreAuthorize(SpringEvalExpressions.HAS_AUTH_UPDATE_TARGET)
TargetTypeAssignmentResult unassignType(@NotEmpty Collection<String> controllerIds);
/**
* Assign a {@link TargetTag} assignment to given {@link Target}s.
*
* @param controllerIds
* to assign for
* @param targetTagId
* to assign
* @return list of assigned targets
*
* @throws EntityNotFoundException
* if given tagId or at least one of the targets do not exist
*/
@PreAuthorize(SpringEvalExpressions.HAS_AUTH_READ_REPOSITORY_AND_UPDATE_TARGET)
List<Target> assignTag(@NotEmpty Collection<String> controllerIds, long targetTagId);
/**
* Un-assign a {@link TargetTag} assignment to given {@link Target}s.
*
* @param controllerIds to un-assign for
* @param targetTagId to un-assign
* @return the unassigned target or <null> if no target is unassigned
* @throws EntityNotFoundException if Tag with given ID does not exist
*/
@PreAuthorize(SpringEvalExpressions.HAS_AUTH_UPDATE_TARGET)
List<Target> unassignTag(@NotEmpty List<String> controllerIds, long targetTagId);
/**
* Un-assign a {@link TargetTag} assignment to given {@link Target}.
*
* @param controllerId
* to un-assign for
* @param targetTagId
* to un-assign
* @deprecated since 0.6.0 - use {@link #unassignTag(List, long)} instead
* @param controllerId to un-assign for
* @param targetTagId to un-assign
* @return the unassigned target or <null> if no target is unassigned
*
* @throws EntityNotFoundException
* if TAG with given ID does not exist
* @throws EntityNotFoundException if TAG with given ID does not exist
*/
@Deprecated(forRemoval = true)
@PreAuthorize(SpringEvalExpressions.HAS_AUTH_UPDATE_TARGET)
Target unassignTag(@NotEmpty String controllerId, long targetTagId);
/**
* Un-assign a {@link TargetType} assignment to given {@link Target}.
*
* @param controllerId
* to un-assign for
* @param controllerId to un-assign for
* @return the unassigned target
*
*/

View File

@@ -483,7 +483,8 @@ public class JpaTargetManagement implements TargetManagement {
@Retryable(include = {
ConcurrencyFailureException.class }, maxAttempts = Constants.TX_RT_MAX, backoff = @Backoff(delay = Constants.TX_RT_DELAY))
public TargetTagAssignmentResult toggleTagAssignment(final Collection<String> controllerIds, final String tagName) {
final TargetTag tag = targetTagRepository.findByNameEquals(tagName)
final TargetTag tag = targetTagRepository
.findByNameEquals(tagName)
.orElseThrow(() -> new EntityNotFoundException(TargetTag.class, tagName));
final List<JpaTarget> allTargets = targetRepository
.findAll(TargetSpecifications.byControllerIdWithTagsInJoin(controllerIds));
@@ -568,9 +569,9 @@ public class JpaTargetManagement implements TargetManagement {
@Transactional
@Retryable(include = {
ConcurrencyFailureException.class }, maxAttempts = Constants.TX_RT_MAX, backoff = @Backoff(delay = Constants.TX_RT_DELAY))
public List<Target> assignTag(final Collection<String> controllerIds, final long tagId) {
final JpaTargetTag tag = targetTagRepository.findById(tagId)
.orElseThrow(() -> new EntityNotFoundException(TargetTag.class, tagId));
public List<Target> assignTag(final Collection<String> controllerIds, final long targetTagId) {
final JpaTargetTag tag = targetTagRepository.findById(targetTagId)
.orElseThrow(() -> new EntityNotFoundException(TargetTag.class, targetTagId));
final List<JpaTarget> allTargets = targetRepository
.findAll(TargetSpecifications.byControllerIdWithTagsInJoin(controllerIds));
@@ -591,6 +592,33 @@ public class JpaTargetManagement implements TargetManagement {
return result;
}
@Override
@Transactional
@Retryable(include = {
ConcurrencyFailureException.class }, maxAttempts = Constants.TX_RT_MAX, backoff = @Backoff(delay = Constants.TX_RT_DELAY))
public List<Target> unassignTag(final List<String> controllerIds, final long targetTagId) {
final JpaTargetTag tag = targetTagRepository.findById(targetTagId)
.orElseThrow(() -> new EntityNotFoundException(TargetTag.class, targetTagId));
final List<JpaTarget> allTargets = targetRepository
.findAll(TargetSpecifications.byControllerIdWithTagsInJoin(controllerIds));
if (allTargets.size() < controllerIds.size()) {
throw new EntityNotFoundException(Target.class, controllerIds,
allTargets.stream().map(Target::getControllerId).toList());
}
targetRepository.getAccessController()
.ifPresent(acm -> acm.assertOperationAllowed(AccessController.Operation.UPDATE, allTargets));
allTargets.forEach(target -> target.removeTag(tag));
final List<Target> result = allTargets.stream().map(targetRepository::save).map(Target.class::cast).toList();
// No reason to save the tag
entityManager.detach(tag);
return result;
}
@Override
@Transactional
@Retryable(include = {