chart.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package forms
  2. import (
  3. "fmt"
  4. "net/url"
  5. "strconv"
  6. "github.com/porter-dev/porter/internal/helm"
  7. "github.com/porter-dev/porter/internal/repository"
  8. )
  9. // ChartForm is the generic base type for CRUD operations on charts
  10. type ChartForm struct {
  11. *helm.Form
  12. }
  13. // PopulateHelmOptionsFromQueryParams populates fields in the ChartForm using the passed
  14. // url.Values (the parsed query params)
  15. func (cf *ChartForm) PopulateHelmOptionsFromQueryParams(vals url.Values) {
  16. fmt.Println("HI THERE", vals, vals["context"])
  17. if context, ok := vals["context"]; ok && len(context) == 1 {
  18. fmt.Println("SETTING CONTEXT", context[0])
  19. cf.Context = context[0]
  20. }
  21. if namespace, ok := vals["namespace"]; ok && len(namespace) == 1 {
  22. cf.Namespace = namespace[0]
  23. }
  24. if storage, ok := vals["storage"]; ok && len(storage) == 1 {
  25. cf.Storage = storage[0]
  26. }
  27. }
  28. // PopulateHelmOptionsFromUserID uses the passed user ID to populate the HelmOptions object
  29. func (cf *ChartForm) PopulateHelmOptionsFromUserID(userID uint, repo repository.UserRepository) error {
  30. user, err := repo.ReadUser(userID)
  31. if err != nil {
  32. return err
  33. }
  34. cf.AllowedContexts = user.ContextToSlice()
  35. cf.KubeConfig = user.RawKubeConfig
  36. return nil
  37. }
  38. // ListChartForm represents the accepted values for listing Helm charts
  39. type ListChartForm struct {
  40. *ChartForm
  41. *helm.ListFilter
  42. }
  43. // PopulateListFromQueryParams populates fields in the ListChartForm using the passed
  44. // url.Values (the parsed query params). It calls the underlying
  45. // PopulateHelmOptionsFromQueryParams
  46. func (lcf *ListChartForm) PopulateListFromQueryParams(vals url.Values) {
  47. lcf.PopulateHelmOptionsFromQueryParams(vals)
  48. if limit, ok := vals["limit"]; ok && len(limit) == 1 {
  49. if limitInt, err := strconv.ParseInt(limit[0], 10, 64); err == nil {
  50. lcf.ListFilter.Limit = int(limitInt)
  51. }
  52. }
  53. if skip, ok := vals["skip"]; ok && len(skip) == 1 {
  54. if skipInt, err := strconv.ParseInt(skip[0], 10, 64); err == nil {
  55. lcf.ListFilter.Skip = int(skipInt)
  56. }
  57. }
  58. if byDate, ok := vals["byDate"]; ok && len(byDate) == 1 {
  59. if byDateBool, err := strconv.ParseBool(byDate[0]); err == nil {
  60. lcf.ListFilter.ByDate = byDateBool
  61. }
  62. }
  63. if statusFilter, ok := vals["statusFilter"]; ok {
  64. lcf.ListFilter.StatusFilter = statusFilter
  65. }
  66. }
  67. // GetChartForm represents the accepted values for getting a single Helm chart
  68. type GetChartForm struct {
  69. *ChartForm
  70. Name string `json:"name" form:"required"`
  71. Revision int `json:"revision"`
  72. }