get.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package infra
  2. import (
  3. "errors"
  4. "fmt"
  5. "net/http"
  6. "github.com/porter-dev/porter/api/server/handlers"
  7. "github.com/porter-dev/porter/api/server/shared"
  8. "github.com/porter-dev/porter/api/server/shared/apierrors"
  9. "github.com/porter-dev/porter/api/server/shared/config"
  10. "github.com/porter-dev/porter/api/types"
  11. "github.com/porter-dev/porter/internal/models"
  12. "gorm.io/gorm"
  13. )
  14. type InfraGetHandler struct {
  15. handlers.PorterHandlerWriter
  16. }
  17. func NewInfraGetHandler(
  18. config *config.Config,
  19. writer shared.ResultWriter,
  20. ) *InfraGetHandler {
  21. return &InfraGetHandler{
  22. PorterHandlerWriter: handlers.NewDefaultPorterHandler(config, nil, writer),
  23. }
  24. }
  25. func (c *InfraGetHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  26. infra, _ := r.Context().Value(types.InfraScope).(*models.Infra)
  27. res := infra.ToInfraType()
  28. // look for the latest operation and attach it, if it exists
  29. operation, err := c.Repo().Infra().GetLatestOperation(infra)
  30. if err != nil {
  31. if errors.Is(err, gorm.ErrRecordNotFound) {
  32. c.HandleAPIError(w, r, apierrors.NewErrInternal(fmt.Errorf("latest operation not found")))
  33. return
  34. }
  35. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  36. return
  37. }
  38. op, err := operation.ToOperationType()
  39. if err != nil {
  40. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  41. return
  42. }
  43. res.LatestOperation = op
  44. c.WriteResult(w, r, res)
  45. }