2
0

template_handler.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package api
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/http"
  6. "net/url"
  7. "strings"
  8. "github.com/go-chi/chi"
  9. "github.com/porter-dev/porter/internal/forms"
  10. "github.com/porter-dev/porter/internal/helm/loader"
  11. "github.com/porter-dev/porter/internal/templater/parser"
  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.PorterChartList{}
  25. for _, entry := range repoIndex.Entries {
  26. indexChart := entry[0]
  27. porterChart := models.PorterChartList{}
  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. // HandleReadTemplate reads a given template with name and version field
  36. func (app *App) HandleReadTemplate(w http.ResponseWriter, r *http.Request) {
  37. name := chi.URLParam(r, "name")
  38. version := chi.URLParam(r, "version")
  39. // if version passed as latest, pass empty string to loader to get latest
  40. if version == "latest" {
  41. version = ""
  42. }
  43. form := &forms.ChartForm{
  44. Name: name,
  45. Version: version,
  46. RepoURL: "https://porter-dev.github.io/chart-repo/",
  47. }
  48. // if a repo_url is passed as query param, it will be populated
  49. vals, err := url.ParseQuery(r.URL.RawQuery)
  50. if err != nil {
  51. app.handleErrorFormDecoding(err, ErrReleaseDecode, w)
  52. return
  53. }
  54. form.PopulateRepoURLFromQueryParams(vals)
  55. chart, err := loader.LoadChart(form.RepoURL, form.Name, form.Version)
  56. if err != nil {
  57. fmt.Println("ERROR LOADING CHART", form.RepoURL, form.Name, form.Version, err)
  58. app.handleErrorFormDecoding(err, ErrReleaseDecode, w)
  59. return
  60. }
  61. parserDef := &parser.ClientConfigDefault{
  62. HelmChart: chart,
  63. }
  64. res := &models.PorterChartRead{}
  65. res.Metadata = chart.Metadata
  66. res.Values = chart.Values
  67. for _, file := range chart.Files {
  68. if strings.Contains(file.Name, "form.yaml") {
  69. formYAML, err := parser.FormYAMLFromBytes(parserDef, file.Data)
  70. fmt.Println("FORM RESULT:", formYAML, err)
  71. if err != nil {
  72. break
  73. }
  74. res.Form = formYAML
  75. } else if strings.Contains(file.Name, "README.md") {
  76. res.Markdown = string(file.Data)
  77. }
  78. }
  79. bytesRes, _ := json.Marshal(res)
  80. fmt.Println("RAW RESPONSE:", string(bytesRes), res)
  81. json.NewEncoder(w).Encode(res)
  82. }