From bfeab4c859bce09c913544ffdd020beb371a8754 Mon Sep 17 00:00:00 2001 From: Yoriyasu Yano <430092+yorinasub17@users.noreply.github.com> Date: Tue, 18 Feb 2020 11:05:27 -0800 Subject: [PATCH] Include generate blocks and remote_state.generate to read_terragrunt_config --- codegen/generate.go | 9 +++++---- config/config.go | 11 +++++++---- config/config_as_cty.go | 14 ++++++++++++++ config/config_as_cty_test.go | 20 ++++++++++++++++++++ remote/remote_state.go | 12 +++++++----- test/fixture-read-config/full/main.tf | 3 +++ test/fixture-read-config/full/source.hcl | 14 ++++++++++++++ test/fixture-read-config/full/terragrunt.hcl | 1 + test/integration_test.go | 18 ++++++++++++++++++ 9 files changed, 89 insertions(+), 13 deletions(-) diff --git a/codegen/generate.go b/codegen/generate.go index 528eca8b92..ddb0fcb677 100644 --- a/codegen/generate.go +++ b/codegen/generate.go @@ -46,10 +46,11 @@ const ( // Configuration for generating code type GenerateConfig struct { - Path string + Path string `cty:"path"` IfExists GenerateConfigExists - CommentPrefix string - Contents string + IfExistsStr string `cty:"if_exists"` + CommentPrefix string `cty:"comment_prefix"` + Contents string `cty:"contents"` } // WriteToFile will generate a new file at the given target path with the given contents. If a file already exists at @@ -75,7 +76,7 @@ func WriteToFile(logger *log.Logger, basePath string, config GenerateConfig) err } // Add the signature as a prefix to the file - contentsToWrite := fmt.Sprintf("%s%s\n%s", DefaultCommentPrefix, TerragruntGeneratedSignature, config.Contents) + contentsToWrite := fmt.Sprintf("%s%s\n%s", config.CommentPrefix, TerragruntGeneratedSignature, config.Contents) if err := ioutil.WriteFile(targetPath, []byte(contentsToWrite), 0644); err != nil { return errors.WithStackTrace(err) diff --git a/config/config.go b/config/config.go index 2d0023147e..36e72a8fae 100644 --- a/config/config.go +++ b/config/config.go @@ -724,11 +724,14 @@ func convertToTerragruntConfig( return nil, err } genConfig := codegen.GenerateConfig{ - Path: block.Path, - IfExists: ifExists, - Contents: block.Contents, + Path: block.Path, + IfExists: ifExists, + IfExistsStr: block.IfExists, + Contents: block.Contents, } - if block.CommentPrefix != nil { + if block.CommentPrefix == nil { + genConfig.CommentPrefix = codegen.DefaultCommentPrefix + } else { genConfig.CommentPrefix = *block.CommentPrefix } terragruntConfig.GenerateConfigs[block.Name] = genConfig diff --git a/config/config_as_cty.go b/config/config_as_cty.go index feec61d0b6..d0ec9fe8d1 100644 --- a/config/config_as_cty.go +++ b/config/config_as_cty.go @@ -59,6 +59,14 @@ func terragruntConfigAsCty(config *TerragruntConfig) (cty.Value, error) { output["dependency"] = dependencyCty } + generateCty, err := gostructToCty(config.GenerateConfigs) + if err != nil { + return cty.NilVal, err + } + if generateCty != cty.NilVal { + output["generate"] = generateCty + } + inputsCty, err := convertToCtyWithJson(config.Inputs) if err != nil { return cty.NilVal, err @@ -124,6 +132,12 @@ func remoteStateAsCty(remoteState *remote.RemoteState) (cty.Value, error) { output["backend"] = gostringToCty(remoteState.Backend) output["disable_init"] = goboolToCty(remoteState.DisableInit) + generateCty, err := gostructToCty(remoteState.Generate) + if err != nil { + return cty.NilVal, err + } + output["generate"] = generateCty + ctyJsonVal, err := convertToCtyWithJson(remoteState.Config) if err != nil { return cty.NilVal, err diff --git a/config/config_as_cty_test.go b/config/config_as_cty_test.go index 6ebd0d9899..32f08ac125 100644 --- a/config/config_as_cty_test.go +++ b/config/config_as_cty_test.go @@ -9,6 +9,7 @@ import ( "github.com/stretchr/testify/require" "github.com/zclconf/go-cty/cty" + "github.com/gruntwork-io/terragrunt/codegen" "github.com/gruntwork-io/terragrunt/remote" ) @@ -75,6 +76,17 @@ func TestTerragruntConfigAsCtyDrift(t *testing.T) { RenderedOutputs: &mockOutputs, }, }, + GenerateConfigs: map[string]codegen.GenerateConfig{ + "provider": codegen.GenerateConfig{ + Path: "foo", + IfExists: codegen.ExistsOverwriteTerragrunt, + IfExistsStr: "overwrite_terragrunt", + CommentPrefix: "# ", + Contents: `terraform { + backend "s3" {} +}`, + }, + }, } ctyVal, err := terragruntConfigAsCty(&testConfig) require.NoError(t, err) @@ -105,6 +117,10 @@ func TestRemoteStateAsCtyDrift(t *testing.T) { testConfig := remote.RemoteState{ Backend: "foo", DisableInit: true, + Generate: &remote.RemoteStateGenerate{ + Path: "foo", + IfExists: "overwrite_terragrunt", + }, Config: map[string]interface{}{ "bar": "baz", }, @@ -172,6 +188,8 @@ func terragruntConfigStructFieldToMapKey(t *testing.T, fieldName string) (string return "locals", true case "TerragruntDependencies": return "dependency", true + case "GenerateConfigs": + return "generate", true case "IsPartial": return "", false default: @@ -187,6 +205,8 @@ func remoteStateStructFieldToMapKey(t *testing.T, fieldName string) (string, boo return "backend", true case "DisableInit": return "disable_init", true + case "Generate": + return "generate", true case "Config": return "config", true default: diff --git a/remote/remote_state.go b/remote/remote_state.go index 8ce7618d88..533352db3a 100644 --- a/remote/remote_state.go +++ b/remote/remote_state.go @@ -24,8 +24,8 @@ func (remoteState *RemoteState) String() string { // Code gen configuration for Terraform remote state type RemoteStateGenerate struct { - Path string - IfExists string + Path string `cty:"path"` + IfExists string `cty:"if_exists"` } type RemoteStateInitializer interface { @@ -193,9 +193,11 @@ func (remoteState *RemoteState) GenerateTerraformCode(terragruntOptions *options return err } codegenConfig := codegen.GenerateConfig{ - Path: remoteState.Generate.Path, - IfExists: ifExistsEnum, - Contents: string(configBytes), + Path: remoteState.Generate.Path, + IfExists: ifExistsEnum, + IfExistsStr: remoteState.Generate.IfExists, + Contents: string(configBytes), + CommentPrefix: codegen.DefaultCommentPrefix, } return codegen.WriteToFile(terragruntOptions.Logger, terragruntOptions.WorkingDir, codegenConfig) } diff --git a/test/fixture-read-config/full/main.tf b/test/fixture-read-config/full/main.tf index 4d366c0a3c..6a5e900d83 100644 --- a/test/fixture-read-config/full/main.tf +++ b/test/fixture-read-config/full/main.tf @@ -1,6 +1,9 @@ variable "localstg" {} output "localstg" { value = var.localstg } +variable "generate" {} +output "generate" { value = var.generate } + variable "remote_state" {} output "remote_state" { value = var.remote_state } diff --git a/test/fixture-read-config/full/source.hcl b/test/fixture-read-config/full/source.hcl index 75ec9b6b2b..a2673087d7 100644 --- a/test/fixture-read-config/full/source.hcl +++ b/test/fixture-read-config/full/source.hcl @@ -2,8 +2,22 @@ locals { the_answer = 42 } +generate "provider" { + path = "provider.tf" + if_exists = "overwrite_terragrunt" + contents = <