Skip to content

Commit d6e7890

Browse files
GokceGKmarceljk
andauthored
chore(serverupdate): switch to new SDK structure (#1376)
* chore(serverupdate): use new multi API version support SDK * fix linter issues * remove unneeded check * remove unneeded check * Update stackit/internal/services/serverupdate/schedule/resource_test.go Co-authored-by: Marcel Jacek <72880145+marceljk@users.noreply.github.com> * Update stackit/internal/services/serverupdate/schedule/resource_test.go Co-authored-by: Marcel Jacek <72880145+marceljk@users.noreply.github.com> * Update stackit/internal/services/serverupdate/schedule/resource_test.go Co-authored-by: Marcel Jacek <72880145+marceljk@users.noreply.github.com> * Update stackit/internal/services/serverupdate/schedule/resource_test.go Co-authored-by: Marcel Jacek <72880145+marceljk@users.noreply.github.com> * Update stackit/internal/services/serverupdate/schedule/resource_test.go Co-authored-by: Marcel Jacek <72880145+marceljk@users.noreply.github.com> * Update stackit/internal/services/serverupdate/schedule/schedules_datasource_test.go Co-authored-by: Marcel Jacek <72880145+marceljk@users.noreply.github.com> * fix linter issues --------- Co-authored-by: Marcel Jacek <72880145+marceljk@users.noreply.github.com>
1 parent 61a486c commit d6e7890

12 files changed

Lines changed: 129 additions & 134 deletions

File tree

stackit/internal/services/serverupdate/enable/datasource.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
1111
"github.com/hashicorp/terraform-plugin-framework/types"
1212
"github.com/hashicorp/terraform-plugin-log/tflog"
13-
"github.com/stackitcloud/stackit-sdk-go/services/serverupdate"
13+
serverupdate "github.com/stackitcloud/stackit-sdk-go/services/serverupdate/v2api"
1414

1515
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/conversion"
1616
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core"
@@ -130,7 +130,7 @@ func (d *serverUpdateEnableDataSource) Read(ctx context.Context, req datasource.
130130
ctx = tflog.SetField(ctx, "server_id", serverId)
131131
ctx = tflog.SetField(ctx, "region", region)
132132

133-
serviceResp, err := d.client.GetServiceResource(ctx, projectId, serverId, region).Execute()
133+
serviceResp, err := d.client.DefaultAPI.GetServiceResource(ctx, projectId, serverId, region).Execute()
134134
if err != nil {
135135
utils.LogError(
136136
ctx,

stackit/internal/services/serverupdate/enable/datasource_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"github.com/google/go-cmp/cmp"
88
"github.com/hashicorp/terraform-plugin-framework/types"
99
"github.com/stackitcloud/stackit-sdk-go/core/utils"
10-
"github.com/stackitcloud/stackit-sdk-go/services/serverupdate"
10+
serverupdate "github.com/stackitcloud/stackit-sdk-go/services/serverupdate/v2api"
1111
)
1212

1313
func TestDataMapFields(t *testing.T) {

stackit/internal/services/serverupdate/enable/resource.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
1616
"github.com/hashicorp/terraform-plugin-log/tflog"
1717
"github.com/stackitcloud/stackit-sdk-go/core/oapierror"
18-
"github.com/stackitcloud/stackit-sdk-go/services/serverupdate"
18+
serverupdate "github.com/stackitcloud/stackit-sdk-go/services/serverupdate/v2api"
1919

2020
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/conversion"
2121
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core"
@@ -189,7 +189,7 @@ func (r *serverUpdateEnableResource) Create(ctx context.Context, req resource.Cr
189189
ctx = tflog.SetField(ctx, "server_id", serverId)
190190
ctx = tflog.SetField(ctx, "region", region)
191191

192-
err := r.client.EnableServiceResource(ctx, projectId, serverId, region).EnableServiceResourcePayload(serverupdate.EnableServiceResourcePayload{}).Execute()
192+
err := r.client.DefaultAPI.EnableServiceResource(ctx, projectId, serverId, region).EnableServiceResourcePayload(serverupdate.EnableServiceResourcePayload{}).Execute()
193193
if err != nil {
194194
var oapiErr *oapierror.GenericOpenAPIError
195195
ok := errors.As(err, &oapiErr)
@@ -201,7 +201,7 @@ func (r *serverUpdateEnableResource) Create(ctx context.Context, req resource.Cr
201201
tflog.Info(ctx, "Server update is already enabled for this server. Please check duplicate resources.")
202202
}
203203

204-
serviceResp, err := r.client.GetServiceResource(ctx, projectId, serverId, region).Execute()
204+
serviceResp, err := r.client.DefaultAPI.GetServiceResource(ctx, projectId, serverId, region).Execute()
205205
if err != nil {
206206
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading server update enable", fmt.Sprintf("Calling API: %v", err))
207207
return
@@ -243,7 +243,7 @@ func (r *serverUpdateEnableResource) Read(ctx context.Context, req resource.Read
243243
ctx = tflog.SetField(ctx, "server_id", serverId)
244244
ctx = tflog.SetField(ctx, "region", region)
245245

246-
serviceResp, err := r.client.GetServiceResource(ctx, projectId, serverId, region).Execute()
246+
serviceResp, err := r.client.DefaultAPI.GetServiceResource(ctx, projectId, serverId, region).Execute()
247247
if err != nil {
248248
oapiErr, ok := err.(*oapierror.GenericOpenAPIError) //nolint:errorlint //complaining that error.As should be used to catch wrapped errors, but this error should not be wrapped
249249
if ok && oapiErr.StatusCode == http.StatusNotFound {
@@ -297,7 +297,7 @@ func (r *serverUpdateEnableResource) Delete(ctx context.Context, req resource.De
297297
ctx = tflog.SetField(ctx, "server_id", serverId)
298298
ctx = tflog.SetField(ctx, "region", region)
299299

300-
err := r.client.DisableServiceResource(ctx, projectId, serverId, region).Execute()
300+
err := r.client.DefaultAPI.DisableServiceResource(ctx, projectId, serverId, region).Execute()
301301
if err != nil {
302302
core.LogAndAddError(ctx, &resp.Diagnostics, "Error deleting server update enable", fmt.Sprintf("Calling API: %v", err))
303303
return

stackit/internal/services/serverupdate/enable/resource_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"github.com/hashicorp/terraform-plugin-framework/types"
99

1010
"github.com/stackitcloud/stackit-sdk-go/core/utils"
11-
"github.com/stackitcloud/stackit-sdk-go/services/serverupdate"
11+
serverupdate "github.com/stackitcloud/stackit-sdk-go/services/serverupdate/v2api"
1212
)
1313

1414
func TestMapFields(t *testing.T) {

stackit/internal/services/serverupdate/schedule/resource.go

Lines changed: 38 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@ import (
77
"strconv"
88
"strings"
99

10+
"github.com/hashicorp/terraform-plugin-framework-validators/int32validator"
11+
"github.com/hashicorp/terraform-plugin-framework/resource/schema/int32planmodifier"
12+
1013
serverupdateUtils "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/serverupdate/utils"
1114

12-
"github.com/hashicorp/terraform-plugin-framework-validators/int64validator"
1315
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
1416
"github.com/hashicorp/terraform-plugin-framework/path"
1517
"github.com/hashicorp/terraform-plugin-framework/resource"
1618
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
17-
"github.com/hashicorp/terraform-plugin-framework/resource/schema/int64planmodifier"
1819
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
1920
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
2021
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
@@ -28,7 +29,7 @@ import (
2829
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/validate"
2930

3031
"github.com/stackitcloud/stackit-sdk-go/core/oapierror"
31-
"github.com/stackitcloud/stackit-sdk-go/services/serverupdate"
32+
serverupdate "github.com/stackitcloud/stackit-sdk-go/services/serverupdate/v2api"
3233
)
3334

3435
// Ensure the implementation satisfies the expected interfaces.
@@ -43,11 +44,11 @@ type Model struct {
4344
ID types.String `tfsdk:"id"`
4445
ProjectId types.String `tfsdk:"project_id"`
4546
ServerId types.String `tfsdk:"server_id"`
46-
UpdateScheduleId types.Int64 `tfsdk:"update_schedule_id"`
47+
UpdateScheduleId types.Int32 `tfsdk:"update_schedule_id"`
4748
Name types.String `tfsdk:"name"`
4849
Rrule types.String `tfsdk:"rrule"`
4950
Enabled types.Bool `tfsdk:"enabled"`
50-
MaintenanceWindow types.Int64 `tfsdk:"maintenance_window"`
51+
MaintenanceWindow types.Int32 `tfsdk:"maintenance_window"`
5152
Region types.String `tfsdk:"region"`
5253
}
5354

@@ -136,14 +137,14 @@ func (r *scheduleResource) Schema(_ context.Context, _ resource.SchemaRequest, r
136137
stringvalidator.LengthBetween(1, 255),
137138
},
138139
},
139-
"update_schedule_id": schema.Int64Attribute{
140+
"update_schedule_id": schema.Int32Attribute{
140141
Description: "Update schedule ID.",
141142
Computed: true,
142-
PlanModifiers: []planmodifier.Int64{
143-
int64planmodifier.UseStateForUnknown(),
143+
PlanModifiers: []planmodifier.Int32{
144+
int32planmodifier.UseStateForUnknown(),
144145
},
145-
Validators: []validator.Int64{
146-
int64validator.AtLeast(1),
146+
Validators: []validator.Int32{
147+
int32validator.AtLeast(1),
147148
},
148149
},
149150
"project_id": schema.StringAttribute{
@@ -186,12 +187,12 @@ func (r *scheduleResource) Schema(_ context.Context, _ resource.SchemaRequest, r
186187
Description: "Is the update schedule enabled or disabled.",
187188
Required: true,
188189
},
189-
"maintenance_window": schema.Int64Attribute{
190+
"maintenance_window": schema.Int32Attribute{
190191
Description: "Maintenance window [1..24]. Updates start within the defined hourly window. Depending on the updates, the process may exceed this timeframe and require an automatic restart.",
191192
Required: true,
192-
Validators: []validator.Int64{
193-
int64validator.AtLeast(1),
194-
int64validator.AtMost(24),
193+
Validators: []validator.Int32{
194+
int32validator.AtLeast(1),
195+
int32validator.AtMost(24),
195196
},
196197
},
197198
"region": schema.StringAttribute{
@@ -244,15 +245,15 @@ func (r *scheduleResource) Create(ctx context.Context, req resource.CreateReques
244245
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating server update schedule", fmt.Sprintf("Creating API payload: %v", err))
245246
return
246247
}
247-
scheduleResp, err := r.client.CreateUpdateSchedule(ctx, projectId, serverId, region).CreateUpdateSchedulePayload(*payload).Execute()
248+
scheduleResp, err := r.client.DefaultAPI.CreateUpdateSchedule(ctx, projectId, serverId, region).CreateUpdateSchedulePayload(*payload).Execute()
248249
if err != nil {
249250
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating server update schedule", fmt.Sprintf("Calling API: %v", err))
250251
return
251252
}
252253

253254
ctx = core.LogResponse(ctx)
254255

255-
ctx = tflog.SetField(ctx, "update_schedule_id", *scheduleResp.Id)
256+
ctx = tflog.SetField(ctx, "update_schedule_id", scheduleResp.Id)
256257

257258
// Map response body to schema
258259
err = mapFields(scheduleResp, &model, region)
@@ -281,14 +282,14 @@ func (r *scheduleResource) Read(ctx context.Context, req resource.ReadRequest, r
281282

282283
projectId := model.ProjectId.ValueString()
283284
serverId := model.ServerId.ValueString()
284-
updateScheduleId := model.UpdateScheduleId.ValueInt64()
285+
updateScheduleId := model.UpdateScheduleId.ValueInt32()
285286
region := r.providerData.GetRegionWithOverride(model.Region)
286287
ctx = tflog.SetField(ctx, "project_id", projectId)
287288
ctx = tflog.SetField(ctx, "server_id", serverId)
288289
ctx = tflog.SetField(ctx, "region", region)
289290
ctx = tflog.SetField(ctx, "update_schedule_id", updateScheduleId)
290291

291-
scheduleResp, err := r.client.GetUpdateSchedule(ctx, projectId, serverId, strconv.FormatInt(updateScheduleId, 10), region).Execute()
292+
scheduleResp, err := r.client.DefaultAPI.GetUpdateSchedule(ctx, projectId, serverId, strconv.FormatInt(int64(updateScheduleId), 10), region).Execute()
292293
if err != nil {
293294
oapiErr, ok := err.(*oapierror.GenericOpenAPIError) //nolint:errorlint //complaining that error.As should be used to catch wrapped errors, but this error should not be wrapped
294295
if ok && oapiErr.StatusCode == http.StatusNotFound {
@@ -330,7 +331,7 @@ func (r *scheduleResource) Update(ctx context.Context, req resource.UpdateReques
330331

331332
projectId := model.ProjectId.ValueString()
332333
serverId := model.ServerId.ValueString()
333-
updateScheduleId := model.UpdateScheduleId.ValueInt64()
334+
updateScheduleId := model.UpdateScheduleId.ValueInt32()
334335
region := model.Region.ValueString()
335336
ctx = tflog.SetField(ctx, "project_id", projectId)
336337
ctx = tflog.SetField(ctx, "server_id", serverId)
@@ -344,7 +345,7 @@ func (r *scheduleResource) Update(ctx context.Context, req resource.UpdateReques
344345
return
345346
}
346347

347-
scheduleResp, err := r.client.UpdateUpdateSchedule(ctx, projectId, serverId, strconv.FormatInt(updateScheduleId, 10), region).UpdateUpdateSchedulePayload(*payload).Execute()
348+
scheduleResp, err := r.client.DefaultAPI.UpdateUpdateSchedule(ctx, projectId, serverId, strconv.FormatInt(int64(updateScheduleId), 10), region).UpdateUpdateSchedulePayload(*payload).Execute()
348349
if err != nil {
349350
core.LogAndAddError(ctx, &resp.Diagnostics, "Error updating server update schedule", fmt.Sprintf("Calling API: %v", err))
350351
return
@@ -379,14 +380,14 @@ func (r *scheduleResource) Delete(ctx context.Context, req resource.DeleteReques
379380

380381
projectId := model.ProjectId.ValueString()
381382
serverId := model.ServerId.ValueString()
382-
updateScheduleId := model.UpdateScheduleId.ValueInt64()
383+
updateScheduleId := model.UpdateScheduleId.ValueInt32()
383384
region := model.Region.ValueString()
384385
ctx = tflog.SetField(ctx, "project_id", projectId)
385386
ctx = tflog.SetField(ctx, "server_id", serverId)
386387
ctx = tflog.SetField(ctx, "region", region)
387388
ctx = tflog.SetField(ctx, "update_schedule_id", updateScheduleId)
388389

389-
err := r.client.DeleteUpdateSchedule(ctx, projectId, serverId, strconv.FormatInt(updateScheduleId, 10), region).Execute()
390+
err := r.client.DefaultAPI.DeleteUpdateSchedule(ctx, projectId, serverId, strconv.FormatInt(int64(updateScheduleId), 10), region).Execute()
390391
if err != nil {
391392
core.LogAndAddError(ctx, &resp.Diagnostics, "Error deleting server update schedule", fmt.Sprintf("Calling API: %v", err))
392393
return
@@ -432,19 +433,16 @@ func mapFields(schedule *serverupdate.UpdateSchedule, model *Model, region strin
432433
if model == nil {
433434
return fmt.Errorf("model input is nil")
434435
}
435-
if schedule.Id == nil {
436-
return fmt.Errorf("response id is nil")
437-
}
438436

439-
model.UpdateScheduleId = types.Int64PointerValue(schedule.Id)
437+
model.UpdateScheduleId = types.Int32Value(schedule.Id)
440438
model.ID = utils.BuildInternalTerraformId(
441439
model.ProjectId.ValueString(), region, model.ServerId.ValueString(),
442-
strconv.FormatInt(model.UpdateScheduleId.ValueInt64(), 10),
440+
strconv.FormatInt(int64(model.UpdateScheduleId.ValueInt32()), 10),
443441
)
444-
model.Name = types.StringPointerValue(schedule.Name)
445-
model.Rrule = types.StringPointerValue(schedule.Rrule)
446-
model.Enabled = types.BoolPointerValue(schedule.Enabled)
447-
model.MaintenanceWindow = types.Int64PointerValue(schedule.MaintenanceWindow)
442+
model.Name = types.StringValue(schedule.Name)
443+
model.Rrule = types.StringValue(schedule.Rrule)
444+
model.Enabled = types.BoolValue(schedule.Enabled)
445+
model.MaintenanceWindow = types.Int32Value(schedule.MaintenanceWindow)
448446
model.Region = types.StringValue(region)
449447
return nil
450448
}
@@ -458,7 +456,7 @@ func enableUpdatesService(ctx context.Context, model *Model, client *serverupdat
458456
payload := serverupdate.EnableServiceResourcePayload{}
459457

460458
tflog.Debug(ctx, "Enabling server update service")
461-
err := client.EnableServiceResource(ctx, projectId, serverId, region).EnableServiceResourcePayload(payload).Execute()
459+
err := client.DefaultAPI.EnableServiceResource(ctx, projectId, serverId, region).EnableServiceResourcePayload(payload).Execute()
462460
if err != nil {
463461
if strings.Contains(err.Error(), "Tried to activate already active service") {
464462
tflog.Debug(ctx, "Service for server update already enabled")
@@ -476,10 +474,10 @@ func toCreatePayload(model *Model) (*serverupdate.CreateUpdateSchedulePayload, e
476474
}
477475

478476
return &serverupdate.CreateUpdateSchedulePayload{
479-
Enabled: conversion.BoolValueToPointer(model.Enabled),
480-
Name: conversion.StringValueToPointer(model.Name),
481-
Rrule: conversion.StringValueToPointer(model.Rrule),
482-
MaintenanceWindow: conversion.Int64ValueToPointer(model.MaintenanceWindow),
477+
Enabled: model.Enabled.ValueBool(),
478+
Name: model.Name.ValueString(),
479+
Rrule: model.Rrule.ValueString(),
480+
MaintenanceWindow: model.MaintenanceWindow.ValueInt32(),
483481
}, nil
484482
}
485483

@@ -489,9 +487,9 @@ func toUpdatePayload(model *Model) (*serverupdate.UpdateUpdateSchedulePayload, e
489487
}
490488

491489
return &serverupdate.UpdateUpdateSchedulePayload{
492-
Enabled: conversion.BoolValueToPointer(model.Enabled),
493-
Name: conversion.StringValueToPointer(model.Name),
494-
Rrule: conversion.StringValueToPointer(model.Rrule),
495-
MaintenanceWindow: conversion.Int64ValueToPointer(model.MaintenanceWindow),
490+
Enabled: model.Enabled.ValueBool(),
491+
Name: model.Name.ValueString(),
492+
Rrule: model.Rrule.ValueString(),
493+
MaintenanceWindow: model.MaintenanceWindow.ValueInt32(),
496494
}, nil
497495
}

0 commit comments

Comments
 (0)