From 804c091fdb7623cae80616c18fd10f5c5a98f66e Mon Sep 17 00:00:00 2001 From: Thomas De Meyer Date: Tue, 10 Sep 2024 15:43:01 +0200 Subject: [PATCH 1/3] feat: added global and site-level variables --- .../unreleased/Added-20240910-154131.yaml | 4 ++ go.mod | 1 + go.sum | 4 ++ internal/cmd/generate_test.go | 15 +++++ .../expected/main/test-1/main.tf | 50 +++++++++++++++ .../cases/generate/with-variables/main.yaml | 40 ++++++++++++ internal/config/global.go | 4 ++ internal/config/parse_test.go | 16 ++--- internal/config/schemas/schema-1.yaml | 62 ++++++++++++++++++- internal/config/site.go | 4 ++ internal/config/variable/base.go | 14 +++++ internal/config/variable/base_test.go | 55 ++++++++++++++++ internal/config/variable/scalar.go | 8 +-- internal/config/variable/scalar_test.go | 22 +++---- internal/generator/component.go | 24 ++++--- internal/generator/hcl_test.go | 24 +++---- internal/graph/dependency_test.go | 2 +- 17 files changed, 301 insertions(+), 48 deletions(-) create mode 100644 .changes/unreleased/Added-20240910-154131.yaml create mode 100755 internal/cmd/testdata/cases/generate/with-variables/expected/main/test-1/main.tf create mode 100644 internal/cmd/testdata/cases/generate/with-variables/main.yaml create mode 100644 internal/config/variable/base_test.go diff --git a/.changes/unreleased/Added-20240910-154131.yaml b/.changes/unreleased/Added-20240910-154131.yaml new file mode 100644 index 00000000..fa5af875 --- /dev/null +++ b/.changes/unreleased/Added-20240910-154131.yaml @@ -0,0 +1,4 @@ +kind: Added +body: Added option to set global and site-level configurations. These will be merged + into the component variables during generation +time: 2024-09-10T15:41:31.371358062+02:00 diff --git a/go.mod b/go.mod index 88b5ec39..ea7281b1 100644 --- a/go.mod +++ b/go.mod @@ -24,6 +24,7 @@ require ( github.com/mitchellh/mapstructure v1.5.0 github.com/olekukonko/tablewriter v0.0.5 github.com/rs/zerolog v1.33.0 + github.com/santhosh-tekuri/jsonschema/v6 v6.0.1 github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966 github.com/spf13/afero v1.11.0 github.com/spf13/cobra v1.8.1 diff --git a/go.sum b/go.sum index 5fc429bb..16a7317e 100644 --- a/go.sum +++ b/go.sum @@ -243,6 +243,8 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM= github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/dlclark/regexp2 v1.11.0 h1:G/nrcoOa7ZXlpoa/91N3X7mM3r8eIlMBBJZvsz/mxKI= +github.com/dlclark/regexp2 v1.11.0/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8= github.com/dominikbraun/graph v0.23.0 h1:TdZB4pPqCLFxYhdyMFb1TBdFxp8XLcJfTTBQucVPgCo= github.com/dominikbraun/graph v0.23.0/go.mod h1:yOjYyogZLY1LSG9E33JWZJiq5k83Qy2C6POAuiViluc= github.com/elazarl/goproxy v0.0.0-20230808193330-2592e75ae04a h1:mATvB/9r/3gvcejNsXKSkQ6lcIaNec2nyfOdlTBR2lU= @@ -510,6 +512,8 @@ github.com/sagikazarmark/locafero v0.4.0 h1:HApY1R9zGo4DBgr7dqsTH/JJxLTTsOt7u6ke github.com/sagikazarmark/locafero v0.4.0/go.mod h1:Pe1W6UlPYUk/+wc/6KFhbORCfqzgYEpgQ3O5fPuL3H4= github.com/sagikazarmark/slog-shim v0.1.0 h1:diDBnUNK9N/354PgrxMywXnAwEr1QZcOr6gto+ugjYE= github.com/sagikazarmark/slog-shim v0.1.0/go.mod h1:SrcSrq8aKtyuqEI1uvTDTK1arOWRIczQRv+GVI1AkeQ= +github.com/santhosh-tekuri/jsonschema/v6 v6.0.1 h1:PKK9DyHxif4LZo+uQSgXNqs0jj5+xZwwfKHgph2lxBw= +github.com/santhosh-tekuri/jsonschema/v6 v6.0.1/go.mod h1:JXeL+ps8p7/KNMjDQk3TCwPpBy0wYklyWTfbkIzdIFU= github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 h1:n661drycOFuPLCN3Uc8sB6B/s6Z4t2xvBgU1htSHuq8= github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3/go.mod h1:A0bzQcvG0E7Rwjx0REVgAGH58e96+X0MeOfepqsbeW4= github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0= diff --git a/internal/cmd/generate_test.go b/internal/cmd/generate_test.go index 2cb36002..258e6eb5 100644 --- a/internal/cmd/generate_test.go +++ b/internal/cmd/generate_test.go @@ -154,3 +154,18 @@ func (s *GenerateTestSuite) TestGenerateWithAlias() { assert.NoError(s.T(), err) assert.NoError(s.T(), CompareDirectories(path.Join(workdir, "deployments"), path.Join(workdir, "expected"))) } + +func (s *GenerateTestSuite) TestGenerateWithVariables() { + pwd, _ := os.Getwd() + workdir := path.Join(pwd, "testdata/cases/generate/with-variables") + + cmd := RootCmd + cmd.SetArgs([]string{ + "generate", + "--output-path", path.Join(workdir, "deployments"), + "--file", path.Join(workdir, "main.yaml"), + }) + err := cmd.Execute() + assert.NoError(s.T(), err) + assert.NoError(s.T(), CompareDirectories(path.Join(workdir, "deployments"), path.Join(workdir, "expected"))) +} diff --git a/internal/cmd/testdata/cases/generate/with-variables/expected/main/test-1/main.tf b/internal/cmd/testdata/cases/generate/with-variables/expected/main/test-1/main.tf new file mode 100755 index 00000000..9493479e --- /dev/null +++ b/internal/cmd/testdata/cases/generate/with-variables/expected/main/test-1/main.tf @@ -0,0 +1,50 @@ +# This file is auto-generated by MACH composer +# site: test-1 +terraform { + backend "local" { + path = "./states/test-1.tfstate" + } + required_providers { + aws = { + version = "~> 3.74.1" + } + } +} + +# File sources +# Resources +# Configuring AWS +provider "aws" { + region = "eu-west-1" +} + +locals { + tags = { + Site = "test-1" + Environment = "test" + } +} + +# Component: component +module "component" { + source = "/home/thomas/Projects/mach-composer/mach-composer-cli/internal/cmd/testdata/modules/application" + variables = { + component_variable = "component_variable_value" + global_variable = "global_variable_value" + site_variable = "site_variable_value" + } + secrets = {} + component_version = "test" + environment = "test" + site = "test-1" + tags = local.tags + providers = { + aws = aws, + } +} + +output "component" { + description = "The module outputs for component" + sensitive = true + value = module.component +} diff --git a/internal/cmd/testdata/cases/generate/with-variables/main.yaml b/internal/cmd/testdata/cases/generate/with-variables/main.yaml new file mode 100644 index 00000000..65dd4ba2 --- /dev/null +++ b/internal/cmd/testdata/cases/generate/with-variables/main.yaml @@ -0,0 +1,40 @@ +mach_composer: + version: 1 + plugins: + aws: + source: mach-composer/aws + version: 0.1.0 + +global: + cloud: "aws" + environment: test + terraform_config: + remote_state: + plugin: local + path: ./states + variables: + global_variable: "global_variable_value" + site_variable: "overridden_site_variable_value" + component_variable: "overridden_component_variable_value" + +sites: + - identifier: test-1 + variables: + site_variable: "site_variable_value" + component_variable: "overridden_component_variable_value" + aws: + account_id: "12345" + region: eu-west-1 + components: + - name: component + variables: + component_variable: "component_variable_value" + +components: + - name: component + source: ./testdata/modules/application + version: "test" + branch: main + integrations: + - aws + diff --git a/internal/config/global.go b/internal/config/global.go index 7bc65be4..04bd95fb 100644 --- a/internal/config/global.go +++ b/internal/config/global.go @@ -3,6 +3,7 @@ package config import ( "fmt" "github.com/mach-composer/mach-composer-cli/internal/cli" + "github.com/mach-composer/mach-composer-cli/internal/config/variable" "github.com/mach-composer/mach-composer-cli/internal/utils" "gopkg.in/yaml.v3" ) @@ -12,6 +13,9 @@ type GlobalConfig struct { Cloud string `yaml:"cloud"` TerraformStateProvider string `yaml:"-"` TerraformConfig *TerraformConfig `yaml:"terraform_config"` + + Variables variable.VariablesMap `yaml:"variables"` + Secrets variable.VariablesMap `yaml:"secrets"` } type TerraformConfig struct { diff --git a/internal/config/parse_test.go b/internal/config/parse_test.go index 40898d5b..5c1f7b99 100644 --- a/internal/config/parse_test.go +++ b/internal/config/parse_test.go @@ -61,12 +61,12 @@ func TestOpenBasic(t *testing.T) { Name: "your-component", Deployment: &Deployment{Type: DeploymentSite}, Variables: variable.VariablesMap{ - "FOO_VAR": variable.MustCreateNewScalarVariable(t, "my-value"), - "BAR_VAR": variable.MustCreateNewScalarVariable(t, "${var.foo}"), - "MULTIPLE_VARS": variable.MustCreateNewScalarVariable(t, "${var.foo.bar} ${var.bar.foo}"), + "FOO_VAR": variable.MustCreateNewScalarVariable("my-value"), + "BAR_VAR": variable.MustCreateNewScalarVariable("${var.foo}"), + "MULTIPLE_VARS": variable.MustCreateNewScalarVariable("${var.foo.bar} ${var.bar.foo}"), }, Secrets: variable.VariablesMap{ - "MY_SECRET": variable.MustCreateNewScalarVariable(t, "secretvalue"), + "MY_SECRET": variable.MustCreateNewScalarVariable("secretvalue"), }, Definition: &component, }, @@ -145,12 +145,12 @@ func TestOpenComplex(t *testing.T) { Name: "your-component", Deployment: &Deployment{Type: DeploymentSite}, Variables: variable.VariablesMap{ - "FOO_VAR": variable.MustCreateNewScalarVariable(t, "my-value"), - "BAR_VAR": variable.MustCreateNewScalarVariable(t, "${var.foo}"), - "MULTIPLE_VARS": variable.MustCreateNewScalarVariable(t, "${var.foo.bar} ${var.bar.foo}"), + "FOO_VAR": variable.MustCreateNewScalarVariable("my-value"), + "BAR_VAR": variable.MustCreateNewScalarVariable("${var.foo}"), + "MULTIPLE_VARS": variable.MustCreateNewScalarVariable("${var.foo.bar} ${var.bar.foo}"), }, Secrets: variable.VariablesMap{ - "MY_SECRET": variable.MustCreateNewScalarVariable(t, "secretvalue"), + "MY_SECRET": variable.MustCreateNewScalarVariable("secretvalue"), }, Definition: &component, }, diff --git a/internal/config/schemas/schema-1.yaml b/internal/config/schemas/schema-1.yaml index b65dfed9..5b59a328 100644 --- a/internal/config/schemas/schema-1.yaml +++ b/internal/config/schemas/schema-1.yaml @@ -83,6 +83,12 @@ definitions: $ref: "#/definitions/TerraformConfig" cloud: type: string + variables: + $ref: "#/definitions/MachComposerVariables" + description: Global variables. These will be merged with the site specific variables, where the site variables will take precedence + secrets: + $ref: "#/definitions/MachComposerSecrets" + description: Global secrets. These will be merged with the site specific secrets, where the site secrets will take precedence TerraformConfig: type: object @@ -137,6 +143,12 @@ definitions: - $ref: "#/definitions/SiteEndpointConfig" deployment: $ref: "#/definitions/MachComposerDeployment" + variables: + $ref: "#/definitions/MachComposerVariables" + description: Site specific variables. These will be merged with the component variables, where the component variables will take precedence + secrets: + $ref: "#/definitions/MachComposerSecrets" + description: Site specific secrets. These will be merged with the component secrets, where the component secrets will take precedence components: type: array items: @@ -163,9 +175,9 @@ definitions: name: type: string variables: - type: object + $ref: "#/definitions/MachComposerVariables" secrets: - type: object + $ref: "#/definitions/MachComposerSecrets" store_variables: description: Commercetools store specific variables deprecationMessage: The `store_variables` configuration is deprecated @@ -227,6 +239,52 @@ definitions: "[a-zA-Z0-9]+": type: string + MachComposerVariables: + description: | + Variables are used to configure the components. They are passed to the terraform module as a terraform + variable with the name `variables` containing an object with the same fields: + + ```yaml + components: + - name: my-component + variables: + my_string_field: "my string value" + my_number_field: 42 + ``` + + ``` + variable "variables" { + type = object({ + my_string_field = string + my_number_field = number + }) + } + ``` + type: object + + MachComposerSecrets: + description: | + Secrets are used to configure the components. They are passed to the terraform module as a terraform + variable with the name `secrets` containing an object with the same fields: + + ```yaml + components: + - name: my-component + secrets: + my_string_field: "my string value" + my_number_field: 42 + ``` + + ``` + variable "secrets" { + type = object({ + my_string_field = string + my_number_field = number + }) + } + ``` + type: object + MachComposerDeployment: type: object description: | diff --git a/internal/config/site.go b/internal/config/site.go index 89511710..f839f348 100644 --- a/internal/config/site.go +++ b/internal/config/site.go @@ -2,6 +2,7 @@ package config import ( "fmt" + "github.com/mach-composer/mach-composer-cli/internal/config/variable" "github.com/rs/zerolog/log" "github.com/elliotchance/pie/v2" @@ -29,6 +30,9 @@ type SiteConfig struct { Deployment *Deployment `yaml:"deployment"` RawEndpoints map[string]any `yaml:"endpoints"` + Variables variable.VariablesMap `yaml:"variables"` + Secrets variable.VariablesMap `yaml:"secrets"` + Components SiteComponentConfigs `yaml:"components"` } diff --git a/internal/config/variable/base.go b/internal/config/variable/base.go index 67a5a3d0..2dab8cbe 100644 --- a/internal/config/variable/base.go +++ b/internal/config/variable/base.go @@ -16,6 +16,20 @@ const ( type VariablesMap map[string]Variable +// MergeVariablesMaps merges multiple VariablesMap into a single VariablesMap. The last map will override the previous +// ones. Only the keys on the first level are merged. +func MergeVariablesMaps(maps ...VariablesMap) VariablesMap { + var result = make(VariablesMap) + + for _, m := range maps { + for key, val := range m { + result[key] = val + } + } + + return result +} + type TransformValueFunc func(value any) (any, error) type Variable interface { diff --git a/internal/config/variable/base_test.go b/internal/config/variable/base_test.go new file mode 100644 index 00000000..ef3d34e5 --- /dev/null +++ b/internal/config/variable/base_test.go @@ -0,0 +1,55 @@ +package variable + +import ( + "github.com/stretchr/testify/assert" + "testing" +) + +func TestMergeVariablesMaps(t *testing.T) { + tests := []struct { + name string + maps []VariablesMap + expected VariablesMap + }{ + { + name: "merge two maps with different keys", + maps: []VariablesMap{ + {"key1": MustCreateNewScalarVariable("value1")}, + {"key2": MustCreateNewScalarVariable("value2")}, + }, + expected: VariablesMap{ + "key1": MustCreateNewScalarVariable("value1"), + "key2": MustCreateNewScalarVariable("value2"), + }, + }, + { + name: "merge two maps with same keys", + maps: []VariablesMap{ + {"key1": MustCreateNewScalarVariable("value1")}, + {"key1": MustCreateNewScalarVariable("value2")}, + }, + expected: VariablesMap{ + "key1": MustCreateNewScalarVariable("value2"), + }, + }, + { + name: "merge three maps with same keys", + maps: []VariablesMap{ + {"key1": MustCreateNewScalarVariable("value1")}, + {"key2": MustCreateNewScalarVariable("value2")}, + {"key1": MustCreateNewScalarVariable("value3")}, + }, + expected: VariablesMap{ + "key1": MustCreateNewScalarVariable("value3"), + "key2": MustCreateNewScalarVariable("value2"), + }, + }, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + result := MergeVariablesMaps(tc.maps...) + assert.Equal(t, tc.expected, result) + }) + } +} diff --git a/internal/config/variable/scalar.go b/internal/config/variable/scalar.go index 33be2025..773f2050 100644 --- a/internal/config/variable/scalar.go +++ b/internal/config/variable/scalar.go @@ -3,11 +3,9 @@ package variable import ( "fmt" "github.com/mach-composer/mach-composer-cli/internal/state" - "github.com/stretchr/testify/require" "path" "regexp" "strings" - "testing" ) var varComponentRegex = regexp.MustCompile(`\${(component(?:\.[^}]+)+)}`) @@ -135,8 +133,10 @@ func RemoteStateTransformFunc(repository *state.Repository, siteIdentifier strin } } -func MustCreateNewScalarVariable(t *testing.T, value any) *ScalarVariable { +func MustCreateNewScalarVariable(value any) *ScalarVariable { v, err := NewScalarVariable(value) - require.NoError(t, err) + if err != nil { + panic(err) + } return v } diff --git a/internal/config/variable/scalar_test.go b/internal/config/variable/scalar_test.go index c926123a..5ff15804 100644 --- a/internal/config/variable/scalar_test.go +++ b/internal/config/variable/scalar_test.go @@ -106,7 +106,7 @@ func TestInterpolateComponentVarsSuccess(t *testing.T) { { Description: "Simple test case", Input: &VariablesMap{ - "key": MustCreateNewScalarVariable(t, "${component.foobar.var}"), + "key": MustCreateNewScalarVariable("${component.foobar.var}"), }, Expected: map[string]any{"key": "${module.foobar.var}"}, Refs: []string{"foobar"}, @@ -114,7 +114,7 @@ func TestInterpolateComponentVarsSuccess(t *testing.T) { { Description: "Scalar test case", Input: &VariablesMap{ - "key": MustCreateNewScalarVariable(t, "-> ${component.foobar.var} <-"), + "key": MustCreateNewScalarVariable("-> ${component.foobar.var} <-"), }, Expected: map[string]any{"key": "-> ${module.foobar.var} <-"}, Refs: []string{"foobar"}, @@ -122,21 +122,21 @@ func TestInterpolateComponentVarsSuccess(t *testing.T) { { Description: "Integer test case", Input: &VariablesMap{ - "key": MustCreateNewScalarVariable(t, 123), + "key": MustCreateNewScalarVariable(123), }, Expected: map[string]any{"key": 123}, }, { Description: "Boolean test case", Input: &VariablesMap{ - "key": MustCreateNewScalarVariable(t, true), + "key": MustCreateNewScalarVariable(true), }, Expected: map[string]any{"key": true}, }, { Description: "Float test case", Input: &VariablesMap{ - "key": MustCreateNewScalarVariable(t, 1.1), + "key": MustCreateNewScalarVariable(1.1), }, Expected: map[string]any{"key": 1.1}, }, @@ -145,8 +145,8 @@ func TestInterpolateComponentVarsSuccess(t *testing.T) { Input: &VariablesMap{ "key": &MapVariable{ Elements: map[string]Variable{ - "key-1": MustCreateNewScalarVariable(t, "${component.foobar.var}"), - "key-2": MustCreateNewScalarVariable(t, "-> ${component.foobar.var} - ${component.foobar.bar}<-"), + "key-1": MustCreateNewScalarVariable("${component.foobar.var}"), + "key-2": MustCreateNewScalarVariable("-> ${component.foobar.var} - ${component.foobar.bar}<-"), }, }, }, @@ -163,8 +163,8 @@ func TestInterpolateComponentVarsSuccess(t *testing.T) { Input: &VariablesMap{ "key": &SliceVariable{ Elements: []Variable{ - MustCreateNewScalarVariable(t, `${component.site-topgeschenken.service_link_map["topbloemen-nl"]}`), - MustCreateNewScalarVariable(t, `${component.site-topgeschenken.service_link_map["topbloemen-be"]}`), + MustCreateNewScalarVariable(`${component.site-topgeschenken.service_link_map["topbloemen-nl"]}`), + MustCreateNewScalarVariable(`${component.site-topgeschenken.service_link_map["topbloemen-be"]}`), }, }, }, @@ -182,12 +182,12 @@ func TestInterpolateComponentVarsSuccess(t *testing.T) { Elements: []Variable{ &MapVariable{ Elements: map[string]Variable{ - "key": MustCreateNewScalarVariable(t, `${component.site-topgeschenken.service_link_map["topbloemen-nl"]}`), + "key": MustCreateNewScalarVariable(`${component.site-topgeschenken.service_link_map["topbloemen-nl"]}`), }, }, &MapVariable{ Elements: map[string]Variable{ - "key": MustCreateNewScalarVariable(t, `${component.site-topgeschenken.service_link_map["topbloemen-be"]}`), + "key": MustCreateNewScalarVariable(`${component.site-topgeschenken.service_link_map["topbloemen-be"]}`), }, }, }, diff --git a/internal/generator/component.go b/internal/generator/component.go index 4b7654b4..8c4d521b 100644 --- a/internal/generator/component.go +++ b/internal/generator/component.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "github.com/mach-composer/mach-composer-cli/internal/config" + "github.com/mach-composer/mach-composer-cli/internal/config/variable" "github.com/mach-composer/mach-composer-cli/internal/graph" "github.com/mach-composer/mach-composer-cli/internal/utils" "runtime" @@ -181,15 +182,18 @@ func renderComponentModule(_ context.Context, cfg *config.MachConfig, n *graph.S tc.ComponentSecrets = "secrets = {}" } - if len(n.SiteComponentConfig.Variables) > 0 { - val, err := serializeToHCL("variables", n.SiteComponentConfig.Variables, n.SiteComponentConfig.Deployment.Type, cfg.StateRepository, n.SiteConfig.Identifier) + var variablesMap = variable.MergeVariablesMaps(n.ProjectConfig.Global.Variables, n.SiteConfig.Variables, n.SiteComponentConfig.Variables) + if len(variablesMap) > 0 { + val, err := serializeToHCL("variables", variablesMap, n.SiteComponentConfig.Deployment.Type, cfg.StateRepository, n.SiteConfig.Identifier) if err != nil { return "", err } tc.ComponentVariables = val } - if len(n.SiteComponentConfig.Secrets) > 0 { - val, err := serializeToHCL("secrets", n.SiteComponentConfig.Secrets, n.SiteComponentConfig.Deployment.Type, cfg.StateRepository, n.SiteConfig.Identifier) + + var secretsMap = variable.MergeVariablesMaps(n.ProjectConfig.Global.Secrets, n.SiteConfig.Secrets, n.SiteComponentConfig.Secrets) + if len(secretsMap) > 0 { + val, err := serializeToHCL("secrets", secretsMap, n.SiteComponentConfig.Deployment.Type, cfg.StateRepository, n.SiteConfig.Identifier) if err != nil { return "", err } @@ -201,12 +205,12 @@ func renderComponentModule(_ context.Context, cfg *config.MachConfig, n *graph.S return "", err } - // Escape backslashes in paths (Windows path separator) - if runtime.GOOS == "windows" { - tc.Source = strings.Replace(vs, "\\", "\\\\", -1) - } else { - tc.Source = vs - } + // Escape backslashes in paths (Windows path separator) + if runtime.GOOS == "windows" { + tc.Source = strings.Replace(vs, "\\", "\\\\", -1) + } else { + tc.Source = vs + } val, err := utils.RenderGoTemplate(string(tpl), tc) if err != nil { diff --git a/internal/generator/hcl_test.go b/internal/generator/hcl_test.go index 30b6eb5f..0a2e6a1d 100644 --- a/internal/generator/hcl_test.go +++ b/internal/generator/hcl_test.go @@ -18,42 +18,42 @@ func TestSerializeToHCL(t *testing.T) { tests := []test{ { input: variable.VariablesMap{ - "foo": variable.MustCreateNewScalarVariable(t, "bar"), + "foo": variable.MustCreateNewScalarVariable("bar"), }, output: "variables = {\n foo = \"bar\"\n}\n", deploymentType: config.DeploymentSite, }, { input: variable.VariablesMap{ - "foo": variable.MustCreateNewScalarVariable(t, "c:\\foo\\bar"), + "foo": variable.MustCreateNewScalarVariable("c:\\foo\\bar"), }, output: "variables = {\n foo = \"c:\\\\foo\\\\bar\"\n}\n", deploymentType: config.DeploymentSite, }, { input: variable.VariablesMap{ - "foo": variable.MustCreateNewScalarVariable(t, 1), + "foo": variable.MustCreateNewScalarVariable(1), }, output: "variables = {\n foo = 1\n}\n", deploymentType: config.DeploymentSite, }, { input: variable.VariablesMap{ - "foo": variable.MustCreateNewScalarVariable(t, 1.5), + "foo": variable.MustCreateNewScalarVariable(1.5), }, output: "variables = {\n foo = 1.5\n}\n", deploymentType: config.DeploymentSite, }, { input: variable.VariablesMap{ - "foo": variable.MustCreateNewScalarVariable(t, true), + "foo": variable.MustCreateNewScalarVariable(true), }, output: "variables = {\n foo = true\n}\n", deploymentType: config.DeploymentSite, }, { input: variable.VariablesMap{ - "foo": variable.MustCreateNewScalarVariable(t, false), + "foo": variable.MustCreateNewScalarVariable(false), }, output: "variables = {\n foo = false\n}\n", deploymentType: config.DeploymentSite, @@ -61,8 +61,8 @@ func TestSerializeToHCL(t *testing.T) { { input: variable.VariablesMap{ "foo": variable.NewSliceVariable([]variable.Variable{ - variable.MustCreateNewScalarVariable(t, "${foo}"), - variable.MustCreateNewScalarVariable(t, "${bar}"), + variable.MustCreateNewScalarVariable("${foo}"), + variable.MustCreateNewScalarVariable("${bar}"), }), }, output: "variables = {\n foo = [foo, bar]\n}\n", @@ -70,7 +70,7 @@ func TestSerializeToHCL(t *testing.T) { }, { input: variable.VariablesMap{ - "foo": variable.MustCreateNewScalarVariable(t, "${data.sops_external.variables.data[\"foo-bar\"]}"), + "foo": variable.MustCreateNewScalarVariable("${data.sops_external.variables.data[\"foo-bar\"]}"), }, output: "variables = {\n foo = data.sops_external.variables.data[\"foo-bar\"]\n}\n", deploymentType: config.DeploymentSite, @@ -78,7 +78,7 @@ func TestSerializeToHCL(t *testing.T) { { input: variable.VariablesMap{ "foo": variable.NewMapVariable(map[string]variable.Variable{ - "63000012": variable.MustCreateNewScalarVariable(t, "foobar"), + "63000012": variable.MustCreateNewScalarVariable("foobar"), }), }, output: "variables = {\n foo = {\n \"63000012\" = \"foobar\"\n }\n}\n", @@ -89,8 +89,8 @@ func TestSerializeToHCL(t *testing.T) { "foo": variable.NewMapVariable(map[string]variable.Variable{ "my-key": variable.NewMapVariable(map[string]variable.Variable{ "my-value": variable.NewSliceVariable([]variable.Variable{ - variable.MustCreateNewScalarVariable(t, "nl-NL"), - variable.MustCreateNewScalarVariable(t, "en-US"), + variable.MustCreateNewScalarVariable("nl-NL"), + variable.MustCreateNewScalarVariable("en-US"), }), }), }), diff --git a/internal/graph/dependency_test.go b/internal/graph/dependency_test.go index 78e70aee..6351d102 100644 --- a/internal/graph/dependency_test.go +++ b/internal/graph/dependency_test.go @@ -170,7 +170,7 @@ func TestToDependencyGraphInferred(t *testing.T) { Type: config.DeploymentSiteComponent, }, Variables: variable.VariablesMap{ - "value-1": variable.MustCreateNewScalarVariable(t, "${component.site-component-1.value-1}"), + "value-1": variable.MustCreateNewScalarVariable("${component.site-component-1.value-1}"), }, }, }, From cabc280684978f19e925aa274c5c4db9441d6726 Mon Sep 17 00:00:00 2001 From: Thomas De Meyer Date: Tue, 10 Sep 2024 15:53:35 +0200 Subject: [PATCH 2/3] feat: added secret to integration test --- .../generate/with-variables/expected/main/test-1/main.tf | 6 +++++- .../cmd/testdata/cases/generate/with-variables/main.yaml | 9 +++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/internal/cmd/testdata/cases/generate/with-variables/expected/main/test-1/main.tf b/internal/cmd/testdata/cases/generate/with-variables/expected/main/test-1/main.tf index 9493479e..2ce8d80b 100755 --- a/internal/cmd/testdata/cases/generate/with-variables/expected/main/test-1/main.tf +++ b/internal/cmd/testdata/cases/generate/with-variables/expected/main/test-1/main.tf @@ -33,7 +33,11 @@ module "component" { global_variable = "global_variable_value" site_variable = "site_variable_value" } - secrets = {} + secrets = { + component_secret = "component_secret_value" + global_secret = "global_secret_value" + site_secret = "site_secret_value" + } component_version = "test" environment = "test" site = "test-1" diff --git a/internal/cmd/testdata/cases/generate/with-variables/main.yaml b/internal/cmd/testdata/cases/generate/with-variables/main.yaml index 65dd4ba2..ec56e30e 100644 --- a/internal/cmd/testdata/cases/generate/with-variables/main.yaml +++ b/internal/cmd/testdata/cases/generate/with-variables/main.yaml @@ -16,12 +16,19 @@ global: global_variable: "global_variable_value" site_variable: "overridden_site_variable_value" component_variable: "overridden_component_variable_value" + secrets: + global_secret: "global_secret_value" + site_secret: "overridden_site_secret_value" + component_secret: "overridden_component_secret_value" sites: - identifier: test-1 variables: site_variable: "site_variable_value" component_variable: "overridden_component_variable_value" + secrets: + site_secret: "site_secret_value" + component_secret: "overridden_component_secret_value" aws: account_id: "12345" region: eu-west-1 @@ -29,6 +36,8 @@ sites: - name: component variables: component_variable: "component_variable_value" + secrets: + component_secret: "component_secret_value" components: - name: component From 38a4bc2cae8e0d23ec91fe6b6e149120c5eb92e7 Mon Sep 17 00:00:00 2001 From: Thomas De Meyer Date: Wed, 11 Sep 2024 07:46:11 +0200 Subject: [PATCH 3/3] feat: replaced path alias --- .../cases/generate/with-variables/expected/main/test-1/main.tf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/cmd/testdata/cases/generate/with-variables/expected/main/test-1/main.tf b/internal/cmd/testdata/cases/generate/with-variables/expected/main/test-1/main.tf index 2ce8d80b..1baf7fa2 100755 --- a/internal/cmd/testdata/cases/generate/with-variables/expected/main/test-1/main.tf +++ b/internal/cmd/testdata/cases/generate/with-variables/expected/main/test-1/main.tf @@ -27,7 +27,7 @@ locals { # Component: component module "component" { - source = "/home/thomas/Projects/mach-composer/mach-composer-cli/internal/cmd/testdata/modules/application" + source = "{{ .PWD }}/testdata/modules/application" variables = { component_variable = "component_variable_value" global_variable = "global_variable_value"