ClusterController.java

  1. package no.nav.data.team.cluster;

  2. import io.swagger.v3.oas.annotations.Operation;
  3. import io.swagger.v3.oas.annotations.responses.ApiResponse;
  4. import io.swagger.v3.oas.annotations.tags.Tag;
  5. import jakarta.validation.Valid;
  6. import lombok.extern.slf4j.Slf4j;
  7. import no.nav.data.common.exceptions.ValidationException;
  8. import no.nav.data.common.rest.RestResponsePage;
  9. import no.nav.data.common.utils.StreamUtils;
  10. import no.nav.data.team.cluster.domain.Cluster;
  11. import no.nav.data.team.cluster.dto.ClusterRequest;
  12. import no.nav.data.team.cluster.dto.ClusterResponse;
  13. import no.nav.data.team.shared.domain.DomainObjectStatus;
  14. import org.springframework.http.HttpStatus;
  15. import org.springframework.http.ResponseEntity;
  16. import org.springframework.web.bind.annotation.DeleteMapping;
  17. import org.springframework.web.bind.annotation.GetMapping;
  18. import org.springframework.web.bind.annotation.PathVariable;
  19. import org.springframework.web.bind.annotation.PostMapping;
  20. import org.springframework.web.bind.annotation.PutMapping;
  21. import org.springframework.web.bind.annotation.RequestBody;
  22. import org.springframework.web.bind.annotation.RequestMapping;
  23. import org.springframework.web.bind.annotation.RestController;
  24. import org.springframework.web.bind.annotation.RequestParam;

  25. import java.util.Objects;
  26. import java.util.UUID;

  27. import static no.nav.data.common.utils.StreamUtils.convert;

  28. @Slf4j
  29. @RestController
  30. @RequestMapping("/cluster")
  31. @Tag(name = "Cluster", description = "Cluster endpoint")
  32. public class ClusterController {

  33.     private final ClusterService service;

  34.     public ClusterController(ClusterService service) {
  35.         this.service = service;
  36.     }

  37.     @Operation(summary = "Get All Clusters")
  38.     @ApiResponse(description = "ok")
  39.     @GetMapping
  40.     public ResponseEntity<RestResponsePage<ClusterResponse>> getAll(
  41.             @RequestParam(name = "status", required = false, defaultValue = "ACTIVE,PLANNED,INACTIVE") String stringStatus

  42.     ) {
  43.         log.info("Get all Clusters");

  44.         var cluster = service.getAll();

  45.         var queryStatusList = DomainObjectStatus.fromQueryParameter(stringStatus);

  46.         cluster = cluster.stream().filter(t -> queryStatusList.contains(t.getStatus())).toList();


  47.         return ResponseEntity.ok(new RestResponsePage<>(StreamUtils.convert(cluster, Cluster::convertToResponse)));
  48.     }

  49.     @Operation(summary = "Get Cluster")
  50.     @ApiResponse(description = "ok")
  51.     @GetMapping("/{id}")
  52.     public ResponseEntity<ClusterResponse> getById(@PathVariable UUID id) {
  53.         log.info("Get Cluster id={}", id);
  54.         return ResponseEntity.ok(service.get(id).convertToResponse());
  55.     }

  56.     @Operation(summary = "Search Cluster")
  57.     @ApiResponse(description = "Cluster fetched")
  58.     @GetMapping("/search/{name}")
  59.     public ResponseEntity<RestResponsePage<ClusterResponse>> searchClusterByName(@PathVariable String name) {
  60.         log.info("Received request for Cluster with the name like {}", name);
  61.         if (name.length() < 3) {
  62.             throw new ValidationException("Search Cluster must be at least 3 characters");
  63.         }
  64.         var cluster= service.search(name);
  65.         log.info("Returned {} clusters", cluster.size());
  66.         return new ResponseEntity<>(new RestResponsePage<>(convert(cluster, Cluster::convertToResponse)), HttpStatus.OK);
  67.     }

  68.     @Operation(summary = "Create Cluster")
  69.     @ApiResponse(responseCode = "201", description = "Cluster created")
  70.     @PostMapping
  71.     public ResponseEntity<ClusterResponse> createCluster(@RequestBody ClusterRequest request) {
  72.         log.info("Create Cluster");
  73.         var cluster = service.save(request);
  74.         return new ResponseEntity<>(cluster.convertToResponse(), HttpStatus.CREATED);
  75.     }

  76.     @Operation(summary = "Update Cluster")
  77.     @ApiResponse(description = "Cluster updated")
  78.     @PutMapping("/{id}")
  79.     public ResponseEntity<ClusterResponse> updateCluster(@PathVariable UUID id, @Valid @RequestBody ClusterRequest request) {
  80.         log.debug("Update Cluster id={}", id);
  81.         if (!Objects.equals(id, request.getIdAsUUID())) {
  82.             throw new ValidationException(String.format("id mismatch in request %s and path %s", request.getId(), id));
  83.         }
  84.         var cluster = service.save(request);
  85.         return ResponseEntity.ok(cluster.convertToResponse());
  86.     }

  87.     @Operation(summary = "Delete Cluster")
  88.     @ApiResponse(description = "Cluster deleted")
  89.     @DeleteMapping("/{id}")
  90.     public ResponseEntity<ClusterResponse> deleteClusterById(@PathVariable UUID id) {
  91.         log.info("Delete Cluster id={}", id);
  92.         var cluster = service.delete(id);
  93.         return ResponseEntity.ok(cluster.convertToResponse());
  94.     }

  95.     static class ClusterPageResponse extends RestResponsePage<ClusterResponse> {

  96.     }

  97. }