list.go 1.1 KB

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