infra_handler.go 910 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. package api
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "strconv"
  6. "github.com/go-chi/chi"
  7. "github.com/porter-dev/porter/internal/models"
  8. )
  9. // HandleListProjectInfra returns a list of infrasa for a project
  10. func (app *App) HandleListProjectInfra(w http.ResponseWriter, r *http.Request) {
  11. projID, err := strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
  12. if err != nil || projID == 0 {
  13. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  14. return
  15. }
  16. infras, err := app.Repo.Infra().ListInfrasByProjectID(uint(projID))
  17. if err != nil {
  18. app.handleErrorRead(err, ErrProjectDataRead, w)
  19. return
  20. }
  21. extInfras := make([]*models.InfraExternal, 0)
  22. for _, infra := range infras {
  23. extInfras = append(extInfras, infra.Externalize())
  24. }
  25. w.WriteHeader(http.StatusOK)
  26. if err := json.NewEncoder(w).Encode(extInfras); err != nil {
  27. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  28. return
  29. }
  30. }