list_operations.go 1.1 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/internal/models"
  10. )
  11. type InfraListOperationsHandler struct {
  12. handlers.PorterHandlerWriter
  13. }
  14. func NewInfraListOperationsHandler(
  15. config *config.Config,
  16. writer shared.ResultWriter,
  17. ) *InfraListOperationsHandler {
  18. return &InfraListOperationsHandler{
  19. PorterHandlerWriter: handlers.NewDefaultPorterHandler(config, nil, writer),
  20. }
  21. }
  22. func (c *InfraListOperationsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  23. infra, _ := r.Context().Value(types.InfraScope).(*models.Infra)
  24. ops, err := c.Repo().Infra().ListOperations(infra.ID)
  25. if err != nil {
  26. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  27. return
  28. }
  29. res := make([]*types.OperationMeta, 0)
  30. for _, op := range ops {
  31. res = append(res, op.ToOperationMetaType())
  32. }
  33. c.WriteResult(w, r, res)
  34. }