Skip to content
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

Backport of config generation: escape map keys with whitespace into v1.9 #35771

Merged
merged 1 commit into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion internal/genconfig/generate_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package genconfig
import (
"encoding/json"
"fmt"
"regexp"
"sort"
"strings"

Expand All @@ -19,6 +20,12 @@ import (
"github.com/hashicorp/terraform/internal/tfdiags"
)

var (
// whitespace is a regular expression that matches one or more whitespace
// characters.
whitespace = regexp.MustCompile(`\s+`)
)

// GenerateResourceContents generates HCL configuration code for the provided
// resource and state value.
//
Expand Down Expand Up @@ -435,7 +442,7 @@ func writeConfigNestedTypeAttributeFromExisting(addr addrs.AbsResourceInstance,
buf.WriteString(fmt.Sprintf("%s = {\n", name))
for _, key := range keys {
buf.WriteString(strings.Repeat(" ", indent+2))
buf.WriteString(fmt.Sprintf("%s = {", key))
buf.WriteString(fmt.Sprintf("%s = {", hclEscapeString(key)))

// This entire value is marked
if vals[key].IsMarked() {
Expand Down Expand Up @@ -572,3 +579,21 @@ func ctyCollectionValues(val cty.Value) []cty.Value {

return ret
}

// hclEscapeString formats the input string into a format that is safe for
// rendering within HCL.
//
// Note, this function doesn't actually do a very good job of this currently. We
// need to expose some internal functions from HCL in a future version and call
// them from here. For now, just use "%q" formatting.
//
// Note, the similar function in jsonformat/computed/renderers/map.go is doing
// something similar.
func hclEscapeString(str string) string {
// TODO: Replace this with more complete HCL logic instead of the simple
// go workaround.
if whitespace.MatchString(str) {
return fmt.Sprintf("%q", str)
}
return str
}
88 changes: 88 additions & 0 deletions internal/genconfig/generate_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,94 @@ resource "tfcoremock_sensitive_values" "values" {
object = null # sensitive
set = null # sensitive
string = null # sensitive
}`,
},
"simple_map_with_whitespace_in_keys": {
schema: &configschema.Block{
Attributes: map[string]*configschema.Attribute{
"map": {
Type: cty.Map(cty.String),
Optional: true,
},
},
},
addr: addrs.AbsResourceInstance{
Module: addrs.RootModuleInstance,
Resource: addrs.ResourceInstance{
Resource: addrs.Resource{
Mode: addrs.ManagedResourceMode,
Type: "testing_resource",
Name: "resource",
},
Key: addrs.NoKey,
},
},
provider: addrs.LocalProviderConfig{
LocalName: "testing",
},
value: cty.ObjectVal(map[string]cty.Value{
"map": cty.MapVal(map[string]cty.Value{
"key with spaces": cty.StringVal("spaces"),
"key_with_underscores": cty.StringVal("underscores"),
}),
}),
expected: `resource "testing_resource" "resource" {
map = {
"key with spaces" = "spaces"
key_with_underscores = "underscores"
}
}`,
},
"nested_map_with_whitespace_in_keys": {
schema: &configschema.Block{
Attributes: map[string]*configschema.Attribute{
"map": {
NestedType: &configschema.Object{
Attributes: map[string]*configschema.Attribute{
"value": {
Type: cty.String,
Optional: true,
},
},
Nesting: configschema.NestingMap,
},
Optional: true,
},
},
},
addr: addrs.AbsResourceInstance{
Module: addrs.RootModuleInstance,
Resource: addrs.ResourceInstance{
Resource: addrs.Resource{
Mode: addrs.ManagedResourceMode,
Type: "testing_resource",
Name: "resource",
},
Key: addrs.NoKey,
},
},
provider: addrs.LocalProviderConfig{
LocalName: "testing",
},
value: cty.ObjectVal(map[string]cty.Value{
"map": cty.MapVal(map[string]cty.Value{
"key with spaces": cty.ObjectVal(map[string]cty.Value{
"value": cty.StringVal("spaces"),
}),
"key_with_underscores": cty.ObjectVal(map[string]cty.Value{
"value": cty.StringVal("underscores"),
}),
}),
}),
expected: `resource "testing_resource" "resource" {
map = {
"key with spaces" = {
value = "spaces"
}
key_with_underscores = {
value = "underscores"
}
}
}`,
},
}
Expand Down