Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ require (
github.com/hashicorp/go-multierror v1.1.1
github.com/hashicorp/go-retryablehttp v0.7.8
github.com/hashicorp/terraform-plugin-framework v1.16.0
github.com/hashicorp/terraform-plugin-framework-validators v0.18.1-0.20250909114857-8e55d8ccabdb
github.com/hashicorp/terraform-plugin-go v0.29.0
github.com/hashicorp/terraform-plugin-log v0.9.0
github.com/hashicorp/terraform-plugin-mux v0.21.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,8 @@ github.com/hashicorp/terraform-plugin-docs v0.23.0 h1:sipnfD4/9EJBg9zekym+s1H6qm
github.com/hashicorp/terraform-plugin-docs v0.23.0/go.mod h1:J4b5AtMRgJlDrwCQz+G4hKABgHY5m56PnsRmdAzBwW8=
github.com/hashicorp/terraform-plugin-framework v1.16.0 h1:tP0f+yJg0Z672e7levixDe5EpWwrTrNryPM9kDMYIpE=
github.com/hashicorp/terraform-plugin-framework v1.16.0/go.mod h1:0xFOxLy5lRzDTayc4dzK/FakIgBhNf/lC4499R9cV4Y=
github.com/hashicorp/terraform-plugin-framework-validators v0.18.1-0.20250909114857-8e55d8ccabdb h1:wRiOv+xaGRrBuc8r774OtrELwQCiSLLQNrkH00ZLO90=
github.com/hashicorp/terraform-plugin-framework-validators v0.18.1-0.20250909114857-8e55d8ccabdb/go.mod h1:vU2y54LtDNHGLjDD7LH/if+4KBKZ5ljTrgDdrM6y8Pc=
github.com/hashicorp/terraform-plugin-go v0.29.0 h1:1nXKl/nSpaYIUBU1IG/EsDOX0vv+9JxAltQyDMpq5mU=
github.com/hashicorp/terraform-plugin-go v0.29.0/go.mod h1:vYZbIyvxyy0FWSmDHChCqKvI40cFTDGSb3D8D70i9GM=
github.com/hashicorp/terraform-plugin-log v0.9.0 h1:i7hOA+vdAItN1/7UrfBqBwvYPQ9TFvymaRGZED3FCV0=
Expand Down
122 changes: 122 additions & 0 deletions internal/services/instance/action_server_action.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package instance

import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/action"
"github.com/hashicorp/terraform-plugin-framework/action/schema"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/scaleway/scaleway-sdk-go/api/instance/v1"
"github.com/scaleway/scaleway-sdk-go/scw"
)

var (
_ action.Action = (*ServerAction)(nil)
_ action.ActionWithConfigure = (*ServerAction)(nil)
)

type ServerAction struct {
instanceAPI *instance.API
}

func (a *ServerAction) Configure(ctx context.Context, req action.ConfigureRequest, resp *action.ConfigureResponse) {
if req.ProviderData == nil {
return
}

client, ok := req.ProviderData.(*scw.Client)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Action Configure Type",
fmt.Sprintf("Expected *scw.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)

return
}

a.instanceAPI = instance.NewAPI(client)
}

func (a *ServerAction) Metadata(ctx context.Context, req action.MetadataRequest, resp *action.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_instance_server_action"
}

type ServerActionModel struct {
ServerID types.String `tfsdk:"server_id"`
Zone types.String `tfsdk:"zone"`
Action types.String `tfsdk:"action"`
Wait types.Bool `tfsdk:"wait"`
}

func NewServerAction() action.Action {
return &ServerAction{}
}

func (a *ServerAction) Schema(ctx context.Context, req action.SchemaRequest, resp *action.SchemaResponse) {
actionsValues := instance.ServerAction("").Values()

actionStringValues := make([]string, 0, len(actionsValues))
for _, actionValue := range actionsValues {
actionStringValues = append(actionStringValues, actionValue.String())
}

resp.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
"action": schema.StringAttribute{
Required: true,
Description: "Type of action to perform",
Validators: []validator.String{
stringvalidator.OneOfCaseInsensitive(actionStringValues...),
},
},
"server_id": schema.StringAttribute{
Required: true,
Description: "Server id to send the action to",
},
"zone": schema.StringAttribute{
Optional: true,
Description: "Zone of server to send the action to",
},
"wait": schema.BoolAttribute{
Optional: true,
Description: "Wait for server to finish action",
},
},
}
}

func (a *ServerAction) Invoke(ctx context.Context, req action.InvokeRequest, resp *action.InvokeResponse) {
var data ServerActionModel
// Read action config data into the model
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)

if resp.Diagnostics.HasError() {
return
}

_, err := a.instanceAPI.ServerAction(&instance.ServerActionRequest{
ServerID: data.ServerID.String(),
Zone: scw.Zone(data.Zone.String()),
Action: instance.ServerAction(data.Action.String()),
})
if err != nil {
resp.Diagnostics.AddError(
"error in server action",
fmt.Sprintf("%s", err))
}

if data.Wait.ValueBool() {
_, errWait := a.instanceAPI.WaitForServer(&instance.WaitForServerRequest{
ServerID: data.ServerID.String(),
Zone: scw.Zone(data.Zone.String()),
})
if errWait != nil {
resp.Diagnostics.AddError(
"error in wait server",
fmt.Sprintf("%s", err))
}
}
}
80 changes: 80 additions & 0 deletions internal/services/instance/action_server_action_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package instance_test

Check failure on line 1 in internal/services/instance/action_server_action_test.go

View workflow job for this annotation

GitHub Actions / tfproviderlint

AT012: file contains multiple acceptance test name prefixes: [TestAccActionServerReboot TestAccActionServerAction]

import (
"testing"

"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/acctest"
)

func TestAccActionServerReboot_Basic(t *testing.T) {
tt := acctest.NewTestTools(t)
defer tt.Cleanup()

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t) },
ProtoV6ProviderFactories: tt.ProviderFactories,
Steps: []resource.TestStep{
{
Config: `
resource "scaleway_instance_server" "main" {
name = "test-terraform-datasource-private-nic"
type = "DEV1-S"
image = "ubuntu_jammy"

lifecycle {
action_trigger {
events = [after_create]
actions = [action.scaleway_instance_server_action.main]
}
}
}

action "scaleway_instance_server_action" "main" {
config {
action = "reboot"
server_id = scaleway_instance_server.main.id
}
}
`,
Check: resource.ComposeTestCheckFunc(),
},
},
})
}

func TestAccActionServerAction_UncorrectVerb(t *testing.T) {
tt := acctest.NewTestTools(t)
defer tt.Cleanup()

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t) },
ProtoV6ProviderFactories: tt.ProviderFactories,
Steps: []resource.TestStep{
{
Config: `
resource "scaleway_instance_server" "main" {
name = "test-terraform-datasource-private-nic"
type = "DEV1-S"
image = "ubuntu_jammy"

lifecycle {
action_trigger {
events = [after_create]
actions = [action.scaleway_instance_server_action.main]
}
}
}

action "scaleway_instance_server_action" "main" {
config {
action = "incorrectVerb"
server_id = scaleway_instance_server.main.id
}
}
`,
Check: resource.ComposeTestCheckFunc(),
},
},
})
}

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
version: 2
interactions: []
12 changes: 10 additions & 2 deletions provider/framework.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@ import (
"github.com/hashicorp/terraform-plugin-framework/provider"
"github.com/hashicorp/terraform-plugin-framework/provider/schema"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/services/instance"
)

var _ provider.Provider = &ScalewayProvider{}
var (
_ provider.Provider = &ScalewayProvider{}
_ provider.ProviderWithActions = (*ScalewayProvider)(nil)
)

type ScalewayProvider struct{}

Expand Down Expand Up @@ -81,7 +85,11 @@ func (p *ScalewayProvider) DataSources(_ context.Context) []func() datasource.Da
}

func (p *ScalewayProvider) Actions(_ context.Context) []func() action.Action {
return []func() action.Action{}
var res []func() action.Action

res = append(res, instance.NewServerAction)

return res
}

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