chart.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package forms
  2. import (
  3. "net/url"
  4. "strconv"
  5. "github.com/porter-dev/porter/internal/helm"
  6. "github.com/porter-dev/porter/internal/repository"
  7. )
  8. // ChartForm is the generic base type for CRUD operations on charts
  9. type ChartForm struct {
  10. *helm.Form
  11. }
  12. // PopulateHelmOptionsFromQueryParams populates fields in the ChartForm using the passed
  13. // url.Values (the parsed query params)
  14. func (cf *ChartForm) PopulateHelmOptionsFromQueryParams(vals url.Values) {
  15. if context, ok := vals["context"]; ok && len(context) == 1 {
  16. cf.Context = context[0]
  17. }
  18. if namespace, ok := vals["namespace"]; ok && len(namespace) == 1 {
  19. cf.Namespace = namespace[0]
  20. }
  21. if storage, ok := vals["storage"]; ok && len(storage) == 1 {
  22. cf.Storage = storage[0]
  23. }
  24. }
  25. // PopulateHelmOptionsFromUserID uses the passed user ID to populate the HelmOptions object
  26. func (cf *ChartForm) PopulateHelmOptionsFromUserID(userID uint, repo repository.UserRepository) error {
  27. user, err := repo.ReadUser(userID)
  28. if err != nil {
  29. return err
  30. }
  31. cf.AllowedContexts = user.ContextToSlice()
  32. cf.KubeConfig = user.RawKubeConfig
  33. return nil
  34. }
  35. // ListChartForm represents the accepted values for listing Helm charts
  36. type ListChartForm struct {
  37. *ChartForm
  38. *helm.ListFilter
  39. }
  40. // PopulateListFromQueryParams populates fields in the ListChartForm using the passed
  41. // url.Values (the parsed query params). It calls the underlying
  42. // PopulateHelmOptionsFromQueryParams
  43. func (lcf *ListChartForm) PopulateListFromQueryParams(vals url.Values) {
  44. lcf.ChartForm.PopulateHelmOptionsFromQueryParams(vals)
  45. if namespace, ok := vals["namespace"]; ok && len(namespace) == 1 {
  46. lcf.ListFilter.Namespace = namespace[0]
  47. }
  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. }
  73. // ListChartHistoryForm represents the accepted values for getting a single Helm chart
  74. type ListChartHistoryForm struct {
  75. *ChartForm
  76. Name string `json:"name" form:"required"`
  77. }