@@ -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