Skip to content

Commit 2cf3dde

Browse files
author
Yago Carlos Fernandez Gou
committed
Adjust resource implementation and implement review comments
1 parent 8d8efdf commit 2cf3dde

File tree

1 file changed

+22
-23
lines changed

1 file changed

+22
-23
lines changed

stackit/internal/services/intake/runner/resource.go

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"net/http"
88
"strings"
99

10-
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
1110
"github.com/hashicorp/terraform-plugin-framework/attr"
1211
"github.com/hashicorp/terraform-plugin-framework/resource"
1312
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
@@ -18,7 +17,6 @@ import (
1817
"github.com/hashicorp/terraform-plugin-framework/types"
1918
"github.com/hashicorp/terraform-plugin-log/tflog"
2019
"github.com/stackitcloud/stackit-sdk-go/core/oapierror"
21-
sdkUtils "github.com/stackitcloud/stackit-sdk-go/core/utils"
2220
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/conversion"
2321
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core"
2422
intakeUtils "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/intake/utils"
@@ -42,12 +40,12 @@ type Model struct {
4240
Id types.String `tfsdk:"id"` // needed by TF
4341
ProjectId types.String `tfsdk:"project_id"`
4442
RunnerId types.String `tfsdk:"runner_id"`
45-
Region types.String `tfsdk:"region"`
4643
Name types.String `tfsdk:"name"`
4744
Description types.String `tfsdk:"description"`
4845
Labels types.Map `tfsdk:"labels"`
4946
MaxMessageSizeKiB types.Int64 `tfsdk:"max_message_size_kib"`
5047
MaxMessagesPerHour types.Int64 `tfsdk:"max_messages_per_hour"`
48+
Region types.String `tfsdk:"region"`
5149
}
5250

5351
// NewRunnerResource is a helper function to simplify the provider implementation.
@@ -132,12 +130,16 @@ func (r *runnerResource) Schema(_ context.Context, _ resource.SchemaRequest, res
132130
"id": schema.StringAttribute{
133131
Description: descriptions["id"],
134132
Computed: true,
133+
PlanModifiers: []planmodifier.String{
134+
stringplanmodifier.UseStateForUnknown(),
135+
},
135136
},
136137
"project_id": schema.StringAttribute{
137138
Description: descriptions["project_id"],
138139
Required: true,
139140
PlanModifiers: []planmodifier.String{
140141
stringplanmodifier.RequiresReplace(),
142+
stringplanmodifier.UseStateForUnknown(),
141143
},
142144
Validators: []validator.String{
143145
validate.UUID(),
@@ -154,6 +156,9 @@ func (r *runnerResource) Schema(_ context.Context, _ resource.SchemaRequest, res
154156
"name": schema.StringAttribute{
155157
Description: descriptions["name"],
156158
Required: true,
159+
PlanModifiers: []planmodifier.String{
160+
stringplanmodifier.RequiresReplace(),
161+
},
157162
},
158163
"description": schema.StringAttribute{
159164
Description: descriptions["description"],
@@ -187,9 +192,6 @@ func (r *runnerResource) Schema(_ context.Context, _ resource.SchemaRequest, res
187192
PlanModifiers: []planmodifier.String{
188193
stringplanmodifier.RequiresReplace(),
189194
},
190-
Validators: []validator.String{
191-
stringvalidator.OneOf("eu01"), // Currently Intake supports only EU01 region
192-
},
193195
},
194196
},
195197
}
@@ -217,7 +219,7 @@ func (r *runnerResource) Create(ctx context.Context, req resource.CreateRequest,
217219
return
218220
}
219221

220-
// Create new bar
222+
// Create new runner
221223
runnerResp, err := r.client.CreateIntakeRunner(ctx, projectId, region).CreateIntakeRunnerPayload(*payload).Execute()
222224
if err != nil {
223225
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating runner", fmt.Sprintf("Calling API: %v", err))
@@ -232,7 +234,7 @@ func (r *runnerResource) Create(ctx context.Context, req resource.CreateRequest,
232234
return
233235
}
234236

235-
err = mapFields(runnerResp, &model)
237+
err = mapFields(runnerResp, &model, region)
236238
if err != nil {
237239
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating runner", fmt.Sprintf("Processing API payload: %v", err))
238240
return
@@ -277,7 +279,7 @@ func (r *runnerResource) Read(ctx context.Context, req resource.ReadRequest, res
277279
ctx = core.LogResponse(ctx)
278280

279281
// Map response body to schema
280-
err = mapFields(runnerResp, &model)
282+
err = mapFields(runnerResp, &model, region)
281283
if err != nil {
282284
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading runner", fmt.Sprintf("Processing API payload: %v", err))
283285
return
@@ -304,7 +306,7 @@ func (r *runnerResource) Update(ctx context.Context, req resource.UpdateRequest,
304306

305307
projectId := model.ProjectId.ValueString()
306308
runnerId := model.RunnerId.ValueString()
307-
region := r.providerData.GetRegionWithOverride(model.Region)
309+
region := model.Region.ValueString()
308310
ctx = tflog.SetField(ctx, "project_id", projectId)
309311
ctx = tflog.SetField(ctx, "runner_id", runnerId)
310312
ctx = tflog.SetField(ctx, "region", region)
@@ -330,7 +332,7 @@ func (r *runnerResource) Update(ctx context.Context, req resource.UpdateRequest,
330332
}
331333

332334
// Map response body to schema
333-
err = mapFields(runnerResp, &model)
335+
err = mapFields(runnerResp, &model, region)
334336
if err != nil {
335337
core.LogAndAddError(ctx, &resp.Diagnostics, "Error updating runner", fmt.Sprintf("Processing API response: %v", err))
336338
return
@@ -361,7 +363,7 @@ func (r *runnerResource) Delete(ctx context.Context, req resource.DeleteRequest,
361363
ctx = tflog.SetField(ctx, "region", region)
362364
ctx = tflog.SetField(ctx, "runner_id", runnerId)
363365

364-
// Delete existing bar
366+
// Delete existing runner
365367
err := r.client.DeleteIntakeRunner(ctx, projectId, region, runnerId).Execute()
366368
if err != nil {
367369
var oapiErr *oapierror.GenericOpenAPIError
@@ -407,7 +409,7 @@ func (r *runnerResource) ImportState(ctx context.Context, req resource.ImportSta
407409
}
408410

409411
// Maps runner fields to the provider internal model
410-
func mapFields(runnerResp *intake.IntakeRunnerResponse, model *Model) error {
412+
func mapFields(runnerResp *intake.IntakeRunnerResponse, model *Model, region string) error {
411413
if runnerResp == nil {
412414
return fmt.Errorf("response input is nil")
413415
}
@@ -422,7 +424,7 @@ func mapFields(runnerResp *intake.IntakeRunnerResponse, model *Model) error {
422424

423425
model.Id = utils.BuildInternalTerraformId(
424426
model.ProjectId.ValueString(),
425-
model.Region.ValueString(),
427+
region,
426428
runnerId,
427429
)
428430

@@ -447,12 +449,13 @@ func mapFields(runnerResp *intake.IntakeRunnerResponse, model *Model) error {
447449
} else {
448450
model.Description = types.StringPointerValue(runnerResp.Description)
449451
}
452+
model.Region = types.StringValue(region)
450453
model.MaxMessageSizeKiB = types.Int64PointerValue(runnerResp.MaxMessageSizeKiB)
451454
model.MaxMessagesPerHour = types.Int64PointerValue(runnerResp.MaxMessagesPerHour)
452455
return nil
453456
}
454457

455-
// Build CreateBarPayload from provider's model
458+
// Build CreateIntakeRunnerPayload from provider's model
456459
func toCreatePayload(model *Model) (*intake.CreateIntakeRunnerPayload, error) {
457460
if model == nil {
458461
return nil, fmt.Errorf("nil model")
@@ -480,6 +483,7 @@ func toCreatePayload(model *Model) (*intake.CreateIntakeRunnerPayload, error) {
480483
}, nil
481484
}
482485

486+
// Build UpdateIntakeRunnerPayload from provider's model
483487
func toUpdatePayload(model, state *Model) (*intake.UpdateIntakeRunnerPayload, error) {
484488
if model == nil {
485489
return nil, fmt.Errorf("model is nil")
@@ -492,14 +496,9 @@ func toUpdatePayload(model, state *Model) (*intake.UpdateIntakeRunnerPayload, er
492496
payload.MaxMessageSizeKiB = conversion.Int64ValueToPointer(model.MaxMessageSizeKiB)
493497
payload.MaxMessagesPerHour = conversion.Int64ValueToPointer(model.MaxMessagesPerHour)
494498

495-
// Handle optional fields
496-
if !model.Description.IsUnknown() || model.Description.IsNull() {
497-
if model.Description.IsNull() {
498-
payload.Description = sdkUtils.Ptr("")
499-
} else {
500-
payload.Description = conversion.StringValueToPointer(model.Description)
501-
}
502-
}
499+
// Optional fields
500+
payload.DisplayName = conversion.StringValueToPointer(model.Name)
501+
payload.Description = conversion.StringValueToPointer(model.Description)
503502

504503
var labels map[string]string
505504
if !model.Labels.IsUnknown() {

0 commit comments

Comments
 (0)