forked from hashicorp/terraform-provider-azurerm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_group_name.go
60 lines (49 loc) · 1.65 KB
/
resource_group_name.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
package azurerm
import (
"fmt"
"regexp"
"strings"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceGroupNameSchema() *schema.Schema {
return &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateArmResourceGroupName,
}
}
func resourceGroupNameDiffSuppressSchema() *schema.Schema {
return &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
DiffSuppressFunc: resourceAzurermResourceGroupNameDiffSuppress,
ValidateFunc: validateArmResourceGroupName,
}
}
func resourceGroupNameForDataSourceSchema() *schema.Schema {
return &schema.Schema{
Type: schema.TypeString,
Required: true,
}
}
func validateArmResourceGroupName(v interface{}, k string) (ws []string, es []error) {
value := v.(string)
if len(value) > 80 {
es = append(es, fmt.Errorf("%q may not exceed 80 characters in length", k))
}
if strings.HasSuffix(value, ".") {
es = append(es, fmt.Errorf("%q may not end with a period", k))
}
// regex pulled from https://docs.microsoft.com/en-us/rest/api/resources/resourcegroups/createorupdate
if matched := regexp.MustCompile(`^[-\w\._\(\)]+$`).Match([]byte(value)); !matched {
es = append(es, fmt.Errorf("%q may only contain alphanumeric characters, dash, underscores, parentheses and periods", k))
}
return
}
// Resource group names can be capitalised, but we store them in lowercase.
// Use a custom diff function to avoid creation of new resources.
func resourceAzurermResourceGroupNameDiffSuppress(k, old, new string, d *schema.ResourceData) bool {
return strings.ToLower(old) == strings.ToLower(new)
}