template_handler.go 2.5 KB

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