From aa4bc61ce0d55511d33cde00132322d380ad55e3 Mon Sep 17 00:00:00 2001 From: Charles Treatman Date: Thu, 7 Dec 2023 10:23:05 -0600 Subject: [PATCH 1/2] refactor: clean up config package When the config struct was moved to its own package, we added a couple public variables to replace private ones. However, the original private variable definitions were left in place, and all references to the private variables were left unchanged. On closer inspection, one of the private variables is only used by one resource and can remain private, and the other one is for validation, not configuration. This moves the validation variable to a validation package and removes the unused variables. --- equinix/helpers_device.go | 3 ++- equinix/resource_metal_project.go | 8 +++----- internal/config/config.go | 5 ----- internal/validation/strings.go | 12 ++++++++++++ 4 files changed, 17 insertions(+), 11 deletions(-) create mode 100644 internal/validation/strings.go diff --git a/equinix/helpers_device.go b/equinix/helpers_device.go index 028ce803b..19f3f7a0b 100644 --- a/equinix/helpers_device.go +++ b/equinix/helpers_device.go @@ -15,6 +15,7 @@ import ( "github.com/equinix/terraform-provider-equinix/internal/converters" equinix_errors "github.com/equinix/terraform-provider-equinix/internal/errors" + equinix_validation "github.com/equinix/terraform-provider-equinix/internal/validation" metalv1 "github.com/equinix-labs/metal-go/metal/v1" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry" @@ -235,7 +236,7 @@ func ipAddressSchema() *schema.Resource { MinItems: 1, Elem: &schema.Schema{ Type: schema.TypeString, - ValidateFunc: validation.StringMatch(uuidRE, "must be a valid UUID"), + ValidateFunc: equinix_validation.StringIsUuid, }, }, }, diff --git a/equinix/resource_metal_project.go b/equinix/resource_metal_project.go index 5a1cf3769..06890a0d8 100644 --- a/equinix/resource_metal_project.go +++ b/equinix/resource_metal_project.go @@ -3,10 +3,10 @@ package equinix import ( "fmt" "path" - "regexp" "strings" equinix_errors "github.com/equinix/terraform-provider-equinix/internal/errors" + equinix_validation "github.com/equinix/terraform-provider-equinix/internal/validation" "github.com/equinix/terraform-provider-equinix/internal/config" @@ -15,8 +15,6 @@ import ( "github.com/packethost/packngo" ) -var uuidRE = regexp.MustCompile("^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-4[a-fA-F0-9]{3}-[8|9|aA|bB][a-fA-F0-9]{3}-[a-fA-F0-9]{12}$") - func resourceMetalProject() *schema.Resource { return &schema.Resource{ Create: resourceMetalProjectCreate, @@ -57,7 +55,7 @@ func resourceMetalProject() *schema.Resource { return strings.EqualFold(strings.Trim(old, `"`), strings.Trim(new, `"`)) }, ValidateFunc: validation.Any( - validation.StringMatch(uuidRE, "must be a valid UUID"), + equinix_validation.StringIsUuid, validation.StringIsEmpty, ), }, @@ -70,7 +68,7 @@ func resourceMetalProject() *schema.Resource { DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool { return strings.EqualFold(strings.Trim(old, `"`), strings.Trim(new, `"`)) }, - ValidateFunc: validation.StringMatch(uuidRE, "must be a valid UUID"), + ValidateFunc: equinix_validation.StringIsUuid, }, "bgp_config": { Type: schema.TypeList, diff --git a/internal/config/config.go b/internal/config/config.go index f16ddc7ae..d5d116b62 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -28,11 +28,6 @@ import ( xoauth2 "golang.org/x/oauth2" ) -var ( - UuidRE = regexp.MustCompile("^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-4[a-fA-F0-9]{3}-[8|9|aA|bB][a-fA-F0-9]{3}-[a-fA-F0-9]{12}$") - IpAddressTypes = []string{"public_ipv4", "private_ipv4", "public_ipv6"} -) - type ProviderMeta struct { ModuleName string `cty:"module_name"` } diff --git a/internal/validation/strings.go b/internal/validation/strings.go new file mode 100644 index 000000000..6e383d7ef --- /dev/null +++ b/internal/validation/strings.go @@ -0,0 +1,12 @@ +package validation + +import ( + "regexp" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" +) + +var ( + uuidRE = regexp.MustCompile("^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-4[a-fA-F0-9]{3}-[8|9|aA|bB][a-fA-F0-9]{3}-[a-fA-F0-9]{12}$") + StringIsUuid = validation.StringMatch(uuidRE, "must be a valid UUID") +) From 64081e90187e633056856c986286ee7a7e586a34 Mon Sep 17 00:00:00 2001 From: Charles Treatman Date: Fri, 8 Dec 2023 09:30:46 -0600 Subject: [PATCH 2/2] refactor: replace local UUID validator with upstream --- equinix/helpers_device.go | 3 +-- equinix/resource_metal_project.go | 5 ++--- internal/validation/strings.go | 12 ------------ 3 files changed, 3 insertions(+), 17 deletions(-) delete mode 100644 internal/validation/strings.go diff --git a/equinix/helpers_device.go b/equinix/helpers_device.go index 19f3f7a0b..74eccebbf 100644 --- a/equinix/helpers_device.go +++ b/equinix/helpers_device.go @@ -15,7 +15,6 @@ import ( "github.com/equinix/terraform-provider-equinix/internal/converters" equinix_errors "github.com/equinix/terraform-provider-equinix/internal/errors" - equinix_validation "github.com/equinix/terraform-provider-equinix/internal/validation" metalv1 "github.com/equinix-labs/metal-go/metal/v1" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry" @@ -236,7 +235,7 @@ func ipAddressSchema() *schema.Resource { MinItems: 1, Elem: &schema.Schema{ Type: schema.TypeString, - ValidateFunc: equinix_validation.StringIsUuid, + ValidateFunc: validation.IsUUID, }, }, }, diff --git a/equinix/resource_metal_project.go b/equinix/resource_metal_project.go index 06890a0d8..45b5f9b3e 100644 --- a/equinix/resource_metal_project.go +++ b/equinix/resource_metal_project.go @@ -6,7 +6,6 @@ import ( "strings" equinix_errors "github.com/equinix/terraform-provider-equinix/internal/errors" - equinix_validation "github.com/equinix/terraform-provider-equinix/internal/validation" "github.com/equinix/terraform-provider-equinix/internal/config" @@ -55,7 +54,7 @@ func resourceMetalProject() *schema.Resource { return strings.EqualFold(strings.Trim(old, `"`), strings.Trim(new, `"`)) }, ValidateFunc: validation.Any( - equinix_validation.StringIsUuid, + validation.IsUUID, validation.StringIsEmpty, ), }, @@ -68,7 +67,7 @@ func resourceMetalProject() *schema.Resource { DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool { return strings.EqualFold(strings.Trim(old, `"`), strings.Trim(new, `"`)) }, - ValidateFunc: equinix_validation.StringIsUuid, + ValidateFunc: validation.IsUUID, }, "bgp_config": { Type: schema.TypeList, diff --git a/internal/validation/strings.go b/internal/validation/strings.go deleted file mode 100644 index 6e383d7ef..000000000 --- a/internal/validation/strings.go +++ /dev/null @@ -1,12 +0,0 @@ -package validation - -import ( - "regexp" - - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" -) - -var ( - uuidRE = regexp.MustCompile("^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-4[a-fA-F0-9]{3}-[8|9|aA|bB][a-fA-F0-9]{3}-[a-fA-F0-9]{12}$") - StringIsUuid = validation.StringMatch(uuidRE, "must be a valid UUID") -)