Skip to content

Commit b6ca3a4

Browse files
authored
feat: Add subnet and virtual network name validation (#376)
Resolves #268 issue
1 parent c18b0ec commit b6ca3a4

4 files changed

Lines changed: 61 additions & 6 deletions

File tree

resources/types/subnet.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/multycloud/multy/resources"
99
"github.com/multycloud/multy/validate"
1010
"net"
11+
"regexp"
1112
)
1213

1314
/*
@@ -49,9 +50,12 @@ func NewSubnet(s *Subnet, resourceId string, subnet *resourcespb.SubnetArgs, oth
4950
}
5051

5152
func (r *Subnet) Validate(ctx resources.MultyContext) (errs []validate.ValidationError) {
52-
//if vn.Name contains not letters,numbers,_,- { return false }
53-
//if vn.Name length? { return false }
54-
//if vn.AvailbilityZone valid { return false }
53+
nameRestrictionRegex := regexp.MustCompile(validate.WordWithDotHyphenUnder80Pattern)
54+
if !nameRestrictionRegex.MatchString(r.Args.Name) {
55+
errs = append(errs, r.NewValidationError(fmt.Errorf("%s can contain only alphanumerics, underscores, periods, and hyphens;"+
56+
" must start with alphanumeric and end with alphanumeric or underscore and have 1-80 lenght", r.ResourceId), "name"))
57+
}
58+
5559
if len(r.Args.CidrBlock) == 0 { // max len?
5660
errs = append(errs, r.NewValidationError(fmt.Errorf("%s cidr_block length is invalid", r.ResourceId), "cidr_block"))
5761
}

resources/types/virtual_network.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package types
22

33
import (
4+
"fmt"
45
"github.com/multycloud/multy/api/proto/resourcespb"
56
"github.com/multycloud/multy/resources"
67
"github.com/multycloud/multy/validate"
78
"net"
9+
"regexp"
810
)
911

1012
/*
@@ -50,9 +52,12 @@ func NewVirtualNetwork(r *VirtualNetwork, resourceId string, vn *resourcespb.Vir
5052

5153
func (r *VirtualNetwork) Validate(ctx resources.MultyContext) (errs []validate.ValidationError) {
5254
errs = append(errs, r.ResourceWithId.Validate()...)
53-
//if r.Name contains not letters,numbers,_,- { return false }
54-
//if r.Name length? { return false }
55-
//if r.CidrBlock valid CIDR { return false }
55+
nameRestrictionRegex := regexp.MustCompile(validate.WordWithDotHyphenUnder80Pattern)
56+
if !nameRestrictionRegex.MatchString(r.Args.Name) {
57+
errs = append(errs, r.NewValidationError(fmt.Errorf("%s can contain only alphanumerics, underscores, periods, and hyphens;"+
58+
" must start with alphanumeric and end with alphanumeric or underscore and have 1-80 lenght", r.ResourceId), "name"))
59+
}
60+
5661
if len(r.Args.CidrBlock) == 0 { // max len?
5762
errs = append(errs, validate.ValidationError{
5863
ErrorMessage: "cidr_block length is invalid",

validate/validate.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ import (
77
"io/ioutil"
88
)
99

10+
// WordWithDotHyphenUnder80Pattern is a regexp pattern that matches string that contain alphanumerics, underscores, periods,
11+
// and hyphens that start with alphanumeric and End alphanumeric or underscore. Limits size to 1-80.
12+
// Based on https://docs.microsoft.com/en-us/azure/azure-resource-manager/management/resource-name-rules
13+
const WordWithDotHyphenUnder80Pattern = string(`^[a-zA-Z\d]$|^[a-zA-Z\d][\w\-.]{0,78}\w$`)
14+
1015
type ResourceValidationInfo struct {
1116
SourceRanges map[string]hcl.Range
1217
BlockDefRange hcl.Range

validate/validate_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package validate_test
2+
3+
import (
4+
"github.com/multycloud/multy/validate"
5+
"regexp"
6+
"testing"
7+
)
8+
9+
// TestWordWithDotHyphenUnder80Pattern checks whether validate.WordWithDotHyphenUnder80Pattern matches
10+
// expected expressions
11+
func TestWordWithDotHyphenUnder80Pattern(t *testing.T) {
12+
testRegexp, err := regexp.Compile(validate.WordWithDotHyphenUnder80Pattern)
13+
if err != nil {
14+
t.Fatalf("Could not compile regex: %s", validate.WordWithDotHyphenUnder80Pattern)
15+
}
16+
17+
shouldMatch := []string{
18+
"a",
19+
"9",
20+
"aZ9",
21+
"ThisIs67dots..................................................................._",
22+
}
23+
shouldntMatch := []string{
24+
"",
25+
"_",
26+
"<someName",
27+
"ThisIs68dots...................................................................._",
28+
"Maybe?inThe.Middle_",
29+
}
30+
31+
for _, name := range shouldMatch {
32+
if !testRegexp.MatchString(name) {
33+
t.Errorf("%s should match %s, but didn't", validate.WordWithDotHyphenUnder80Pattern, name)
34+
}
35+
}
36+
for _, name := range shouldntMatch {
37+
if testRegexp.MatchString(name) {
38+
t.Errorf("%s shouldn't match %s, but did", validate.WordWithDotHyphenUnder80Pattern, name)
39+
}
40+
}
41+
}

0 commit comments

Comments
 (0)