Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug #955: Centralize regexes used in attribute validation #958

Merged
merged 1 commit into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions apstra/blueprint/datacenter_generic_system.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import (
"fmt"
"math"
"net"
"regexp"
"sort"

"github.com/Juniper/apstra-go-sdk/apstra"
"github.com/Juniper/apstra-go-sdk/apstra/enum"
"github.com/Juniper/terraform-provider-apstra/apstra/constants"
"github.com/Juniper/terraform-provider-apstra/apstra/design"
apstraregexp "github.com/Juniper/terraform-provider-apstra/apstra/regexp"
"github.com/Juniper/terraform-provider-apstra/apstra/utils"
"github.com/hashicorp/terraform-plugin-framework-nettypes/cidrtypes"
"github.com/hashicorp/terraform-plugin-framework-validators/helpers/validatordiag"
Expand Down Expand Up @@ -76,8 +76,7 @@ func (o DatacenterGenericSystem) ResourceAttributes() map[string]resourceSchema.
Computed: true,
PlanModifiers: []planmodifier.String{stringplanmodifier.UseStateForUnknown()},
Validators: []validator.String{
stringvalidator.RegexMatches(regexp.MustCompile("^[A-Za-z0-9.-]+$"),
"only alphanumeric characters, '.' and '-' allowed."),
stringvalidator.RegexMatches(apstraregexp.HostNameConstraint, apstraregexp.HostNameConstraintMsg),
stringvalidator.LengthBetween(1, 32),
},
},
Expand Down
5 changes: 2 additions & 3 deletions apstra/blueprint/datacenter_routing_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import (
"context"
"fmt"
"net"
"regexp"
"strings"

"github.com/Juniper/apstra-go-sdk/apstra"
"github.com/Juniper/terraform-provider-apstra/apstra/constants"
apstraregexp "github.com/Juniper/terraform-provider-apstra/apstra/regexp"
"github.com/Juniper/terraform-provider-apstra/apstra/utils"
apstravalidator "github.com/Juniper/terraform-provider-apstra/apstra/validator"
"github.com/hashicorp/terraform-plugin-framework-validators/listvalidator"
Expand Down Expand Up @@ -42,7 +42,6 @@ type DatacenterRoutingPolicy struct {
}

func (o DatacenterRoutingPolicy) ResourceAttributes() map[string]resourceSchema.Attribute {
nameRE := regexp.MustCompile("^[A-Za-z0-9_-]+$")
return map[string]resourceSchema.Attribute{
"id": resourceSchema.StringAttribute{
MarkdownDescription: "Apstra graph node ID.",
Expand All @@ -54,7 +53,7 @@ func (o DatacenterRoutingPolicy) ResourceAttributes() map[string]resourceSchema.
Required: true,
Validators: []validator.String{
stringvalidator.LengthBetween(1, 17),
stringvalidator.RegexMatches(nameRE, "only underscore, dash and alphanumeric characters allowed."),
stringvalidator.RegexMatches(apstraregexp.AlphaNumW2HLConstraint, apstraregexp.AlphaNumW2HLConstraintMsg),
},
},
"description": resourceSchema.StringAttribute{
Expand Down
8 changes: 3 additions & 5 deletions apstra/blueprint/datacenter_routing_zone.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import (
"context"
"fmt"
"net"
"regexp"

"github.com/Juniper/apstra-go-sdk/apstra"
"github.com/Juniper/apstra-go-sdk/apstra/enum"
"github.com/Juniper/terraform-provider-apstra/apstra/constants"
"github.com/Juniper/terraform-provider-apstra/apstra/design"
apstraregexp "github.com/Juniper/terraform-provider-apstra/apstra/regexp"
"github.com/Juniper/terraform-provider-apstra/apstra/utils"
apstravalidator "github.com/Juniper/terraform-provider-apstra/apstra/validator"
"github.com/hashicorp/terraform-plugin-framework-validators/int64validator"
Expand Down Expand Up @@ -67,8 +67,7 @@ func (o DatacenterRoutingZone) DataSourceAttributes() map[string]dataSourceSchem
Optional: true,
Validators: []validator.String{
stringvalidator.LengthBetween(1, 17),
stringvalidator.RegexMatches(regexp.MustCompile("^[A-Za-z0-9_-]+$"),
"only underscore, dash and alphanumeric characters allowed."),
stringvalidator.RegexMatches(apstraregexp.AlphaNumW2HLConstraint, apstraregexp.AlphaNumW2HLConstraintMsg),
},
},
"vrf_name": dataSourceSchema.StringAttribute{
Expand Down Expand Up @@ -208,8 +207,7 @@ func (o DatacenterRoutingZone) ResourceAttributes() map[string]resourceSchema.At
MarkdownDescription: "Name displayed in the Apstra web UI.",
Required: true,
Validators: []validator.String{
stringvalidator.RegexMatches(regexp.MustCompile("^[A-Za-z0-9_-]+$"),
"only underscore, dash and alphanumeric characters allowed."),
stringvalidator.RegexMatches(apstraregexp.AlphaNumW2HLConstraint, apstraregexp.AlphaNumW2HLConstraintMsg),
stringvalidator.LengthBetween(1, 15),
},
},
Expand Down
5 changes: 2 additions & 3 deletions apstra/blueprint/datacenter_virtual_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@ import (
"context"
"fmt"
"net"
"regexp"
"strconv"

"github.com/Juniper/apstra-go-sdk/apstra"
apiversions "github.com/Juniper/terraform-provider-apstra/apstra/api_versions"
"github.com/Juniper/terraform-provider-apstra/apstra/compatibility"
"github.com/Juniper/terraform-provider-apstra/apstra/constants"
"github.com/Juniper/terraform-provider-apstra/apstra/design"
apstraregexp "github.com/Juniper/terraform-provider-apstra/apstra/regexp"
"github.com/Juniper/terraform-provider-apstra/apstra/utils"
apstravalidator "github.com/Juniper/terraform-provider-apstra/apstra/validator"
"github.com/hashicorp/terraform-plugin-framework-validators/helpers/validatordiag"
Expand Down Expand Up @@ -295,7 +294,7 @@ func (o DatacenterVirtualNetwork) ResourceAttributes() map[string]resourceSchema
Required: true,
Validators: []validator.String{
stringvalidator.LengthBetween(1, 30),
stringvalidator.RegexMatches(regexp.MustCompile(design.AlphaNumericRegexp), "valid characters are: "+design.AlphaNumericChars),
stringvalidator.RegexMatches(apstraregexp.AlphaNumW2HLConstraint, apstraregexp.AlphaNumW2HLConstraintMsg),
},
},
"blueprint_id": resourceSchema.StringAttribute{
Expand Down
4 changes: 2 additions & 2 deletions apstra/blueprint/device_allocation_system_attributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/Juniper/apstra-go-sdk/apstra/enum"
"github.com/Juniper/terraform-provider-apstra/apstra/compatibility"
"github.com/Juniper/terraform-provider-apstra/apstra/constants"
apstraregexp "github.com/Juniper/terraform-provider-apstra/apstra/regexp"
"github.com/Juniper/terraform-provider-apstra/apstra/utils"
"github.com/hashicorp/go-version"
"github.com/hashicorp/terraform-plugin-framework-nettypes/cidrtypes"
Expand Down Expand Up @@ -63,8 +64,7 @@ func (o DeviceAllocationSystemAttributes) ResourceAttributes() map[string]resour
Computed: true,
MarkdownDescription: "Hostname of the System node.",
Validators: []validator.String{
stringvalidator.RegexMatches(regexp.MustCompile("^[A-Za-z0-9.-]+$"),
"only alphanumeric characters, '.' and '-' allowed."),
stringvalidator.RegexMatches(apstraregexp.HostNameConstraint, apstraregexp.HostNameConstraintMsg),
stringvalidator.LengthBetween(1, 32),
},
},
Expand Down
5 changes: 2 additions & 3 deletions apstra/freeform/allocation_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package freeform
import (
"context"
"fmt"
"regexp"
"strings"

"github.com/Juniper/apstra-go-sdk/apstra"
"github.com/Juniper/apstra-go-sdk/apstra/enum"
apstraregexp "github.com/Juniper/terraform-provider-apstra/apstra/regexp"
"github.com/Juniper/terraform-provider-apstra/apstra/utils"
"github.com/hashicorp/terraform-plugin-framework-validators/setvalidator"
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
Expand Down Expand Up @@ -86,8 +86,7 @@ func (o AllocGroup) ResourceAttributes() map[string]resourceSchema.Attribute {
Required: true,
PlanModifiers: []planmodifier.String{stringplanmodifier.RequiresReplace()},
Validators: []validator.String{
stringvalidator.RegexMatches(regexp.MustCompile("^[a-zA-Z0-9.-_]+$"),
"name may consist only of the following characters : a-zA-Z0-9.-_"),
stringvalidator.RegexMatches(apstraregexp.StdNameConstraint, apstraregexp.StdNameConstraintMsg),
},
},
"type": resourceSchema.StringAttribute{
Expand Down
4 changes: 2 additions & 2 deletions apstra/freeform/link.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package freeform
import (
"context"
"fmt"
"regexp"

"github.com/Juniper/apstra-go-sdk/apstra"
apstraregexp "github.com/Juniper/terraform-provider-apstra/apstra/regexp"
"github.com/Juniper/terraform-provider-apstra/apstra/utils"
"github.com/hashicorp/terraform-plugin-framework-validators/mapvalidator"
"github.com/hashicorp/terraform-plugin-framework-validators/setvalidator"
Expand Down Expand Up @@ -100,7 +100,7 @@ func (o Link) ResourceAttributes() map[string]resourceSchema.Attribute {
MarkdownDescription: "Freeform Link name as shown in the Web UI.",
Required: true,
Validators: []validator.String{
stringvalidator.RegexMatches(regexp.MustCompile("^[a-zA-Z0-9.-_]+$"), "name may consist only of the following characters : a-zA-Z0-9.-_"),
stringvalidator.RegexMatches(apstraregexp.StdNameConstraint, apstraregexp.StdNameConstraintMsg),
},
},
"speed": resourceSchema.StringAttribute{
Expand Down
4 changes: 2 additions & 2 deletions apstra/freeform/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ package freeform
import (
"context"
"fmt"
"regexp"
"strconv"
"strings"

"github.com/Juniper/apstra-go-sdk/apstra"
"github.com/Juniper/apstra-go-sdk/apstra/enum"
apstraregexp "github.com/Juniper/terraform-provider-apstra/apstra/regexp"
"github.com/Juniper/terraform-provider-apstra/apstra/utils"
apstravalidator "github.com/Juniper/terraform-provider-apstra/apstra/validator"
"github.com/hashicorp/terraform-plugin-framework-nettypes/cidrtypes"
Expand Down Expand Up @@ -136,7 +136,7 @@ func (o Resource) ResourceAttributes() map[string]resourceSchema.Attribute {
MarkdownDescription: "Freeform Resource name as shown in the Web UI.",
Required: true,
Validators: []validator.String{
stringvalidator.RegexMatches(regexp.MustCompile("^[a-zA-Z0-9.-_]+$"), "name may consist only of the following characters : a-zA-Z0-9.-_"),
stringvalidator.RegexMatches(apstraregexp.StdNameConstraint, apstraregexp.StdNameConstraintMsg),
},
},
"group_id": resourceSchema.StringAttribute{
Expand Down
12 changes: 4 additions & 8 deletions apstra/freeform/resource_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@ package freeform
import (
"context"
"fmt"
"regexp"
"strings"

"github.com/Juniper/apstra-go-sdk/apstra"
"github.com/Juniper/apstra-go-sdk/apstra/enum"
apstraregexp "github.com/Juniper/terraform-provider-apstra/apstra/regexp"
"github.com/Juniper/terraform-provider-apstra/apstra/utils"
apstravalidator "github.com/Juniper/terraform-provider-apstra/apstra/validator"
"github.com/hashicorp/terraform-plugin-framework-validators/int64validator"

"github.com/Juniper/apstra-go-sdk/apstra"
"github.com/Juniper/terraform-provider-apstra/apstra/utils"
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
dataSourceSchema "github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/diag"
Expand Down Expand Up @@ -110,10 +109,7 @@ func (o ResourceGenerator) ResourceAttributes() map[string]resourceSchema.Attrib
MarkdownDescription: "Freeform Resource Generator name as shown in the Web UI.",
Required: true,
Validators: []validator.String{
stringvalidator.RegexMatches(
regexp.MustCompile("^[a-zA-Z0-9.-_]+$"),
"name may consist only of the following characters : a-zA-Z0-9.-_",
),
stringvalidator.RegexMatches(apstraregexp.StdNameConstraint, apstraregexp.StdNameConstraintMsg),
},
},
"scope": resourceSchema.StringAttribute{
Expand Down
6 changes: 2 additions & 4 deletions apstra/freeform/resource_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package freeform
import (
"context"
"encoding/json"
"regexp"

apstraregexp "github.com/Juniper/terraform-provider-apstra/apstra/regexp"
"github.com/hashicorp/terraform-plugin-framework-jsontypes/jsontypes"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringdefault"

Expand Down Expand Up @@ -97,9 +97,7 @@ func (o ResourceGroup) ResourceAttributes() map[string]resourceSchema.Attribute
MarkdownDescription: "Freeform Resource Group name as shown in the Web UI.",
Required: true,
Validators: []validator.String{
stringvalidator.RegexMatches(
regexp.MustCompile("^[a-zA-Z0-9.-_]+$"),
"name may consist only of the following characters : a-zA-Z0-9.-_"),
stringvalidator.RegexMatches(apstraregexp.StdNameConstraint, apstraregexp.StdNameConstraintMsg),
},
},
"parent_id": resourceSchema.StringAttribute{
Expand Down
7 changes: 2 additions & 5 deletions apstra/freeform/resource_group_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package freeform

import (
"context"
"regexp"

"github.com/Juniper/apstra-go-sdk/apstra"
apstraregexp "github.com/Juniper/terraform-provider-apstra/apstra/regexp"
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
dataSourceSchema "github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/diag"
Expand Down Expand Up @@ -84,10 +84,7 @@ func (o GroupGenerator) ResourceAttributes() map[string]resourceSchema.Attribute
MarkdownDescription: "Freeform Group Generator name as shown in the Web UI.",
Required: true,
Validators: []validator.String{
stringvalidator.RegexMatches(
regexp.MustCompile("^[a-zA-Z0-9.-_]+$"),
"name may consist only of the following characters : a-zA-Z0-9.-_",
),
stringvalidator.RegexMatches(apstraregexp.StdNameConstraint, apstraregexp.StdNameConstraintMsg),
},
},
"scope": resourceSchema.StringAttribute{
Expand Down
7 changes: 3 additions & 4 deletions apstra/freeform/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package freeform
import (
"context"
"fmt"
"regexp"

"github.com/Juniper/apstra-go-sdk/apstra"
apstraregexp "github.com/Juniper/terraform-provider-apstra/apstra/regexp"
"github.com/Juniper/terraform-provider-apstra/apstra/utils"
apstravalidator "github.com/Juniper/terraform-provider-apstra/apstra/validator"
"github.com/hashicorp/terraform-plugin-framework-validators/setvalidator"
Expand Down Expand Up @@ -87,7 +87,6 @@ func (o System) DataSourceAttributes() map[string]dataSourceSchema.Attribute {
}

func (o System) ResourceAttributes() map[string]resourceSchema.Attribute {
hostnameRegexp := "^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]*[a-zA-Z0-9])\\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\\-]*[A-Za-z0-9])$"
return map[string]resourceSchema.Attribute{
"blueprint_id": resourceSchema.StringAttribute{
MarkdownDescription: "Apstra Blueprint ID.",
Expand All @@ -104,14 +103,14 @@ func (o System) ResourceAttributes() map[string]resourceSchema.Attribute {
MarkdownDescription: "Freeform System name as shown in the Web UI.",
Required: true,
Validators: []validator.String{
stringvalidator.RegexMatches(regexp.MustCompile("^[a-zA-Z0-9.-_]+$"), "name may consist only of the following characters : a-zA-Z0-9.-_"),
stringvalidator.RegexMatches(apstraregexp.StdNameConstraint, apstraregexp.StdNameConstraintMsg),
},
},
"hostname": resourceSchema.StringAttribute{
MarkdownDescription: "Hostname of the Freeform System.",
Required: true,
Validators: []validator.String{
stringvalidator.RegexMatches(regexp.MustCompile(hostnameRegexp), "must match regex "+hostnameRegexp),
stringvalidator.RegexMatches(apstraregexp.FreeformHostnameConstraint, apstraregexp.FreeformHostnameConstraintMsg),
},
},
"deploy_mode": resourceSchema.StringAttribute{
Expand Down
27 changes: 27 additions & 0 deletions apstra/regexp/regexp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package apstraregexp

import "regexp"

const (
alphaNumW2HLConstraintReString = "^[" + alphaNumW2HLConstraintChars + "]+$"
alphaNumW2HLConstraintChars = "a-zA-Z0-9_-"
AlphaNumW2HLConstraintMsg = "value must consist only of the following characters: `" + alphaNumW2HLConstraintChars + "`."

freeformHostnameConstraintReString = "^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]*[a-zA-Z0-9])\\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\\-]*[A-Za-z0-9])$"
FreeformHostnameConstraintMsg = "value must match regexp: `" + freeformHostnameConstraintReString + "`."

hostNameConstraintReString = "^[" + hostNameConstraintChars + "]+$"
hostNameConstraintChars = "a-zA-Z0-9.-"
HostNameConstraintMsg = "value must consist only of the following characters: `" + hostNameConstraintChars + "`."

stdNameConstraintReString = "^[" + stdNameConstraintChars + "]+$"
stdNameConstraintChars = "a-zA-Z0-9._-"
StdNameConstraintMsg = "value must consist only of the following characters: `" + stdNameConstraintChars + "`."
)

var (
AlphaNumW2HLConstraint = regexp.MustCompile(alphaNumW2HLConstraintReString)
FreeformHostnameConstraint = regexp.MustCompile(freeformHostnameConstraintReString)
HostNameConstraint = regexp.MustCompile(hostNameConstraintReString)
StdNameConstraint = regexp.MustCompile(stdNameConstraintReString)
)
Loading