get_current.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package infra
  2. import (
  3. "errors"
  4. "net/http"
  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/ee/integrations/httpbackend"
  11. "github.com/porter-dev/porter/internal/models"
  12. )
  13. type InfraGetCurrentHandler struct {
  14. handlers.PorterHandlerWriter
  15. }
  16. func NewInfraGetCurrentHandler(
  17. config *config.Config,
  18. writer shared.ResultWriter,
  19. ) *InfraGetCurrentHandler {
  20. return &InfraGetCurrentHandler{
  21. PorterHandlerWriter: handlers.NewDefaultPorterHandler(config, nil, writer),
  22. }
  23. }
  24. func (c *InfraGetCurrentHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  25. infra, _ := r.Context().Value(types.InfraScope).(*models.Infra)
  26. // TODO: move client out of this call
  27. client := httpbackend.NewClient(c.Config().ServerConf.ProvisionerBackendURL)
  28. // get the unique infra name and query from the TF HTTP backend
  29. current, err := client.GetCurrentState(infra.GetUniqueName())
  30. if err != nil && errors.Is(err, httpbackend.ErrNotFound) {
  31. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(
  32. err,
  33. http.StatusNotFound,
  34. ))
  35. return
  36. } else if err != nil {
  37. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  38. return
  39. }
  40. c.WriteResult(w, r, current)
  41. }