Skip to content

Commit 5b864a6

Browse files
authored
chore: migrate verify to its own package (#2451)
1 parent 38c3729 commit 5b864a6

File tree

99 files changed

+361
-243
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

99 files changed

+361
-243
lines changed

internal/verify/email.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package verify
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
7+
"github.com/scaleway/scaleway-sdk-go/validation"
8+
)
9+
10+
func IsEmail() schema.SchemaValidateFunc {
11+
return func(v interface{}, key string) (warnings []string, errors []error) {
12+
email, isString := v.(string)
13+
if !isString {
14+
return nil, []error{fmt.Errorf("invalid email for key '%s': not a string", key)}
15+
}
16+
17+
if !validation.IsEmail(email) {
18+
return nil, []error{fmt.Errorf("invalid email for key '%s': '%s': should contain valid '@' character", key, email)}
19+
}
20+
21+
return
22+
}
23+
}

internal/verify/ip.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package verify
2+
3+
import (
4+
"fmt"
5+
"net"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
8+
)
9+
10+
func IsStandaloneIPorCIDR() schema.SchemaValidateFunc {
11+
return func(val interface{}, key string) (warns []string, errs []error) {
12+
ip, isString := val.(string)
13+
if !isString {
14+
return nil, []error{fmt.Errorf("invalid input for key '%s': not a string", key)}
15+
}
16+
17+
// Check if it's a standalone IP address
18+
if net.ParseIP(ip) != nil {
19+
return
20+
}
21+
22+
// Check if it's an IP with CIDR notation
23+
_, _, err := net.ParseCIDR(ip)
24+
if err != nil {
25+
errs = append(errs, fmt.Errorf("%q is not a valid IP address or CIDR notation: %s", key, ip))
26+
}
27+
28+
return
29+
}
30+
}

internal/verify/ip_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package verify_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/scaleway/terraform-provider-scaleway/v2/internal/verify"
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func TestValidateStandaloneIPorCIDRWithValidIPReturnNothing(t *testing.T) {
11+
assert := assert.New(t)
12+
13+
for _, ip := range []string{"192.168.1.1", "2001:0db8:85a3:0000:0000:8a2e:0370:7334", "10.0.0.0/24", "2001:0db8:85a3::8a2e:0370:7334/64"} {
14+
warnings, errors := verify.IsStandaloneIPorCIDR()(ip, "key")
15+
assert.Empty(warnings)
16+
assert.Empty(errors)
17+
}
18+
}
19+
20+
func TestValidateStandaloneIPorCIDRWithInvalidIPReturnError(t *testing.T) {
21+
assert := assert.New(t)
22+
23+
for _, ip := range []string{"10.0.0", "256.256.256.256", "2001::85a3::8a2e:0370:7334", "10.0.0.0/34"} {
24+
warnings, errors := verify.IsStandaloneIPorCIDR()(ip, "key")
25+
assert.Empty(warnings)
26+
assert.Len(errors, 1)
27+
}
28+
}

internal/verify/uuid.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package verify
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
7+
"github.com/scaleway/scaleway-sdk-go/validation"
8+
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality"
9+
)
10+
11+
// IsUUIDorUUIDWithLocality validates the schema is a UUID or the combination of a locality and a UUID
12+
// e.g. "6ba7b810-9dad-11d1-80b4-00c04fd430c8" or "fr-par-1/6ba7b810-9dad-11d1-80b4-00c04fd430c8".
13+
func IsUUIDorUUIDWithLocality() schema.SchemaValidateFunc {
14+
return func(v interface{}, key string) ([]string, []error) {
15+
return IsUUID()(locality.ExpandID(v), key)
16+
}
17+
}
18+
19+
// IsUUID validates the schema following the canonical UUID format
20+
// "6ba7b810-9dad-11d1-80b4-00c04fd430c8".
21+
func IsUUID() schema.SchemaValidateFunc {
22+
return func(v interface{}, key string) (warnings []string, errors []error) {
23+
uuid, isString := v.(string)
24+
if !isString {
25+
return nil, []error{fmt.Errorf("invalid UUID for key '%s': not a string", key)}
26+
}
27+
28+
if !validation.IsUUID(uuid) {
29+
return nil, []error{fmt.Errorf("invalid UUID for key '%s': '%s' (%d): format should be 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' (36) and contains valid hexadecimal characters", key, uuid, len(uuid))}
30+
}
31+
32+
return
33+
}
34+
}
35+
36+
func IsUUIDWithLocality() schema.SchemaValidateFunc {
37+
return func(v interface{}, key string) (warnings []string, errors []error) {
38+
uuid, isString := v.(string)
39+
if !isString {
40+
errors = []error{fmt.Errorf("invalid UUID for key '%s': not a string", key)}
41+
return
42+
}
43+
_, subUUID, err := locality.ParseLocalizedID(uuid)
44+
if err != nil {
45+
errors = []error{fmt.Errorf("invalid UUID with locality for key '%s': '%s' (%d): format should be 'locality/uuid'", key, uuid, len(uuid))}
46+
return
47+
}
48+
return IsUUID()(subUUID, key)
49+
}
50+
}
Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
1-
package scaleway
1+
package verify_test
22

33
import (
44
"testing"
55

6+
"github.com/scaleway/terraform-provider-scaleway/v2/internal/verify"
67
"github.com/stretchr/testify/assert"
78
)
89

910
func TestValidationUUIDWithInvalidUUIDReturnError(t *testing.T) {
1011
assert := assert.New(t)
1112

1213
for _, uuid := range []string{"fr-par/wrong-uuid/resource", "fr-par/wrong-uuid", "wrong-uuid"} {
13-
warnings, errors := validationUUID()(uuid, "key")
14+
warnings, errors := verify.IsUUID()(uuid, "key")
1415
assert.Empty(warnings)
1516
assert.Len(errors, 1)
1617
}
@@ -19,7 +20,7 @@ func TestValidationUUIDWithInvalidUUIDReturnError(t *testing.T) {
1920
func TestValidationUUIDWithValidUUIDReturnNothing(t *testing.T) {
2021
assert := assert.New(t)
2122

22-
warnings, errors := validationUUID()("6ba7b810-9dad-11d1-80b4-00c04fd430c8", "key")
23+
warnings, errors := verify.IsUUID()("6ba7b810-9dad-11d1-80b4-00c04fd430c8", "key")
2324

2425
assert.Empty(warnings)
2526
assert.Empty(errors)
@@ -29,7 +30,7 @@ func TestValidationUUIDorUUIDWithLocalityWithValidUUIDReturnNothing(t *testing.T
2930
assert := assert.New(t)
3031

3132
for _, uuid := range []string{"fr-par/6ba7b810-9dad-11d1-80b4-00c04fd430c8", "6ba7b810-9dad-11d1-80b4-00c04fd430c8"} {
32-
warnings, errors := validationUUIDorUUIDWithLocality()(uuid, "key")
33+
warnings, errors := verify.IsUUIDorUUIDWithLocality()(uuid, "key")
3334
assert.Empty(warnings)
3435
assert.Empty(errors)
3536
}
@@ -39,7 +40,7 @@ func TestValidationUUIDorUUIDWithLocalityWithInvalidUUIDReturnError(t *testing.T
3940
assert := assert.New(t)
4041

4142
for _, uuid := range []string{"fr-par/wrong-uuid/resource", "fr-par/wrong-uuid", "wrong-uuid"} {
42-
warnings, errors := validationUUIDorUUIDWithLocality()(uuid, "key")
43+
warnings, errors := verify.IsUUIDorUUIDWithLocality()(uuid, "key")
4344
assert.Empty(warnings)
4445
assert.Len(errors, 1)
4546
}
@@ -49,7 +50,7 @@ func TestValidationUUIDWithLocalityWithValidUUIDReturnNothing(t *testing.T) {
4950
assert := assert.New(t)
5051

5152
for _, uuid := range []string{"fr-par/6ba7b810-9dad-11d1-80b4-00c04fd430c8"} {
52-
warnings, errors := validationUUIDWithLocality()(uuid, "key")
53+
warnings, errors := verify.IsUUIDWithLocality()(uuid, "key")
5354
assert.Empty(warnings)
5455
assert.Empty(errors)
5556
}
@@ -59,28 +60,8 @@ func TestValidationUUIDWithLocalityWithInvalidUUIDReturnError(t *testing.T) {
5960
assert := assert.New(t)
6061

6162
for _, uuid := range []string{"fr-par/wrong-uuid/resource", "fr-par/wrong-uuid", "wrong-uuid", "6ba7b810-9dad-11d1-80b4-00c04fd430c8"} {
62-
warnings, errors := validationUUIDWithLocality()(uuid, "key")
63+
warnings, errors := verify.IsUUIDWithLocality()(uuid, "key")
6364
assert.Empty(warnings)
6465
assert.Len(errors, 1, uuid)
6566
}
6667
}
67-
68-
func TestValidateStandaloneIPorCIDRWithValidIPReturnNothing(t *testing.T) {
69-
assert := assert.New(t)
70-
71-
for _, ip := range []string{"192.168.1.1", "2001:0db8:85a3:0000:0000:8a2e:0370:7334", "10.0.0.0/24", "2001:0db8:85a3::8a2e:0370:7334/64"} {
72-
warnings, errors := validateStandaloneIPorCIDR()(ip, "key")
73-
assert.Empty(warnings)
74-
assert.Empty(errors)
75-
}
76-
}
77-
78-
func TestValidateStandaloneIPorCIDRWithInvalidIPReturnError(t *testing.T) {
79-
assert := assert.New(t)
80-
81-
for _, ip := range []string{"10.0.0", "256.256.256.256", "2001::85a3::8a2e:0370:7334", "10.0.0.0/34"} {
82-
warnings, errors := validateStandaloneIPorCIDR()(ip, "key")
83-
assert.Empty(warnings)
84-
assert.Len(errors, 1)
85-
}
86-
}

scaleway/data_source_account_project.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
accountV3 "github.com/scaleway/scaleway-sdk-go/api/account/v3"
99
"github.com/scaleway/scaleway-sdk-go/scw"
1010
"github.com/scaleway/terraform-provider-scaleway/v2/internal/meta"
11+
"github.com/scaleway/terraform-provider-scaleway/v2/internal/verify"
1112
)
1213

1314
func dataSourceScalewayAccountProject() *schema.Resource {
@@ -20,7 +21,7 @@ func dataSourceScalewayAccountProject() *schema.Resource {
2021
Computed: true,
2122
Optional: true,
2223
Description: "The ID of the SSH key",
23-
ValidateFunc: validationUUID(),
24+
ValidateFunc: verify.IsUUID(),
2425
}
2526

2627
return &schema.Resource{

scaleway/data_source_baremetal_option.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/scaleway/scaleway-sdk-go/api/baremetal/v1"
1010
"github.com/scaleway/scaleway-sdk-go/scw"
1111
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/zonal"
12+
"github.com/scaleway/terraform-provider-scaleway/v2/internal/verify"
1213
)
1314

1415
func dataSourceScalewayBaremetalOption() *schema.Resource {
@@ -25,7 +26,7 @@ func dataSourceScalewayBaremetalOption() *schema.Resource {
2526
Type: schema.TypeString,
2627
Optional: true,
2728
Description: "The ID of the option",
28-
ValidateFunc: validationUUIDorUUIDWithLocality(),
29+
ValidateFunc: verify.IsUUIDorUUIDWithLocality(),
2930
ConflictsWith: []string{"name"},
3031
},
3132
"manageable": {

scaleway/data_source_baremetal_os.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/scaleway/scaleway-sdk-go/api/baremetal/v1"
1010
"github.com/scaleway/scaleway-sdk-go/scw"
1111
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/zonal"
12+
"github.com/scaleway/terraform-provider-scaleway/v2/internal/verify"
1213
)
1314

1415
func dataSourceScalewayBaremetalOs() *schema.Resource {
@@ -31,7 +32,7 @@ func dataSourceScalewayBaremetalOs() *schema.Resource {
3132
Type: schema.TypeString,
3233
Optional: true,
3334
Description: "The ID of the os",
34-
ValidateFunc: validationUUIDorUUIDWithLocality(),
35+
ValidateFunc: verify.IsUUIDorUUIDWithLocality(),
3536
ConflictsWith: []string{"name"},
3637
},
3738
"zone": zonal.Schema(),

scaleway/data_source_baremetal_server.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
88
"github.com/scaleway/scaleway-sdk-go/api/baremetal/v1"
99
"github.com/scaleway/scaleway-sdk-go/scw"
10+
"github.com/scaleway/terraform-provider-scaleway/v2/internal/verify"
1011
)
1112

1213
func dataSourceScalewayBaremetalServer() *schema.Resource {
@@ -21,7 +22,7 @@ func dataSourceScalewayBaremetalServer() *schema.Resource {
2122
Type: schema.TypeString,
2223
Optional: true,
2324
Description: "The ID of the server",
24-
ValidateFunc: validationUUIDorUUIDWithLocality(),
25+
ValidateFunc: verify.IsUUIDorUUIDWithLocality(),
2526
ConflictsWith: []string{"name"},
2627
}
2728

scaleway/data_source_block_snapshot.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
77
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
88
block "github.com/scaleway/scaleway-sdk-go/api/block/v1alpha1"
9+
"github.com/scaleway/terraform-provider-scaleway/v2/internal/verify"
910
)
1011

1112
func dataSourceScalewayBlockSnapshot() *schema.Resource {
@@ -19,7 +20,7 @@ func dataSourceScalewayBlockSnapshot() *schema.Resource {
1920
Optional: true,
2021
Description: "The ID of the snapshot",
2122
ConflictsWith: []string{"name"},
22-
ValidateFunc: validationUUIDorUUIDWithLocality(),
23+
ValidateFunc: verify.IsUUIDorUUIDWithLocality(),
2324
}
2425

2526
return &schema.Resource{

scaleway/data_source_block_volume.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
77
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
88
block "github.com/scaleway/scaleway-sdk-go/api/block/v1alpha1"
9+
"github.com/scaleway/terraform-provider-scaleway/v2/internal/verify"
910
)
1011

1112
func dataSourceScalewayBlockVolume() *schema.Resource {
@@ -19,7 +20,7 @@ func dataSourceScalewayBlockVolume() *schema.Resource {
1920
Optional: true,
2021
Description: "The ID of the volume",
2122
ConflictsWith: []string{"name"},
22-
ValidateFunc: validationUUIDorUUIDWithLocality(),
23+
ValidateFunc: verify.IsUUIDorUUIDWithLocality(),
2324
}
2425

2526
return &schema.Resource{

scaleway/data_source_cockpit.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55

66
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
77
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
8+
"github.com/scaleway/terraform-provider-scaleway/v2/internal/verify"
89
)
910

1011
func dataSourceScalewayCockpit() *schema.Resource {
@@ -15,7 +16,7 @@ func dataSourceScalewayCockpit() *schema.Resource {
1516
Type: schema.TypeString,
1617
Description: "The project_id you want to attach the resource to",
1718
Optional: true,
18-
ValidateFunc: validationUUID(),
19+
ValidateFunc: verify.IsUUID(),
1920
}
2021
delete(dsSchema, "plan")
2122

scaleway/data_source_container.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
container "github.com/scaleway/scaleway-sdk-go/api/container/v1beta1"
99
"github.com/scaleway/scaleway-sdk-go/scw"
1010
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality"
11+
"github.com/scaleway/terraform-provider-scaleway/v2/internal/verify"
1112
)
1213

1314
func dataSourceScalewayContainer() *schema.Resource {
@@ -21,20 +22,20 @@ func dataSourceScalewayContainer() *schema.Resource {
2122
Type: schema.TypeString,
2223
Optional: true,
2324
Description: "The ID of the Container",
24-
ValidateFunc: validationUUIDorUUIDWithLocality(),
25+
ValidateFunc: verify.IsUUIDorUUIDWithLocality(),
2526
ConflictsWith: []string{"name"},
2627
}
2728
dsSchema["namespace_id"] = &schema.Schema{
2829
Type: schema.TypeString,
2930
Required: true,
3031
Description: "The ID of the Container namespace",
31-
ValidateFunc: validationUUIDorUUIDWithLocality(),
32+
ValidateFunc: verify.IsUUIDorUUIDWithLocality(),
3233
}
3334
dsSchema["project_id"] = &schema.Schema{
3435
Type: schema.TypeString,
3536
Optional: true,
3637
Description: "The ID of the project to filter the Container",
37-
ValidateFunc: validationUUID(),
38+
ValidateFunc: verify.IsUUID(),
3839
}
3940

4041
return &schema.Resource{

scaleway/data_source_container_namespace.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
88
container "github.com/scaleway/scaleway-sdk-go/api/container/v1beta1"
99
"github.com/scaleway/scaleway-sdk-go/scw"
10+
"github.com/scaleway/terraform-provider-scaleway/v2/internal/verify"
1011
)
1112

1213
func dataSourceScalewayContainerNamespace() *schema.Resource {
@@ -20,7 +21,7 @@ func dataSourceScalewayContainerNamespace() *schema.Resource {
2021
Type: schema.TypeString,
2122
Optional: true,
2223
Description: "The ID of the Container namespace",
23-
ValidateFunc: validationUUIDorUUIDWithLocality(),
24+
ValidateFunc: verify.IsUUIDorUUIDWithLocality(),
2425
ConflictsWith: []string{"name"},
2526
}
2627

0 commit comments

Comments
 (0)