forked from hashicorp/terraform-provider-azurerm
/
devtest.go
58 lines (49 loc) · 1.65 KB
/
devtest.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
package validate
import (
"fmt"
"regexp"
"github.com/Azure/azure-sdk-for-go/services/devtestlabs/mgmt/2016-05-15/dtl"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
)
func DevTestLabName() schema.SchemaValidateFunc {
return validation.StringMatch(
regexp.MustCompile("^[A-Za-z0-9_-]+$"),
"Lab Name can only include alphanumeric characters, underscores, hyphens.")
}
func DevTestVirtualMachineName(maxLength int) schema.SchemaValidateFunc {
return func(i interface{}, k string) ([]string, []error) {
v, ok := i.(string)
if !ok {
return nil, []error{
fmt.Errorf("expected type of %s to be string", k),
}
}
errs := make([]error, 0)
if 1 <= len(v) && len(v) > maxLength {
errs = append(errs, fmt.Errorf("Expected %s to be between 1 and %d characters - got %d", k, maxLength, len(v)))
}
matched, err := regexp.MatchString("^([a-zA-Z0-9]{1})([a-zA-Z0-9-]+)([a-zA-Z0-9]{1})$", v)
if err != nil {
errs = append(errs, fmt.Errorf("Error validating regex: %+v", err))
}
if !matched {
errs = append(errs, fmt.Errorf("%s may contain letters, numbers, or '-', must begin and end with a letter or number, and cannot be all numbers.", k))
}
matched, err = regexp.MatchString("([a-zA-Z-]+)", v)
if err != nil {
errs = append(errs, fmt.Errorf("Error validating regex: %+v", err))
}
if !matched {
errs = append(errs, fmt.Errorf("%s cannot be all numbers.", k))
}
return nil, errs
}
}
func DevTestVirtualNetworkUsagePermissionType() schema.SchemaValidateFunc {
return validation.StringInSlice([]string{
string(dtl.Allow),
string(dtl.Default),
string(dtl.Deny),
}, false)
}