Skip to content

Commit ffd744c

Browse files
committed
feat: add support for action reboot
1 parent 791929a commit ffd744c

File tree

4 files changed

+2483
-1
lines changed

4 files changed

+2483
-1
lines changed
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package instance
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/hashicorp/terraform-plugin-framework/action"
8+
"github.com/hashicorp/terraform-plugin-framework/action/schema"
9+
"github.com/hashicorp/terraform-plugin-framework/types"
10+
"github.com/scaleway/scaleway-sdk-go/api/instance/v1"
11+
"github.com/scaleway/scaleway-sdk-go/scw"
12+
)
13+
14+
type ServerReboot struct {
15+
instanceAPI *instance.API
16+
}
17+
18+
func (a *ServerReboot) Configure(ctx context.Context, req action.ConfigureRequest, resp *action.ConfigureResponse) {
19+
if req.ProviderData == nil {
20+
return
21+
}
22+
23+
client, ok := req.ProviderData.(*scw.Client)
24+
if !ok {
25+
resp.Diagnostics.AddError(
26+
"Unexpected Action Configure Type",
27+
fmt.Sprintf("Expected *scw.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData),
28+
)
29+
30+
return
31+
}
32+
33+
a.instanceAPI = instance.NewAPI(client)
34+
}
35+
36+
func (a *ServerReboot) Metadata(ctx context.Context, req action.MetadataRequest, resp *action.MetadataResponse) {
37+
resp.TypeName = req.ProviderTypeName + "_instance_server_reboot"
38+
}
39+
40+
type ServerRebootModel struct {
41+
ServerID types.String `tfsdk:"server_id"`
42+
Zone types.String `tfsdk:"zone"`
43+
Wait types.Bool `tfsdk:"wait"`
44+
}
45+
46+
func NewServerReboot() action.Action {
47+
return &ServerReboot{}
48+
}
49+
50+
func (a *ServerReboot) Schema(ctx context.Context, req action.SchemaRequest, resp *action.SchemaResponse) {
51+
resp.Schema = schema.Schema{
52+
Attributes: map[string]schema.Attribute{
53+
"server_id": schema.StringAttribute{
54+
Required: true,
55+
Description: "Server id to reboot",
56+
},
57+
"zone": schema.StringAttribute{
58+
Optional: true,
59+
Description: "Zone of server to reboot",
60+
},
61+
"wait": schema.BoolAttribute{
62+
Optional: true,
63+
Description: "Wait for server to finish reboot",
64+
},
65+
},
66+
}
67+
}
68+
69+
func (a *ServerReboot) Invoke(ctx context.Context, req action.InvokeRequest, resp *action.InvokeResponse) {
70+
var data ServerRebootModel
71+
// Read action config data into the model
72+
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
73+
if resp.Diagnostics.HasError() {
74+
return
75+
}
76+
77+
_, err := a.instanceAPI.ServerAction(&instance.ServerActionRequest{
78+
ServerID: data.ServerID.String(),
79+
Zone: scw.Zone(data.Zone.String()),
80+
Action: instance.ServerActionReboot,
81+
})
82+
if err != nil {
83+
resp.Diagnostics.AddError(
84+
"error in server action",
85+
fmt.Sprintf("%s", err))
86+
}
87+
if data.Wait.ValueBool() {
88+
_, errWait := a.instanceAPI.WaitForServer(&instance.WaitForServerRequest{
89+
ServerID: data.ServerID.String(),
90+
Zone: scw.Zone(data.Zone.String()),
91+
})
92+
if errWait != nil {
93+
resp.Diagnostics.AddError(
94+
"error in wait server",
95+
fmt.Sprintf("%s", err))
96+
}
97+
}
98+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package instance_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
7+
"github.com/scaleway/terraform-provider-scaleway/v2/internal/acctest"
8+
)
9+
10+
func TestAccActionServerReboot_Basic(t *testing.T) {
11+
tt := acctest.NewTestTools(t)
12+
defer tt.Cleanup()
13+
14+
resource.ParallelTest(t, resource.TestCase{
15+
PreCheck: func() { acctest.PreCheck(t) },
16+
ProviderFactories: tt.ProviderFactories,
17+
Steps: []resource.TestStep{
18+
{
19+
Config: `
20+
resource "scaleway_instance_server" "main" {
21+
name = "test-terraform-datasource-private-nic"
22+
type = "DEV1-S"
23+
image = "ubuntu_jammy"
24+
}`,
25+
},
26+
{
27+
Config: `
28+
resource "scaleway_instance_server" "main" {
29+
name = "test-terraform-datasource-private-nic"
30+
type = "DEV1-S"
31+
image = "ubuntu_jammy"
32+
}
33+
34+
action "scaleway_instance_server_reboot" "main" {
35+
server_id = scaleway_instance_server.main.id
36+
}
37+
`,
38+
Check: resource.ComposeTestCheckFunc(),
39+
},
40+
},
41+
})
42+
}

internal/services/instance/testdata/action-server-reboot-basic.cassette.yaml

Lines changed: 2338 additions & 0 deletions
Large diffs are not rendered by default.

provider/framework.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ import (
1010
"github.com/hashicorp/terraform-plugin-framework/provider"
1111
"github.com/hashicorp/terraform-plugin-framework/provider/schema"
1212
"github.com/hashicorp/terraform-plugin-framework/resource"
13+
"github.com/scaleway/terraform-provider-scaleway/v2/internal/services/instance"
1314
)
1415

1516
var _ provider.Provider = &ScalewayProvider{}
17+
var _ provider.ProviderWithActions = (*ScalewayProvider)(nil)
1618

1719
type ScalewayProvider struct{}
1820

@@ -81,7 +83,9 @@ func (p *ScalewayProvider) DataSources(_ context.Context) []func() datasource.Da
8183
}
8284

8385
func (p *ScalewayProvider) Actions(_ context.Context) []func() action.Action {
84-
return []func() action.Action{}
86+
var res []func() action.Action
87+
res = append(res, instance.NewServerReboot)
88+
return res
8589
}
8690

8791
func (p *ScalewayProvider) ListResources(_ context.Context) []func() list.ListResource {

0 commit comments

Comments
 (0)