get_current.go 1.2 KB

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