diff --git a/resources/types/subnet.go b/resources/types/subnet.go index ef099ff6..9120f861 100644 --- a/resources/types/subnet.go +++ b/resources/types/subnet.go @@ -8,6 +8,7 @@ import ( "github.com/multycloud/multy/resources" "github.com/multycloud/multy/validate" "net" + "regexp" ) /* @@ -49,9 +50,12 @@ func NewSubnet(s *Subnet, resourceId string, subnet *resourcespb.SubnetArgs, oth } func (r *Subnet) Validate(ctx resources.MultyContext) (errs []validate.ValidationError) { - //if vn.Name contains not letters,numbers,_,- { return false } - //if vn.Name length? { return false } - //if vn.AvailbilityZone valid { return false } + nameRestrictionRegex := regexp.MustCompile(validate.WordWithDotHyphenUnder80Pattern) + if !nameRestrictionRegex.MatchString(r.Args.Name) { + errs = append(errs, r.NewValidationError(fmt.Errorf("%s can contain only alphanumerics, underscores, periods, and hyphens;"+ + " must start with alphanumeric and end with alphanumeric or underscore and have 1-80 lenght", r.ResourceId), "name")) + } + if len(r.Args.CidrBlock) == 0 { // max len? errs = append(errs, r.NewValidationError(fmt.Errorf("%s cidr_block length is invalid", r.ResourceId), "cidr_block")) } diff --git a/resources/types/virtual_network.go b/resources/types/virtual_network.go index 19b9b360..500480b3 100644 --- a/resources/types/virtual_network.go +++ b/resources/types/virtual_network.go @@ -1,10 +1,12 @@ package types import ( + "fmt" "github.com/multycloud/multy/api/proto/resourcespb" "github.com/multycloud/multy/resources" "github.com/multycloud/multy/validate" "net" + "regexp" ) /* @@ -50,9 +52,12 @@ func NewVirtualNetwork(r *VirtualNetwork, resourceId string, vn *resourcespb.Vir func (r *VirtualNetwork) Validate(ctx resources.MultyContext) (errs []validate.ValidationError) { errs = append(errs, r.ResourceWithId.Validate()...) - //if r.Name contains not letters,numbers,_,- { return false } - //if r.Name length? { return false } - //if r.CidrBlock valid CIDR { return false } + nameRestrictionRegex := regexp.MustCompile(validate.WordWithDotHyphenUnder80Pattern) + if !nameRestrictionRegex.MatchString(r.Args.Name) { + errs = append(errs, r.NewValidationError(fmt.Errorf("%s can contain only alphanumerics, underscores, periods, and hyphens;"+ + " must start with alphanumeric and end with alphanumeric or underscore and have 1-80 lenght", r.ResourceId), "name")) + } + if len(r.Args.CidrBlock) == 0 { // max len? errs = append(errs, validate.ValidationError{ ErrorMessage: "cidr_block length is invalid", diff --git a/validate/validate.go b/validate/validate.go index 5600c9cb..fa832f47 100644 --- a/validate/validate.go +++ b/validate/validate.go @@ -7,6 +7,11 @@ import ( "io/ioutil" ) +// WordWithDotHyphenUnder80Pattern is a regexp pattern that matches string that contain alphanumerics, underscores, periods, +// and hyphens that start with alphanumeric and End alphanumeric or underscore. Limits size to 1-80. +// Based on https://docs.microsoft.com/en-us/azure/azure-resource-manager/management/resource-name-rules +const WordWithDotHyphenUnder80Pattern = string(`^[a-zA-Z\d]$|^[a-zA-Z\d][\w\-.]{0,78}\w$`) + type ResourceValidationInfo struct { SourceRanges map[string]hcl.Range BlockDefRange hcl.Range diff --git a/validate/validate_test.go b/validate/validate_test.go new file mode 100644 index 00000000..f907b399 --- /dev/null +++ b/validate/validate_test.go @@ -0,0 +1,41 @@ +package validate_test + +import ( + "github.com/multycloud/multy/validate" + "regexp" + "testing" +) + +// TestWordWithDotHyphenUnder80Pattern checks whether validate.WordWithDotHyphenUnder80Pattern matches +// expected expressions +func TestWordWithDotHyphenUnder80Pattern(t *testing.T) { + testRegexp, err := regexp.Compile(validate.WordWithDotHyphenUnder80Pattern) + if err != nil { + t.Fatalf("Could not compile regex: %s", validate.WordWithDotHyphenUnder80Pattern) + } + + shouldMatch := []string{ + "a", + "9", + "aZ9", + "ThisIs67dots..................................................................._", + } + shouldntMatch := []string{ + "", + "_", + "