-
Notifications
You must be signed in to change notification settings - Fork 133
feat: add support for instance action #3390
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
c5fdeef
feat: add support for action reboot
remyleone 47e2978
Fix
remyleone c435d50
Merge branch 'master' into action_reboot
remyleone 443339e
add support to skip open tofu tests
remyleone fdc8fab
Merge branch 'master' into action_reboot
remyleone a1ef3e3
add skip for opentofu
remyleone fecfa7a
Fix tests
remyleone 2e73542
Fix zone
remyleone 2c14315
fix lint
remyleone File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| --- | ||
| subcategory: "Instances" | ||
| page_title: "Scaleway: scaleway_instance_server_action" | ||
| --- | ||
|
|
||
| # scaleway_instance_server_action (Action) | ||
|
|
||
| <!-- action schema generated by tfplugindocs --> | ||
| ## Schema | ||
|
|
||
| ### Required | ||
|
|
||
| - `action` (String) Type of action to perform | ||
| - `server_id` (String) Server id to send the action to | ||
|
|
||
| ### Optional | ||
|
|
||
| - `wait` (Boolean) Wait for server to finish action | ||
| - `zone` (String) Zone of server to send the action to | ||
|
|
||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| package acctest | ||
|
|
||
| import ( | ||
| "os" | ||
|
|
||
| "github.com/scaleway/terraform-provider-scaleway/v2/internal/env" | ||
| ) | ||
|
|
||
| func IsRunningOpenTofu() bool { | ||
| return os.Getenv(env.AccRunningOpenTofu) == "true" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,145 @@ | ||
| 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" | ||
| "github.com/scaleway/terraform-provider-scaleway/v2/internal/locality" | ||
| "github.com/scaleway/terraform-provider-scaleway/v2/internal/meta" | ||
| ) | ||
|
|
||
| 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 | ||
| } | ||
|
|
||
| m, ok := req.ProviderData.(*meta.Meta) | ||
| 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 | ||
| } | ||
|
|
||
| client := m.ScwClient() | ||
| 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 | ||
| } | ||
|
|
||
| if a.instanceAPI == nil { | ||
| resp.Diagnostics.AddError( | ||
| "Unconfigured instanceAPI", | ||
| "The action was not properly configured. The Scaleway client is missing. "+ | ||
| "This is usually a bug in the provider. Please report it to the maintainers.", | ||
| ) | ||
|
|
||
| return | ||
| } | ||
|
|
||
| actionReq := &instance.ServerActionRequest{ | ||
| ServerID: locality.ExpandID(data.ServerID.ValueString()), | ||
| Action: instance.ServerAction(data.Action.ValueString()), | ||
| } | ||
| if !data.Zone.IsNull() { | ||
| actionReq.Zone = scw.Zone(data.Zone.ValueString()) | ||
| } | ||
|
|
||
| _, err := a.instanceAPI.ServerAction(actionReq) | ||
| if err != nil { | ||
| resp.Diagnostics.AddError( | ||
| "error in server action", | ||
| fmt.Sprintf("%s", err)) | ||
| } | ||
|
|
||
| if data.Wait.ValueBool() { | ||
| waitReq := &instance.WaitForServerRequest{ | ||
| ServerID: locality.ExpandID(data.ServerID.ValueString()), | ||
| Zone: scw.Zone(data.Zone.ValueString()), | ||
| } | ||
|
|
||
| if !data.Zone.IsNull() { | ||
| waitReq.Zone = scw.Zone(data.Zone.ValueString()) | ||
| } | ||
|
|
||
| _, errWait := a.instanceAPI.WaitForServer(waitReq) | ||
| if errWait != nil { | ||
| resp.Diagnostics.AddError( | ||
| "error in wait server", | ||
| fmt.Sprintf("%s", err)) | ||
| } | ||
| } | ||
| } |
124 changes: 124 additions & 0 deletions
124
internal/services/instance/action_server_action_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| package instance_test | ||
|
|
||
| import ( | ||
| "errors" | ||
| "regexp" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "github.com/hashicorp/terraform-plugin-testing/helper/resource" | ||
| "github.com/hashicorp/terraform-plugin-testing/terraform" | ||
| "github.com/scaleway/terraform-provider-scaleway/v2/internal/acctest" | ||
| ) | ||
|
|
||
| func TestAccActionServer_Basic(t *testing.T) { | ||
| if acctest.IsRunningOpenTofu() { | ||
| t.Skip("Skipping TestAccActionServer_Basic because action are not yet supported on OpenTofu") | ||
| } | ||
|
|
||
| tt := acctest.NewTestTools(t) | ||
| defer tt.Cleanup() | ||
|
|
||
| resource.ParallelTest(t, resource.TestCase{ | ||
| 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 | ||
| } | ||
| }`, | ||
| }, | ||
| { | ||
| 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 | ||
| } | ||
| } | ||
|
|
||
| data "scaleway_audit_trail_event" "instance" { | ||
| resource_type = "instance_server" | ||
| resource_id = scaleway_instance_server.main.id | ||
| method_name = "ServerAction" | ||
| }`, | ||
| Check: resource.ComposeTestCheckFunc( | ||
| resource.TestCheckResourceAttrSet("data.scaleway_audit_trail_event.instance", "events.#"), | ||
| func(state *terraform.State) error { | ||
| rs, ok := state.RootModule().Resources["data.scaleway_audit_trail_event.instance"] | ||
| if !ok { | ||
| return errors.New("not found: data.scaleway_audit_trail_event.instance") | ||
| } | ||
|
|
||
| for key, value := range rs.Primary.Attributes { | ||
estellesoulard marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if !strings.Contains(key, "request_body") { | ||
| continue | ||
| } | ||
|
|
||
| if value == `{"action":"reboot"}` { | ||
| return nil | ||
| } | ||
| } | ||
|
|
||
| return errors.New("did not found the reboot event") | ||
| }, | ||
| ), | ||
| }, | ||
| }, | ||
| }) | ||
| } | ||
|
|
||
| func TestAccActionServer_UnknownVerb(t *testing.T) { | ||
| if acctest.IsRunningOpenTofu() { | ||
| t.Skip("Skipping TestAccActionServer_Basic because action are not yet supported on OpenTofu") | ||
| } | ||
|
|
||
| tt := acctest.NewTestTools(t) | ||
| defer tt.Cleanup() | ||
|
|
||
| resource.ParallelTest(t, resource.TestCase{ | ||
| ProtoV6ProviderFactories: tt.ProviderFactories, | ||
| Steps: []resource.TestStep{ | ||
| { | ||
| Config: ` | ||
| action "scaleway_instance_server_action" "main" { | ||
| config { | ||
| action = "unknownVerb" | ||
| server_id = "11111111-1111-1111-1111-111111111111" | ||
| } | ||
| } | ||
| `, | ||
| ExpectError: regexp.MustCompile("Invalid Attribute Value Match"), | ||
| }, | ||
| }, | ||
| }) | ||
| } | ||
1,661 changes: 1,661 additions & 0 deletions
1,661
internal/services/instance/testdata/action-server-basic.cassette.yaml
Large diffs are not rendered by default.
Oops, something went wrong.
3 changes: 3 additions & 0 deletions
3
internal/services/instance/testdata/action-server-unknown-verb.cassette.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| --- | ||
| version: 2 | ||
| interactions: [] |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.