driver_validators.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package preview
  2. import (
  3. "fmt"
  4. "github.com/mitchellh/mapstructure"
  5. "github.com/porter-dev/switchboard/pkg/types"
  6. )
  7. func commonValidator(resource *types.Resource) (*Source, *Target, error) {
  8. source := &Source{}
  9. err := mapstructure.Decode(resource.Source, source)
  10. if err != nil {
  11. return nil, nil, fmt.Errorf("for resource '%s': error parsing source: %w", resource.Name, err)
  12. }
  13. target := &Target{}
  14. err = mapstructure.Decode(resource.Target, target)
  15. if err != nil {
  16. return nil, nil, fmt.Errorf("for resource '%s': error parsing target: %w", resource.Name, err)
  17. }
  18. return source, target, nil
  19. }
  20. func deployDriverValidator(resource *types.Resource) error {
  21. source, _, err := commonValidator(resource)
  22. if err != nil {
  23. return err
  24. }
  25. if source.Repo == "" || source.Repo == "https://charts.getporter.dev" {
  26. appConfig := &ApplicationConfig{}
  27. err = mapstructure.Decode(resource.Config, appConfig)
  28. if err != nil {
  29. return fmt.Errorf("for resource '%s': error parsing config: %w", resource.Name, err)
  30. }
  31. }
  32. return nil
  33. }
  34. func buildImageDriverValidator(resource *types.Resource) error {
  35. _, target, err := commonValidator(resource)
  36. if err != nil {
  37. return err
  38. }
  39. if target.AppName == "" {
  40. return fmt.Errorf("for resource '%s': target app_name is missing", resource.Name)
  41. }
  42. driverConfig := &BuildDriverConfig{}
  43. err = mapstructure.Decode(resource.Config, driverConfig)
  44. if err != nil {
  45. return fmt.Errorf("for resource '%s': error parsing config: %w", resource.Name, err)
  46. }
  47. return nil
  48. }
  49. func pushImageDriverValidator(resource *types.Resource) error {
  50. _, target, err := commonValidator(resource)
  51. if err != nil {
  52. return err
  53. }
  54. if target.AppName == "" {
  55. return fmt.Errorf("for resource '%s': target app_name is missing", resource.Name)
  56. }
  57. driverConfig := &PushDriverConfig{}
  58. err = mapstructure.Decode(resource.Config, driverConfig)
  59. if err != nil {
  60. return fmt.Errorf("for resource '%s': error parsing config: %w", resource.Name, err)
  61. }
  62. return nil
  63. }
  64. func updateConfigDriverValidator(resource *types.Resource) error {
  65. _, target, err := commonValidator(resource)
  66. if err != nil {
  67. return err
  68. }
  69. if target.AppName == "" {
  70. return fmt.Errorf("for resource '%s': target app_name is missing", resource.Name)
  71. }
  72. driverConfig := &UpdateConfigDriverConfig{}
  73. err = mapstructure.Decode(resource.Config, driverConfig)
  74. if err != nil {
  75. return fmt.Errorf("for resource '%s': error parsing config: %w", resource.Name, err)
  76. }
  77. return nil
  78. }
  79. func randomStringDriverValidator(resource *types.Resource) error {
  80. driverConfig := &RandomStringDriverConfig{}
  81. err := mapstructure.Decode(resource.Config, driverConfig)
  82. if err != nil {
  83. return fmt.Errorf("for resource '%s': error parsing config: %w", resource.Name, err)
  84. }
  85. return nil
  86. }
  87. func envGroupDriverValidator(resource *types.Resource) error {
  88. target := &Target{}
  89. err := mapstructure.Decode(resource.Target, target)
  90. if err != nil {
  91. return fmt.Errorf("for resource '%s': error parsing target: %w", resource.Name, err)
  92. }
  93. config := &EnvGroupDriverConfig{}
  94. err = mapstructure.Decode(resource.Config, config)
  95. if err != nil {
  96. return fmt.Errorf("for resource '%s': error parsing config: %w", resource.Name, err)
  97. }
  98. return nil
  99. }
  100. func osEnvDriverValidator(resource *types.Resource) error {
  101. return nil
  102. }