Repository navigation
fix(terraform): write nested maps and lists of maps as valid HCL varfiles #3686
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| package hcl | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "fmt" | ||
| "os" | ||
| "sort" | ||
|
|
||
| "github.com/hashicorp/hcl/v2/hclsyntax" | ||
| "github.com/hashicorp/hcl/v2/hclwrite" | ||
| ctyjson "github.com/zclconf/go-cty/cty/json" | ||
|
|
||
| "github.com/cloudposse/atmos/pkg/perf" | ||
| ) | ||
|
|
||
| // EncodeAttributes renders the top-level keys of data as HCL attributes, the | ||
| // format Terraform expects in .tfvars and -backend-config files. Nested maps | ||
| // become object values and lists, including lists of maps, become tuples. | ||
| // Keys are written in sorted order. | ||
| func EncodeAttributes(data any) ([]byte, error) { | ||
| defer perf.Track(nil, "hcl.EncodeAttributes")() | ||
|
|
||
| j, err := json.Marshal(data) | ||
| if err != nil { | ||
| return nil, fmt.Errorf(errWrapFmt, ErrHCLEncodeFailed, err) | ||
| } | ||
|
|
||
| ty, err := ctyjson.ImpliedType(j) | ||
| if err != nil { | ||
| return nil, fmt.Errorf(errWrapFmt, ErrHCLEncodeFailed, err) | ||
| } | ||
| if !ty.IsObjectType() { | ||
| return nil, fmt.Errorf("%w: HCL attributes can only be generated from a map, got %s", ErrHCLEncodeFailed, ty.FriendlyName()) | ||
| } | ||
|
|
||
| value, err := ctyjson.Unmarshal(j, ty) | ||
| if err != nil { | ||
| return nil, fmt.Errorf(errWrapFmt, ErrHCLEncodeFailed, err) | ||
| } | ||
|
|
||
| names := make([]string, 0, len(ty.AttributeTypes())) | ||
| for name := range ty.AttributeTypes() { | ||
| names = append(names, name) | ||
| } | ||
| sort.Strings(names) | ||
|
|
||
| file := hclwrite.NewEmptyFile() | ||
| body := file.Body() | ||
| for _, name := range names { | ||
| if !hclsyntax.ValidIdentifier(name) { | ||
| return nil, fmt.Errorf("%w: invalid attribute name %q", ErrHCLEncodeFailed, name) | ||
| } | ||
| body.SetAttributeValue(name, value.GetAttr(name)) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Quote the first object key when it is For
|
||
| } | ||
|
|
||
| return hclwrite.Format(file.Bytes()), nil | ||
| } | ||
|
|
||
| // WriteAttributesFile encodes data with EncodeAttributes and writes it to | ||
| // path, replacing any previous content. | ||
| func WriteAttributesFile(path string, data any, mode os.FileMode) error { | ||
| defer perf.Track(nil, "hcl.WriteAttributesFile")() | ||
|
|
||
| content, err := EncodeAttributes(data) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if err := os.WriteFile(path, content, mode); err != nil { | ||
| return fmt.Errorf(errWrapFmt, ErrHCLWriteFile, err) | ||
| } | ||
| return nil | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| package hcl | ||
|
|
||
| import ( | ||
| "os" | ||
| "path/filepath" | ||
| "testing" | ||
|
|
||
| hcl2 "github.com/hashicorp/hcl/v2" | ||
| "github.com/hashicorp/hcl/v2/hclsyntax" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| "github.com/zclconf/go-cty/cty" | ||
| ) | ||
|
|
||
| // parseTfvars parses generated content the same way Terraform reads a .tfvars file. | ||
| func parseTfvars(t *testing.T, content []byte) map[string]cty.Value { | ||
| t.Helper() | ||
|
|
||
| file, diags := hclsyntax.ParseConfig(content, "test.tfvars", hcl2.InitialPos) | ||
| require.False(t, diags.HasErrors(), "generated HCL does not parse: %s\n%s", diags.Error(), content) | ||
|
|
||
| attrs, diags := file.Body.JustAttributes() | ||
| require.False(t, diags.HasErrors(), "generated HCL is not a flat list of attributes: %s\n%s", diags.Error(), content) | ||
|
|
||
| values := make(map[string]cty.Value, len(attrs)) | ||
| for name, attr := range attrs { | ||
| value, diags := attr.Expr.Value(nil) | ||
| require.False(t, diags.HasErrors(), "attribute %q: %s", name, diags.Error()) | ||
| values[name] = value | ||
| } | ||
| return values | ||
| } | ||
|
|
||
| func TestEncodeAttributesNestedValues(t *testing.T) { | ||
| content, err := EncodeAttributes(map[string]any{ | ||
| "enabled": true, | ||
| "mymap": map[string]any{ | ||
| "somekey": map[string]any{ | ||
| "somevalue": true, | ||
| }, | ||
| }, | ||
| "subnets": []any{ | ||
| map[string]any{"name": "a", "cidr": "10.0.0.0/24"}, | ||
| map[string]any{"name": "b", "cidr": "10.0.1.0/24"}, | ||
| }, | ||
| "tags": map[string]any{"Name": "vpc", "kubernetes.io/role/elb": "1"}, | ||
| "template": "${not_interpolated}", | ||
| "ratio": 0.123456789, | ||
| "nothing": nil, | ||
| }) | ||
| require.NoError(t, err) | ||
| values := parseTfvars(t, content) | ||
|
|
||
| assert.Equal(t, cty.True, values["enabled"]) | ||
| assert.Equal(t, cty.True, values["mymap"].GetAttr("somekey").GetAttr("somevalue")) | ||
|
|
||
| subnets := values["subnets"] | ||
| require.Equal(t, 2, subnets.LengthInt()) | ||
| assert.Equal(t, "a", subnets.Index(cty.NumberIntVal(0)).GetAttr("name").AsString()) | ||
| assert.Equal(t, "10.0.1.0/24", subnets.Index(cty.NumberIntVal(1)).GetAttr("cidr").AsString()) | ||
|
|
||
| assert.Equal(t, "1", values["tags"].GetAttr("kubernetes.io/role/elb").AsString()) | ||
| assert.Equal(t, "${not_interpolated}", values["template"].AsString()) | ||
| assert.True(t, values["ratio"].Equals(cty.NumberFloatVal(0.123456789)).True()) | ||
| assert.True(t, values["nothing"].IsNull()) | ||
| } | ||
|
|
||
| func TestEncodeAttributesSortsKeys(t *testing.T) { | ||
| content, err := EncodeAttributes(map[string]any{"b": 1, "a": "x"}) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, "a = \"x\"\nb = 1\n", string(content)) | ||
| } | ||
|
|
||
| func TestEncodeAttributesErrors(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| data any | ||
| }{ | ||
| {name: "list", data: []any{"a", "b"}}, | ||
| {name: "scalar", data: "a"}, | ||
| {name: "unsupported value", data: map[string]any{"ch": make(chan int)}}, | ||
| {name: "invalid attribute name", data: map[string]any{"bad.key": "x"}}, | ||
| {name: "attribute name with a space", data: map[string]any{"bad key": "x"}}, | ||
| } | ||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| _, err := EncodeAttributes(tt.data) | ||
| assert.ErrorIs(t, err, ErrHCLEncodeFailed) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestWriteAttributesFileTruncatesExistingFile(t *testing.T) { | ||
| path := filepath.Join(t.TempDir(), "test.tfvars") | ||
|
|
||
| require.NoError(t, WriteAttributesFile(path, map[string]any{ | ||
| "enabled": true, | ||
| "environment": "ue2", | ||
| "description": "a long description that makes the first file noticeably longer", | ||
| }, 0o644)) | ||
|
|
||
| require.NoError(t, WriteAttributesFile(path, map[string]any{"enabled": false}, 0o644)) | ||
|
|
||
| got, err := os.ReadFile(path) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, "enabled = false\n", string(got)) | ||
| } | ||
|
|
||
| func TestWriteAttributesFileErrors(t *testing.T) { | ||
| dir := t.TempDir() | ||
|
|
||
| err := WriteAttributesFile(filepath.Join(dir, "missing", "test.tfvars"), map[string]any{"a": 1}, 0o644) | ||
| assert.ErrorIs(t, err, ErrHCLWriteFile) | ||
|
|
||
| err = WriteAttributesFile(filepath.Join(dir, "test.tfvars"), []any{1}, 0o644) | ||
| assert.ErrorIs(t, err, ErrHCLEncodeFailed) | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.