list.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. package api_contract
  2. import (
  3. "net/http"
  4. "strconv"
  5. "github.com/porter-dev/porter/api/server/handlers"
  6. "github.com/porter-dev/porter/api/server/shared"
  7. "github.com/porter-dev/porter/api/server/shared/apierrors"
  8. "github.com/porter-dev/porter/api/server/shared/config"
  9. "github.com/porter-dev/porter/api/types"
  10. "github.com/porter-dev/porter/internal/models"
  11. "github.com/porter-dev/porter/internal/repository"
  12. "github.com/porter-dev/porter/internal/telemetry"
  13. )
  14. // APIContractRevisionListHandler is the handler for the GET /api/projects/{project_id}/contracts endpoint
  15. type APIContractRevisionListHandler struct {
  16. handlers.PorterHandlerReadWriter
  17. }
  18. // NewAPIContractRevisionListHandler returns a new APIContractRevisionListHandler
  19. func NewAPIContractRevisionListHandler(
  20. config *config.Config,
  21. decoderValidator shared.RequestDecoderValidator,
  22. writer shared.ResultWriter,
  23. ) *APIContractRevisionListHandler {
  24. return &APIContractRevisionListHandler{
  25. PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
  26. }
  27. }
  28. // APIContractRevisionListRequest is the request schema for the APIContractRevisionListHandler
  29. type APIContractRevisionListRequest struct {
  30. Latest bool `schema:"latest"`
  31. ClusterID string `schema:"cluster_id"`
  32. }
  33. // ServeHTTP returns a list of Porter API contract revisions for a given project.
  34. // If clusterID is also given, it will list by project_id, cluster_id
  35. // If latest is provided, it will only return the latest revision for each contract
  36. func (c *APIContractRevisionListHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  37. ctx, span := telemetry.NewSpan(r.Context(), "serve-api-contract-revisions")
  38. defer span.End()
  39. proj, _ := ctx.Value(types.ProjectScope).(*models.Project)
  40. request := &APIContractRevisionListRequest{}
  41. if ok := c.DecodeAndValidate(w, r, request); !ok {
  42. err := telemetry.Error(ctx, span, nil, "error decoding request")
  43. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  44. return
  45. }
  46. clusterID := 0
  47. clusterIDParam := request.ClusterID
  48. if clusterIDParam != "" {
  49. i, err := strconv.Atoi(clusterIDParam)
  50. if err != nil {
  51. err = telemetry.Error(ctx, span, err, "error parsing cluster id")
  52. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  53. return
  54. }
  55. clusterID = i
  56. }
  57. telemetry.WithAttributes(span,
  58. telemetry.AttributeKV{Key: "cluster-id", Value: clusterID},
  59. telemetry.AttributeKV{Key: "latest", Value: request.Latest},
  60. )
  61. resp := []*models.APIContractRevision{}
  62. revisions, err := c.Config().Repo.APIContractRevisioner().List(ctx, proj.ID, repository.WithClusterID(uint(clusterID)), repository.WithLatest(request.Latest))
  63. if err != nil {
  64. err = telemetry.Error(ctx, span, err, "error getting latest api contract revisions")
  65. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  66. return
  67. }
  68. resp = append(resp, revisions...)
  69. c.WriteResult(w, r, resp)
  70. }