Skip to content

Commit

Permalink
internal/naming: Fix NamePrefixFromName with hex suffix. (#14475)
Browse files Browse the repository at this point in the history
github.com/hashicorp/terraform-plugin-sdk/helper/resource.PrefixedUniqueId
generates names with a suffix containing hex digits, whereas the naming
package was assuming these were purely decimal. This caused the
name_prefix property not to be set in the statefile for some resources
(eg aws_security_group) when the generated name included hex digits a-f.
  • Loading branch information
alext authored Aug 5, 2020
1 parent a939275 commit 8bf9166
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
2 changes: 1 addition & 1 deletion aws/internal/naming/naming.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/terraform"
)

var resourceUniqueIDSuffixRegexpPattern = fmt.Sprintf("\\d{%d}$", resource.UniqueIDSuffixLength)
var resourceUniqueIDSuffixRegexpPattern = fmt.Sprintf("[[:xdigit:]]{%d}$", resource.UniqueIDSuffixLength)
var resourceUniqueIDSuffixRegexp = regexp.MustCompile(resourceUniqueIDSuffixRegexpPattern)

var resourceUniqueIDRegexpPattern = resourcePrefixedUniqueIDRegexpPattern(resource.UniqueIdPrefix)
Expand Down
31 changes: 31 additions & 0 deletions aws/internal/naming/naming_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,21 @@ func TestHasResourceUniqueIdSuffix(t *testing.T) {
Input: "test-20060102150405000000000001",
Expected: true,
},
{
TestName: "correct suffix with hex, incorrect prefix",
Input: "test-200601021504050000000000a1",
Expected: true,
},
{
TestName: "correct suffix, correct prefix",
Input: "terraform-20060102150405000000000001",
Expected: true,
},
{
TestName: "correct suffix with hex, correct prefix",
Input: "terraform-2006010215040500000000000a",
Expected: true,
},
}

for _, testCase := range testCases {
Expand Down Expand Up @@ -152,6 +162,11 @@ func TestNamePrefixFromName(t *testing.T) {
Input: "test-20060102150405000000000001",
Expected: strPtr("test-"),
},
{
TestName: "correct prefix with hyphen, correct suffix with hex",
Input: "test-200601021504050000000000f1",
Expected: strPtr("test-"),
},
{
TestName: "incorrect prefix, correct suffix",
Input: "terraform-20060102150405000000000001",
Expand All @@ -177,4 +192,20 @@ func TestNamePrefixFromName(t *testing.T) {
}
})
}

t.Run("extracting prefix from generated name", func(t *testing.T) {
for i := 0; i < 10; i++ {
prefix := "test-"
input := Generate("", prefix)
got := NamePrefixFromName(input)

if got == nil {
t.Errorf("run%d: got nil, expected %s for input %s", i, prefix, input)
}

if got != nil && prefix != *got {
t.Errorf("run%d: got %s, expected %s for input %s", i, *got, prefix, input)
}
}
})
}

0 comments on commit 8bf9166

Please sign in to comment.