Bumps [github.com/hashicorp/terraform-plugin-sdk/v2](https://github.com/hashicorp/terraform-plugin-sdk) from 2.26.1 to 2.27.0. - [Release notes](https://github.com/hashicorp/terraform-plugin-sdk/releases) - [Changelog](https://github.com/hashicorp/terraform-plugin-sdk/blob/main/CHANGELOG.md) - [Commits](https://github.com/hashicorp/terraform-plugin-sdk/compare/v2.26.1...v2.27.0) --- updated-dependencies: - dependency-name: github.com/hashicorp/terraform-plugin-sdk/v2 dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] <support@github.com>
49 lines
1.6 KiB
Go
49 lines
1.6 KiB
Go
// Copyright (c) HashiCorp, Inc.
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
package id
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
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
|
|
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.
|
|
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.
|
|
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)
|
|
}
|