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

4701 aws_service_discovery_private_dns_namespace name length limit #4702

Merged
merged 1 commit into from
Jun 2, 2018
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
11 changes: 10 additions & 1 deletion aws/resource_aws_service_discovery_private_dns_namespace.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package aws

import (
"crypto/sha1"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We actually do not need to perform any hashing as resource.PrefixedUniqueId() already includes a mutex with a monotonic counter to ensure it is unique even during concurrent operations with the same prefix:

https://github.com/hashicorp/terraform/blob/ad259b2b9569cadd4a24f051e271fa5e669066f2/helper/resource/id.go#L35-L45

We just need to trim the input string 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a lot simpler. Why even bother with a prefix based on the name if it is irrelevant for uniqueness? Why not use "" or "terraform" or "private_dns_namespace"?

Thanks for merging and cleaning up

"encoding/hex"
"fmt"
"time"

Expand Down Expand Up @@ -47,7 +49,14 @@ func resourceAwsServiceDiscoveryPrivateDnsNamespace() *schema.Resource {
func resourceAwsServiceDiscoveryPrivateDnsNamespaceCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).sdconn

requestId := resource.PrefixedUniqueId(fmt.Sprintf("tf-%s", d.Get("name").(string)))
// The CreatorRequestId has a limit of 64 bytes; roughly 30 bytes are used for timestamp and prefix.
name := d.Get("name").(string)
if len(name) > 32 {
hash := sha1.Sum([]byte(name))
name = hex.EncodeToString(hash[:])[0:32]
}

requestId := resource.PrefixedUniqueId(fmt.Sprintf("tf-%s", name))
input := &servicediscovery.CreatePrivateDnsNamespaceInput{
Name: aws.String(d.Get("name").(string)),
Vpc: aws.String(d.Get("vpc").(string)),
Expand Down
18 changes: 18 additions & 0 deletions aws/resource_aws_service_discovery_private_dns_namespace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,24 @@ func TestAccAWSServiceDiscoveryPrivateDnsNamespace_basic(t *testing.T) {
})
}

func TestAccAWSServiceDiscoveryPrivateDnsNamespace_longname(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAwsServiceDiscoveryPrivateDnsNamespaceDestroy,
Steps: []resource.TestStep{
{
Config: testAccServiceDiscoveryPrivateDnsNamespaceConfig(acctest.RandString(8) + "." + acctest.RandString(8) + "." + acctest.RandString(8) + "." + acctest.RandString(8)),
Check: resource.ComposeTestCheckFunc(
testAccCheckAwsServiceDiscoveryPrivateDnsNamespaceExists("aws_service_discovery_private_dns_namespace.test"),
resource.TestCheckResourceAttrSet("aws_service_discovery_private_dns_namespace.test", "arn"),
resource.TestCheckResourceAttrSet("aws_service_discovery_private_dns_namespace.test", "hosted_zone"),
),
},
},
})
}

func testAccCheckAwsServiceDiscoveryPrivateDnsNamespaceDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).sdconn

Expand Down