diff options
Diffstat (limited to 'vendor/github.com/hashicorp/terraform/helper/resource/id.go')
-rw-r--r-- | vendor/github.com/hashicorp/terraform/helper/resource/id.go | 33 |
1 files changed, 17 insertions, 16 deletions
diff --git a/vendor/github.com/hashicorp/terraform/helper/resource/id.go b/vendor/github.com/hashicorp/terraform/helper/resource/id.go index 629582b..1cde67c 100644 --- a/vendor/github.com/hashicorp/terraform/helper/resource/id.go +++ b/vendor/github.com/hashicorp/terraform/helper/resource/id.go | |||
@@ -1,21 +1,17 @@ | |||
1 | package resource | 1 | package resource |
2 | 2 | ||
3 | import ( | 3 | import ( |
4 | "crypto/rand" | ||
5 | "fmt" | 4 | "fmt" |
6 | "math/big" | 5 | "strings" |
7 | "sync" | 6 | "sync" |
7 | "time" | ||
8 | ) | 8 | ) |
9 | 9 | ||
10 | const UniqueIdPrefix = `terraform-` | 10 | const UniqueIdPrefix = `terraform-` |
11 | 11 | ||
12 | // idCounter is a randomly seeded monotonic counter for generating ordered | 12 | // idCounter is a monotonic counter for generating ordered unique ids. |
13 | // unique ids. It uses a big.Int so we can easily increment a long numeric | ||
14 | // string. The max possible hex value here with 12 random bytes is | ||
15 | // "01000000000000000000000000", so there's no chance of rollover during | ||
16 | // operation. | ||
17 | var idMutex sync.Mutex | 13 | var idMutex sync.Mutex |
18 | var idCounter = big.NewInt(0).SetBytes(randomBytes(12)) | 14 | var idCounter uint32 |
19 | 15 | ||
20 | // Helper for a resource to generate a unique identifier w/ default prefix | 16 | // Helper for a resource to generate a unique identifier w/ default prefix |
21 | func UniqueId() string { | 17 | func UniqueId() string { |
@@ -25,15 +21,20 @@ func UniqueId() string { | |||
25 | // Helper for a resource to generate a unique identifier w/ given prefix | 21 | // Helper for a resource to generate a unique identifier w/ given prefix |
26 | // | 22 | // |
27 | // After the prefix, the ID consists of an incrementing 26 digit value (to match | 23 | // After the prefix, the ID consists of an incrementing 26 digit value (to match |
28 | // previous timestamp output). | 24 | // previous timestamp output). After the prefix, the ID consists of a timestamp |
25 | // and an incrementing 8 hex digit value The timestamp means that multiple IDs | ||
26 | // created with the same prefix will sort in the order of their creation, even | ||
27 | // across multiple terraform executions, as long as the clock is not turned back | ||
28 | // between calls, and as long as any given terraform execution generates fewer | ||
29 | // than 4 billion IDs. | ||
29 | func PrefixedUniqueId(prefix string) string { | 30 | func PrefixedUniqueId(prefix string) string { |
31 | // Be precise to 4 digits of fractional seconds, but remove the dot before the | ||
32 | // fractional seconds. | ||
33 | timestamp := strings.Replace( | ||
34 | time.Now().UTC().Format("20060102150405.0000"), ".", "", 1) | ||
35 | |||
30 | idMutex.Lock() | 36 | idMutex.Lock() |
31 | defer idMutex.Unlock() | 37 | defer idMutex.Unlock() |
32 | return fmt.Sprintf("%s%026x", prefix, idCounter.Add(idCounter, big.NewInt(1))) | 38 | idCounter++ |
33 | } | 39 | return fmt.Sprintf("%s%s%08x", prefix, timestamp, idCounter) |
34 | |||
35 | func randomBytes(n int) []byte { | ||
36 | b := make([]byte, n) | ||
37 | rand.Read(b) | ||
38 | return b | ||
39 | } | 40 | } |