Merge pull request #17551 from hashicorp/paultyng-patch-1

Export a const for validation methods
This commit is contained in:
Paul Tyng 2018-03-12 19:06:04 -04:00 committed by GitHub
commit 9442c4b46e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 8 deletions

View File

@ -18,6 +18,11 @@ func UniqueId() string {
return PrefixedUniqueId(UniqueIdPrefix) 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 // 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 // After the prefix, the ID consists of an incrementing 26 digit value (to match

View File

@ -15,8 +15,6 @@ func TestUniqueId(t *testing.T) {
return rest[:18], rest[18:] return rest[:18], rest[18:]
} }
const prefix = "terraform-"
iterations := 10000 iterations := 10000
ids := make(map[string]struct{}) ids := make(map[string]struct{})
var id, lastId string var id, lastId string
@ -27,14 +25,14 @@ func TestUniqueId(t *testing.T) {
t.Fatalf("Got duplicated id! %s", id) 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) 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) t.Fatalf("PrefixedUniqueId is out of sync with UniqueIDSuffixLength, post-prefix part has wrong length! %s", rest)
} }
timestamp, increment := split(rest) timestamp, increment := split(rest)
@ -58,8 +56,8 @@ func TestUniqueId(t *testing.T) {
id1 := UniqueId() id1 := UniqueId()
time.Sleep(time.Millisecond) time.Sleep(time.Millisecond)
id2 := UniqueId() id2 := UniqueId()
timestamp1, _ := split(strings.TrimPrefix(id1, prefix)) timestamp1, _ := split(strings.TrimPrefix(id1, UniqueIdPrefix))
timestamp2, _ := split(strings.TrimPrefix(id2, prefix)) timestamp2, _ := split(strings.TrimPrefix(id2, UniqueIdPrefix))
if timestamp1 == timestamp2 { if timestamp1 == timestamp2 {
t.Fatalf("Timestamp part should update at least once a millisecond %s %s", t.Fatalf("Timestamp part should update at least once a millisecond %s %s",