template_handler.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package api
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "net/url"
  6. "strings"
  7. "github.com/go-chi/chi"
  8. "github.com/porter-dev/porter/internal/forms"
  9. "github.com/porter-dev/porter/internal/helm/loader"
  10. "github.com/porter-dev/porter/internal/templater/parser"
  11. "helm.sh/helm/v3/pkg/chart"
  12. "github.com/porter-dev/porter/internal/models"
  13. )
  14. // HandleListTemplates retrieves a list of Porter templates
  15. // TODO: test and reduce fragility (handle untar/parse error for individual charts)
  16. // TODO: separate markdown retrieval into its own query if necessary
  17. func (app *App) HandleListTemplates(w http.ResponseWriter, r *http.Request) {
  18. repoIndex, err := loader.LoadRepoIndex("https://porter-dev.github.io/chart-repo/index.yaml")
  19. if err != nil {
  20. app.handleErrorFormDecoding(err, ErrReleaseDecode, w)
  21. return
  22. }
  23. // Loop over charts in index.yaml
  24. porterCharts := []models.PorterChart{}
  25. for _, entry := range repoIndex.Entries {
  26. indexChart := entry[0]
  27. porterChart := models.PorterChart{}
  28. porterChart.Name = indexChart.Name
  29. porterChart.Description = indexChart.Description
  30. porterChart.Icon = indexChart.Icon
  31. porterCharts = append(porterCharts, porterChart)
  32. }
  33. json.NewEncoder(w).Encode(porterCharts)
  34. }
  35. // ChartWithForm is a base helm chart with the form yaml appended
  36. type ChartWithForm struct {
  37. *chart.Chart
  38. Form *models.FormYAML `json:"form"`
  39. }
  40. // HandleReadTemplate reads a given template with name and version field
  41. func (app *App) HandleReadTemplate(w http.ResponseWriter, r *http.Request) {
  42. name := chi.URLParam(r, "name")
  43. version := chi.URLParam(r, "version")
  44. // if version passed as latest, pass empty string to loader to get latest
  45. if version == "latest" {
  46. version = ""
  47. }
  48. form := &forms.ChartForm{
  49. Name: name,
  50. Version: version,
  51. RepoURL: "https://porter-dev.github.io/chart-repo/",
  52. }
  53. // if a repo_url is passed as query param, it will be populated
  54. vals, err := url.ParseQuery(r.URL.RawQuery)
  55. if err != nil {
  56. app.handleErrorFormDecoding(err, ErrReleaseDecode, w)
  57. return
  58. }
  59. form.PopulateRepoURLFromQueryParams(vals)
  60. chart, err := loader.LoadChart(form.RepoURL, form.Name, form.Version)
  61. if err != nil {
  62. app.handleErrorFormDecoding(err, ErrReleaseDecode, w)
  63. return
  64. }
  65. parserDef := &parser.ClientConfigDefault{
  66. HelmChart: chart,
  67. }
  68. res := &ChartWithForm{chart, nil}
  69. for _, file := range chart.Files {
  70. if strings.Contains(file.Name, "form.yaml") {
  71. formYAML, err := parser.FormYAMLFromBytes(parserDef, file.Data)
  72. if err != nil {
  73. break
  74. }
  75. res.Form = formYAML
  76. }
  77. }
  78. }