Переглянути джерело

add validator for random string driver

Mohammed Nafees 3 роки тому
батько
коміт
541015ec73

+ 1 - 3
api/server/handlers/environment/validate_porter_yaml.go

@@ -115,9 +115,7 @@ func (c *ValidatePorterYAMLHandler) ServeHTTP(w http.ResponseWriter, r *http.Req
 		return
 	}
 
-	validator := preview.NewPorterYAMLValidator()
-
-	res.Errors = append(res.Errors, validator.Validate(contents)...)
+	res.Errors = append(res.Errors, preview.Validate(contents)...)
 
 	c.WriteResult(w, r, res)
 }

+ 3 - 2
cli/cmd/apply.go

@@ -20,6 +20,7 @@ import (
 	"github.com/porter-dev/porter/cli/cmd/deploy"
 	"github.com/porter-dev/porter/cli/cmd/deploy/wait"
 	"github.com/porter-dev/porter/cli/cmd/preview"
+	previewInt "github.com/porter-dev/porter/internal/integrations/preview"
 	"github.com/porter-dev/porter/internal/templater/utils"
 	"github.com/porter-dev/switchboard/pkg/drivers"
 	"github.com/porter-dev/switchboard/pkg/models"
@@ -194,8 +195,8 @@ type ApplicationConfig struct {
 }
 
 type DeployDriver struct {
-	source      *preview.Source
-	target      *preview.Target
+	source      *previewInt.Source
+	target      *previewInt.Target
 	output      map[string]interface{}
 	lookupTable *map[string]drivers.Driver
 	logger      *zerolog.Logger

+ 3 - 2
cli/cmd/preview/build_image_driver.go

@@ -14,6 +14,7 @@ import (
 	"github.com/porter-dev/porter/cli/cmd/config"
 	"github.com/porter-dev/porter/cli/cmd/deploy"
 	"github.com/porter-dev/porter/cli/cmd/docker"
+	"github.com/porter-dev/porter/internal/integrations/preview"
 	"github.com/porter-dev/switchboard/pkg/drivers"
 	"github.com/porter-dev/switchboard/pkg/models"
 )
@@ -36,8 +37,8 @@ type BuildDriverConfig struct {
 }
 
 type BuildDriver struct {
-	source      *Source
-	target      *Target
+	source      *preview.Source
+	target      *preview.Target
 	config      *BuildDriverConfig
 	lookupTable *map[string]drivers.Driver
 	output      map[string]interface{}

+ 2 - 1
cli/cmd/preview/env_group_driver.go

@@ -8,6 +8,7 @@ import (
 	"github.com/mitchellh/mapstructure"
 	"github.com/porter-dev/porter/api/types"
 	"github.com/porter-dev/porter/cli/cmd/config"
+	"github.com/porter-dev/porter/internal/integrations/preview"
 	"github.com/porter-dev/switchboard/pkg/drivers"
 	"github.com/porter-dev/switchboard/pkg/models"
 )
@@ -19,7 +20,7 @@ type EnvGroupDriverConfig struct {
 type EnvGroupDriver struct {
 	output      map[string]interface{}
 	lookupTable *map[string]drivers.Driver
-	target      *Target
+	target      *preview.Target
 	config      *EnvGroupDriverConfig
 }
 

+ 2 - 1
cli/cmd/preview/push_image_driver.go

@@ -11,6 +11,7 @@ import (
 	"github.com/porter-dev/porter/cli/cmd/config"
 	"github.com/porter-dev/porter/cli/cmd/deploy"
 	"github.com/porter-dev/porter/cli/cmd/docker"
+	"github.com/porter-dev/porter/internal/integrations/preview"
 	"github.com/porter-dev/switchboard/pkg/drivers"
 	"github.com/porter-dev/switchboard/pkg/models"
 )
@@ -23,7 +24,7 @@ type PushDriverConfig struct {
 }
 
 type PushDriver struct {
-	target      *Target
+	target      *preview.Target
 	config      *PushDriverConfig
 	lookupTable *map[string]drivers.Driver
 	output      map[string]interface{}

+ 3 - 7
cli/cmd/preview/random_string_driver.go

@@ -4,6 +4,7 @@ import (
 	"crypto/rand"
 
 	"github.com/mitchellh/mapstructure"
+	"github.com/porter-dev/porter/internal/integrations/preview"
 	"github.com/porter-dev/switchboard/pkg/drivers"
 	"github.com/porter-dev/switchboard/pkg/models"
 )
@@ -11,14 +12,9 @@ import (
 const defaultCharset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
 const lowerCharset = "abcdefghijklmnopqrstuvwxyz"
 
-type RandomStringDriverConfig struct {
-	Length int
-	Lower  bool
-}
-
 type RandomStringDriver struct {
 	output map[string]interface{}
-	config *RandomStringDriverConfig
+	config *preview.RandomStringDriverConfig
 }
 
 func NewRandomStringDriver(resource *models.Resource, opts *drivers.SharedDriverOpts) (drivers.Driver, error) {
@@ -26,7 +22,7 @@ func NewRandomStringDriver(resource *models.Resource, opts *drivers.SharedDriver
 		output: make(map[string]interface{}),
 	}
 
-	driverConfig := &RandomStringDriverConfig{}
+	driverConfig := &preview.RandomStringDriverConfig{}
 
 	err := mapstructure.Decode(resource.Config, driverConfig)
 

+ 3 - 2
cli/cmd/preview/update_config_driver.go

@@ -14,6 +14,7 @@ import (
 	"github.com/porter-dev/porter/cli/cmd/config"
 	"github.com/porter-dev/porter/cli/cmd/deploy"
 	"github.com/porter-dev/porter/cli/cmd/deploy/wait"
+	"github.com/porter-dev/porter/internal/integrations/preview"
 	"github.com/porter-dev/porter/internal/templater/utils"
 	"github.com/porter-dev/switchboard/pkg/drivers"
 	"github.com/porter-dev/switchboard/pkg/models"
@@ -37,8 +38,8 @@ type UpdateConfigDriverConfig struct {
 }
 
 type UpdateConfigDriver struct {
-	source      *Source
-	target      *Target
+	source      *preview.Source
+	target      *preview.Target
 	config      *UpdateConfigDriverConfig
 	lookupTable *map[string]drivers.Driver
 	output      map[string]interface{}

+ 5 - 19
cli/cmd/preview/utils.go

@@ -8,25 +8,11 @@ import (
 
 	"github.com/porter-dev/porter/api/types"
 	"github.com/porter-dev/porter/cli/cmd/config"
+	"github.com/porter-dev/porter/internal/integrations/preview"
 )
 
-type Source struct {
-	Name          string
-	Repo          string
-	Version       string
-	IsApplication bool
-	SourceValues  map[string]interface{}
-}
-
-type Target struct {
-	AppName   string
-	Project   uint
-	Cluster   uint
-	Namespace string
-}
-
-func GetSource(resourceName string, input map[string]interface{}) (*Source, error) {
-	output := &Source{}
+func GetSource(resourceName string, input map[string]interface{}) (*preview.Source, error) {
+	output := &preview.Source{}
 
 	// first read from env vars
 	output.Name = os.Getenv("PORTER_SOURCE_NAME")
@@ -113,8 +99,8 @@ func GetSource(resourceName string, input map[string]interface{}) (*Source, erro
 		resourceName, output.Name, output.Repo)
 }
 
-func GetTarget(resourceName string, input map[string]interface{}) (*Target, error) {
-	output := &Target{}
+func GetTarget(resourceName string, input map[string]interface{}) (*preview.Target, error) {
+	output := &preview.Target{}
 
 	// first read from env vars
 	if projectEnv := os.Getenv("PORTER_PROJECT"); projectEnv != "" {

+ 100 - 0
internal/integrations/preview/driver_validators.go

@@ -0,0 +1,100 @@
+package preview
+
+import (
+	"fmt"
+
+	"github.com/mitchellh/mapstructure"
+	"github.com/porter-dev/switchboard/pkg/types"
+)
+
+func commonValidator(resource *types.Resource) (*Source, *Target, error) {
+	source := &Source{}
+
+	err := mapstructure.Decode(resource.Source, source)
+
+	if err != nil {
+		return nil, nil, fmt.Errorf("error parsing source for resource '%s': %w", resource.Name, err)
+	}
+
+	target := &Target{}
+
+	err = mapstructure.Decode(resource.Target, target)
+
+	if err != nil {
+		return nil, nil, fmt.Errorf("error parsing target for resource '%s': %w", resource.Name, err)
+	}
+
+	return source, target, nil
+}
+
+func deployDriverValidator(resource *types.Resource) error {
+	_, _, err := commonValidator(resource)
+
+	if err != nil {
+		return err
+	}
+
+	return nil
+}
+
+func buildImageDriverValidator(resource *types.Resource) error {
+	_, _, err := commonValidator(resource)
+
+	if err != nil {
+		return err
+	}
+
+	return nil
+}
+
+func pushImageDriverValidator(resource *types.Resource) error {
+	_, _, err := commonValidator(resource)
+
+	if err != nil {
+		return err
+	}
+
+	return nil
+}
+
+func updateConfigDriverValidator(resource *types.Resource) error {
+	_, _, err := commonValidator(resource)
+
+	if err != nil {
+		return err
+	}
+
+	return nil
+}
+
+func randomStringDriverValidator(resource *types.Resource) error {
+	_, _, err := commonValidator(resource)
+
+	if err != nil {
+		return err
+	}
+
+	driverConfig := &RandomStringDriverConfig{}
+
+	err = mapstructure.Decode(resource.Config, driverConfig)
+
+	if err != nil {
+		return fmt.Errorf("error parsing config for resource '%s': %w", resource.Name, err)
+	}
+
+	return nil
+}
+
+func envGroupDriverValidator(resource *types.Resource) error {
+	_, _, err := commonValidator(resource)
+
+	if err != nil {
+		return err
+	}
+
+	return nil
+}
+
+func osEnvDriverValidator(resource *types.Resource) error {
+	return nil
+}

+ 0 - 7
internal/integrations/preview/push_image_driver_validator.go

@@ -1,7 +0,0 @@
-package preview
-
-import "github.com/porter-dev/switchboard/pkg/types"
-
-func pushImageDriverValidator(resource *types.Resource) error {
-	return nil
-}

+ 21 - 0
internal/integrations/preview/utils.go

@@ -0,0 +1,21 @@
+package preview
+
+type Source struct {
+	Name          string
+	Repo          string
+	Version       string
+	IsApplication bool
+	SourceValues  map[string]interface{}
+}
+
+type Target struct {
+	AppName   string
+	Project   uint
+	Cluster   uint
+	Namespace string
+}
+
+type RandomStringDriverConfig struct {
+	Length int
+	Lower  bool
+}

+ 10 - 12
internal/integrations/preview/validate.go

@@ -17,21 +17,19 @@ var (
 
 type driverBasedResourceValidator func(*types.Resource) error
 
-type porterYAMLValidator struct {
-	driverValidators map[string]driverBasedResourceValidator
-}
-
-func NewPorterYAMLValidator() *porterYAMLValidator {
-	driverValidators := make(map[string]driverBasedResourceValidator)
+var driverValidators = make(map[string]driverBasedResourceValidator)
 
+func init() {
+	driverValidators["deploy"] = deployDriverValidator
+	driverValidators["build-image"] = buildImageDriverValidator
 	driverValidators["push-image"] = pushImageDriverValidator
-
-	return &porterYAMLValidator{
-		driverValidators: driverValidators,
-	}
+	driverValidators["update-config"] = updateConfigDriverValidator
+	driverValidators["random-string"] = randomStringDriverValidator
+	driverValidators["env-group"] = envGroupDriverValidator
+	driverValidators["os-env"] = osEnvDriverValidator
 }
 
-func (v *porterYAMLValidator) Validate(contents string) []error {
+func Validate(contents string) []error {
 	var errors []error
 
 	resGroup, err := parser.ParseRawBytes([]byte(contents))
@@ -42,7 +40,7 @@ func (v *porterYAMLValidator) Validate(contents string) []error {
 	}
 
 	for _, res := range resGroup.Resources {
-		if validator, ok := v.driverValidators[res.Driver]; ok {
+		if validator, ok := driverValidators[res.Driver]; ok {
 			if err := validator(res); err != nil {
 				errors = append(errors, err)
 			}