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 configgen: escape any invalid HCL syntax in map keys into v1.9 #35847

Merged
merged 1 commit into from
Oct 15, 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
10 changes: 2 additions & 8 deletions internal/genconfig/generate_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ package genconfig
import (
"encoding/json"
"fmt"
"regexp"
"sort"
"strings"

"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hclsyntax"
"github.com/hashicorp/hcl/v2/hclwrite"
"github.com/zclconf/go-cty/cty"
ctyjson "github.com/zclconf/go-cty/cty/json"
Expand All @@ -20,12 +20,6 @@ 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 @@ -592,7 +586,7 @@ func ctyCollectionValues(val cty.Value) []cty.Value {
func hclEscapeString(str string) string {
// TODO: Replace this with more complete HCL logic instead of the simple
// go workaround.
if whitespace.MatchString(str) {
if !hclsyntax.ValidIdentifier(str) {
return fmt.Sprintf("%q", str)
}
return str
Expand Down
88 changes: 88 additions & 0 deletions internal/genconfig/generate_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -686,6 +686,94 @@ resource "tfcoremock_sensitive_values" "values" {
value = "underscores"
}
}
}`,
},
"simple_map_with_periods_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.periods": cty.StringVal("periods"),
"key_with_underscores": cty.StringVal("underscores"),
}),
}),
expected: `resource "testing_resource" "resource" {
map = {
"key.with.periods" = "periods"
key_with_underscores = "underscores"
}
}`,
},
"nested_map_with_periods_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.periods": cty.ObjectVal(map[string]cty.Value{
"value": cty.StringVal("periods"),
}),
"key_with_underscores": cty.ObjectVal(map[string]cty.Value{
"value": cty.StringVal("underscores"),
}),
}),
}),
expected: `resource "testing_resource" "resource" {
map = {
"key.with.periods" = {
value = "periods"
}
key_with_underscores = {
value = "underscores"
}
}
}`,
},
}
Expand Down
Loading