github.com/bendemaree/terraform@v0.5.4-0.20150613200311-f50d97d6eee6/helper/resource/id.go (about) 1 package resource 2 3 import ( 4 "crypto/rand" 5 "encoding/base32" 6 "fmt" 7 "strings" 8 ) 9 10 const UniqueIdPrefix = `terraform-` 11 12 // Helper for a resource to generate a unique identifier 13 // 14 // This uses a simple RFC 4122 v4 UUID with some basic cosmetic filters 15 // applied (base32, remove padding, downcase) to make visually distinguishing 16 // identifiers easier. 17 func UniqueId() string { 18 return fmt.Sprintf("%s%s", UniqueIdPrefix, 19 strings.ToLower( 20 strings.Replace( 21 base32.StdEncoding.EncodeToString(uuidV4()), 22 "=", "", -1))) 23 } 24 25 func uuidV4() []byte { 26 var uuid [16]byte 27 28 // Set all the other bits to randomly (or pseudo-randomly) chosen 29 // values. 30 rand.Read(uuid[:]) 31 32 // Set the two most significant bits (bits 6 and 7) of the 33 // clock_seq_hi_and_reserved to zero and one, respectively. 34 uuid[8] = (uuid[8] | 0x80) & 0x8f 35 36 // Set the four most significant bits (bits 12 through 15) of the 37 // time_hi_and_version field to the 4-bit version number from Section 4.1.3. 38 uuid[6] = (uuid[6] | 0x40) & 0x4f 39 40 return uuid[:] 41 }