|
| 1 | +package instance |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator" |
| 8 | + "github.com/hashicorp/terraform-plugin-framework/action" |
| 9 | + "github.com/hashicorp/terraform-plugin-framework/action/schema" |
| 10 | + "github.com/hashicorp/terraform-plugin-framework/schema/validator" |
| 11 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 12 | + "github.com/scaleway/scaleway-sdk-go/api/instance/v1" |
| 13 | + "github.com/scaleway/scaleway-sdk-go/scw" |
| 14 | + "github.com/scaleway/terraform-provider-scaleway/v2/internal/locality" |
| 15 | + "github.com/scaleway/terraform-provider-scaleway/v2/internal/meta" |
| 16 | +) |
| 17 | + |
| 18 | +var ( |
| 19 | + _ action.Action = (*ServerAction)(nil) |
| 20 | + _ action.ActionWithConfigure = (*ServerAction)(nil) |
| 21 | +) |
| 22 | + |
| 23 | +type ServerAction struct { |
| 24 | + instanceAPI *instance.API |
| 25 | +} |
| 26 | + |
| 27 | +func (a *ServerAction) Configure(ctx context.Context, req action.ConfigureRequest, resp *action.ConfigureResponse) { |
| 28 | + if req.ProviderData == nil { |
| 29 | + return |
| 30 | + } |
| 31 | + |
| 32 | + m, ok := req.ProviderData.(*meta.Meta) |
| 33 | + if !ok { |
| 34 | + resp.Diagnostics.AddError( |
| 35 | + "Unexpected Action Configure Type", |
| 36 | + fmt.Sprintf("Expected *scw.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData), |
| 37 | + ) |
| 38 | + |
| 39 | + return |
| 40 | + } |
| 41 | + |
| 42 | + client := m.ScwClient() |
| 43 | + a.instanceAPI = instance.NewAPI(client) |
| 44 | +} |
| 45 | + |
| 46 | +func (a *ServerAction) Metadata(ctx context.Context, req action.MetadataRequest, resp *action.MetadataResponse) { |
| 47 | + resp.TypeName = req.ProviderTypeName + "_instance_server_action" |
| 48 | +} |
| 49 | + |
| 50 | +type ServerActionModel struct { |
| 51 | + ServerID types.String `tfsdk:"server_id"` |
| 52 | + Zone types.String `tfsdk:"zone"` |
| 53 | + Action types.String `tfsdk:"action"` |
| 54 | + Wait types.Bool `tfsdk:"wait"` |
| 55 | +} |
| 56 | + |
| 57 | +func NewServerAction() action.Action { |
| 58 | + return &ServerAction{} |
| 59 | +} |
| 60 | + |
| 61 | +func (a *ServerAction) Schema(ctx context.Context, req action.SchemaRequest, resp *action.SchemaResponse) { |
| 62 | + actionsValues := instance.ServerAction("").Values() |
| 63 | + |
| 64 | + actionStringValues := make([]string, 0, len(actionsValues)) |
| 65 | + for _, actionValue := range actionsValues { |
| 66 | + actionStringValues = append(actionStringValues, actionValue.String()) |
| 67 | + } |
| 68 | + |
| 69 | + resp.Schema = schema.Schema{ |
| 70 | + Attributes: map[string]schema.Attribute{ |
| 71 | + "action": schema.StringAttribute{ |
| 72 | + Required: true, |
| 73 | + Description: "Type of action to perform", |
| 74 | + Validators: []validator.String{ |
| 75 | + stringvalidator.OneOfCaseInsensitive(actionStringValues...), |
| 76 | + }, |
| 77 | + }, |
| 78 | + "server_id": schema.StringAttribute{ |
| 79 | + Required: true, |
| 80 | + Description: "Server id to send the action to", |
| 81 | + }, |
| 82 | + "zone": schema.StringAttribute{ |
| 83 | + Optional: true, |
| 84 | + Description: "Zone of server to send the action to", |
| 85 | + }, |
| 86 | + "wait": schema.BoolAttribute{ |
| 87 | + Optional: true, |
| 88 | + Description: "Wait for server to finish action", |
| 89 | + }, |
| 90 | + }, |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +func (a *ServerAction) Invoke(ctx context.Context, req action.InvokeRequest, resp *action.InvokeResponse) { |
| 95 | + var data ServerActionModel |
| 96 | + // Read action config data into the model |
| 97 | + resp.Diagnostics.Append(req.Config.Get(ctx, &data)...) |
| 98 | + |
| 99 | + if resp.Diagnostics.HasError() { |
| 100 | + return |
| 101 | + } |
| 102 | + |
| 103 | + if a.instanceAPI == nil { |
| 104 | + resp.Diagnostics.AddError( |
| 105 | + "Unconfigured instanceAPI", |
| 106 | + "The action was not properly configured. The Scaleway client is missing. "+ |
| 107 | + "This is usually a bug in the provider. Please report it to the maintainers.", |
| 108 | + ) |
| 109 | + |
| 110 | + return |
| 111 | + } |
| 112 | + |
| 113 | + actionReq := &instance.ServerActionRequest{ |
| 114 | + ServerID: locality.ExpandID(data.ServerID.ValueString()), |
| 115 | + Action: instance.ServerAction(data.Action.ValueString()), |
| 116 | + } |
| 117 | + if !data.Zone.IsNull() { |
| 118 | + actionReq.Zone = scw.Zone(data.Zone.ValueString()) |
| 119 | + } |
| 120 | + |
| 121 | + _, err := a.instanceAPI.ServerAction(actionReq) |
| 122 | + if err != nil { |
| 123 | + resp.Diagnostics.AddError( |
| 124 | + "error in server action", |
| 125 | + fmt.Sprintf("%s", err)) |
| 126 | + } |
| 127 | + |
| 128 | + if data.Wait.ValueBool() { |
| 129 | + waitReq := &instance.WaitForServerRequest{ |
| 130 | + ServerID: locality.ExpandID(data.ServerID.ValueString()), |
| 131 | + Zone: scw.Zone(data.Zone.ValueString()), |
| 132 | + } |
| 133 | + |
| 134 | + if !data.Zone.IsNull() { |
| 135 | + waitReq.Zone = scw.Zone(data.Zone.ValueString()) |
| 136 | + } |
| 137 | + |
| 138 | + _, errWait := a.instanceAPI.WaitForServer(waitReq) |
| 139 | + if errWait != nil { |
| 140 | + resp.Diagnostics.AddError( |
| 141 | + "error in wait server", |
| 142 | + fmt.Sprintf("%s", err)) |
| 143 | + } |
| 144 | + } |
| 145 | +} |
0 commit comments