-
Notifications
You must be signed in to change notification settings - Fork 23
/
naming.go
49 lines (37 loc) · 1.21 KB
/
naming.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
package main
import (
"regexp"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)
const MaxRoleLen = 64
// NameWithPrefix returns in order:
// the name if non-empty,
// a prefix generated name if non-empty,
// or fully generated name prefixed with "terraform-".
func NameWithPrefix(name string, namePrefix string) string {
if name != "" {
return name
}
if namePrefix != "" {
return resource.PrefixedUniqueId(namePrefix)
}
return resource.UniqueId()
}
func NamePrefixFromName(name string) *string {
namePrefixIndex := len(name) - resource.UniqueIDSuffixLength
if namePrefixIndex <= 0 {
return nil
}
namePrefix := name[:namePrefixIndex]
return &namePrefix
}
// Validate Role name based on https://docs.aws.amazon.com/IAM/latest/APIReference/API_CreateRole.html
var ValidRoleName = validation.All(
validation.StringLenBetween(1, MaxRoleLen),
validation.StringMatch(regexp.MustCompile(`^[\w+=,.@-]+$`), "must match [\\w+=,.@-]"),
)
var ValidRolePrefix = validation.All(
validation.StringLenBetween(1, MaxRoleLen-resource.UniqueIDSuffixLength),
validation.StringMatch(regexp.MustCompile(`^[\w+=,.@-]+$`), "must match [\\w+=,.@-]"),
)