Skip to content

Commit

Permalink
refactor: move validators out of provider.go
Browse files Browse the repository at this point in the history
  • Loading branch information
ctreatma committed Dec 18, 2023
1 parent 0e3709c commit c65de29
Show file tree
Hide file tree
Showing 11 changed files with 48 additions and 43 deletions.
5 changes: 3 additions & 2 deletions equinix/data_source_ecx_l2_sellerprofiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strings"

"github.com/equinix/terraform-provider-equinix/internal/config"
equinix_validation "github.com/equinix/terraform-provider-equinix/internal/validation"

"github.com/equinix/ecx-go/v2"
"github.com/hashicorp/go-cty/cty"
Expand Down Expand Up @@ -52,7 +53,7 @@ func dataSourceECXL2SellerProfiles() *schema.Resource {
Description: ecxL2SellerProfilesDescriptions["Metros"],
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: stringIsMetroCode(),
ValidateFunc: equinix_validation.StringIsMetroCode,
},
},
ecxL2SellerProfilesSchemaNames["SpeedBands"]: {
Expand All @@ -62,7 +63,7 @@ func dataSourceECXL2SellerProfiles() *schema.Resource {
Description: ecxL2SellerProfilesDescriptions["SpeedBands"],
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: stringIsSpeedBand(),
ValidateFunc: equinix_validation.StringIsSpeedBand,
},
},
ecxL2SellerProfilesSchemaNames["OrganizationName"]: {
Expand Down
3 changes: 2 additions & 1 deletion equinix/data_source_network_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strings"

"github.com/equinix/terraform-provider-equinix/internal/config"
equinix_validation "github.com/equinix/terraform-provider-equinix/internal/validation"

"github.com/equinix/ne-go"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
Expand Down Expand Up @@ -61,7 +62,7 @@ func dataSourceNetworkAccount() *schema.Resource {
networkAccountSchemaNames["MetroCode"]: {
Type: schema.TypeString,
Required: true,
ValidateFunc: stringIsMetroCode(),
ValidateFunc: equinix_validation.StringIsMetroCode,
Description: networkAccountDescriptions["MetroCode"],
},
},
Expand Down
3 changes: 2 additions & 1 deletion equinix/data_source_network_device_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strings"

"github.com/equinix/terraform-provider-equinix/internal/config"
equinix_validation "github.com/equinix/terraform-provider-equinix/internal/validation"

"github.com/equinix/ne-go"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
Expand Down Expand Up @@ -74,7 +75,7 @@ func dataSourceNetworkDeviceType() *schema.Resource {
MinItems: 1,
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: stringIsMetroCode(),
ValidateFunc: equinix_validation.StringIsMetroCode,
},
Description: networkDeviceTypeDescriptions["MetroCodes"],
},
Expand Down
19 changes: 0 additions & 19 deletions equinix/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"net/http"
"reflect"
"regexp"
"strings"
"time"

Expand Down Expand Up @@ -274,24 +273,6 @@ func hasModelErrorCode(errors []v4.ModelError, code string) bool {
return false
}

func stringIsMetroCode() schema.SchemaValidateFunc {
return validation.StringMatch(regexp.MustCompile("^[A-Z]{2}$"), "MetroCode must consist of two capital letters")
}

func stringIsEmailAddress() schema.SchemaValidateFunc {
return validation.StringMatch(regexp.MustCompile("^[^ @]+@[^ @]+$"), "not valid email address")
}

func stringIsPortDefinition() schema.SchemaValidateFunc {
return validation.StringMatch(
regexp.MustCompile("^(([0-9]+(,[0-9]+){0,9})|([0-9]+-[0-9]+)|(any))$"),
"port definition has to be: up to 10 comma sepparated numbers (22,23), range (20-23) or word 'any'")
}

func stringIsSpeedBand() schema.SchemaValidateFunc {
return validation.StringMatch(regexp.MustCompile("^[0-9]+(MB|GB)$"), "SpeedBand should consist of digit followed by MB or GB")
}

func stringsFound(source []string, target []string) bool {
for i := range source {
if !isStringInSlice(source[i], target) {
Expand Down
7 changes: 4 additions & 3 deletions equinix/resource_ecx_l2_connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

"github.com/equinix/terraform-provider-equinix/internal/config"
equinix_validation "github.com/equinix/terraform-provider-equinix/internal/validation"

"github.com/equinix/ecx-go/v2"
"github.com/equinix/rest-go"
Expand Down Expand Up @@ -210,7 +211,7 @@ func createECXL2ConnectionResourceSchema() map[string]*schema.Schema {
MinItems: 1,
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: stringIsEmailAddress(),
ValidateFunc: equinix_validation.StringIsEmailAddress,
},
Description: ecxL2ConnectionDescriptions["Notifications"],
},
Expand Down Expand Up @@ -324,7 +325,7 @@ func createECXL2ConnectionResourceSchema() map[string]*schema.Schema {
Optional: true,
Computed: true,
ForceNew: true,
ValidateFunc: stringIsMetroCode(),
ValidateFunc: equinix_validation.StringIsMetroCode,
Description: ecxL2ConnectionDescriptions["SellerMetroCode"],
},
ecxL2ConnectionSchemaNames["AuthorizationKey"]: {
Expand Down Expand Up @@ -576,7 +577,7 @@ func createECXL2ConnectionSecondaryResourceSchema() map[string]*schema.Schema {
Optional: true,
Computed: true,
ForceNew: true,
ValidateFunc: stringIsMetroCode(),
ValidateFunc: equinix_validation.StringIsMetroCode,
Description: ecxL2ConnectionDescriptions["SellerMetroCode"],
},
ecxL2ConnectionSchemaNames["AuthorizationKey"]: {
Expand Down
11 changes: 6 additions & 5 deletions equinix/resource_ecx_l2_serviceprofile.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"

"github.com/equinix/terraform-provider-equinix/internal/config"
equinix_validation "github.com/equinix/terraform-provider-equinix/internal/validation"

"github.com/equinix/ecx-go/v2"
"github.com/equinix/rest-go"
Expand Down Expand Up @@ -205,7 +206,7 @@ func createECXL2ServiceProfileResourceSchema() map[string]*schema.Schema {
Description: ecxL2ServiceProfileDescriptions["OnBandwidthThresholdNotification"],
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: stringIsEmailAddress(),
ValidateFunc: equinix_validation.StringIsEmailAddress,
},
},
ecxL2ServiceProfileSchemaNames["OnProfileApprovalRejectNotification"]: {
Expand All @@ -215,7 +216,7 @@ func createECXL2ServiceProfileResourceSchema() map[string]*schema.Schema {
Description: ecxL2ServiceProfileDescriptions["OnProfileApprovalRejectNotification"],
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: stringIsEmailAddress(),
ValidateFunc: equinix_validation.StringIsEmailAddress,
},
},
ecxL2ServiceProfileSchemaNames["OnVcApprovalRejectionNotification"]: {
Expand All @@ -225,7 +226,7 @@ func createECXL2ServiceProfileResourceSchema() map[string]*schema.Schema {
Description: ecxL2ServiceProfileDescriptions["OnVcApprovalRejectionNotification"],
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: stringIsEmailAddress(),
ValidateFunc: equinix_validation.StringIsEmailAddress,
},
},
ecxL2ServiceProfileSchemaNames["OverSubscription"]: {
Expand All @@ -248,7 +249,7 @@ func createECXL2ServiceProfileResourceSchema() map[string]*schema.Schema {
Description: ecxL2ServiceProfileDescriptions["PrivateUserEmails"],
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: stringIsEmailAddress(),
ValidateFunc: equinix_validation.StringIsEmailAddress,
},
},
ecxL2ServiceProfileSchemaNames["RequiredRedundancy"]: {
Expand Down Expand Up @@ -312,7 +313,7 @@ func createECXL2ServiceProfileResourceSchema() map[string]*schema.Schema {
ecxL2ServiceProfilePortSchemaNames["MetroCode"]: {
Type: schema.TypeString,
Required: true,
ValidateFunc: stringIsMetroCode(),
ValidateFunc: equinix_validation.StringIsMetroCode,
Description: ecxL2ServiceProfilePortDescriptions["MetroCode"],
},
},
Expand Down
7 changes: 4 additions & 3 deletions equinix/resource_network_acl_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/http"

"github.com/equinix/terraform-provider-equinix/internal/config"
equinix_validation "github.com/equinix/terraform-provider-equinix/internal/validation"

"github.com/equinix/ne-go"
"github.com/equinix/rest-go"
Expand Down Expand Up @@ -114,7 +115,7 @@ func createNetworkACLTemplateSchema() map[string]*schema.Schema {
Type: schema.TypeString,
Optional: true,
Deprecated: networkACLTemplateDeprecateDescriptions["MetroCode"],
ValidateFunc: stringIsMetroCode(),
ValidateFunc: equinix_validation.StringIsMetroCode,
Description: networkACLTemplateDescriptions["MetroCode"],
},
networkACLTemplateSchemaNames["DeviceUUID"]: {
Expand Down Expand Up @@ -187,13 +188,13 @@ func createNetworkACLTemplateInboundRuleSchema() map[string]*schema.Schema {
networkACLTemplateInboundRuleSchemaNames["SrcPort"]: {
Type: schema.TypeString,
Required: true,
ValidateFunc: stringIsPortDefinition(),
ValidateFunc: equinix_validation.StringIsPortDefinition,
Description: networkACLTemplateInboundRuleDescriptions["SrcPort"],
},
networkACLTemplateInboundRuleSchemaNames["DstPort"]: {
Type: schema.TypeString,
Required: true,
ValidateFunc: stringIsPortDefinition(),
ValidateFunc: equinix_validation.StringIsPortDefinition,
Description: networkACLTemplateInboundRuleDescriptions["DstPort"],
},
networkACLTemplateInboundRuleSchemaNames["Description"]: {
Expand Down
9 changes: 5 additions & 4 deletions equinix/resource_network_device.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"time"

"github.com/equinix/terraform-provider-equinix/internal/config"
equinix_validation "github.com/equinix/terraform-provider-equinix/internal/validation"

"github.com/equinix/ne-go"
"github.com/equinix/rest-go"
Expand Down Expand Up @@ -252,7 +253,7 @@ func createNetworkDeviceSchema() map[string]*schema.Schema {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: stringIsMetroCode(),
ValidateFunc: equinix_validation.StringIsMetroCode,
Description: neDeviceDescriptions["MetroCode"],
},
neDeviceSchemaNames["IBX"]: {
Expand Down Expand Up @@ -374,7 +375,7 @@ func createNetworkDeviceSchema() map[string]*schema.Schema {
MinItems: 1,
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: stringIsEmailAddress(),
ValidateFunc: equinix_validation.StringIsEmailAddress,
},
Description: neDeviceDescriptions["Notifications"],
},
Expand Down Expand Up @@ -523,7 +524,7 @@ func createNetworkDeviceSchema() map[string]*schema.Schema {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: stringIsMetroCode(),
ValidateFunc: equinix_validation.StringIsMetroCode,
Description: neDeviceDescriptions["MetroCode"],
},
neDeviceSchemaNames["IBX"]: {
Expand Down Expand Up @@ -608,7 +609,7 @@ func createNetworkDeviceSchema() map[string]*schema.Schema {
MinItems: 1,
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: stringIsEmailAddress(),
ValidateFunc: equinix_validation.StringIsEmailAddress,
},
Description: neDeviceDescriptions["Notifications"],
},
Expand Down
8 changes: 4 additions & 4 deletions equinix/resource_network_device_link.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import (
"fmt"
"time"

"github.com/equinix/terraform-provider-equinix/internal/hashcode"

"github.com/equinix/terraform-provider-equinix/internal/config"
"github.com/equinix/terraform-provider-equinix/internal/hashcode"
equinix_validation "github.com/equinix/terraform-provider-equinix/internal/validation"

"github.com/equinix/ne-go"
"github.com/hashicorp/go-cty/cty"
Expand Down Expand Up @@ -197,13 +197,13 @@ func createNetworkDeviceLinkConnectionResourceSchema() map[string]*schema.Schema
networkDeviceLinkConnectionSchemaNames["SourceMetroCode"]: {
Type: schema.TypeString,
Required: true,
ValidateFunc: stringIsMetroCode(),
ValidateFunc: equinix_validation.StringIsMetroCode,
Description: networkDeviceLinkConnectionDescriptions["SourceMetroCode"],
},
networkDeviceLinkConnectionSchemaNames["DestinationMetroCode"]: {
Type: schema.TypeString,
Required: true,
ValidateFunc: stringIsMetroCode(),
ValidateFunc: equinix_validation.StringIsMetroCode,
Description: networkDeviceLinkConnectionDescriptions["DestinationMetroCode"],
},
networkDeviceLinkConnectionSchemaNames["SourceZoneCode"]: {
Expand Down
3 changes: 2 additions & 1 deletion equinix/resource_network_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/equinix/ne-go"
"github.com/equinix/rest-go"
"github.com/equinix/terraform-provider-equinix/internal/config"
equinix_validation "github.com/equinix/terraform-provider-equinix/internal/validation"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
Expand Down Expand Up @@ -75,7 +76,7 @@ func createNetworkFileSchema() map[string]*schema.Schema {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: stringIsMetroCode(),
ValidateFunc: equinix_validation.StringIsMetroCode,
Description: networkFileDescriptions["MetroCode"],
},
networkFileSchemaNames["DeviceTypeCode"]: {
Expand Down
16 changes: 16 additions & 0 deletions internal/validation/strings.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package validation

import (
"regexp"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)

var (
StringIsMetroCode = validation.StringMatch(regexp.MustCompile("^[A-Z]{2}$"), "MetroCode must consist of two capital letters")
StringIsEmailAddress = validation.StringMatch(regexp.MustCompile("^[^ @]+@[^ @]+$"), "not valid email address")
StringIsPortDefinition = validation.StringMatch(
regexp.MustCompile("^(([0-9]+(,[0-9]+){0,9})|([0-9]+-[0-9]+)|(any))$"),
"port definition has to be: up to 10 comma sepparated numbers (22,23), range (20-23) or word 'any'")
StringIsSpeedBand = validation.StringMatch(regexp.MustCompile("^[0-9]+(MB|GB)$"), "SpeedBand should consist of digit followed by MB or GB")
)

0 comments on commit c65de29

Please sign in to comment.