diff --git a/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/management/JpaTargetManagement.java b/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/management/JpaTargetManagement.java index 3e046ce2f8..7483e7bc7a 100644 --- a/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/management/JpaTargetManagement.java +++ b/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/management/JpaTargetManagement.java @@ -569,6 +569,9 @@ private List findTargetsByInSpecification(final Collection co @Retryable(include = { ConcurrencyFailureException.class }, maxAttempts = Constants.TX_RT_MAX, backoff = @Backoff(delay = Constants.TX_RT_DELAY)) public List assignTag(final Collection controllerIds, final long tagId) { + final JpaTargetTag tag = targetTagRepository.findById(tagId) + .orElseThrow(() -> new EntityNotFoundException(TargetTag.class, tagId)); + final List allTargets = targetRepository .findAll(TargetSpecifications.byControllerIdWithTagsInJoin(controllerIds)); if (allTargets.size() < controllerIds.size()) { @@ -579,9 +582,6 @@ public List assignTag(final Collection controllerIds, final long targetRepository.getAccessController() .ifPresent(acm -> acm.assertOperationAllowed(AccessController.Operation.UPDATE, allTargets)); - final JpaTargetTag tag = targetTagRepository.findById(tagId) - .orElseThrow(() -> new EntityNotFoundException(TargetTag.class, tagId)); - allTargets.forEach(target -> target.addTag(tag)); final List result = allTargets.stream().map(targetRepository::save).map(Target.class::cast).toList(); diff --git a/hawkbit-rest/hawkbit-mgmt-api/src/main/java/org/eclipse/hawkbit/mgmt/rest/api/MgmtTargetTagRestApi.java b/hawkbit-rest/hawkbit-mgmt-api/src/main/java/org/eclipse/hawkbit/mgmt/rest/api/MgmtTargetTagRestApi.java index e146bde472..8b8b83b46d 100644 --- a/hawkbit-rest/hawkbit-mgmt-api/src/main/java/org/eclipse/hawkbit/mgmt/rest/api/MgmtTargetTagRestApi.java +++ b/hawkbit-rest/hawkbit-mgmt-api/src/main/java/org/eclipse/hawkbit/mgmt/rest/api/MgmtTargetTagRestApi.java @@ -32,6 +32,7 @@ import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PutMapping; +import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestParam; /** @@ -321,19 +322,51 @@ ResponseEntity toggleTagAssignment(@PathVariable(" List assignedTargetRequestBodies); /** - * Handles the POST request to assign targets to the given tag id. + * Handles the PUT request to assign targets to the given tag id. * - * @param targetTagId - * the ID of the target tag to retrieve - * @param assignedTargetRequestBodies - * list of controller ids to be assigned + * @param targetTagId the ID of the target tag to retrieve + * @param assignedTargetControlIdsStream stream of controller ids to be assigned * * @return the list of assigned targets. */ @Operation(summary = "Assign target(s) to given tagId", description = "Handles the POST request of target assignment. Already assigned target will be ignored.") @ApiResponses(value = { - @ApiResponse(responseCode = "200", description = "Successfully retrieved"), + @ApiResponse(responseCode = "200", description = "Successfully assigned"), + @ApiResponse(responseCode = "400", description = "Bad Request - e.g. invalid parameters", + content = @Content(mediaType = "application/json", schema = @Schema(implementation = ExceptionInfo.class))), + @ApiResponse(responseCode = "401", description = "The request requires user authentication."), + @ApiResponse(responseCode = "403", description = "Insufficient permissions, entity is not allowed to be " + + "changed (i.e. read-only) or data volume restriction applies."), + @ApiResponse(responseCode = "405", description = "The http request method is not allowed on the resource."), + @ApiResponse(responseCode = "406", description = "In case accept header is specified and not application/json."), + @ApiResponse(responseCode = "409", description = "E.g. in case an entity is created or modified by another " + + "user in another request at the same time. You may retry your modification request."), + @ApiResponse(responseCode = "415", description = "The request was attempt with a media-type which is not " + + "supported by the server for this resource."), + @ApiResponse(responseCode = "429", description = "Too many requests. The server will refuse further attempts " + + "and the client has to wait another second.") + }) + @PutMapping( + value = MgmtRestConstants.TARGET_TAG_V1_REQUEST_MAPPING + MgmtRestConstants.TARGET_TAG_TARGETS_REQUEST_MAPPING, + consumes = { MediaTypes.HAL_JSON_VALUE, MediaType.APPLICATION_JSON_VALUE } ) + ResponseEntity assignTargetsByControllerIds( + @PathVariable("targetTagId") Long targetTagId, + @Schema(description = "List of controller ids to be assigned", example = "[\"controllerId1\", \"controllerId2\"]") + @RequestBody List assignedTargetControlIds); + + /** + * Handles the POST request to assign targets to the given tag id. + * + * @param targetTagId the ID of the target tag to retrieve + * @param assignedTargetRequestBodies list of controller ids to be assigned + * + * @return the list of assigned targets. + */ + @Operation(summary = "Assign target(s) to given tagId and return targets", + description = "Handles the POST request of target assignment. Already assigned target will be ignored.") + @ApiResponses(value = { + @ApiResponse(responseCode = "200", description = "Successfully assigned"), @ApiResponse(responseCode = "400", description = "Bad Request - e.g. invalid parameters", content = @Content(mediaType = "application/json", schema = @Schema(implementation = ExceptionInfo.class))), @ApiResponse(responseCode = "401", description = "The request requires user authentication."), diff --git a/hawkbit-rest/hawkbit-mgmt-resource/src/main/java/org/eclipse/hawkbit/mgmt/rest/resource/MgmtTargetTagResource.java b/hawkbit-rest/hawkbit-mgmt-resource/src/main/java/org/eclipse/hawkbit/mgmt/rest/resource/MgmtTargetTagResource.java index 0ecad23eaf..7b853f1a26 100644 --- a/hawkbit-rest/hawkbit-mgmt-resource/src/main/java/org/eclipse/hawkbit/mgmt/rest/resource/MgmtTargetTagResource.java +++ b/hawkbit-rest/hawkbit-mgmt-resource/src/main/java/org/eclipse/hawkbit/mgmt/rest/resource/MgmtTargetTagResource.java @@ -12,6 +12,7 @@ import java.util.List; import java.util.stream.Collectors; +import io.swagger.v3.oas.annotations.media.Schema; import lombok.extern.slf4j.Slf4j; import org.eclipse.hawkbit.mgmt.json.model.PagedList; import org.eclipse.hawkbit.mgmt.json.model.tag.MgmtAssignedTargetRequestBody; @@ -177,10 +178,20 @@ public ResponseEntity toggleTagAssignment( return ResponseEntity.ok(tagAssigmentResultRest); } + @Override + public ResponseEntity assignTargetsByControllerIds( + @PathVariable("targetTagId") Long targetTagId, + @Schema(description = "List of controller ids to be assigned", example = "[\"controllerId1\", \"controllerId2\"]") + @RequestBody List assignedTargetControlIds) { + log.debug("Assign {} targets for target tag {}", assignedTargetControlIds.size(), targetTagId); + this.targetManagement.assignTag(assignedTargetControlIds, targetTagId); + return ResponseEntity.ok().build(); + } + @Override public ResponseEntity> assignTargets(@PathVariable("targetTagId") final Long targetTagId, @RequestBody final List assignedTargetRequestBodies) { - log.debug("Assign Targets {} for target tag {}", assignedTargetRequestBodies.size(), targetTagId); + log.debug("Assign targets {} for target tag {}", assignedTargetRequestBodies, targetTagId); final List assignedTarget = this.targetManagement .assignTag(findTargetControllerIds(assignedTargetRequestBodies), targetTagId); return ResponseEntity.ok(MgmtTargetMapper.toResponse(assignedTarget, tenantConfigHelper)); @@ -204,5 +215,4 @@ private List findTargetControllerIds( return assignedTargetRequestBodies.stream().map(MgmtAssignedTargetRequestBody::getControllerId) .collect(Collectors.toList()); } - -} +} \ No newline at end of file