|
| 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 | +} |
0 commit comments