Skip to content

Commit 9a16187

Browse files
authored
feat: add enum validation (#2600)
* bump sdk go * add enumvalues interface * replace all validatefunc * remove deprecated k8s migration pn * lint * add ValidateEnumIgnoreCase
1 parent 039a459 commit 9a16187

32 files changed

+303
-490
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ require (
2323
github.com/nats-io/jwt/v2 v2.5.7
2424
github.com/nats-io/nats.go v1.35.0
2525
github.com/robfig/cron/v3 v3.0.1
26-
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.26.0.20240503144623-358f61d22470
26+
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.27.0.20240603133732-526ae94f1caa
2727
github.com/stretchr/testify v1.9.0
2828
golang.org/x/crypto v0.23.0
2929
gopkg.in/dnaeon/go-vcr.v3 v3.2.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,8 +243,8 @@ github.com/posener/complete v1.2.3 h1:NP0eAhjcjImqslEwo/1hq7gpajME0fTLTezBKDqfXq
243243
github.com/posener/complete v1.2.3/go.mod h1:WZIdtGGp+qx0sLrYKtIRAruyNpv6hFCicSgv7Sy7s/s=
244244
github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
245245
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
246-
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.26.0.20240503144623-358f61d22470 h1:jOsaUs0Mpg/2UCEcB/z3YpyG1UtgsjOPSKCRaOdMntg=
247-
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.26.0.20240503144623-358f61d22470/go.mod h1:fCa7OJZ/9DRTnOKmxvT6pn+LPWUptQAmHF/SBJUGEcg=
246+
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.27.0.20240603133732-526ae94f1caa h1:i+rtstvLVsx9zBDmVir/PssINIURdrXaCRpBW5+ctCs=
247+
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.27.0.20240603133732-526ae94f1caa/go.mod h1:fCa7OJZ/9DRTnOKmxvT6pn+LPWUptQAmHF/SBJUGEcg=
248248
github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 h1:n661drycOFuPLCN3Uc8sB6B/s6Z4t2xvBgU1htSHuq8=
249249
github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3/go.mod h1:A0bzQcvG0E7Rwjx0REVgAGH58e96+X0MeOfepqsbeW4=
250250
github.com/shopspring/decimal v1.2.0 h1:abSATXmQEYyShuxI4/vyW3tV1MrKAJzCZ/0zLUXYbsQ=

internal/services/baremetal/offer_data_source.go

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ import (
66

77
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
88
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
9-
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
109
"github.com/scaleway/scaleway-sdk-go/api/baremetal/v1"
1110
"github.com/scaleway/scaleway-sdk-go/scw"
1211
"github.com/scaleway/terraform-provider-scaleway/v2/internal/datasource"
1312
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/zonal"
13+
"github.com/scaleway/terraform-provider-scaleway/v2/internal/verify"
1414
)
1515

1616
func DataSourceOffer() *schema.Resource {
@@ -25,15 +25,11 @@ func DataSourceOffer() *schema.Resource {
2525
ConflictsWith: []string{"offer_id"},
2626
},
2727
"subscription_period": {
28-
Type: schema.TypeString,
29-
Optional: true,
30-
ValidateFunc: validation.StringInSlice([]string{
31-
baremetal.OfferSubscriptionPeriodUnknownSubscriptionPeriod.String(),
32-
baremetal.OfferSubscriptionPeriodHourly.String(),
33-
baremetal.OfferSubscriptionPeriodMonthly.String(),
34-
}, false),
35-
Description: "Period of subscription the desired offer",
36-
ConflictsWith: []string{"offer_id"},
28+
Type: schema.TypeString,
29+
Optional: true,
30+
ValidateDiagFunc: verify.ValidateEnum[baremetal.OfferSubscriptionPeriod](),
31+
Description: "Period of subscription the desired offer",
32+
ConflictsWith: []string{"offer_id"},
3733
},
3834
"offer_id": {
3935
Type: schema.TypeString,

internal/services/cockpit/grafana_user.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/scaleway/scaleway-sdk-go/scw"
1313
"github.com/scaleway/terraform-provider-scaleway/v2/internal/httperrors"
1414
"github.com/scaleway/terraform-provider-scaleway/v2/internal/services/account"
15+
"github.com/scaleway/terraform-provider-scaleway/v2/internal/verify"
1516
)
1617

1718
func ResourceCockpitGrafanaUser() *schema.Resource {
@@ -43,14 +44,11 @@ func ResourceCockpitGrafanaUser() *schema.Resource {
4344
Sensitive: true,
4445
},
4546
"role": {
46-
Type: schema.TypeString,
47-
Required: true,
48-
ForceNew: true,
49-
Description: "The role of the Grafana user",
50-
ValidateFunc: validation.StringInSlice([]string{
51-
cockpit.GrafanaUserRoleEditor.String(),
52-
cockpit.GrafanaUserRoleViewer.String(),
53-
}, false),
47+
Type: schema.TypeString,
48+
Required: true,
49+
ForceNew: true,
50+
Description: "The role of the Grafana user",
51+
ValidateDiagFunc: verify.ValidateEnum[cockpit.GrafanaUserRole](),
5452
},
5553
"project_id": account.ProjectIDSchema(),
5654
},

internal/services/cockpit/source.go

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ import (
55

66
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
77
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
8-
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
98
"github.com/scaleway/scaleway-sdk-go/api/cockpit/v1"
109
"github.com/scaleway/scaleway-sdk-go/scw"
1110
"github.com/scaleway/terraform-provider-scaleway/v2/internal/httperrors"
1211
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/regional"
1312
"github.com/scaleway/terraform-provider-scaleway/v2/internal/services/account"
1413
"github.com/scaleway/terraform-provider-scaleway/v2/internal/types"
14+
"github.com/scaleway/terraform-provider-scaleway/v2/internal/verify"
1515
)
1616

1717
func ResourceCockpitSource() *schema.Resource {
@@ -36,15 +36,11 @@ func ResourceCockpitSource() *schema.Resource {
3636
Description: "Name of the datasource",
3737
},
3838
"type": {
39-
Type: schema.TypeString,
40-
Optional: true,
41-
ForceNew: true,
42-
Description: "The type of the datasource",
43-
ValidateFunc: validation.StringInSlice([]string{
44-
cockpit.DataSourceTypeMetrics.String(),
45-
cockpit.DataSourceTypeLogs.String(),
46-
cockpit.DataSourceTypeTraces.String(),
47-
}, false),
39+
Type: schema.TypeString,
40+
Optional: true,
41+
ForceNew: true,
42+
Description: "The type of the datasource",
43+
ValidateDiagFunc: verify.ValidateEnum[cockpit.DataSourceType](),
4844
},
4945
// computed
5046
"url": {

internal/services/container/container.go

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality"
1313
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/regional"
1414
"github.com/scaleway/terraform-provider-scaleway/v2/internal/types"
15+
"github.com/scaleway/terraform-provider-scaleway/v2/internal/verify"
1516
)
1617

1718
const (
@@ -106,14 +107,11 @@ func ResourceContainer() *schema.Resource {
106107
Description: "The maximum amount of time in seconds during which your container can process a request before we stop it. Defaults to 300s.",
107108
},
108109
"privacy": {
109-
Type: schema.TypeString,
110-
Optional: true,
111-
Description: "The privacy type define the way to authenticate to your container",
112-
Default: container.ContainerPrivacyPublic,
113-
ValidateFunc: validation.StringInSlice([]string{
114-
container.ContainerPrivacyPublic.String(),
115-
container.ContainerPrivacyPrivate.String(),
116-
}, false),
110+
Type: schema.TypeString,
111+
Optional: true,
112+
Description: "The privacy type define the way to authenticate to your container",
113+
Default: container.ContainerPrivacyPublic,
114+
ValidateDiagFunc: verify.ValidateEnum[container.ContainerPrivacy](),
117115
},
118116
"registry_image": {
119117
Type: schema.TypeString,
@@ -140,14 +138,11 @@ func ResourceContainer() *schema.Resource {
140138
Description: "The native container domain name.",
141139
},
142140
"protocol": {
143-
Type: schema.TypeString,
144-
Optional: true,
145-
Description: "The communication protocol http1 or h2c. Defaults to http1.",
146-
Default: container.ContainerProtocolHTTP1.String(),
147-
ValidateFunc: validation.StringInSlice([]string{
148-
container.ContainerProtocolH2c.String(),
149-
container.ContainerProtocolHTTP1.String(),
150-
}, false),
141+
Type: schema.TypeString,
142+
Optional: true,
143+
Description: "The communication protocol http1 or h2c. Defaults to http1.",
144+
Default: container.ContainerProtocolHTTP1.String(),
145+
ValidateDiagFunc: verify.ValidateEnum[container.ContainerProtocol](),
151146
},
152147
"port": {
153148
Type: schema.TypeInt,
@@ -162,14 +157,11 @@ func ResourceContainer() *schema.Resource {
162157
Default: false,
163158
},
164159
"http_option": {
165-
Type: schema.TypeString,
166-
Optional: true,
167-
Description: "HTTP traffic configuration",
168-
Default: container.ContainerHTTPOptionEnabled.String(),
169-
ValidateFunc: validation.StringInSlice([]string{
170-
container.ContainerHTTPOptionEnabled.String(),
171-
container.ContainerHTTPOptionRedirected.String(),
172-
}, false),
160+
Type: schema.TypeString,
161+
Optional: true,
162+
Description: "HTTP traffic configuration",
163+
Default: container.ContainerHTTPOptionEnabled.String(),
164+
ValidateDiagFunc: verify.ValidateEnum[container.ContainerHTTPOption](),
173165
},
174166
// computed
175167
"status": {

internal/services/documentdb/instance.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ import (
77

88
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
99
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
10-
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
1110
documentdb "github.com/scaleway/scaleway-sdk-go/api/documentdb/v1beta1"
1211
"github.com/scaleway/scaleway-sdk-go/scw"
1312
"github.com/scaleway/terraform-provider-scaleway/v2/internal/dsf"
1413
"github.com/scaleway/terraform-provider-scaleway/v2/internal/httperrors"
1514
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/regional"
1615
"github.com/scaleway/terraform-provider-scaleway/v2/internal/services/account"
1716
"github.com/scaleway/terraform-provider-scaleway/v2/internal/types"
17+
"github.com/scaleway/terraform-provider-scaleway/v2/internal/verify"
1818
)
1919

2020
func ResourceInstance() *schema.Resource {
@@ -73,14 +73,11 @@ func ResourceInstance() *schema.Resource {
7373
Description: "Password for the first user of the database instance",
7474
},
7575
"volume_type": {
76-
Type: schema.TypeString,
77-
Default: documentdb.VolumeTypeBssd,
78-
Optional: true,
79-
ValidateFunc: validation.StringInSlice([]string{
80-
documentdb.VolumeTypeLssd.String(),
81-
documentdb.VolumeTypeBssd.String(),
82-
}, false),
83-
Description: "Type of volume where data are stored",
76+
Type: schema.TypeString,
77+
Default: documentdb.VolumeTypeBssd,
78+
Optional: true,
79+
ValidateDiagFunc: verify.ValidateEnum[documentdb.VolumeType](),
80+
Description: "Type of volume where data are stored",
8481
},
8582
"volume_size_in_gb": {
8683
Type: schema.TypeInt,

internal/services/documentdb/privilege.go

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
99
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
1010
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
11-
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
1211
documentdb "github.com/scaleway/scaleway-sdk-go/api/documentdb/v1beta1"
1312
"github.com/scaleway/scaleway-sdk-go/scw"
1413
"github.com/scaleway/terraform-provider-scaleway/v2/internal/cdf"
@@ -54,16 +53,10 @@ func ResourcePrivilege() *schema.Resource {
5453
Required: true,
5554
},
5655
"permission": {
57-
Type: schema.TypeString,
58-
Description: "Privilege",
59-
ValidateFunc: validation.StringInSlice([]string{
60-
documentdb.PermissionReadonly.String(),
61-
documentdb.PermissionReadwrite.String(),
62-
documentdb.PermissionAll.String(),
63-
documentdb.PermissionCustom.String(),
64-
documentdb.PermissionNone.String(),
65-
}, false),
66-
Required: true,
56+
Type: schema.TypeString,
57+
Description: "Privilege",
58+
ValidateDiagFunc: verify.ValidateEnum[documentdb.Permission](),
59+
Required: true,
6760
},
6861
// Common
6962
"region": regional.Schema(),

internal/services/domain/record.go

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/scaleway/terraform-provider-scaleway/v2/internal/httperrors"
1616
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality"
1717
"github.com/scaleway/terraform-provider-scaleway/v2/internal/services/account"
18+
"github.com/scaleway/terraform-provider-scaleway/v2/internal/verify"
1819
)
1920

2021
var changeKeys = []string{
@@ -81,24 +82,11 @@ func ResourceRecord() *schema.Resource {
8182
},
8283
},
8384
"type": {
84-
Type: schema.TypeString,
85-
Description: "The type of the record",
86-
ValidateFunc: validation.StringInSlice([]string{
87-
domain.RecordTypeA.String(),
88-
domain.RecordTypeAAAA.String(),
89-
domain.RecordTypeALIAS.String(),
90-
domain.RecordTypeCNAME.String(),
91-
domain.RecordTypeDNAME.String(),
92-
domain.RecordTypeMX.String(),
93-
domain.RecordTypeNS.String(),
94-
domain.RecordTypePTR.String(),
95-
domain.RecordTypeSRV.String(),
96-
domain.RecordTypeTXT.String(),
97-
domain.RecordTypeTLSA.String(),
98-
domain.RecordTypeCAA.String(),
99-
}, false),
100-
ForceNew: true,
101-
Required: true,
85+
Type: schema.TypeString,
86+
Description: "The type of the record",
87+
ValidateDiagFunc: verify.ValidateEnum[domain.RecordType](),
88+
ForceNew: true,
89+
Required: true,
10290
},
10391
"data": {
10492
Type: schema.TypeString,
@@ -200,14 +188,10 @@ func ResourceRecord() *schema.Resource {
200188
Description: "User-agent used when checking the URL",
201189
},
202190
"strategy": {
203-
Type: schema.TypeString,
204-
Required: true,
205-
Description: "Strategy to return an IP from the IPs list",
206-
ValidateFunc: validation.StringInSlice([]string{
207-
domain.RecordHTTPServiceConfigStrategyRandom.String(),
208-
domain.RecordHTTPServiceConfigStrategyHashed.String(),
209-
domain.RecordHTTPServiceConfigStrategyAll.String(),
210-
}, false),
191+
Type: schema.TypeString,
192+
Required: true,
193+
Description: "Strategy to return an IP from the IPs list",
194+
ValidateDiagFunc: verify.ValidateEnum[domain.RecordHTTPServiceConfigStrategy](),
211195
},
212196
},
213197
},

internal/services/flexibleip/mac_address.go

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55

66
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
77
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
8-
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
98
flexibleip "github.com/scaleway/scaleway-sdk-go/api/flexibleip/v1alpha1"
109
"github.com/scaleway/scaleway-sdk-go/scw"
1110
"github.com/scaleway/terraform-provider-scaleway/v2/internal/cdf"
@@ -41,14 +40,10 @@ func ResourceMACAddress() *schema.Resource {
4140
Description: "The ID of the flexible IP for which to generate a virtual MAC",
4241
},
4342
"type": {
44-
Type: schema.TypeString,
45-
Required: true,
46-
Description: "The type of the virtual MAC",
47-
ValidateFunc: validation.StringInSlice([]string{
48-
flexibleip.MACAddressTypeVmware.String(),
49-
flexibleip.MACAddressTypeXen.String(),
50-
flexibleip.MACAddressTypeKvm.String(),
51-
}, false),
43+
Type: schema.TypeString,
44+
Required: true,
45+
Description: "The type of the virtual MAC",
46+
ValidateDiagFunc: verify.ValidateEnum[flexibleip.MACAddressType](),
5247
},
5348
"flexible_ip_ids_to_duplicate": {
5449
Type: schema.TypeSet,

0 commit comments

Comments
 (0)