Skip to content

Commit

Permalink
Add support for on not found policy in TargetManagement.(un)assignTags (
Browse files Browse the repository at this point in the history
#1904)

Signed-off-by: Marinov Avgustin <[email protected]>
  • Loading branch information
avgustinmm authored Oct 17, 2024
1 parent 1bc467d commit a976d47
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@
*/
public interface TargetManagement {

enum OnNotFoundPolicy {
FAIL, // default
TAG_AND_SUCCESS,
TAG_AND_ERROR
}

/**
* Counts number of targets with the given distribution set assigned.
*
Expand Down Expand Up @@ -663,6 +669,18 @@ Slice<Target> findByFilterOrderByLinkedDistributionSet(@NotNull Pageable pageabl
@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
* @param onNotFoundPolicy what to do if there are targets that are not found
* @return list of assigned targets
* @throws EntityNotFoundException if given targetTagId 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, final OnNotFoundPolicy onNotFoundPolicy);

/**
* Assign a {@link TargetTag} assignment to given {@link Target}s.
*
Expand All @@ -672,7 +690,21 @@ Slice<Target> findByFilterOrderByLinkedDistributionSet(@NotNull Pageable pageabl
* @throws EntityNotFoundException if given targetTagId 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);
default List<Target> assignTag(@NotEmpty Collection<String> controllerIds, long targetTagId) {
return assignTag(controllerIds, targetTagId, OnNotFoundPolicy.FAIL);
}

/**
* Un-assign a {@link TargetTag} assignment to given {@link Target}s.
*
* @param controllerIds to un-assign for
* @param targetTagId to un-assign
* @param onNotFoundPolicy what to do if there are targets that are not found
* @return list of unassigned targets
* @throws EntityNotFoundException if given targetTagId or at least one of the targets do not exist
*/
@PreAuthorize(SpringEvalExpressions.HAS_AUTH_UPDATE_TARGET)
List<Target> unassignTag(@NotEmpty Collection<String> controllerIds, long targetTagId, final OnNotFoundPolicy onNotFoundPolicy);

/**
* Un-assign a {@link TargetTag} assignment to given {@link Target}s.
Expand All @@ -683,7 +715,9 @@ Slice<Target> findByFilterOrderByLinkedDistributionSet(@NotNull Pageable pageabl
* @throws EntityNotFoundException if given targetTagId or at least one of the targets do not exist
*/
@PreAuthorize(SpringEvalExpressions.HAS_AUTH_UPDATE_TARGET)
List<Target> unassignTag(@NotEmpty Collection<String> controllerIds, long targetTagId);
default List<Target> unassignTag(@NotEmpty Collection<String> controllerIds, long targetTagId) {
return unassignTag(controllerIds, targetTagId, OnNotFoundPolicy.FAIL);
}

/**
* Un-assign a {@link TargetType} assignment to given {@link Target}.
Expand Down Expand Up @@ -978,7 +1012,7 @@ Page<TargetMetadata> findMetaDataByControllerIdAndRsql(@NotNull Pageable pageabl
/**
* Un-assign a {@link TargetTag} assignment to given {@link Target}.
*
* @deprecated since 0.6.0 - use {@link #unassignTag(List, long)} instead
* @deprecated since 0.6.0 - use {@link #unassigнnTag(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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
*/
package org.eclipse.hawkbit.repository.jpa.management;

import java.net.URI;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
Expand All @@ -20,7 +19,6 @@
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -535,8 +533,8 @@ private List<JpaTarget> findTargetsByInSpecification(final Collection<String> co
@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 targetTagId) {
return updateTag(controllerIds, targetTagId, (tag, target) -> {
public List<Target> assignTag(final Collection<String> controllerIds, final long targetTagId, final OnNotFoundPolicy onNotFoundPolicy) {
return updateTag(controllerIds, targetTagId, onNotFoundPolicy, (tag, target) -> {
if (target.getTags().contains(tag)) {
return target;
} else {
Expand All @@ -549,8 +547,8 @@ public List<Target> assignTag(final Collection<String> controllerIds, final long
@Transactional
@Retryable(include = {
ConcurrencyFailureException.class }, maxAttempts = Constants.TX_RT_MAX, backoff = @Backoff(delay = Constants.TX_RT_DELAY))
public List<Target> unassignTag(final Collection<String> controllerIds, final long targetTagId) {
return updateTag(controllerIds, targetTagId, (tag, target) -> {
public List<Target> unassignTag(final Collection<String> controllerIds, final long targetTagId, final OnNotFoundPolicy onNotFoundPolicy) {
return updateTag(controllerIds, targetTagId, onNotFoundPolicy, (tag, target) -> {
if (target.getTags().contains(tag)) {
target.removeTag(tag);
return targetRepository.save(target);
Expand All @@ -560,7 +558,7 @@ public List<Target> unassignTag(final Collection<String> controllerIds, final lo
});
}
private List<Target> updateTag(
final Collection<String> controllerIds, final long targetTagId,
final Collection<String> controllerIds, final long targetTagId, final OnNotFoundPolicy notFoundPolicy,
final BiFunction<JpaTargetTag, JpaTarget, Target> updater) {
final JpaTargetTag tag = targetTagRepository.findById(targetTagId)
.orElseThrow(() -> new EntityNotFoundException(TargetTag.class, targetTagId));
Expand All @@ -570,14 +568,25 @@ private List<Target> updateTag(
.orElseGet(Collections::emptyList) :
targetRepository
.findAll(TargetSpecifications.byControllerIdWithTagsInJoin(controllerIds));
if (targets.size() < controllerIds.size()) {
throw new EntityNotFoundException(Target.class, notFound(controllerIds, targets));
final EntityNotFoundException notFoundException;
if (targets.size() < controllerIds.size() && notFoundPolicy != OnNotFoundPolicy.TAG_AND_SUCCESS) {
notFoundException = new EntityNotFoundException(Target.class, notFound(controllerIds, targets));
if (notFoundPolicy == OnNotFoundPolicy.FAIL) {
throw notFoundException;
}
} else {
notFoundException = null;
}
targetRepository.getAccessController()
.ifPresent(acm -> acm.assertOperationAllowed(AccessController.Operation.UPDATE, targets));

try {
return targets.stream().map(target -> updater.apply(tag, target)).toList();
final List<Target> result = targets.stream().map(target -> updater.apply(tag, target)).toList();
if (notFoundException != null) { // if notFoundPolicy is NotFoundPolicy.TAG_AND_FAIL
throw notFoundException;
} else {
return result; // if all found or notFoundPolicy is NotFoundPolicy.TAG_AND_SUCCESS
}
} finally {
// No reason to save the tag
entityManager.detach(tag);
Expand Down

0 comments on commit a976d47

Please sign in to comment.