list.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package cluster
  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 ClusterListHandler struct {
  12. handlers.PorterHandlerWriter
  13. }
  14. func NewClusterListHandler(
  15. config *config.Config,
  16. writer shared.ResultWriter,
  17. ) *ClusterListHandler {
  18. return &ClusterListHandler{
  19. PorterHandlerWriter: handlers.NewDefaultPorterHandler(config, nil, writer),
  20. }
  21. }
  22. func (p *ClusterListHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  23. // read the project from context
  24. proj, _ := r.Context().Value(types.ProjectScope).(*models.Project)
  25. // read all clusters for this project
  26. clusters, err := p.Repo().Cluster().ListClustersByProjectID(proj.ID)
  27. if err != nil {
  28. p.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  29. return
  30. }
  31. res := make(types.ListClusterResponse, len(clusters))
  32. for i, cluster := range clusters {
  33. res[i] = cluster.ToClusterType()
  34. }
  35. p.WriteResult(w, r, res)
  36. }