get_state.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package infra
  2. import (
  3. "context"
  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/internal/models"
  11. "github.com/porter-dev/porter/provisioner/client"
  12. )
  13. type InfraGetStateHandler struct {
  14. handlers.PorterHandlerWriter
  15. }
  16. func NewInfraGetStateHandler(
  17. config *config.Config,
  18. writer shared.ResultWriter,
  19. ) *InfraGetStateHandler {
  20. return &InfraGetStateHandler{
  21. PorterHandlerWriter: handlers.NewDefaultPorterHandler(config, nil, writer),
  22. }
  23. }
  24. func (c *InfraGetStateHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  25. proj, _ := r.Context().Value(types.ProjectScope).(*models.Project)
  26. infra, _ := r.Context().Value(types.InfraScope).(*models.Infra)
  27. // call apply on the provisioner service
  28. pClient := client.NewClient("http://localhost:8082/api/v1")
  29. resp, err := pClient.GetState(context.Background(), proj.ID, infra.ID)
  30. if err != nil {
  31. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  32. return
  33. }
  34. c.WriteResult(w, r, resp)
  35. }