]> git.immae.eu Git - github/fretlink/terraform-provider-statuscake.git/blame - vendor/github.com/hashicorp/terraform/helper/resource/id.go
vendor: github.com/hashicorp/terraform/...@v0.10.0
[github/fretlink/terraform-provider-statuscake.git] / vendor / github.com / hashicorp / terraform / helper / resource / id.go
CommitLineData
bae9f6d2
JC
1package resource
2
3import (
bae9f6d2 4 "fmt"
c680a8e1 5 "strings"
bae9f6d2 6 "sync"
c680a8e1 7 "time"
bae9f6d2
JC
8)
9
10const UniqueIdPrefix = `terraform-`
11
c680a8e1 12// idCounter is a monotonic counter for generating ordered unique ids.
bae9f6d2 13var idMutex sync.Mutex
c680a8e1 14var idCounter uint32
bae9f6d2
JC
15
16// Helper for a resource to generate a unique identifier w/ default prefix
17func UniqueId() string {
18 return PrefixedUniqueId(UniqueIdPrefix)
19}
20
21// Helper for a resource to generate a unique identifier w/ given prefix
22//
23// After the prefix, the ID consists of an incrementing 26 digit value (to match
c680a8e1
RS
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.
bae9f6d2 30func PrefixedUniqueId(prefix string) string {
c680a8e1
RS
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
bae9f6d2
JC
36 idMutex.Lock()
37 defer idMutex.Unlock()
c680a8e1
RS
38 idCounter++
39 return fmt.Sprintf("%s%s%08x", prefix, timestamp, idCounter)
bae9f6d2 40}