list.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package neon_integration
  2. import (
  3. "net/http"
  4. "time"
  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/telemetry"
  12. )
  13. // ListNeonIntegrationsHandler is a struct for listing all noen integrations for a given project
  14. type ListNeonIntegrationsHandler struct {
  15. handlers.PorterHandlerReadWriter
  16. }
  17. // NewListNeonIntegrationsHandler constructs a ListNeonIntegrationsHandler
  18. func NewListNeonIntegrationsHandler(
  19. config *config.Config,
  20. decoderValidator shared.RequestDecoderValidator,
  21. writer shared.ResultWriter,
  22. ) *ListNeonIntegrationsHandler {
  23. return &ListNeonIntegrationsHandler{
  24. PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
  25. }
  26. }
  27. // NeonIntegration describes a neon integration
  28. type NeonIntegration struct {
  29. CreatedAt time.Time `json:"created_at"`
  30. }
  31. // ListNeonIntegrationsResponse describes the list neon integrations response body
  32. type ListNeonIntegrationsResponse struct {
  33. // Integrations is a list of neon integrations
  34. Integrations []NeonIntegration `json:"integrations"`
  35. }
  36. // ServeHTTP returns a list of neon integrations associated with the specified project
  37. func (h *ListNeonIntegrationsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  38. ctx, span := telemetry.NewSpan(r.Context(), "serve-list-neon-integrations")
  39. defer span.End()
  40. project, _ := ctx.Value(types.ProjectScope).(*models.Project)
  41. resp := ListNeonIntegrationsResponse{}
  42. integrationList := make([]NeonIntegration, 0)
  43. integrations, err := h.Repo().NeonIntegration().Integrations(ctx, project.ID)
  44. if err != nil {
  45. err := telemetry.Error(ctx, span, err, "error getting datastores")
  46. h.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  47. return
  48. }
  49. for _, int := range integrations {
  50. integrationList = append(integrationList, NeonIntegration{
  51. CreatedAt: int.CreatedAt,
  52. })
  53. }
  54. resp.Integrations = integrationList
  55. h.WriteResult(w, r, resp)
  56. }