chart.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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)
  42. func (lcf *ListChartForm) PopulateListFromQueryParams(vals url.Values) {
  43. if namespace, ok := vals["namespace"]; ok && len(namespace) == 1 {
  44. lcf.ListFilter.Namespace = namespace[0]
  45. }
  46. if limit, ok := vals["limit"]; ok && len(limit) == 1 {
  47. if limitInt, err := strconv.ParseInt(limit[0], 10, 64); err == nil {
  48. lcf.ListFilter.Limit = int(limitInt)
  49. }
  50. }
  51. if skip, ok := vals["skip"]; ok && len(skip) == 1 {
  52. if skipInt, err := strconv.ParseInt(skip[0], 10, 64); err == nil {
  53. lcf.ListFilter.Skip = int(skipInt)
  54. }
  55. }
  56. if byDate, ok := vals["byDate"]; ok && len(byDate) == 1 {
  57. if byDateBool, err := strconv.ParseBool(byDate[0]); err == nil {
  58. lcf.ListFilter.ByDate = byDateBool
  59. }
  60. }
  61. if statusFilter, ok := vals["statusFilter"]; ok {
  62. lcf.ListFilter.StatusFilter = statusFilter
  63. }
  64. }
  65. // GetChartForm represents the accepted values for getting a single Helm chart
  66. type GetChartForm struct {
  67. *ChartForm
  68. Name string `json:"name" form:"required"`
  69. Revision int `json:"revision"`
  70. }
  71. // ListChartHistoryForm represents the accepted values for getting a single Helm chart
  72. type ListChartHistoryForm struct {
  73. *ChartForm
  74. Name string `json:"name" form:"required"`
  75. }
  76. // RollbackChartForm represents the accepted values for getting a single Helm chart
  77. type RollbackChartForm struct {
  78. *ChartForm
  79. Name string `json:"name" form:"required"`
  80. Revision int `json:"revision" form:"required"`
  81. }
  82. // UpgradeChartForm represents the accepted values for updating a Helm chart
  83. type UpgradeChartForm struct {
  84. *ChartForm
  85. Name string `json:"name" form:"required"`
  86. Values string `json:"values" form:"required"`
  87. }