chart.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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.PopulateHelmOptionsFromQueryParams(vals)
  45. if limit, ok := vals["limit"]; ok && len(limit) == 1 {
  46. if limitInt, err := strconv.ParseInt(limit[0], 10, 64); err == nil {
  47. lcf.ListFilter.Limit = int(limitInt)
  48. }
  49. }
  50. if skip, ok := vals["skip"]; ok && len(skip) == 1 {
  51. if skipInt, err := strconv.ParseInt(skip[0], 10, 64); err == nil {
  52. lcf.ListFilter.Skip = int(skipInt)
  53. }
  54. }
  55. if byDate, ok := vals["byDate"]; ok && len(byDate) == 1 {
  56. if byDateBool, err := strconv.ParseBool(byDate[0]); err == nil {
  57. lcf.ListFilter.ByDate = byDateBool
  58. }
  59. }
  60. if statusFilter, ok := vals["statusFilter"]; ok {
  61. lcf.ListFilter.StatusFilter = statusFilter
  62. }
  63. }
  64. // GetChartForm represents the accepted values for getting a single Helm chart
  65. type GetChartForm struct {
  66. *ChartForm
  67. Name string `json:"name" form:"required"`
  68. Revision int `json:"revision"`
  69. }
  70. // ListChartHistoryForm represents the accepted values for getting a single Helm chart
  71. type ListChartHistoryForm struct {
  72. *ChartForm
  73. Name string `json:"name" form:"required"`
  74. }