Skip to content

Commit 82f3bea

Browse files
committed
add validators
1 parent 2bbb99a commit 82f3bea

File tree

5 files changed

+35
-11
lines changed

5 files changed

+35
-11
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ require (
2222
github.com/hashicorp/go-multierror v1.1.1
2323
github.com/hashicorp/go-retryablehttp v0.7.8
2424
github.com/hashicorp/terraform-plugin-framework v1.16.0
25+
github.com/hashicorp/terraform-plugin-framework-validators v0.18.1-0.20250909114857-8e55d8ccabdb
2526
github.com/hashicorp/terraform-plugin-go v0.29.0
2627
github.com/hashicorp/terraform-plugin-log v0.9.0
2728
github.com/hashicorp/terraform-plugin-mux v0.21.0

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,8 @@ github.com/hashicorp/terraform-plugin-docs v0.23.0 h1:sipnfD4/9EJBg9zekym+s1H6qm
326326
github.com/hashicorp/terraform-plugin-docs v0.23.0/go.mod h1:J4b5AtMRgJlDrwCQz+G4hKABgHY5m56PnsRmdAzBwW8=
327327
github.com/hashicorp/terraform-plugin-framework v1.16.0 h1:tP0f+yJg0Z672e7levixDe5EpWwrTrNryPM9kDMYIpE=
328328
github.com/hashicorp/terraform-plugin-framework v1.16.0/go.mod h1:0xFOxLy5lRzDTayc4dzK/FakIgBhNf/lC4499R9cV4Y=
329+
github.com/hashicorp/terraform-plugin-framework-validators v0.18.1-0.20250909114857-8e55d8ccabdb h1:wRiOv+xaGRrBuc8r774OtrELwQCiSLLQNrkH00ZLO90=
330+
github.com/hashicorp/terraform-plugin-framework-validators v0.18.1-0.20250909114857-8e55d8ccabdb/go.mod h1:vU2y54LtDNHGLjDD7LH/if+4KBKZ5ljTrgDdrM6y8Pc=
329331
github.com/hashicorp/terraform-plugin-go v0.29.0 h1:1nXKl/nSpaYIUBU1IG/EsDOX0vv+9JxAltQyDMpq5mU=
330332
github.com/hashicorp/terraform-plugin-go v0.29.0/go.mod h1:vYZbIyvxyy0FWSmDHChCqKvI40cFTDGSb3D8D70i9GM=
331333
github.com/hashicorp/terraform-plugin-log v0.9.0 h1:i7hOA+vdAItN1/7UrfBqBwvYPQ9TFvymaRGZED3FCV0=

internal/provider/provider_framework.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func (p *ScalewayProvider) DataSources(_ context.Context) []func() datasource.Da
8484

8585
func (p *ScalewayProvider) Actions(_ context.Context) []func() action.Action {
8686
var res []func() action.Action
87-
res = append(res, instance.NewServerReboot)
87+
res = append(res, instance.NewServerAction)
8888
return res
8989
}
9090

internal/services/instance/action_server_reboot.go renamed to internal/services/instance/action_server_action.go

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,24 @@ import (
44
"context"
55
"fmt"
66

7+
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
78
"github.com/hashicorp/terraform-plugin-framework/action"
89
"github.com/hashicorp/terraform-plugin-framework/action/schema"
10+
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
911
"github.com/hashicorp/terraform-plugin-framework/types"
1012
"github.com/scaleway/scaleway-sdk-go/api/instance/v1"
1113
"github.com/scaleway/scaleway-sdk-go/scw"
1214
)
1315

14-
type ServerReboot struct {
16+
var (
17+
_ action.Action = (*ServerAction)(nil)
18+
)
19+
20+
type ServerAction struct {
1521
instanceAPI *instance.API
1622
}
1723

18-
func (a *ServerReboot) Configure(ctx context.Context, req action.ConfigureRequest, resp *action.ConfigureResponse) {
24+
func (a *ServerAction) Configure(ctx context.Context, req action.ConfigureRequest, resp *action.ConfigureResponse) {
1925
if req.ProviderData == nil {
2026
return
2127
}
@@ -33,23 +39,38 @@ func (a *ServerReboot) Configure(ctx context.Context, req action.ConfigureReques
3339
a.instanceAPI = instance.NewAPI(client)
3440
}
3541

36-
func (a *ServerReboot) Metadata(ctx context.Context, req action.MetadataRequest, resp *action.MetadataResponse) {
42+
func (a *ServerAction) Metadata(ctx context.Context, req action.MetadataRequest, resp *action.MetadataResponse) {
3743
resp.TypeName = req.ProviderTypeName + "_instance_server_reboot"
3844
}
3945

40-
type ServerRebootModel struct {
46+
type ServerActionModel struct {
4147
ServerID types.String `tfsdk:"server_id"`
4248
Zone types.String `tfsdk:"zone"`
4349
Wait types.Bool `tfsdk:"wait"`
50+
Action types.String `tfsdk:"action"`
4451
}
4552

46-
func NewServerReboot() action.Action {
47-
return &ServerReboot{}
53+
func NewServerAction() action.Action {
54+
return &ServerAction{}
4855
}
4956

50-
func (a *ServerReboot) Schema(ctx context.Context, req action.SchemaRequest, resp *action.SchemaResponse) {
57+
func (a *ServerAction) Schema(ctx context.Context, req action.SchemaRequest, resp *action.SchemaResponse) {
58+
actionsValues := instance.ServerAction("").Values()
59+
60+
actionStringValues := make([]string, 0, len(actionsValues))
61+
for _, actionValue := range actionsValues {
62+
actionStringValues = append(actionStringValues, actionValue.String())
63+
}
64+
5165
resp.Schema = schema.Schema{
5266
Attributes: map[string]schema.Attribute{
67+
"action": schema.StringAttribute{
68+
Required: true,
69+
Description: "Type of action to perform",
70+
Validators: []validator.String{
71+
stringvalidator.OneOfCaseInsensitive(actionStringValues...),
72+
},
73+
},
5374
"server_id": schema.StringAttribute{
5475
Required: true,
5576
Description: "Server id to reboot",
@@ -66,8 +87,8 @@ func (a *ServerReboot) Schema(ctx context.Context, req action.SchemaRequest, res
6687
}
6788
}
6889

69-
func (a *ServerReboot) Invoke(ctx context.Context, req action.InvokeRequest, resp *action.InvokeResponse) {
70-
var data ServerRebootModel
90+
func (a *ServerAction) Invoke(ctx context.Context, req action.InvokeRequest, resp *action.InvokeResponse) {
91+
var data ServerActionModel
7192
// Read action config data into the model
7293
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
7394
if resp.Diagnostics.HasError() {
@@ -77,7 +98,7 @@ func (a *ServerReboot) Invoke(ctx context.Context, req action.InvokeRequest, res
7798
_, err := a.instanceAPI.ServerAction(&instance.ServerActionRequest{
7899
ServerID: data.ServerID.String(),
79100
Zone: scw.Zone(data.Zone.String()),
80-
Action: instance.ServerActionReboot,
101+
Action: instance.ServerAction(data.Action.String()),
81102
})
82103
if err != nil {
83104
resp.Diagnostics.AddError(

0 commit comments

Comments
 (0)