|
| 1 | +package runcommand |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "strconv" |
| 7 | + |
| 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/hashicorp/terraform-plugin-log/tflog" |
| 13 | + "github.com/stackitcloud/stackit-sdk-go/services/runcommand/v1api" |
| 14 | + "github.com/stackitcloud/stackit-sdk-go/services/runcommand/v1api/wait" |
| 15 | + |
| 16 | + "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/conversion" |
| 17 | + "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core" |
| 18 | + runCommandUtils "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/runcommand/utils" |
| 19 | + "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/validate" |
| 20 | +) |
| 21 | + |
| 22 | +// Ensure the implementation satisfies the expected interfaces. |
| 23 | +var ( |
| 24 | + _ action.Action = &runCommandAction{} |
| 25 | + _ action.ActionWithConfigure = &runCommandAction{} |
| 26 | +) |
| 27 | + |
| 28 | +type runCommandModel struct { |
| 29 | + ProjectId types.String `tfsdk:"project_id"` |
| 30 | + ServerId types.String `tfsdk:"server_id"` |
| 31 | + Region types.String `tfsdk:"region"` |
| 32 | + CommandTemplateName types.String `tfsdk:"command_template_name"` |
| 33 | + Parameters types.Map `tfsdk:"parameters"` |
| 34 | +} |
| 35 | + |
| 36 | +// NewRunCommandAction is a helper function to simplify the provider implementation. |
| 37 | +func NewRunCommandAction() action.Action { |
| 38 | + return &runCommandAction{} |
| 39 | +} |
| 40 | + |
| 41 | +// runCommandAction is the action implementation. |
| 42 | +type runCommandAction struct { |
| 43 | + client *v1api.APIClient |
| 44 | + providerData core.ProviderData |
| 45 | +} |
| 46 | + |
| 47 | +// Metadata returns the action type name. |
| 48 | +func (a *runCommandAction) Metadata(_ context.Context, req action.MetadataRequest, resp *action.MetadataResponse) { |
| 49 | + resp.TypeName = req.ProviderTypeName + "_run_command" |
| 50 | +} |
| 51 | + |
| 52 | +// Configure adds the provider configured client to the action. |
| 53 | +func (a *runCommandAction) Configure(ctx context.Context, req action.ConfigureRequest, resp *action.ConfigureResponse) { |
| 54 | + var ok bool |
| 55 | + a.providerData, ok = conversion.ParseProviderData(ctx, req.ProviderData, &resp.Diagnostics) |
| 56 | + if !ok { |
| 57 | + return |
| 58 | + } |
| 59 | + a.client = runCommandUtils.ConfigureClient(ctx, &a.providerData, a.providerData.DefaultRegion, &resp.Diagnostics) |
| 60 | + if resp.Diagnostics.HasError() { |
| 61 | + return |
| 62 | + } |
| 63 | + tflog.Info(ctx, "Run command client configured") |
| 64 | +} |
| 65 | + |
| 66 | +// Schema defines the schema for the action. |
| 67 | +func (a *runCommandAction) Schema(_ context.Context, _ action.SchemaRequest, resp *action.SchemaResponse) { |
| 68 | + descriptions := map[string]string{ |
| 69 | + "main": "Executes a command on an IaaS server using the STACKIT Run Commands API. " + core.ResourceRegionFallbackDocstring, |
| 70 | + "project_id": "STACKIT Project ID to which the server belongs.", |
| 71 | + "server_id": "The ID of the server on which to execute the command.", |
| 72 | + "region": "The region of the server. If not defined, the provider default_region is used.", |
| 73 | + "command_template_name": "The name of the command template to execute (e.g. RunShellScript). Available templates can be listed with: `stackit server command template list`", |
| 74 | + "parameters": "Optional parameters passed to the command template as key-value pairs.", |
| 75 | + } |
| 76 | + |
| 77 | + resp.Schema = schema.Schema{ |
| 78 | + Description: descriptions["main"], |
| 79 | + Attributes: map[string]schema.Attribute{ |
| 80 | + "project_id": schema.StringAttribute{ |
| 81 | + Description: descriptions["project_id"], |
| 82 | + Required: true, |
| 83 | + Validators: []validator.String{ |
| 84 | + validate.UUID(), |
| 85 | + validate.NoSeparator(), |
| 86 | + }, |
| 87 | + }, |
| 88 | + "server_id": schema.StringAttribute{ |
| 89 | + Description: descriptions["server_id"], |
| 90 | + Required: true, |
| 91 | + Validators: []validator.String{ |
| 92 | + validate.UUID(), |
| 93 | + validate.NoSeparator(), |
| 94 | + }, |
| 95 | + }, |
| 96 | + "region": schema.StringAttribute{ |
| 97 | + Description: descriptions["region"], |
| 98 | + Optional: true, |
| 99 | + }, |
| 100 | + "command_template_name": schema.StringAttribute{ |
| 101 | + Description: descriptions["command_template_name"], |
| 102 | + Required: true, |
| 103 | + }, |
| 104 | + "parameters": schema.MapAttribute{ |
| 105 | + Description: descriptions["parameters"], |
| 106 | + Optional: true, |
| 107 | + ElementType: types.StringType, |
| 108 | + }, |
| 109 | + }, |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +// Invoke executes the run command action. |
| 114 | +func (a *runCommandAction) Invoke(ctx context.Context, req action.InvokeRequest, resp *action.InvokeResponse) { |
| 115 | + var model runCommandModel |
| 116 | + resp.Diagnostics.Append(req.Config.Get(ctx, &model)...) |
| 117 | + if resp.Diagnostics.HasError() { |
| 118 | + return |
| 119 | + } |
| 120 | + |
| 121 | + ctx = core.InitProviderContext(ctx) |
| 122 | + |
| 123 | + projectId := model.ProjectId.ValueString() |
| 124 | + serverId := model.ServerId.ValueString() |
| 125 | + region := a.providerData.GetRegionWithOverride(model.Region) |
| 126 | + |
| 127 | + ctx = tflog.SetField(ctx, "project_id", projectId) |
| 128 | + ctx = tflog.SetField(ctx, "server_id", serverId) |
| 129 | + ctx = tflog.SetField(ctx, "region", region) |
| 130 | + ctx = tflog.SetField(ctx, "command_template_name", model.CommandTemplateName.ValueString()) |
| 131 | + |
| 132 | + payload, err := toCreatePayload(ctx, &model) |
| 133 | + if err != nil { |
| 134 | + core.LogAndAddError(ctx, &resp.Diagnostics, "Error invoking run command", fmt.Sprintf("Building API payload: %v", err)) |
| 135 | + return |
| 136 | + } |
| 137 | + |
| 138 | + resp.SendProgress(action.InvokeProgressEvent{ |
| 139 | + Message: fmt.Sprintf("Waiting for agent on server %s to be ready...", serverId), |
| 140 | + }) |
| 141 | + |
| 142 | + // waits for the agent to register (404 while booting) and submits the command in one step |
| 143 | + createResp, err := wait.AgentReadyWaitHandler(ctx, a.client.DefaultAPI, projectId, serverId, *payload).WaitWithContext(ctx) |
| 144 | + if err != nil { |
| 145 | + core.LogAndAddError(ctx, &resp.Diagnostics, "Error invoking run command", fmt.Sprintf("Waiting for agent / calling API: %v", err)) |
| 146 | + return |
| 147 | + } |
| 148 | + if createResp == nil || createResp.Id == nil { |
| 149 | + core.LogAndAddError(ctx, &resp.Diagnostics, "Error invoking run command", "API returned empty response or missing command ID") |
| 150 | + return |
| 151 | + } |
| 152 | + |
| 153 | + commandId := createResp.GetId() |
| 154 | + commandIdStr := strconv.Itoa(int(commandId)) |
| 155 | + ctx = tflog.SetField(ctx, "command_id", commandIdStr) |
| 156 | + |
| 157 | + resp.SendProgress(action.InvokeProgressEvent{ |
| 158 | + Message: fmt.Sprintf("Command %q submitted (ID: %s). Waiting for completion...", model.CommandTemplateName.ValueString(), commandIdStr), |
| 159 | + }) |
| 160 | + |
| 161 | + details, err := wait.RunCommandWaitHandler(ctx, a.client.DefaultAPI, projectId, serverId, commandIdStr).WaitWithContext(ctx) |
| 162 | + if err != nil { |
| 163 | + core.LogAndAddError(ctx, &resp.Diagnostics, "Error waiting for run command", fmt.Sprintf("Polling API: %v", err)) |
| 164 | + return |
| 165 | + } |
| 166 | + |
| 167 | + if details.GetStatus() == v1api.COMMANDDETAILSSTATUS_FAILED { |
| 168 | + resp.Diagnostics.AddError( |
| 169 | + "Run command failed", |
| 170 | + fmt.Sprintf("Command %s finished with status %q (exit code: %d).\nOutput:\n%s", commandIdStr, details.GetStatus(), details.GetExitCode(), details.GetOutput()), |
| 171 | + ) |
| 172 | + return |
| 173 | + } |
| 174 | + |
| 175 | + tflog.Info(ctx, fmt.Sprintf("Run command %s completed successfully", commandIdStr)) |
| 176 | +} |
| 177 | + |
| 178 | +func toCreatePayload(ctx context.Context, model *runCommandModel) (*v1api.CreateCommandPayload, error) { |
| 179 | + if model == nil { |
| 180 | + return nil, fmt.Errorf("nil model") |
| 181 | + } |
| 182 | + |
| 183 | + payload := v1api.NewCreateCommandPayload(model.CommandTemplateName.ValueString()) |
| 184 | + |
| 185 | + if !model.Parameters.IsNull() && !model.Parameters.IsUnknown() { |
| 186 | + params := map[string]string{} |
| 187 | + diags := model.Parameters.ElementsAs(ctx, ¶ms, false) |
| 188 | + if diags.HasError() { |
| 189 | + return nil, fmt.Errorf("converting parameters: %v", diags.Errors()) |
| 190 | + } |
| 191 | + payload.SetParameters(params) |
| 192 | + } |
| 193 | + |
| 194 | + return payload, nil |
| 195 | +} |
0 commit comments