-
Notifications
You must be signed in to change notification settings - Fork 11
/
id.go
62 lines (54 loc) · 2.22 KB
/
id.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package resource
import (
"fmt"
"strings"
"sync"
"time"
)
// UniqueIdPrefix is a string prefix automatically added to return values of
// the UniqueId function.
//
// Deprecated: Copy this value to the provider codebase or use
// github.com/hashicorp/terraform-plugin-sdk/v2/helper/id.UniquePrefix.
const UniqueIdPrefix = `terraform-`
// idCounter is a monotonic counter for generating ordered unique ids.
var idMutex sync.Mutex
var idCounter uint32
// Helper for a resource to generate a unique identifier w/ default prefix
//
// Deprecated: Copy this function to the provider codebase or use
// github.com/hashicorp/terraform-plugin-sdk/v2/helper/id.Unique.
func UniqueId() string {
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.
//
// Deprecated: Copy this value to the provider codebase or use
// github.com/hashicorp/terraform-plugin-sdk/v2/helper/id.UniqueSuffixLength.
const UniqueIDSuffixLength = 26
// 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
// previous timestamp output). After the prefix, the ID consists of a timestamp
// and an incrementing 8 hex digit value The timestamp means that multiple IDs
// created with the same prefix will sort in the order of their creation, even
// across multiple terraform executions, as long as the clock is not turned back
// between calls, and as long as any given terraform execution generates fewer
// than 4 billion IDs.
//
// Deprecated: Copy this function to the provider codebase or use
// github.com/hashicorp/terraform-plugin-sdk/v2/helper/id.PrefixedUnique.
func PrefixedUniqueId(prefix string) string {
// Be precise to 4 digits of fractional seconds, but remove the dot before the
// fractional seconds.
timestamp := strings.Replace(
time.Now().UTC().Format("20060102150405.0000"), ".", "", 1)
idMutex.Lock()
defer idMutex.Unlock()
idCounter++
return fmt.Sprintf("%s%s%08x", prefix, timestamp, idCounter)
}