Explorar el Código

add remaining driver validators

Mohammed Nafees hace 3 años
padre
commit
231cd3648b

+ 5 - 2
api/server/handlers/environment/validate_porter_yaml.go

@@ -5,6 +5,7 @@ import (
 	"errors"
 	"fmt"
 	"net/http"
+	"strings"
 
 	"github.com/google/go-github/v41/github"
 	"github.com/porter-dev/porter/api/server/authz"
@@ -71,7 +72,9 @@ func (c *ValidatePorterYAMLHandler) ServeHTTP(w http.ResponseWriter, r *http.Req
 		return
 	}
 
-	res := &types.ValidatePorterYAMLResponse{}
+	res := &types.ValidatePorterYAMLResponse{
+		Errors: []string{},
+	}
 
 	if req.Branch == "" { // get the default branch name
 		repo, _, err := ghClient.Repositories.Get(r.Context(), env.GitRepoOwner, env.GitRepoName)
@@ -109,7 +112,7 @@ func (c *ValidatePorterYAMLHandler) ServeHTTP(w http.ResponseWriter, r *http.Req
 		return
 	}
 
-	if contents == "" {
+	if strings.TrimSpace(contents) == "" {
 		res.Errors = append(res.Errors, preview.ErrEmptyPorterYAMLFile.Error())
 		c.WriteResult(w, r, res)
 		return

+ 5 - 28
cli/cmd/apply.go

@@ -171,29 +171,6 @@ func hasDeploymentHookEnvVars() bool {
 	return true
 }
 
-type ApplicationConfig struct {
-	WaitForJob bool
-
-	// If set to true, this does not run an update, it only creates the initial application and job,
-	// skipping subsequent updates
-	OnlyCreate bool
-
-	Build struct {
-		UseCache   bool `mapstructure:"use_cache"`
-		Method     string
-		Context    string
-		Dockerfile string
-		Image      string
-		Builder    string
-		Buildpacks []string
-		Env        map[string]string
-	}
-
-	EnvGroups []types.EnvGroupMeta `mapstructure:"env_groups"`
-
-	Values map[string]interface{}
-}
-
 type DeployDriver struct {
 	source      *previewInt.Source
 	target      *previewInt.Target
@@ -448,7 +425,7 @@ func (d *DeployDriver) applyApplication(resource *models.Resource, client *api.C
 	return resource, err
 }
 
-func (d *DeployDriver) createApplication(resource *models.Resource, client *api.Client, sharedOpts *deploy.SharedOpts, appConf *ApplicationConfig) (*models.Resource, error) {
+func (d *DeployDriver) createApplication(resource *models.Resource, client *api.Client, sharedOpts *deploy.SharedOpts, appConf *previewInt.ApplicationConfig) (*models.Resource, error) {
 	// create new release
 	color.New(color.FgGreen).Printf("Creating %s release: %s\n", d.source.Name, resource.Name)
 
@@ -534,7 +511,7 @@ func (d *DeployDriver) createApplication(resource *models.Resource, client *api.
 	return resource, handleSubdomainCreate(subdomain, err)
 }
 
-func (d *DeployDriver) updateApplication(resource *models.Resource, client *api.Client, sharedOpts *deploy.SharedOpts, appConf *ApplicationConfig) (*models.Resource, error) {
+func (d *DeployDriver) updateApplication(resource *models.Resource, client *api.Client, sharedOpts *deploy.SharedOpts, appConf *previewInt.ApplicationConfig) (*models.Resource, error) {
 	color.New(color.FgGreen).Println("Updating existing release:", resource.Name)
 
 	if len(appConf.Build.Env) > 0 {
@@ -622,7 +599,7 @@ func (d *DeployDriver) Output() (map[string]interface{}, error) {
 	return d.output, nil
 }
 
-func (d *DeployDriver) getApplicationConfig(resource *models.Resource) (*ApplicationConfig, error) {
+func (d *DeployDriver) getApplicationConfig(resource *models.Resource) (*previewInt.ApplicationConfig, error) {
 	populatedConf, err := drivers.ConstructConfig(&drivers.ConstructConfigOpts{
 		RawConf:      resource.Config,
 		LookupTable:  *d.lookupTable,
@@ -633,7 +610,7 @@ func (d *DeployDriver) getApplicationConfig(resource *models.Resource) (*Applica
 		return nil, err
 	}
 
-	appConf := &ApplicationConfig{}
+	appConf := &previewInt.ApplicationConfig{}
 
 	err = mapstructure.Decode(populatedConf, appConf)
 
@@ -994,7 +971,7 @@ func (t *CloneEnvGroupHook) PreApply() error {
 			continue
 		}
 
-		appConf := &ApplicationConfig{}
+		appConf := &previewInt.ApplicationConfig{}
 
 		err := mapstructure.Decode(res.Config, &appConf)
 		if err != nil {

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

@@ -19,27 +19,10 @@ import (
 	"github.com/porter-dev/switchboard/pkg/models"
 )
 
-type BuildDriverConfig struct {
-	Build struct {
-		UsePackCache bool `mapstructure:"use_pack_cache"`
-		Method       string
-		Context      string
-		Dockerfile   string
-		Builder      string
-		Buildpacks   []string
-		Image        string
-		Env          map[string]string
-	}
-
-	EnvGroups []types.EnvGroupMeta `mapstructure:"env_groups"`
-
-	Values map[string]interface{}
-}
-
 type BuildDriver struct {
 	source      *preview.Source
 	target      *preview.Target
-	config      *BuildDriverConfig
+	config      *preview.BuildDriverConfig
 	lookupTable *map[string]drivers.Driver
 	output      map[string]interface{}
 }
@@ -336,7 +319,7 @@ func (d *BuildDriver) Output() (map[string]interface{}, error) {
 	return d.output, nil
 }
 
-func (d *BuildDriver) getConfig(resource *models.Resource) (*BuildDriverConfig, error) {
+func (d *BuildDriver) getConfig(resource *models.Resource) (*preview.BuildDriverConfig, error) {
 	populatedConf, err := drivers.ConstructConfig(&drivers.ConstructConfigOpts{
 		RawConf:      resource.Config,
 		LookupTable:  *d.lookupTable,
@@ -347,7 +330,7 @@ func (d *BuildDriver) getConfig(resource *models.Resource) (*BuildDriverConfig,
 		return nil, err
 	}
 
-	config := &BuildDriverConfig{}
+	config := &preview.BuildDriverConfig{}
 
 	err = mapstructure.Decode(populatedConf, config)
 

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

@@ -13,15 +13,11 @@ import (
 	"github.com/porter-dev/switchboard/pkg/models"
 )
 
-type EnvGroupDriverConfig struct {
-	EnvGroups []*types.EnvGroup `mapstructure:"env_groups"`
-}
-
 type EnvGroupDriver struct {
 	output      map[string]interface{}
 	lookupTable *map[string]drivers.Driver
 	target      *preview.Target
-	config      *EnvGroupDriverConfig
+	config      *preview.EnvGroupDriverConfig
 }
 
 func NewEnvGroupDriver(resource *models.Resource, opts *drivers.SharedDriverOpts) (drivers.Driver, error) {
@@ -113,7 +109,7 @@ func (d *EnvGroupDriver) Output() (map[string]interface{}, error) {
 	return d.output, nil
 }
 
-func (d *EnvGroupDriver) getConfig(resource *models.Resource) (*EnvGroupDriverConfig, error) {
+func (d *EnvGroupDriver) getConfig(resource *models.Resource) (*preview.EnvGroupDriverConfig, error) {
 	populatedConf, err := drivers.ConstructConfig(&drivers.ConstructConfigOpts{
 		RawConf:      resource.Config,
 		LookupTable:  *d.lookupTable,
@@ -124,7 +120,7 @@ func (d *EnvGroupDriver) getConfig(resource *models.Resource) (*EnvGroupDriverCo
 		return nil, err
 	}
 
-	config := &EnvGroupDriverConfig{}
+	config := &preview.EnvGroupDriverConfig{}
 
 	err = mapstructure.Decode(populatedConf, config)
 

+ 3 - 10
cli/cmd/preview/push_image_driver.go

@@ -16,16 +16,9 @@ import (
 	"github.com/porter-dev/switchboard/pkg/models"
 )
 
-type PushDriverConfig struct {
-	Push struct {
-		UsePackCache bool `mapstructure:"use_pack_cache"`
-		Image        string
-	}
-}
-
 type PushDriver struct {
 	target      *preview.Target
-	config      *PushDriverConfig
+	config      *preview.PushDriverConfig
 	lookupTable *map[string]drivers.Driver
 	output      map[string]interface{}
 }
@@ -158,7 +151,7 @@ func (d *PushDriver) Output() (map[string]interface{}, error) {
 	return d.output, nil
 }
 
-func (d *PushDriver) getConfig(resource *models.Resource) (*PushDriverConfig, error) {
+func (d *PushDriver) getConfig(resource *models.Resource) (*preview.PushDriverConfig, error) {
 	populatedConf, err := drivers.ConstructConfig(&drivers.ConstructConfigOpts{
 		RawConf:      resource.Config,
 		LookupTable:  *d.lookupTable,
@@ -169,7 +162,7 @@ func (d *PushDriver) getConfig(resource *models.Resource) (*PushDriverConfig, er
 		return nil, err
 	}
 
-	config := &PushDriverConfig{}
+	config := &preview.PushDriverConfig{}
 
 	err = mapstructure.Decode(populatedConf, config)
 

+ 1 - 1
cli/cmd/preview/random_string_driver.go

@@ -50,7 +50,7 @@ func (d *RandomStringDriver) Apply(resource *models.Resource) (*models.Resource,
 		useCharset = lowerCharset
 	}
 
-	d.output["value"] = randomString(d.config.Length, useCharset)
+	d.output["value"] = randomString(int(d.config.Length), useCharset)
 
 	return resource, nil
 }

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

@@ -20,27 +20,10 @@ import (
 	"github.com/porter-dev/switchboard/pkg/models"
 )
 
-type UpdateConfigDriverConfig struct {
-	WaitForJob bool
-
-	// If set to true, this does not run an update, it only creates the initial application and job,
-	// skipping subsequent updates
-	OnlyCreate bool
-
-	UpdateConfig struct {
-		Image string
-		Tag   string
-	} `mapstructure:"update_config"`
-
-	EnvGroups []types.EnvGroupMeta `mapstructure:"env_groups"`
-
-	Values map[string]interface{}
-}
-
 type UpdateConfigDriver struct {
 	source      *preview.Source
 	target      *preview.Target
-	config      *UpdateConfigDriverConfig
+	config      *preview.UpdateConfigDriverConfig
 	lookupTable *map[string]drivers.Driver
 	output      map[string]interface{}
 }
@@ -226,7 +209,7 @@ func (d *UpdateConfigDriver) Output() (map[string]interface{}, error) {
 	return d.output, nil
 }
 
-func (d *UpdateConfigDriver) getConfig(resource *models.Resource) (*UpdateConfigDriverConfig, error) {
+func (d *UpdateConfigDriver) getConfig(resource *models.Resource) (*preview.UpdateConfigDriverConfig, error) {
 	populatedConf, err := drivers.ConstructConfig(&drivers.ConstructConfigOpts{
 		RawConf:      resource.Config,
 		LookupTable:  *d.lookupTable,
@@ -237,7 +220,7 @@ func (d *UpdateConfigDriver) getConfig(resource *models.Resource) (*UpdateConfig
 		return nil, err
 	}
 
-	config := &UpdateConfigDriverConfig{}
+	config := &preview.UpdateConfigDriverConfig{}
 
 	err = mapstructure.Decode(populatedConf, config)
 

+ 65 - 15
internal/integrations/preview/driver_validators.go

@@ -13,7 +13,7 @@ func commonValidator(resource *types.Resource) (*Source, *Target, error) {
 	err := mapstructure.Decode(resource.Source, source)
 
 	if err != nil {
-		return nil, nil, fmt.Errorf("error parsing source for resource '%s': %w", resource.Name, err)
+		return nil, nil, fmt.Errorf("for resource '%s': error parsing source: %w", resource.Name, err)
 	}
 
 	target := &Target{}
@@ -21,75 +21,125 @@ func commonValidator(resource *types.Resource) (*Source, *Target, error) {
 	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 nil, nil, fmt.Errorf("for resource '%s': error parsing target: %w", resource.Name, err)
 	}
 
 	return source, target, nil
 }
 
 func deployDriverValidator(resource *types.Resource) error {
-	_, _, err := commonValidator(resource)
+	source, _, err := commonValidator(resource)
 
 	if err != nil {
 		return err
 	}
 
+	if source.Repo == "" || source.Repo == "https://charts.getporter.dev" {
+		appConfig := &ApplicationConfig{}
+
+		err = mapstructure.Decode(resource.Config, appConfig)
+
+		if err != nil {
+			return fmt.Errorf("for resource '%s': error parsing config: %w", resource.Name, err)
+		}
+	}
+
 	return nil
 }
 
 func buildImageDriverValidator(resource *types.Resource) error {
-	_, _, err := commonValidator(resource)
+	_, target, err := commonValidator(resource)
 
 	if err != nil {
 		return err
 	}
 
+	if target.AppName == "" {
+		return fmt.Errorf("for resource '%s': target app_name is missing", resource.Name)
+	}
+
+	driverConfig := &BuildDriverConfig{}
+
+	err = mapstructure.Decode(resource.Config, driverConfig)
+
+	if err != nil {
+		return fmt.Errorf("for resource '%s': error parsing config: %w", resource.Name, err)
+	}
+
 	return nil
 }
 
 func pushImageDriverValidator(resource *types.Resource) error {
-	_, _, err := commonValidator(resource)
+	_, target, err := commonValidator(resource)
 
 	if err != nil {
 		return err
 	}
 
+	if target.AppName == "" {
+		return fmt.Errorf("for resource '%s': target app_name is missing", resource.Name)
+	}
+
+	driverConfig := &PushDriverConfig{}
+
+	err = mapstructure.Decode(resource.Config, driverConfig)
+
+	if err != nil {
+		return fmt.Errorf("for resource '%s': error parsing config: %w", resource.Name, err)
+	}
+
 	return nil
 }
 
 func updateConfigDriverValidator(resource *types.Resource) error {
-	_, _, err := commonValidator(resource)
+	_, target, err := commonValidator(resource)
 
 	if err != nil {
 		return err
 	}
 
-	return nil
-}
+	if target.AppName == "" {
+		return fmt.Errorf("for resource '%s': target app_name is missing", resource.Name)
+	}
 
-func randomStringDriverValidator(resource *types.Resource) error {
-	_, _, err := commonValidator(resource)
+	driverConfig := &UpdateConfigDriverConfig{}
+
+	err = mapstructure.Decode(resource.Config, driverConfig)
 
 	if err != nil {
-		return err
+		return fmt.Errorf("for resource '%s': error parsing config: %w", resource.Name, err)
 	}
 
+	return nil
+}
+
+func randomStringDriverValidator(resource *types.Resource) error {
 	driverConfig := &RandomStringDriverConfig{}
 
-	err = mapstructure.Decode(resource.Config, driverConfig)
+	err := mapstructure.Decode(resource.Config, driverConfig)
 
 	if err != nil {
-		return fmt.Errorf("error parsing config for resource '%s': %w", resource.Name, err)
+		return fmt.Errorf("for resource '%s': error parsing config: %w", resource.Name, err)
 	}
 
 	return nil
 }
 
 func envGroupDriverValidator(resource *types.Resource) error {
-	_, _, err := commonValidator(resource)
+	target := &Target{}
+
+	err := mapstructure.Decode(resource.Target, target)
 
 	if err != nil {
-		return err
+		return fmt.Errorf("for resource '%s': error parsing target: %w", resource.Name, err)
+	}
+
+	config := &EnvGroupDriverConfig{}
+
+	err = mapstructure.Decode(resource.Config, config)
+
+	if err != nil {
+		return fmt.Errorf("for resource '%s': error parsing config: %w", resource.Name, err)
 	}
 
 	return nil

+ 71 - 1
internal/integrations/preview/utils.go

@@ -1,5 +1,7 @@
 package preview
 
+import "github.com/porter-dev/porter/api/types"
+
 type Source struct {
 	Name          string
 	Repo          string
@@ -16,6 +18,74 @@ type Target struct {
 }
 
 type RandomStringDriverConfig struct {
-	Length int
+	Length uint
 	Lower  bool
 }
+
+type EnvGroupDriverConfig struct {
+	EnvGroups []*types.EnvGroup `mapstructure:"env_groups"`
+}
+
+type UpdateConfigDriverConfig struct {
+	WaitForJob bool
+
+	// If set to true, this does not run an update, it only creates the initial application and job,
+	// skipping subsequent updates
+	OnlyCreate bool
+
+	UpdateConfig struct {
+		Image string
+		Tag   string
+	} `mapstructure:"update_config"`
+
+	EnvGroups []types.EnvGroupMeta `mapstructure:"env_groups"`
+
+	Values map[string]interface{}
+}
+
+type PushDriverConfig struct {
+	Push struct {
+		UsePackCache bool `mapstructure:"use_pack_cache"`
+		Image        string
+	}
+}
+
+type BuildDriverConfig struct {
+	Build struct {
+		UsePackCache bool `mapstructure:"use_pack_cache"`
+		Method       string
+		Context      string
+		Dockerfile   string
+		Builder      string
+		Buildpacks   []string
+		Image        string
+		Env          map[string]string
+	}
+
+	EnvGroups []types.EnvGroupMeta `mapstructure:"env_groups"`
+
+	Values map[string]interface{}
+}
+
+type ApplicationConfig struct {
+	WaitForJob bool
+
+	// If set to true, this does not run an update, it only creates the initial application and job,
+	// skipping subsequent updates
+	OnlyCreate bool
+
+	Build struct {
+		UseCache   bool `mapstructure:"use_cache"`
+		Method     string
+		Context    string
+		Dockerfile string
+		Image      string
+		Builder    string
+		Buildpacks []string
+		Env        map[string]string
+	}
+
+	EnvGroups []types.EnvGroupMeta `mapstructure:"env_groups"`
+
+	Values map[string]interface{}
+}

+ 1 - 1
internal/integrations/preview/validate.go

@@ -45,7 +45,7 @@ func Validate(contents string) []error {
 				errors = append(errors, err)
 			}
 		} else {
-			errors = append(errors, fmt.Errorf("%w: %s", ErrUnsupportedDriver, res.Driver))
+			errors = append(errors, fmt.Errorf("for resource '%s': %w: %s", res.Name, ErrUnsupportedDriver, res.Driver))
 		}
 	}