Skip to content

Commit

Permalink
feat: add enum validation (#2600)
Browse files Browse the repository at this point in the history
* bump sdk go

* add enumvalues interface

* replace all validatefunc

* remove deprecated k8s migration pn

* lint

* add ValidateEnumIgnoreCase
  • Loading branch information
yfodil committed Jun 6, 2024
1 parent 039a459 commit 9a16187
Show file tree
Hide file tree
Showing 32 changed files with 303 additions and 490 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ require (
github.com/nats-io/jwt/v2 v2.5.7
github.com/nats-io/nats.go v1.35.0
github.com/robfig/cron/v3 v3.0.1
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.26.0.20240503144623-358f61d22470
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.27.0.20240603133732-526ae94f1caa
github.com/stretchr/testify v1.9.0
golang.org/x/crypto v0.23.0
gopkg.in/dnaeon/go-vcr.v3 v3.2.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,8 @@ github.com/posener/complete v1.2.3 h1:NP0eAhjcjImqslEwo/1hq7gpajME0fTLTezBKDqfXq
github.com/posener/complete v1.2.3/go.mod h1:WZIdtGGp+qx0sLrYKtIRAruyNpv6hFCicSgv7Sy7s/s=
github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.26.0.20240503144623-358f61d22470 h1:jOsaUs0Mpg/2UCEcB/z3YpyG1UtgsjOPSKCRaOdMntg=
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.26.0.20240503144623-358f61d22470/go.mod h1:fCa7OJZ/9DRTnOKmxvT6pn+LPWUptQAmHF/SBJUGEcg=
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.27.0.20240603133732-526ae94f1caa h1:i+rtstvLVsx9zBDmVir/PssINIURdrXaCRpBW5+ctCs=
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.27.0.20240603133732-526ae94f1caa/go.mod h1:fCa7OJZ/9DRTnOKmxvT6pn+LPWUptQAmHF/SBJUGEcg=
github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 h1:n661drycOFuPLCN3Uc8sB6B/s6Z4t2xvBgU1htSHuq8=
github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3/go.mod h1:A0bzQcvG0E7Rwjx0REVgAGH58e96+X0MeOfepqsbeW4=
github.com/shopspring/decimal v1.2.0 h1:abSATXmQEYyShuxI4/vyW3tV1MrKAJzCZ/0zLUXYbsQ=
Expand Down
16 changes: 6 additions & 10 deletions internal/services/baremetal/offer_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import (

"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"
"github.com/scaleway/scaleway-sdk-go/api/baremetal/v1"
"github.com/scaleway/scaleway-sdk-go/scw"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/datasource"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/zonal"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/verify"
)

func DataSourceOffer() *schema.Resource {
Expand All @@ -25,15 +25,11 @@ func DataSourceOffer() *schema.Resource {
ConflictsWith: []string{"offer_id"},
},
"subscription_period": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice([]string{
baremetal.OfferSubscriptionPeriodUnknownSubscriptionPeriod.String(),
baremetal.OfferSubscriptionPeriodHourly.String(),
baremetal.OfferSubscriptionPeriodMonthly.String(),
}, false),
Description: "Period of subscription the desired offer",
ConflictsWith: []string{"offer_id"},
Type: schema.TypeString,
Optional: true,
ValidateDiagFunc: verify.ValidateEnum[baremetal.OfferSubscriptionPeriod](),
Description: "Period of subscription the desired offer",
ConflictsWith: []string{"offer_id"},
},
"offer_id": {
Type: schema.TypeString,
Expand Down
14 changes: 6 additions & 8 deletions internal/services/cockpit/grafana_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/scaleway/scaleway-sdk-go/scw"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/httperrors"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/services/account"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/verify"
)

func ResourceCockpitGrafanaUser() *schema.Resource {
Expand Down Expand Up @@ -43,14 +44,11 @@ func ResourceCockpitGrafanaUser() *schema.Resource {
Sensitive: true,
},
"role": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "The role of the Grafana user",
ValidateFunc: validation.StringInSlice([]string{
cockpit.GrafanaUserRoleEditor.String(),
cockpit.GrafanaUserRoleViewer.String(),
}, false),
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "The role of the Grafana user",
ValidateDiagFunc: verify.ValidateEnum[cockpit.GrafanaUserRole](),
},
"project_id": account.ProjectIDSchema(),
},
Expand Down
16 changes: 6 additions & 10 deletions internal/services/cockpit/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import (

"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"
"github.com/scaleway/scaleway-sdk-go/api/cockpit/v1"
"github.com/scaleway/scaleway-sdk-go/scw"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/httperrors"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/regional"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/services/account"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/types"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/verify"
)

func ResourceCockpitSource() *schema.Resource {
Expand All @@ -36,15 +36,11 @@ func ResourceCockpitSource() *schema.Resource {
Description: "Name of the datasource",
},
"type": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Description: "The type of the datasource",
ValidateFunc: validation.StringInSlice([]string{
cockpit.DataSourceTypeMetrics.String(),
cockpit.DataSourceTypeLogs.String(),
cockpit.DataSourceTypeTraces.String(),
}, false),
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Description: "The type of the datasource",
ValidateDiagFunc: verify.ValidateEnum[cockpit.DataSourceType](),
},
// computed
"url": {
Expand Down
40 changes: 16 additions & 24 deletions internal/services/container/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/regional"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/types"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/verify"
)

const (
Expand Down Expand Up @@ -106,14 +107,11 @@ func ResourceContainer() *schema.Resource {
Description: "The maximum amount of time in seconds during which your container can process a request before we stop it. Defaults to 300s.",
},
"privacy": {
Type: schema.TypeString,
Optional: true,
Description: "The privacy type define the way to authenticate to your container",
Default: container.ContainerPrivacyPublic,
ValidateFunc: validation.StringInSlice([]string{
container.ContainerPrivacyPublic.String(),
container.ContainerPrivacyPrivate.String(),
}, false),
Type: schema.TypeString,
Optional: true,
Description: "The privacy type define the way to authenticate to your container",
Default: container.ContainerPrivacyPublic,
ValidateDiagFunc: verify.ValidateEnum[container.ContainerPrivacy](),
},
"registry_image": {
Type: schema.TypeString,
Expand All @@ -140,14 +138,11 @@ func ResourceContainer() *schema.Resource {
Description: "The native container domain name.",
},
"protocol": {
Type: schema.TypeString,
Optional: true,
Description: "The communication protocol http1 or h2c. Defaults to http1.",
Default: container.ContainerProtocolHTTP1.String(),
ValidateFunc: validation.StringInSlice([]string{
container.ContainerProtocolH2c.String(),
container.ContainerProtocolHTTP1.String(),
}, false),
Type: schema.TypeString,
Optional: true,
Description: "The communication protocol http1 or h2c. Defaults to http1.",
Default: container.ContainerProtocolHTTP1.String(),
ValidateDiagFunc: verify.ValidateEnum[container.ContainerProtocol](),
},
"port": {
Type: schema.TypeInt,
Expand All @@ -162,14 +157,11 @@ func ResourceContainer() *schema.Resource {
Default: false,
},
"http_option": {
Type: schema.TypeString,
Optional: true,
Description: "HTTP traffic configuration",
Default: container.ContainerHTTPOptionEnabled.String(),
ValidateFunc: validation.StringInSlice([]string{
container.ContainerHTTPOptionEnabled.String(),
container.ContainerHTTPOptionRedirected.String(),
}, false),
Type: schema.TypeString,
Optional: true,
Description: "HTTP traffic configuration",
Default: container.ContainerHTTPOptionEnabled.String(),
ValidateDiagFunc: verify.ValidateEnum[container.ContainerHTTPOption](),
},
// computed
"status": {
Expand Down
15 changes: 6 additions & 9 deletions internal/services/documentdb/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ import (

"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"
documentdb "github.com/scaleway/scaleway-sdk-go/api/documentdb/v1beta1"
"github.com/scaleway/scaleway-sdk-go/scw"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/dsf"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/httperrors"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/regional"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/services/account"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/types"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/verify"
)

func ResourceInstance() *schema.Resource {
Expand Down Expand Up @@ -73,14 +73,11 @@ func ResourceInstance() *schema.Resource {
Description: "Password for the first user of the database instance",
},
"volume_type": {
Type: schema.TypeString,
Default: documentdb.VolumeTypeBssd,
Optional: true,
ValidateFunc: validation.StringInSlice([]string{
documentdb.VolumeTypeLssd.String(),
documentdb.VolumeTypeBssd.String(),
}, false),
Description: "Type of volume where data are stored",
Type: schema.TypeString,
Default: documentdb.VolumeTypeBssd,
Optional: true,
ValidateDiagFunc: verify.ValidateEnum[documentdb.VolumeType](),
Description: "Type of volume where data are stored",
},
"volume_size_in_gb": {
Type: schema.TypeInt,
Expand Down
15 changes: 4 additions & 11 deletions internal/services/documentdb/privilege.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
documentdb "github.com/scaleway/scaleway-sdk-go/api/documentdb/v1beta1"
"github.com/scaleway/scaleway-sdk-go/scw"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/cdf"
Expand Down Expand Up @@ -54,16 +53,10 @@ func ResourcePrivilege() *schema.Resource {
Required: true,
},
"permission": {
Type: schema.TypeString,
Description: "Privilege",
ValidateFunc: validation.StringInSlice([]string{
documentdb.PermissionReadonly.String(),
documentdb.PermissionReadwrite.String(),
documentdb.PermissionAll.String(),
documentdb.PermissionCustom.String(),
documentdb.PermissionNone.String(),
}, false),
Required: true,
Type: schema.TypeString,
Description: "Privilege",
ValidateDiagFunc: verify.ValidateEnum[documentdb.Permission](),
Required: true,
},
// Common
"region": regional.Schema(),
Expand Down
36 changes: 10 additions & 26 deletions internal/services/domain/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/scaleway/terraform-provider-scaleway/v2/internal/httperrors"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/services/account"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/verify"
)

var changeKeys = []string{
Expand Down Expand Up @@ -81,24 +82,11 @@ func ResourceRecord() *schema.Resource {
},
},
"type": {
Type: schema.TypeString,
Description: "The type of the record",
ValidateFunc: validation.StringInSlice([]string{
domain.RecordTypeA.String(),
domain.RecordTypeAAAA.String(),
domain.RecordTypeALIAS.String(),
domain.RecordTypeCNAME.String(),
domain.RecordTypeDNAME.String(),
domain.RecordTypeMX.String(),
domain.RecordTypeNS.String(),
domain.RecordTypePTR.String(),
domain.RecordTypeSRV.String(),
domain.RecordTypeTXT.String(),
domain.RecordTypeTLSA.String(),
domain.RecordTypeCAA.String(),
}, false),
ForceNew: true,
Required: true,
Type: schema.TypeString,
Description: "The type of the record",
ValidateDiagFunc: verify.ValidateEnum[domain.RecordType](),
ForceNew: true,
Required: true,
},
"data": {
Type: schema.TypeString,
Expand Down Expand Up @@ -200,14 +188,10 @@ func ResourceRecord() *schema.Resource {
Description: "User-agent used when checking the URL",
},
"strategy": {
Type: schema.TypeString,
Required: true,
Description: "Strategy to return an IP from the IPs list",
ValidateFunc: validation.StringInSlice([]string{
domain.RecordHTTPServiceConfigStrategyRandom.String(),
domain.RecordHTTPServiceConfigStrategyHashed.String(),
domain.RecordHTTPServiceConfigStrategyAll.String(),
}, false),
Type: schema.TypeString,
Required: true,
Description: "Strategy to return an IP from the IPs list",
ValidateDiagFunc: verify.ValidateEnum[domain.RecordHTTPServiceConfigStrategy](),
},
},
},
Expand Down
13 changes: 4 additions & 9 deletions internal/services/flexibleip/mac_address.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

"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"
flexibleip "github.com/scaleway/scaleway-sdk-go/api/flexibleip/v1alpha1"
"github.com/scaleway/scaleway-sdk-go/scw"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/cdf"
Expand Down Expand Up @@ -41,14 +40,10 @@ func ResourceMACAddress() *schema.Resource {
Description: "The ID of the flexible IP for which to generate a virtual MAC",
},
"type": {
Type: schema.TypeString,
Required: true,
Description: "The type of the virtual MAC",
ValidateFunc: validation.StringInSlice([]string{
flexibleip.MACAddressTypeVmware.String(),
flexibleip.MACAddressTypeXen.String(),
flexibleip.MACAddressTypeKvm.String(),
}, false),
Type: schema.TypeString,
Required: true,
Description: "The type of the virtual MAC",
ValidateDiagFunc: verify.ValidateEnum[flexibleip.MACAddressType](),
},
"flexible_ip_ids_to_duplicate": {
Type: schema.TypeSet,
Expand Down
25 changes: 10 additions & 15 deletions internal/services/function/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/regional"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/services/account"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/types"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/verify"
)

func ResourceFunction() *schema.Resource {
Expand Down Expand Up @@ -79,13 +80,10 @@ func ResourceFunction() *schema.Resource {
ValidateDiagFunc: validation.MapKeyLenBetween(0, 100),
},
"privacy": {
Type: schema.TypeString,
Description: "Privacy of the function. Can be either `private` or `public`",
Required: true,
ValidateFunc: validation.StringInSlice([]string{
function.FunctionPrivacyPublic.String(),
function.FunctionPrivacyPrivate.String(),
}, false),
Type: schema.TypeString,
Description: "Privacy of the function. Can be either `private` or `public`",
Required: true,
ValidateDiagFunc: verify.ValidateEnum[function.FunctionPrivacy](),
},
"runtime": {
Type: schema.TypeString,
Expand Down Expand Up @@ -139,14 +137,11 @@ func ResourceFunction() *schema.Resource {
Description: "Define if the function should be deployed, terraform will wait for function to be deployed",
},
"http_option": {
Type: schema.TypeString,
Optional: true,
Description: "HTTP traffic configuration",
Default: function.FunctionHTTPOptionEnabled.String(),
ValidateFunc: validation.StringInSlice([]string{
function.FunctionHTTPOptionEnabled.String(),
function.FunctionHTTPOptionRedirected.String(),
}, false),
Type: schema.TypeString,
Optional: true,
Description: "HTTP traffic configuration",
Default: function.FunctionHTTPOptionEnabled.String(),
ValidateDiagFunc: verify.ValidateEnum[function.FunctionHTTPOption](),
},
"cpu_limit": {
Type: schema.TypeInt,
Expand Down
Loading

0 comments on commit 9a16187

Please sign in to comment.