From f18e4f29216758343822ad0fd6e73677e9411f5f Mon Sep 17 00:00:00 2001 From: Paul Tyng Date: Sat, 10 Mar 2018 12:40:37 -0500 Subject: [PATCH 1/3] Export a const for validation methods --- helper/resource/id.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/helper/resource/id.go b/helper/resource/id.go index 1cde67c1a..44949550e 100644 --- a/helper/resource/id.go +++ b/helper/resource/id.go @@ -18,6 +18,11 @@ func UniqueId() string { return PrefixedUniqueId(UniqueIdPrefix) } +// UniqueIDSuffixLength is the string length of the suffix generated by +// PrefixedUniqueId. This can be used by length validation functions to +// ensure prefixes are the correct length for the target field. +const UniqueIDSuffixLength = 26 + // Helper for a resource to generate a unique identifier w/ given prefix // // After the prefix, the ID consists of an incrementing 26 digit value (to match From 707d7febe3ae840c88428ebdb81d9014886f9564 Mon Sep 17 00:00:00 2001 From: Paul Tyng Date: Sat, 10 Mar 2018 20:56:13 -0500 Subject: [PATCH 2/3] Use constants in test --- helper/resource/id_test.go | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/helper/resource/id_test.go b/helper/resource/id_test.go index 7e5f27bfb..26845832e 100644 --- a/helper/resource/id_test.go +++ b/helper/resource/id_test.go @@ -15,8 +15,6 @@ func TestUniqueId(t *testing.T) { return rest[:18], rest[18:] } - const prefix = "terraform-" - iterations := 10000 ids := make(map[string]struct{}) var id, lastId string @@ -27,13 +25,13 @@ func TestUniqueId(t *testing.T) { t.Fatalf("Got duplicated id! %s", id) } - if !strings.HasPrefix(id, prefix) { + if !strings.HasPrefix(id, UniqueIdPrefix) { t.Fatalf("Unique ID didn't have terraform- prefix! %s", id) } - rest := strings.TrimPrefix(id, prefix) + rest := strings.TrimPrefix(id, UniqueIdPrefix) - if len(rest) != 26 { + if len(rest) != UniqueIDSuffixLength { t.Fatalf("Post-prefix part has wrong length! %s", rest) } @@ -58,8 +56,8 @@ func TestUniqueId(t *testing.T) { id1 := UniqueId() time.Sleep(time.Millisecond) id2 := UniqueId() - timestamp1, _ := split(strings.TrimPrefix(id1, prefix)) - timestamp2, _ := split(strings.TrimPrefix(id2, prefix)) + timestamp1, _ := split(strings.TrimPrefix(id1, UniqueIdPrefix)) + timestamp2, _ := split(strings.TrimPrefix(id2, UniqueIdPrefix)) if timestamp1 == timestamp2 { t.Fatalf("Timestamp part should update at least once a millisecond %s %s", From 528cbecfcef98e0d4440c6a1eafeba72f953a731 Mon Sep 17 00:00:00 2001 From: Paul Tyng Date: Sat, 10 Mar 2018 21:53:54 -0500 Subject: [PATCH 3/3] Make failure message more explicit --- helper/resource/id_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/helper/resource/id_test.go b/helper/resource/id_test.go index 26845832e..f1560dab1 100644 --- a/helper/resource/id_test.go +++ b/helper/resource/id_test.go @@ -32,7 +32,7 @@ func TestUniqueId(t *testing.T) { rest := strings.TrimPrefix(id, UniqueIdPrefix) if len(rest) != UniqueIDSuffixLength { - t.Fatalf("Post-prefix part has wrong length! %s", rest) + t.Fatalf("PrefixedUniqueId is out of sync with UniqueIDSuffixLength, post-prefix part has wrong length! %s", rest) } timestamp, increment := split(rest)