|
1 | 1 | package common |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "bytes" |
5 | 4 | "errors" |
6 | | - "fmt" |
7 | | - "regexp" |
8 | | - "strings" |
9 | 5 | "time" |
10 | | - |
11 | | - "crypto/sha256" |
12 | | - |
13 | | - "github.com/aohorodnyk/uid" |
14 | 6 | ) |
15 | 7 |
|
16 | 8 | type Cloud struct { |
@@ -46,30 +38,3 @@ func (c *Cloud) GetClosestRegion(regions map[string]Region) (string, error) { |
46 | 38 |
|
47 | 39 | return "", errors.New("native region not found") |
48 | 40 | } |
49 | | - |
50 | | -// NormalizeIdentifier normalizes user-provided identifiers by adapting them to |
51 | | -// the most strict cloud provider naming requisites. |
52 | | -func NormalizeIdentifier(identifier string, long bool) string { |
53 | | - lowercase := strings.ToLower(identifier) |
54 | | - normalized := regexp.MustCompile("[^a-z0-9]+").ReplaceAllString(lowercase, "-") |
55 | | - normalized = regexp.MustCompile("(^-)|(-$)").ReplaceAllString(normalized, "") |
56 | | - if len(normalized) > 50 { |
57 | | - normalized = normalized[:50] |
58 | | - } |
59 | | - if long { |
60 | | - return fmt.Sprintf("tpi-%s-%s", normalized, generateSuffix(identifier, 8)) |
61 | | - } |
62 | | - return generateSuffix(identifier, 8) |
63 | | -} |
64 | | - |
65 | | -// generateSuffix deterministically generates a Base36 suffix of `size` |
66 | | -// characters using `identifier` as the seed. This is useful to enhance |
67 | | -// user-provided identifiers so they become valid "globally unique names" for |
68 | | -// AWS S3, Google Storage and other services that require them. |
69 | | -func generateSuffix(identifier string, size uint32) string { |
70 | | - digest := sha256.Sum256([]byte(identifier)) |
71 | | - random := uid.NewRandCustom(bytes.NewReader(digest[:])) |
72 | | - encoder := uid.NewEncoderBase36() |
73 | | - provider := uid.NewProviderCustom(size, random, encoder) |
74 | | - return provider.MustGenerate().String() |
75 | | -} |
0 commit comments