repo.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. package repo
  2. import (
  3. "fmt"
  4. "github.com/porter-dev/porter/api/types"
  5. "github.com/porter-dev/porter/internal/helm/loader"
  6. "github.com/porter-dev/porter/internal/models"
  7. "helm.sh/helm/v3/pkg/chart"
  8. "github.com/porter-dev/porter/internal/repository"
  9. )
  10. // HelmRepo wraps the gorm HelmRepo model
  11. type HelmRepo models.HelmRepo
  12. // ListCharts lists Porter charts for a given helm repo
  13. func (hr *HelmRepo) ListCharts(repo repository.Repository) (types.ListTemplatesResponse, error) {
  14. if hr.BasicAuthIntegrationID != 0 {
  15. return hr.listChartsBasic(repo)
  16. }
  17. return nil, fmt.Errorf("error listing charts")
  18. }
  19. // GetChart retrieves a Porter chart for a given helm repo
  20. func (hr *HelmRepo) GetChart(
  21. repo repository.Repository,
  22. chartName, chartVersion string,
  23. ) (*chart.Chart, error) {
  24. if hr.BasicAuthIntegrationID != 0 {
  25. return hr.getChartBasic(repo, chartName, chartVersion)
  26. }
  27. return nil, fmt.Errorf("error listing charts")
  28. }
  29. func (hr *HelmRepo) listChartsBasic(
  30. repo repository.Repository,
  31. ) (types.ListTemplatesResponse, error) {
  32. // get the basic auth integration
  33. basic, err := repo.BasicIntegration().ReadBasicIntegration(
  34. hr.ProjectID,
  35. hr.BasicAuthIntegrationID,
  36. )
  37. if err != nil {
  38. return nil, err
  39. }
  40. client := &loader.BasicAuthClient{
  41. Username: string(basic.Username),
  42. Password: string(basic.Password),
  43. }
  44. repoIndex, err := loader.LoadRepoIndex(client, hr.RepoURL)
  45. if err != nil {
  46. return nil, err
  47. }
  48. return loader.RepoIndexToPorterChartList(repoIndex, hr.RepoURL), nil
  49. }
  50. func (hr *HelmRepo) getChartBasic(
  51. repo repository.Repository,
  52. chartName, chartVersion string,
  53. ) (*chart.Chart, error) {
  54. // get the basic auth integration
  55. basic, err := repo.BasicIntegration().ReadBasicIntegration(
  56. hr.ProjectID,
  57. hr.BasicAuthIntegrationID,
  58. )
  59. if err != nil {
  60. return nil, err
  61. }
  62. client := &loader.BasicAuthClient{
  63. Username: string(basic.Username),
  64. Password: string(basic.Password),
  65. }
  66. return loader.LoadChart(client, hr.RepoURL, chartName, chartVersion)
  67. }