diff --git a/tooling/templatize/cmd/rolloutoptions.go b/tooling/templatize/cmd/rolloutoptions.go index bed870bf5..a8757e63a 100644 --- a/tooling/templatize/cmd/rolloutoptions.go +++ b/tooling/templatize/cmd/rolloutoptions.go @@ -93,7 +93,7 @@ func (o *ValidatedRolloutOptions) Complete() (*RolloutOptions, error) { return nil, err } - variables, err := completed.ConfigProvider.GetVariables(o.Cloud, o.DeployEnv, o.Region, config.NewConfigEvaluationContext(o.Region, o.RegionShort, o.Stamp)) + variables, err := completed.ConfigProvider.GetVariables(o.Cloud, o.DeployEnv, o.Region, config.NewConfigReplacements(o.Region, o.RegionShort, o.Stamp)) if err != nil { return nil, fmt.Errorf("failed to get variables: %w", err) } diff --git a/tooling/templatize/cmd/run/options.go b/tooling/templatize/cmd/run/options.go index 3fe4c438d..d33d16cdd 100644 --- a/tooling/templatize/cmd/run/options.go +++ b/tooling/templatize/cmd/run/options.go @@ -106,7 +106,7 @@ func (o *RunOptions) RunPipeline(ctx context.Context) error { o.RolloutOptions.Cloud, o.RolloutOptions.DeployEnv, o.RolloutOptions.Region, - config.NewConfigEvaluationContext( + config.NewConfigReplacements( o.RolloutOptions.Region, o.RolloutOptions.RegionShort, o.RolloutOptions.Stamp, diff --git a/tooling/templatize/pkg/config/config.go b/tooling/templatize/pkg/config/config.go index 7e8bbce34..58eafdcca 100644 --- a/tooling/templatize/pkg/config/config.go +++ b/tooling/templatize/pkg/config/config.go @@ -10,44 +10,40 @@ import ( "gopkg.in/yaml.v3" ) -func DefaultConfigEvaluationContext() *ConfigEvaluationContext { - return &ConfigEvaluationContext{ - Region: "", - RegionShort: "", - Stamp: "", - } +func DefaultConfigReplacements() *ConfigReplacements { + return NewConfigReplacements("", "", "") } -func NewConfigEvaluationContext(region, regionShort, stamp string) *ConfigEvaluationContext { - return &ConfigEvaluationContext{ - Region: region, - RegionShort: regionShort, - Stamp: stamp, +func NewConfigReplacements(regionReplacement, regionShortReplacement, stampReplacement string) *ConfigReplacements { + return &ConfigReplacements{ + RegionReplacement: regionReplacement, + RegionShortReplacement: regionShortReplacement, + StampReplacement: stampReplacement, } } -type ConfigEvaluationContext struct { - Region string - RegionShort string - Stamp string +type ConfigReplacements struct { + RegionReplacement string + RegionShortReplacement string + StampReplacement string } -func (c *ConfigEvaluationContext) AsMap() map[string]interface{} { +func (c *ConfigReplacements) AsMap() map[string]interface{} { return map[string]interface{}{ "ctx": map[string]interface{}{ - "region": c.Region, - "regionShort": c.RegionShort, - "stamp": c.Stamp, + "region": c.RegionReplacement, + "regionShort": c.RegionShortReplacement, + "stamp": c.StampReplacement, }, } } type ConfigProvider interface { Validate(cloud, deployEnv string) error - GetVariables(cloud, deployEnv, region string, configEvalContext *ConfigEvaluationContext) (Variables, error) - GetDeployEnvVariables(cloud, deployEnv, region string, configEvalContext *ConfigEvaluationContext) (Variables, error) + GetVariables(cloud, deployEnv, region string, configReplacements *ConfigReplacements) (Variables, error) + GetDeployEnvVariables(cloud, deployEnv string, configReplacements *ConfigReplacements) (Variables, error) GetRegions(cloud, deployEnv string) ([]string, error) - GetRegionOverrides(cloud, deployEnv, region string, configEvalContext *ConfigEvaluationContext) (Variables, error) + GetRegionOverrides(cloud, deployEnv, region string, configReplacements *ConfigReplacements) (Variables, error) } func NewConfigProvider(config string) ConfigProvider { @@ -93,14 +89,14 @@ func mergeVariables(base, override Variables) Variables { return base } -func (cp *configProviderImpl) GetVariables(cloud, deployEnv, region string, configEvalContext *ConfigEvaluationContext) (Variables, error) { - variables, err := cp.GetDeployEnvVariables(cloud, deployEnv, region, configEvalContext) +func (cp *configProviderImpl) GetVariables(cloud, deployEnv, region string, configReplacements *ConfigReplacements) (Variables, error) { + variables, err := cp.GetDeployEnvVariables(cloud, deployEnv, configReplacements) if err != nil { return nil, err } // region overrides - regionOverrides, err := cp.GetRegionOverrides(cloud, deployEnv, region, configEvalContext) + regionOverrides, err := cp.GetRegionOverrides(cloud, deployEnv, region, configReplacements) if err != nil { return nil, err } @@ -110,7 +106,7 @@ func (cp *configProviderImpl) GetVariables(cloud, deployEnv, region string, conf } func (cp *configProviderImpl) Validate(cloud, deployEnv string) error { - config, err := cp.loadConfig(DefaultConfigEvaluationContext()) + config, err := cp.loadConfig(DefaultConfigReplacements()) if err != nil { return err } @@ -124,8 +120,8 @@ func (cp *configProviderImpl) Validate(cloud, deployEnv string) error { return nil } -func (cp *configProviderImpl) GetDeployEnvVariables(cloud, deployEnv, region string, configEvalContext *ConfigEvaluationContext) (Variables, error) { - config, err := cp.loadConfig(configEvalContext) +func (cp *configProviderImpl) GetDeployEnvVariables(cloud, deployEnv string, configReplacements *ConfigReplacements) (Variables, error) { + config, err := cp.loadConfig(configReplacements) if err != nil { return nil, err } @@ -143,7 +139,7 @@ func (cp *configProviderImpl) GetDeployEnvVariables(cloud, deployEnv, region str } func (cp *configProviderImpl) GetRegions(cloud, deployEnv string) ([]string, error) { - config, err := cp.loadConfig(DefaultConfigEvaluationContext()) + config, err := cp.loadConfig(DefaultConfigReplacements()) if err != nil { return nil, err } @@ -155,18 +151,18 @@ func (cp *configProviderImpl) GetRegions(cloud, deployEnv string) ([]string, err return regions, nil } -func (cp *configProviderImpl) GetRegionOverrides(cloud, deployEnv, region string, configEvalContext *ConfigEvaluationContext) (Variables, error) { - config, err := cp.loadConfig(configEvalContext) +func (cp *configProviderImpl) GetRegionOverrides(cloud, deployEnv, region string, configReplacements *ConfigReplacements) (Variables, error) { + config, err := cp.loadConfig(configReplacements) if err != nil { return nil, err } return config.GetRegionOverrides(cloud, deployEnv, region), nil } -func (cp *configProviderImpl) loadConfig(configEvalContext *ConfigEvaluationContext) (VariableOverrides, error) { +func (cp *configProviderImpl) loadConfig(configReplacements *ConfigReplacements) (VariableOverrides, error) { // TODO validate that field names are unique regardless of casing // parse, execute and unmarshal the config file as a template to generate the final config file - bytes, err := PreprocessFile(cp.config, configEvalContext.AsMap()) + bytes, err := PreprocessFile(cp.config, configReplacements.AsMap()) if err != nil { return nil, err } diff --git a/tooling/templatize/pkg/config/config_test.go b/tooling/templatize/pkg/config/config_test.go index 762b5fa45..14827ccb0 100644 --- a/tooling/templatize/pkg/config/config_test.go +++ b/tooling/templatize/pkg/config/config_test.go @@ -14,7 +14,7 @@ func TestConfigProvider(t *testing.T) { configProvider := NewConfigProvider("../../testdata/config.yaml") - variables, err := configProvider.GetVariables("public", "int", region, NewConfigEvaluationContext(region, regionShort, stamp)) + variables, err := configProvider.GetVariables("public", "int", region, NewConfigReplacements(region, regionShort, stamp)) assert.NoError(t, err) assert.NotNil(t, variables) diff --git a/tooling/templatize/pkg/ev2/utils.go b/tooling/templatize/pkg/ev2/utils.go index cbd319ea6..6d55d3506 100644 --- a/tooling/templatize/pkg/ev2/utils.go +++ b/tooling/templatize/pkg/ev2/utils.go @@ -10,12 +10,12 @@ import ( // This package contains helper functions to extract EV2 conformant data from a config.yaml file. // -func newEv2EvaluationContext() *config.ConfigEvaluationContext { - return &config.ConfigEvaluationContext{ - Region: "$location()", - RegionShort: "$(regionShortName)", - Stamp: "$stamp()", - } +func newEv2ConfigReplacements() *config.ConfigReplacements { + return config.NewConfigReplacements( + "$location()", + "$(regionShortName)", + "$stamp()", + ) } // GetNonRegionalServiceConfigVariables returns all non-regional configuration variables of a config.yaml file. @@ -23,7 +23,7 @@ func newEv2EvaluationContext() *config.ConfigEvaluationContext { // The variable values are formatted to contain EV2 $location(), $stamp() and $(serviceConfigVar) variables. // This function is useful to get the variables to fill the `Settings` section of an EV2 `ServiceConfig.json“ func GetNonRegionalServiceConfigVariables(configProvider config.ConfigProvider, cloud, deployEnv string) (config.Variables, error) { - return configProvider.GetVariables(cloud, deployEnv, "", newEv2EvaluationContext()) + return configProvider.GetVariables(cloud, deployEnv, "", newEv2ConfigReplacements()) } // GetRegionalServiceConfigVariableOverrides returns the regional overrides of a config.yaml file. @@ -36,7 +36,7 @@ func GetRegionalServiceConfigVariableOverrides(configProvider config.ConfigProvi } overrides := make(map[string]config.Variables) for _, region := range regions { - regionOverrides, err := configProvider.GetRegionOverrides(cloud, deployEnv, region, newEv2EvaluationContext()) + regionOverrides, err := configProvider.GetRegionOverrides(cloud, deployEnv, region, newEv2ConfigReplacements()) if err != nil { return nil, err } @@ -49,7 +49,7 @@ func GetRegionalServiceConfigVariableOverrides(configProvider config.ConfigProvi // It uses the provided configProvider to fetch the variables, flattens them into a __VAR__ = $config(var) formatted map. // This function is useful to get the find/replace pairs for an EV2 `ScopeBinding.json` func ScopeBindingVariables(configProvider config.ConfigProvider, cloud, deployEnv string) (map[string]string, error) { - vars, err := configProvider.GetVariables(cloud, deployEnv, "", newEv2EvaluationContext()) + vars, err := configProvider.GetVariables(cloud, deployEnv, "", newEv2ConfigReplacements()) if err != nil { return nil, err } @@ -65,7 +65,7 @@ func ScopeBindingVariables(configProvider config.ConfigProvider, cloud, deployEn // while maintaining EV2 conformant system variables. // This function is useful to process a pipeline.yaml file so that it contains EV2 system variables. func PreprocessFileForEV2(configProvider config.ConfigProvider, cloud, deployEnv string, templateFile string) ([]byte, error) { - vars, err := configProvider.GetVariables(cloud, deployEnv, "", newEv2EvaluationContext()) + vars, err := configProvider.GetVariables(cloud, deployEnv, "", newEv2ConfigReplacements()) if err != nil { return nil, err }