Fix already assigned targets (#919)
* Dont count not existing targets as already assigned to DS Signed-off-by: Sebastian Firsching <sebastian.firsching@bosch-si.com> * Add test for deploymentManagement Signed-off-by: Sebastian Firsching <sebastian.firsching@bosch-si.com> * Delete 404 error message from docs when target is not found Signed-off-by: Sebastian Firsching <sebastian.firsching@bosch-si.com> * Add text to implementation notes Signed-off-by: Sebastian Firsching <sebastian.firsching@bosch-si.com> * Add assertions to test Signed-off-by: Sebastian Firsching <sebastian.firsching@bosch-si.com> * Add expected behaviour to test description Signed-off-by: Sebastian Firsching <sebastian.firsching@bosch-si.com> * Refactor deploymentMgmtTest Signed-off-by: Sebastian Firsching <sebastian.firsching@bosch-si.com> * Filter out non-existing controllerIds Signed-off-by: Sebastian Firsching <sebastian.firsching@bosch-si.com> * Extend test descriptions Signed-off-by: Sebastian Firsching <sebastian.firsching@bosch-si.com> * Refactor createTargets method Signed-off-by: Sebastian Firsching <sebastian.firsching@bosch-si.com> * Add createTargetAndJsonArray method Signed-off-by: Sebastian Firsching <sebastian.firsching@bosch-si.com> * Correct expected test result Signed-off-by: Sebastian Firsching <sebastian.firsching@bosch-si.com> * Adapt rest docs Signed-off-by: Sebastian Firsching <sebastian.firsching@bosch-si.com> * Correct test Signed-off-by: Sebastian Firsching <sebastian.firsching@bosch-si.com> * Only count targets that exist for total and adapt test Signed-off-by: Sebastian Firsching <sebastian.firsching@bosch-si.com> * Use only existign targetWithActionTypes for assignment Signed-off-by: Sebastian Firsching <sebastian.firsching@bosch-si.com> * Rename targetIds to providedTargetIds Signed-off-by: Sebastian Firsching <sebastian.firsching@bosch-si.com>
This commit is contained in:
committed by
Dominic Schabel
parent
5feb5873c4
commit
8d3ba68be9
@@ -279,19 +279,26 @@ public class JpaDeploymentManagement extends JpaActionManagement implements Depl
|
||||
final AbstractDsAssignmentStrategy assignmentStrategy) {
|
||||
|
||||
final JpaDistributionSet distributionSetEntity = getAndValidateDsById(dsID);
|
||||
final List<String> targetIds = targetsWithActionType.stream().map(TargetWithActionType::getControllerId)
|
||||
final List<String> providedTargetIds = targetsWithActionType.stream().map(TargetWithActionType::getControllerId)
|
||||
.distinct().collect(Collectors.toList());
|
||||
|
||||
final List<JpaTarget> targetEntities = assignmentStrategy.findTargetsForAssignment(targetIds,
|
||||
final List<String> existingTargetIds = Lists.partition(providedTargetIds, Constants.MAX_ENTRIES_IN_STATEMENT)
|
||||
.stream().map(targetRepository::filterNonExistingControllerIds).flatMap(List::stream)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
final List<JpaTarget> targetEntities = assignmentStrategy.findTargetsForAssignment(existingTargetIds,
|
||||
distributionSetEntity.getId());
|
||||
|
||||
if (targetEntities.isEmpty()) {
|
||||
return allTargetsAlreadyAssignedResult(distributionSetEntity, targetsWithActionType.size());
|
||||
return allTargetsAlreadyAssignedResult(distributionSetEntity, existingTargetIds.size());
|
||||
}
|
||||
|
||||
final List<JpaAction> assignedActions = doAssignDistributionSetToTargets(targetsWithActionType, actionMessage,
|
||||
assignmentStrategy, distributionSetEntity, targetEntities);
|
||||
return buildAssignmentResult(distributionSetEntity, assignedActions, targetsWithActionType.size());
|
||||
final List<TargetWithActionType> existingTargetsWithActionType = targetsWithActionType.stream()
|
||||
.filter(target -> existingTargetIds.contains(target.getControllerId())).collect(Collectors.toList());
|
||||
|
||||
final List<JpaAction> assignedActions = doAssignDistributionSetToTargets(existingTargetsWithActionType,
|
||||
actionMessage, assignmentStrategy, distributionSetEntity, targetEntities);
|
||||
return buildAssignmentResult(distributionSetEntity, assignedActions, existingTargetsWithActionType.size());
|
||||
}
|
||||
|
||||
private DistributionSetAssignmentResult allTargetsAlreadyAssignedResult(
|
||||
|
||||
@@ -271,4 +271,14 @@ public interface TargetRepository extends BaseEntityRepository<JpaTarget, Long>,
|
||||
@Transactional
|
||||
@Query("DELETE FROM JpaTarget t WHERE t.tenant = :tenant")
|
||||
void deleteByTenant(@Param("tenant") String tenant);
|
||||
|
||||
/**
|
||||
* Filters a given list of controllerIds and returns only the existing IDs.
|
||||
*
|
||||
* @param controllerIds
|
||||
* The IDs to be filtered
|
||||
* @return only the existing controllerIds
|
||||
*/
|
||||
@Query("SELECT t.controllerId FROM JpaTarget t WHERE t.controllerId IN ?1")
|
||||
List<String> filterNonExistingControllerIds(Iterable<String> controllerIds);
|
||||
}
|
||||
|
||||
@@ -37,11 +37,11 @@ import org.eclipse.hawkbit.repository.event.remote.entity.DistributionSetUpdated
|
||||
import org.eclipse.hawkbit.repository.event.remote.entity.SoftwareModuleCreatedEvent;
|
||||
import org.eclipse.hawkbit.repository.event.remote.entity.TargetCreatedEvent;
|
||||
import org.eclipse.hawkbit.repository.event.remote.entity.TargetUpdatedEvent;
|
||||
import org.eclipse.hawkbit.repository.exception.AssignmentQuotaExceededException;
|
||||
import org.eclipse.hawkbit.repository.exception.EntityNotFoundException;
|
||||
import org.eclipse.hawkbit.repository.exception.ForceQuitActionNotAllowedException;
|
||||
import org.eclipse.hawkbit.repository.exception.IncompleteDistributionSetException;
|
||||
import org.eclipse.hawkbit.repository.exception.MultiAssignmentIsNotEnabledException;
|
||||
import org.eclipse.hawkbit.repository.exception.AssignmentQuotaExceededException;
|
||||
import org.eclipse.hawkbit.repository.jpa.configuration.Constants;
|
||||
import org.eclipse.hawkbit.repository.jpa.model.JpaAction;
|
||||
import org.eclipse.hawkbit.repository.jpa.model.JpaActionStatus;
|
||||
@@ -187,7 +187,8 @@ public class DeploymentManagementTest extends AbstractJpaIntegrationTest {
|
||||
assignDistributionSet(ds1, targets);
|
||||
|
||||
targets.add(testdataFactory.createTarget("assignmentTest2"));
|
||||
assertThatExceptionOfType(AssignmentQuotaExceededException.class).isThrownBy(() -> assignDistributionSet(ds2, targets));
|
||||
assertThatExceptionOfType(AssignmentQuotaExceededException.class)
|
||||
.isThrownBy(() -> assignDistributionSet(ds2, targets));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -647,6 +648,40 @@ public class DeploymentManagementTest extends AbstractJpaIntegrationTest {
|
||||
deploymentManagement.assignDistributionSets(Arrays.asList(targetToDS0, targetToDS1)))).isEqualTo(2);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Description("Assigning distribution set to the list of targets with a non-existing one leads to successful assignment of valid targets, while not found targets are silently ignored.")
|
||||
public void assignDistributionSetToNotExistingTarget() {
|
||||
final String notExistingId = "notExistingTarget";
|
||||
|
||||
final DistributionSet createdDs = testdataFactory.createDistributionSet();
|
||||
|
||||
final String[] knownTargetIdsArray = { "1", "2" };
|
||||
final List<String> knownTargetIds = Lists.newArrayList(knownTargetIdsArray);
|
||||
testdataFactory.createTargets(knownTargetIdsArray);
|
||||
|
||||
// add not existing target to targets
|
||||
knownTargetIds.add(notExistingId);
|
||||
|
||||
final List<DistributionSetAssignmentResult> assignDistributionSetsResults = assignDistributionSetToTargets(
|
||||
createdDs, knownTargetIds);
|
||||
|
||||
for (final DistributionSetAssignmentResult assignDistributionSetsResult : assignDistributionSetsResults) {
|
||||
assertThat(assignDistributionSetsResult.getAlreadyAssigned()).isEqualTo(0);
|
||||
assertThat(assignDistributionSetsResult.getAssigned()).isEqualTo(2);
|
||||
assertThat(assignDistributionSetsResult.getTotal()).isEqualTo(2);
|
||||
}
|
||||
}
|
||||
|
||||
private List<DistributionSetAssignmentResult> assignDistributionSetToTargets(final DistributionSet distributionSet,
|
||||
final Iterable<String> targetIds) {
|
||||
final List<DeploymentRequest> deploymentRequests = new ArrayList<>();
|
||||
for (final String controllerId : targetIds) {
|
||||
deploymentRequests.add(new DeploymentRequest(controllerId, distributionSet.getId(), ActionType.FORCED, 0,
|
||||
null, null, null, null));
|
||||
}
|
||||
return deploymentManagement.assignDistributionSets(deploymentRequests);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Description("Duplicate Assignments are removed from a request when multiassignment is disabled, otherwise not")
|
||||
@ExpectEvents({ @Expect(type = TargetCreatedEvent.class, count = 1),
|
||||
|
||||
Reference in New Issue
Block a user