list_charts.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package helmrepo
  2. import (
  3. "net/http"
  4. "k8s.io/helm/pkg/repo"
  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/helm/loader"
  11. "github.com/porter-dev/porter/internal/models"
  12. )
  13. type ChartListHandler struct {
  14. handlers.PorterHandlerReadWriter
  15. }
  16. func NewChartListHandler(
  17. config *config.Config,
  18. decoderValidator shared.RequestDecoderValidator,
  19. writer shared.ResultWriter,
  20. ) *ChartListHandler {
  21. return &ChartListHandler{
  22. PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
  23. }
  24. }
  25. func (t *ChartListHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  26. proj, _ := r.Context().Value(types.ProjectScope).(*models.Project)
  27. helmRepo, _ := r.Context().Value(types.HelmRepoScope).(*models.HelmRepo)
  28. var repoIndex *repo.IndexFile
  29. var err error
  30. if helmRepo.BasicAuthIntegrationID != 0 {
  31. // read the basic integration id
  32. basic, err := t.Repo().BasicIntegration().ReadBasicIntegration(proj.ID, helmRepo.BasicAuthIntegrationID)
  33. if err != nil {
  34. t.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  35. return
  36. }
  37. repoIndex, err = loader.LoadRepoIndex(&loader.BasicAuthClient{
  38. Username: string(basic.Username),
  39. Password: string(basic.Password),
  40. }, helmRepo.RepoURL)
  41. } else {
  42. repoIndex, err = loader.LoadRepoIndexPublic(helmRepo.RepoURL)
  43. }
  44. if err != nil {
  45. t.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  46. return
  47. }
  48. charts := loader.RepoIndexToPorterChartList(repoIndex, helmRepo.RepoURL)
  49. t.WriteResult(w, r, charts)
  50. }