diff --git a/helper/resource/id.go b/helper/resource/id.go index 1cde67c1aa..44949550e7 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 diff --git a/helper/resource/id_test.go b/helper/resource/id_test.go index 7e5f27bfb7..f1560dab13 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,14 +25,14 @@ 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 { - t.Fatalf("Post-prefix part has wrong length! %s", rest) + if len(rest) != UniqueIDSuffixLength { + t.Fatalf("PrefixedUniqueId is out of sync with UniqueIDSuffixLength, post-prefix part has wrong length! %s", rest) } timestamp, increment := split(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",