2018-01-29 11:52:19 -06:00
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
2023-02-06 19:44:37 -06:00
|
|
|
"math/rand"
|
2018-02-15 02:56:13 -06:00
|
|
|
"regexp"
|
2023-02-06 19:44:37 -06:00
|
|
|
"time"
|
2018-02-15 02:56:13 -06:00
|
|
|
|
2023-02-06 19:44:37 -06:00
|
|
|
"github.com/google/uuid"
|
2018-01-29 11:52:19 -06:00
|
|
|
)
|
|
|
|
|
2023-02-06 19:44:37 -06:00
|
|
|
var uidrand = rand.New(rand.NewSource(time.Now().UnixNano()))
|
|
|
|
var alphaRunes = []rune("abcdefghijklmnopqrstuvwxyz")
|
|
|
|
var hexLetters = []rune("abcdef")
|
2018-02-15 02:56:13 -06:00
|
|
|
|
2023-02-06 19:44:37 -06:00
|
|
|
// Legacy UID pattern
|
2019-01-28 15:37:44 -06:00
|
|
|
var validUIDPattern = regexp.MustCompile(`^[a-zA-Z0-9\-\_]*$`).MatchString
|
2018-02-15 02:56:13 -06:00
|
|
|
|
2019-01-28 15:37:44 -06:00
|
|
|
// IsValidShortUID checks if short unique identifier contains valid characters
|
2023-02-06 19:44:37 -06:00
|
|
|
// NOTE: future Grafana UIDs will need conform to https://github.com/kubernetes/apimachinery/blob/master/pkg/util/validation/validation.go#L43
|
2019-01-28 15:37:44 -06:00
|
|
|
func IsValidShortUID(uid string) bool {
|
|
|
|
return validUIDPattern(uid)
|
2018-02-15 02:56:13 -06:00
|
|
|
}
|
|
|
|
|
2021-09-10 04:22:13 -05:00
|
|
|
// IsShortUIDTooLong checks if short unique identifier is too long
|
|
|
|
func IsShortUIDTooLong(uid string) bool {
|
|
|
|
return len(uid) > 40
|
|
|
|
}
|
|
|
|
|
2023-02-06 19:44:37 -06:00
|
|
|
// GenerateShortUID will generate a UUID that can also be a k8s name
|
|
|
|
// it is guaranteed to have a character as the first letter
|
|
|
|
// This UID will be a valid k8s name
|
2019-01-28 15:37:44 -06:00
|
|
|
func GenerateShortUID() string {
|
2023-02-06 19:44:37 -06:00
|
|
|
uid, err := uuid.NewRandom()
|
|
|
|
if err != nil {
|
|
|
|
// This should never happen... but this seems better than a panic
|
|
|
|
for i := range uid {
|
|
|
|
uid[i] = byte(uidrand.Intn(255))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
uuid := uid.String()
|
|
|
|
if rune(uuid[0]) < rune('a') {
|
|
|
|
return string(hexLetters[uidrand.Intn(len(hexLetters))]) + uuid[1:]
|
|
|
|
}
|
|
|
|
return uuid
|
2018-01-29 11:52:19 -06:00
|
|
|
}
|