Skip to content

Commit

Permalink
helper/resource: ok let's actually use RFC4122
Browse files Browse the repository at this point in the history
  • Loading branch information
phinze committed Apr 22, 2015
1 parent 33de319 commit 92ebb60
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions helper/resource/id.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,30 @@ const UniqueIdPrefix = `terraform-`
// Helper for a resource to generate a unique identifier
//
// This uses a simple RFC 4122 v4 UUID with some basic cosmetic filters
// applied (remove padding, downcase) to help distinguishing visually between
// identifiers.
// applied (base32, remove padding, downcase) to make visually distinguishing
// identifiers easier.
func UniqueId() string {
var uuid [16]byte
rand.Read(uuid[:])
return fmt.Sprintf("%s%s", UniqueIdPrefix,
strings.ToLower(
strings.Replace(
base32.StdEncoding.EncodeToString(uuid[:]),
base32.StdEncoding.EncodeToString(uuidV4()),
"=", "", -1)))
}

func uuidV4() []byte {
var uuid [16]byte

// Set all the other bits to randomly (or pseudo-randomly) chosen
// values.
rand.Read(uuid[:])

// Set the two most significant bits (bits 6 and 7) of the
// clock_seq_hi_and_reserved to zero and one, respectively.
uuid[8] = (uuid[8] | 0x80) & 0x8f

// Set the four most significant bits (bits 12 through 15) of the
// time_hi_and_version field to the 4-bit version number from Section 4.1.3.
uuid[6] = (uuid[6] | 0x40) & 0x4f

return uuid[:]
}

0 comments on commit 92ebb60

Please sign in to comment.