Skip to content

Commit

Permalink
Include generate blocks and remote_state.generate to read_terragrunt_…
Browse files Browse the repository at this point in the history
…config
  • Loading branch information
yorinasub17 committed Feb 18, 2020
1 parent d9e2543 commit bfeab4c
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 13 deletions.
9 changes: 5 additions & 4 deletions codegen/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
11 changes: 7 additions & 4 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions config/config_as_cty.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions config/config_as_cty_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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",
},
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand Down
12 changes: 7 additions & 5 deletions remote/remote_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
Expand Down
3 changes: 3 additions & 0 deletions test/fixture-read-config/full/main.tf
Original file line number Diff line number Diff line change
@@ -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 }

Expand Down
14 changes: 14 additions & 0 deletions test/fixture-read-config/full/source.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,22 @@ locals {
the_answer = 42
}

generate "provider" {
path = "provider.tf"
if_exists = "overwrite_terragrunt"
contents = <<EOF
provider "aws" {
region = "us-east-1"
}
EOF
}

remote_state {
backend = "local"
generate = {
path = "backend.tf"
if_exists = "overwrite_terragrunt"
}
config = {
path = "foo.tfstate"
}
Expand Down
1 change: 1 addition & 0 deletions test/fixture-read-config/full/terragrunt.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ locals {

inputs = {
localstg = local.config.locals
generate = local.config.generate
remote_state = local.config.remote_state
terraformtg = local.config.terraform
dependencies = local.config.dependencies
Expand Down
18 changes: 18 additions & 0 deletions test/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2324,6 +2324,23 @@ func TestReadTerragruntConfigFull(t *testing.T) {
"paths": []interface{}{"../module-a"},
},
)
generateOut := map[string]interface{}{}
require.NoError(t, json.Unmarshal([]byte(outputs["generate"].Value.(string)), &generateOut))
assert.Equal(
t,
generateOut,
map[string]interface{}{
"provider": map[string]interface{}{
"path": "provider.tf",
"if_exists": "overwrite_terragrunt",
"comment_prefix": "# ",
"contents": `provider "aws" {
region = "us-east-1"
}
`,
},
},
)
remoteStateOut := map[string]interface{}{}
require.NoError(t, json.Unmarshal([]byte(outputs["remote_state"].Value.(string)), &remoteStateOut))
assert.Equal(
Expand All @@ -2332,6 +2349,7 @@ func TestReadTerragruntConfigFull(t *testing.T) {
map[string]interface{}{
"backend": "local",
"disable_init": false,
"generate": map[string]interface{}{"path": "backend.tf", "if_exists": "overwrite_terragrunt"},
"config": map[string]interface{}{"path": "foo.tfstate"},
},
)
Expand Down

0 comments on commit bfeab4c

Please sign in to comment.