Skip to content

Commit 822b9fc

Browse files
h3adexmarceljk
andauthored
bugfix(serverbackup): state drift in volume-id because of changed order (#979)
* fix: state drift in backup-properties.volume-id because of changed order in API response --------- Signed-off-by: Mauritz Uphoff <mauritz.uphoff@stackit.cloud> Co-authored-by: Marcel Jacek <Marcel.Jacek@stackit.cloud>
1 parent 7c1920f commit 822b9fc

File tree

4 files changed

+32
-8
lines changed

4 files changed

+32
-8
lines changed

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

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77
"strconv"
88
"strings"
99

10+
"github.com/hashicorp/terraform-plugin-framework-validators/listvalidator"
11+
"github.com/hashicorp/terraform-plugin-framework/diag"
1012
serverbackupUtils "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/serverbackup/utils"
1113

1214
"github.com/hashicorp/terraform-plugin-framework-validators/int64validator"
@@ -206,6 +208,9 @@ func (r *scheduleResource) Schema(_ context.Context, _ resource.SchemaRequest, r
206208
"volume_ids": schema.ListAttribute{
207209
ElementType: types.StringType,
208210
Optional: true,
211+
Validators: []validator.List{
212+
listvalidator.SizeAtLeast(1),
213+
},
209214
},
210215
"name": schema.StringAttribute{
211216
Required: true,
@@ -455,14 +460,31 @@ func mapFields(ctx context.Context, schedule *serverbackup.BackupSchedule, model
455460
model.BackupProperties = nil
456461
return nil
457462
}
458-
ids, diags := types.ListValueFrom(ctx, types.StringType, schedule.BackupProperties.VolumeIds)
459-
if diags.HasError() {
460-
return fmt.Errorf("failed to map hosts: %w", core.DiagsToError(diags))
463+
464+
volIds := types.ListNull(types.StringType)
465+
if schedule.BackupProperties.VolumeIds != nil {
466+
var modelVolIds []string
467+
if model.BackupProperties != nil {
468+
var err error
469+
modelVolIds, err = utils.ListValuetoStringSlice(model.BackupProperties.VolumeIds)
470+
if err != nil {
471+
return err
472+
}
473+
}
474+
475+
respVolIds := *schedule.BackupProperties.VolumeIds
476+
reconciledVolIds := utils.ReconcileStringSlices(modelVolIds, respVolIds)
477+
478+
var diags diag.Diagnostics
479+
volIds, diags = types.ListValueFrom(ctx, types.StringType, reconciledVolIds)
480+
if diags.HasError() {
481+
return fmt.Errorf("failed to map volumeIds: %w", core.DiagsToError(diags))
482+
}
461483
}
462484
model.BackupProperties = &scheduleBackupPropertiesModel{
463485
BackupName: types.StringValue(*schedule.BackupProperties.Name),
464486
RetentionPeriod: types.Int64Value(*schedule.BackupProperties.RetentionPeriod),
465-
VolumeIds: ids,
487+
VolumeIds: volIds,
466488
}
467489
model.Region = types.StringValue(region)
468490
return nil

stackit/internal/services/serverbackup/serverbackup_acc_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ func TestAccServerBackupScheduleMinResource(t *testing.T) {
8888
},
8989
// Creation
9090
{
91-
Config: resourceMinConfig,
91+
Config: testutil.ServerBackupProviderConfig() + "\n" + resourceMinConfig,
9292
ConfigVariables: testConfigVarsMin,
9393
Check: resource.ComposeAggregateTestCheckFunc(
9494
// Backup schedule data
@@ -180,7 +180,7 @@ func TestAccServerBackupScheduleMaxResource(t *testing.T) {
180180
},
181181
// Creation
182182
{
183-
Config: resourceMaxConfig,
183+
Config: testutil.ServerBackupProviderConfig() + "\n" + resourceMaxConfig,
184184
ConfigVariables: testConfigVarsMax,
185185
Check: resource.ComposeAggregateTestCheckFunc(
186186
// Backup schedule data

stackit/internal/services/serverupdate/serverupdate_acc_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ func TestAccServerUpdateScheduleMinResource(t *testing.T) {
9090
// Creation
9191
{
9292
ConfigVariables: testConfigVarsMin,
93-
Config: resourceMinConfig,
93+
Config: testutil.ServerUpdateProviderConfig() + "\n" + resourceMinConfig,
9494
Check: resource.ComposeAggregateTestCheckFunc(
9595
// Update schedule data
9696
resource.TestCheckResourceAttr("stackit_server_update_schedule.test_schedule", "project_id", testutil.ConvertConfigVariable(testConfigVarsMin["project_id"])),
@@ -177,7 +177,7 @@ func TestAccServerUpdateScheduleMaxResource(t *testing.T) {
177177
// Creation
178178
{
179179
ConfigVariables: testConfigVarsMax,
180-
Config: resourceMaxConfig,
180+
Config: testutil.ServerUpdateProviderConfig() + "\n" + resourceMaxConfig,
181181
Check: resource.ComposeAggregateTestCheckFunc(
182182
// Update schedule data
183183
resource.TestCheckResourceAttr("stackit_server_update_schedule.test_schedule", "project_id", testutil.ConvertConfigVariable(testConfigVarsMax["project_id"])),

stackit/internal/testutil/testutil.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,11 +379,13 @@ func ServerBackupProviderConfig() string {
379379
return `
380380
provider "stackit" {
381381
default_region = "eu01"
382+
enable_beta_resources = true
382383
}`
383384
}
384385
return fmt.Sprintf(`
385386
provider "stackit" {
386387
server_backup_custom_endpoint = "%s"
388+
enable_beta_resources = true
387389
}`,
388390
ServerBackupCustomEndpoint,
389391
)

0 commit comments

Comments
 (0)