Skip to content

Commit

Permalink
feat: update aws provider template (#345)
Browse files Browse the repository at this point in the history
add gotemplate function to handle HCL Expression

update aws provider template to use toHCLExpression function
  • Loading branch information
zackhee997 authored Nov 5, 2024
1 parent 4238e54 commit 404c379
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 52 deletions.
22 changes: 5 additions & 17 deletions templates/templates/common/aws_provider.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,18 @@ provider "aws" {
allowed_account_ids = ["{{ .AccountID }}"]
{{- if and .IgnoreTags (and .IgnoreTags.Enabled (deRefBool .IgnoreTags.Enabled)) }}
ignore_tags {
{{ if and .IgnoreTags.Keys (not (eq (len .IgnoreTags.Keys) 0)) }} keys = [
{{- range $k := .IgnoreTags.Keys }}
"{{ $k }}",
{{- if and .IgnoreTags.Keys (not (eq (len .IgnoreTags.Keys) 0)) }}
keys= {{ toHCLExpression .IgnoreTags.Keys }}
{{- end }}
]
{{- end }}
{{ if and .IgnoreTags.KeyPrefixes (not (eq (len .IgnoreTags.KeyPrefixes) 0)) }} key_prefixes = [
{{- range $k := .IgnoreTags.KeyPrefixes }}
"{{ $k }}",
{{- end }}
]
{{- if and .IgnoreTags.KeyPrefixes (not (eq (len .IgnoreTags.KeyPrefixes) 0)) }}
key_prefixes = {{ toHCLExpression .IgnoreTags.KeyPrefixes }}
{{- end }}
}
{{- end }}
{{- if and .DefaultTags (and .DefaultTags.Enabled (deRefBool .DefaultTags.Enabled)) }}
default_tags {
{{- if .DefaultTags.Tags }}
tags = merge(var.tags,{
{{- range $k, $v := .DefaultTags.Tags }}
{{- if $v }}
{{ $k }} = "{{ $v }}"
{{- end }}
{{- end }}
})
tags = merge(var.tags, {{ toHCLExpression .DefaultTags.Tags }})
{{- else }}
tags = var.tags
{{- end }}
Expand Down
5 changes: 3 additions & 2 deletions testdata/v2_aws_default_tags/fogg.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,10 @@ envs:
aws:
default_tags:
tags:
Component: Vox
"Component": Vox
# tags are merged and overwrite
Project: Overwrite
"Project": Overwrite
"hsh:team": "TIES"
modules:
- source: "terraform/modules/my_module"
fred:
Expand Down
9 changes: 3 additions & 6 deletions testdata/v2_aws_default_tags/terraform/envs/bar/corge/fogg.tf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 2 additions & 6 deletions testdata/v2_aws_default_tags/terraform/envs/bar/qux/fogg.tf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 6 additions & 9 deletions testdata/v2_aws_default_tags/terraform/envs/foo/vox/fogg.tf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 3 additions & 6 deletions testdata/v2_aws_ignore_tags/terraform/envs/bar/corge/fogg.tf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 2 additions & 6 deletions testdata/v2_aws_ignore_tags/terraform/envs/bar/qux/fogg.tf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 34 additions & 0 deletions util/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,39 @@ func toHCLAssignment(name string, value any) string {
return string(f.Bytes())
}

// toHCLExpression converts a Go value to its HCL representation as a string.
// It can be used within templates to include the value in expressions or function calls.
//
// tags = merge(var.tags, {{ toHCLExpression .DefaultTags.Tags }})
//
// tags = merge(var.tags, {
// Component = "Vox"
// Env = "Foo"
// })
//
// This is designed to be called from a template.
func toHCLExpression(value any) string {
f := hclwrite.NewEmptyFile()
rootBody := f.Body()

// Since we don't have the cty type information for the value and since it can be arbitrary, we use
// JSON as an intermediate representation.
jsonBytes, err := json.Marshal(value)
if err != nil {
// Swallow errors inside of a template.
return ""
}
var ctyVal ctyjson.SimpleJSONValue
if err := ctyVal.UnmarshalJSON(jsonBytes); err != nil {
// Swallow errors inside of a template.
return ""
}

tokens := hclwrite.TokensForValue(ctyVal.Value)
rootBody.AppendUnstructuredTokens(tokens)
return strings.TrimSpace(string(f.Bytes()))
}

// deRef is a generic function to dereference a pointer to it's actual value type.
//
// This is designed to be called from a template.
Expand All @@ -153,6 +186,7 @@ func OpenTemplate(label string, source io.Reader, templates fs.FS) (*template.Te
funcs["deRefBool"] = deRef[bool]
funcs["toHclBlock"] = toHCLBlock
funcs["toHclAssignment"] = toHCLAssignment
funcs["toHCLExpression"] = toHCLExpression

s, err := io.ReadAll(source)
if err != nil {
Expand Down

0 comments on commit 404c379

Please sign in to comment.