Bugfix inconsistent applies of ListAttributes (#328)

* sort the list of ACLs for MongoDBFlex

* Fix and test other cases of ListAttribute ordering

* fix linting

* revert sorting changes, introduce new reconcilestrlist function

* merge main

* Fix rabbitmq

* fix segmentation fault

* Improve testing

* pass context to mapfields, minor name fixes
This commit is contained in:
Diogo Ferrão 2024-04-16 11:20:19 +01:00 committed by GitHub
parent 18d3f4d1fb
commit 9bd1da7cee
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
26 changed files with 1011 additions and 146 deletions

View file

@ -164,7 +164,7 @@ func (d *recordSetDataSource) Read(ctx context.Context, req datasource.ReadReque
return
}
err = mapFields(zoneResp, &model)
err = mapFields(ctx, zoneResp, &model)
if err != nil {
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading record set", fmt.Sprintf("Processing API payload: %v", err))
return

View file

@ -8,7 +8,6 @@ import (
"github.com/hashicorp/terraform-plugin-framework-validators/int64validator"
"github.com/hashicorp/terraform-plugin-framework-validators/listvalidator"
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
@ -23,6 +22,7 @@ import (
"github.com/stackitcloud/stackit-sdk-go/services/dns/wait"
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/conversion"
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core"
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/utils"
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/validate"
)
@ -249,7 +249,7 @@ func (r *recordSetResource) Create(ctx context.Context, req resource.CreateReque
}
// Map response body to schema
err = mapFields(waitResp, &model)
err = mapFields(ctx, waitResp, &model)
if err != nil {
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating record set", fmt.Sprintf("Processing API payload: %v", err))
return
@ -285,7 +285,7 @@ func (r *recordSetResource) Read(ctx context.Context, req resource.ReadRequest,
}
// Map response body to schema
err = mapFields(recordSetResp, &model)
err = mapFields(ctx, recordSetResp, &model)
if err != nil {
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading record set", fmt.Sprintf("Processing API payload: %v", err))
return
@ -335,7 +335,7 @@ func (r *recordSetResource) Update(ctx context.Context, req resource.UpdateReque
return
}
err = mapFields(waitResp, &model)
err = mapFields(ctx, waitResp, &model)
if err != nil {
core.LogAndAddError(ctx, &resp.Diagnostics, "Error updating record set", fmt.Sprintf("Processing API payload: %v", err))
return
@ -396,7 +396,7 @@ func (r *recordSetResource) ImportState(ctx context.Context, req resource.Import
tflog.Info(ctx, "DNS record set state imported")
}
func mapFields(recordSetResp *dns.RecordSetResponse, model *Model) error {
func mapFields(ctx context.Context, recordSetResp *dns.RecordSetResponse, model *Model) error {
if recordSetResp == nil || recordSetResp.Rrset == nil {
return fmt.Errorf("response input is nil")
}
@ -417,15 +417,25 @@ func mapFields(recordSetResp *dns.RecordSetResponse, model *Model) error {
if recordSet.Records == nil {
model.Records = types.ListNull(types.StringType)
} else {
records := []attr.Value{}
respRecords := []string{}
for _, record := range *recordSet.Records {
records = append(records, types.StringPointerValue(record.Content))
respRecords = append(respRecords, *record.Content)
}
recordsList, diags := types.ListValue(types.StringType, records)
modelRecords, err := utils.ListValuetoStringSlice(model.Records)
if err != nil {
return err
}
reconciledRecords := utils.ReconcileStringSlices(modelRecords, respRecords)
recordsTF, diags := types.ListValueFrom(ctx, types.StringType, reconciledRecords)
if diags.HasError() {
return fmt.Errorf("failed to map records: %w", core.DiagsToError(diags))
}
model.Records = recordsList
model.Records = recordsTF
}
idParts := []string{
model.ProjectId.ValueString(),

View file

@ -1,6 +1,7 @@
package dns
import (
"context"
"testing"
"github.com/google/go-cmp/cmp"
@ -88,6 +89,52 @@ func TestMapFields(t *testing.T) {
},
true,
},
{
"unordered_records",
Model{
ProjectId: types.StringValue("pid"),
ZoneId: types.StringValue("zid"),
Records: types.ListValueMust(types.StringType, []attr.Value{
types.StringValue("record_2"),
types.StringValue("record_1"),
}),
},
&dns.RecordSetResponse{
Rrset: &dns.RecordSet{
Id: utils.Ptr("rid"),
Active: utils.Ptr(true),
Comment: utils.Ptr("comment"),
Error: utils.Ptr("error"),
Name: utils.Ptr("name"),
Records: &[]dns.Record{
{Content: utils.Ptr("record_1")},
{Content: utils.Ptr("record_2")},
},
State: utils.Ptr("state"),
Ttl: utils.Ptr(int64(1)),
Type: utils.Ptr("type"),
},
},
Model{
Id: types.StringValue("pid,zid,rid"),
RecordSetId: types.StringValue("rid"),
ZoneId: types.StringValue("zid"),
ProjectId: types.StringValue("pid"),
Active: types.BoolValue(true),
Comment: types.StringValue("comment"),
Error: types.StringValue("error"),
Name: types.StringValue("name"),
FQDN: types.StringValue("name"),
Records: types.ListValueMust(types.StringType, []attr.Value{
types.StringValue("record_2"),
types.StringValue("record_1"),
}),
State: types.StringValue("state"),
TTL: types.Int64Value(1),
Type: types.StringValue("type"),
},
true,
},
{
"null_fields_and_int_conversions",
Model{
@ -148,7 +195,7 @@ func TestMapFields(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.description, func(t *testing.T) {
err := mapFields(tt.input, &tt.state)
err := mapFields(context.Background(), tt.input, &tt.state)
if !tt.isValid && err == nil {
t.Fatalf("Should have failed")
}

View file

@ -194,7 +194,7 @@ func (d *zoneDataSource) Read(ctx context.Context, req datasource.ReadRequest, r
return
}
err = mapFields(zoneResp, &model)
err = mapFields(ctx, zoneResp, &model)
if err != nil {
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading zone", fmt.Sprintf("Processing API payload: %v", err))
return

View file

@ -9,7 +9,6 @@ import (
"github.com/hashicorp/terraform-plugin-framework-validators/int64validator"
"github.com/hashicorp/terraform-plugin-framework-validators/listvalidator"
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
@ -26,6 +25,7 @@ import (
"github.com/stackitcloud/stackit-sdk-go/services/dns/wait"
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/conversion"
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core"
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/utils"
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/validate"
)
@ -329,7 +329,7 @@ func (r *zoneResource) Create(ctx context.Context, req resource.CreateRequest, r
}
// Map response body to schema
err = mapFields(waitResp, &model)
err = mapFields(ctx, waitResp, &model)
if err != nil {
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating zone", fmt.Sprintf("Processing API payload: %v", err))
return
@ -363,7 +363,7 @@ func (r *zoneResource) Read(ctx context.Context, req resource.ReadRequest, resp
}
// Map response body to schema
err = mapFields(zoneResp, &model)
err = mapFields(ctx, zoneResp, &model)
if err != nil {
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading zone", fmt.Sprintf("Processing API payload: %v", err))
return
@ -409,7 +409,7 @@ func (r *zoneResource) Update(ctx context.Context, req resource.UpdateRequest, r
return
}
err = mapFields(waitResp, &model)
err = mapFields(ctx, waitResp, &model)
if err != nil {
core.LogAndAddError(ctx, &resp.Diagnostics, "Error updating zone", fmt.Sprintf("Processing API payload: %v", err))
return
@ -475,7 +475,7 @@ func (r *zoneResource) ImportState(ctx context.Context, req resource.ImportState
tflog.Info(ctx, "DNS zone state imported")
}
func mapFields(zoneResp *dns.ZoneResponse, model *Model) error {
func mapFields(ctx context.Context, zoneResp *dns.ZoneResponse, model *Model) error {
if zoneResp == nil || zoneResp.Zone == nil {
return fmt.Errorf("response input is nil")
}
@ -512,15 +512,20 @@ func mapFields(zoneResp *dns.ZoneResponse, model *Model) error {
if z.Primaries == nil {
model.Primaries = types.ListNull(types.StringType)
} else {
respZonePrimaries := []attr.Value{}
for _, primary := range *z.Primaries {
respZonePrimaries = append(respZonePrimaries, types.StringValue(primary))
respZonePrimariesList, diags := types.ListValue(types.StringType, respZonePrimaries)
if diags.HasError() {
return fmt.Errorf("creating primaries list: %w", core.DiagsToError(diags))
}
model.Primaries = respZonePrimariesList
respPrimaries := *z.Primaries
modelPrimaries, err := utils.ListValuetoStringSlice(model.Primaries)
if err != nil {
return err
}
reconciledPrimaries := utils.ReconcileStringSlices(modelPrimaries, respPrimaries)
primariesTF, diags := types.ListValueFrom(ctx, types.StringType, reconciledPrimaries)
if diags.HasError() {
return fmt.Errorf("failed to map zone primaries: %w", core.DiagsToError(diags))
}
model.Primaries = primariesTF
}
model.ZoneId = types.StringValue(zoneId)
model.Description = types.StringPointerValue(z.Description)

View file

@ -1,6 +1,7 @@
package dns
import (
"context"
"testing"
"github.com/google/go-cmp/cmp"
@ -13,12 +14,17 @@ import (
func TestMapFields(t *testing.T) {
tests := []struct {
description string
state Model
input *dns.ZoneResponse
expected Model
isValid bool
}{
{
"default_ok",
Model{
ProjectId: types.StringValue("pid"),
ZoneId: types.StringValue("zid"),
},
&dns.ZoneResponse{
Zone: &dns.Zone{
Id: utils.Ptr("zid"),
@ -47,6 +53,10 @@ func TestMapFields(t *testing.T) {
},
{
"values_ok",
Model{
ProjectId: types.StringValue("pid"),
ZoneId: types.StringValue("zid"),
},
&dns.ZoneResponse{
Zone: &dns.Zone{
Id: utils.Ptr("zid"),
@ -104,8 +114,84 @@ func TestMapFields(t *testing.T) {
},
true,
},
{
"primaries_unordered",
Model{
ProjectId: types.StringValue("pid"),
ZoneId: types.StringValue("zid"),
Primaries: types.ListValueMust(types.StringType, []attr.Value{
types.StringValue("primary2"),
types.StringValue("primary1"),
}),
},
&dns.ZoneResponse{
Zone: &dns.Zone{
Id: utils.Ptr("zid"),
Name: utils.Ptr("name"),
DnsName: utils.Ptr("dnsname"),
Acl: utils.Ptr("acl"),
Active: utils.Ptr(false),
CreationStarted: utils.Ptr("bar"),
CreationFinished: utils.Ptr("foo"),
DefaultTTL: utils.Ptr(int64(1)),
ExpireTime: utils.Ptr(int64(2)),
RefreshTime: utils.Ptr(int64(3)),
RetryTime: utils.Ptr(int64(4)),
SerialNumber: utils.Ptr(int64(5)),
NegativeCache: utils.Ptr(int64(6)),
State: utils.Ptr("state"),
Type: utils.Ptr("type"),
Primaries: &[]string{
"primary1",
"primary2",
},
PrimaryNameServer: utils.Ptr("pns"),
UpdateStarted: utils.Ptr("ufoo"),
UpdateFinished: utils.Ptr("ubar"),
Visibility: utils.Ptr("visibility"),
Error: utils.Ptr("error"),
ContactEmail: utils.Ptr("a@b.cd"),
Description: utils.Ptr("description"),
IsReverseZone: utils.Ptr(false),
RecordCount: utils.Ptr(int64(3)),
},
},
Model{
Id: types.StringValue("pid,zid"),
ProjectId: types.StringValue("pid"),
ZoneId: types.StringValue("zid"),
Name: types.StringValue("name"),
DnsName: types.StringValue("dnsname"),
Acl: types.StringValue("acl"),
Active: types.BoolValue(false),
DefaultTTL: types.Int64Value(1),
ExpireTime: types.Int64Value(2),
RefreshTime: types.Int64Value(3),
RetryTime: types.Int64Value(4),
SerialNumber: types.Int64Value(5),
NegativeCache: types.Int64Value(6),
Type: types.StringValue("type"),
State: types.StringValue("state"),
PrimaryNameServer: types.StringValue("pns"),
Primaries: types.ListValueMust(types.StringType, []attr.Value{
types.StringValue("primary2"),
types.StringValue("primary1"),
}),
Visibility: types.StringValue("visibility"),
ContactEmail: types.StringValue("a@b.cd"),
Description: types.StringValue("description"),
IsReverseZone: types.BoolValue(false),
RecordCount: types.Int64Value(3),
},
true,
},
{
"nullable_fields_and_int_conversions_ok",
Model{
Id: types.StringValue("pid,zid"),
ProjectId: types.StringValue("pid"),
ZoneId: types.StringValue("zid"),
},
&dns.ZoneResponse{
Zone: &dns.Zone{
Id: utils.Ptr("zid"),
@ -162,12 +248,17 @@ func TestMapFields(t *testing.T) {
},
{
"response_nil_fail",
Model{},
nil,
Model{},
false,
},
{
"no_resource_id",
Model{
ProjectId: types.StringValue("pid"),
ZoneId: types.StringValue("zid"),
},
&dns.ZoneResponse{},
Model{},
false,
@ -175,10 +266,7 @@ func TestMapFields(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.description, func(t *testing.T) {
state := &Model{
ProjectId: tt.expected.ProjectId,
}
err := mapFields(tt.input, state)
err := mapFields(context.Background(), tt.input, &tt.state)
if !tt.isValid && err == nil {
t.Fatalf("Should have failed")
}
@ -186,7 +274,7 @@ func TestMapFields(t *testing.T) {
t.Fatalf("Should not have failed: %v", err)
}
if tt.isValid {
diff := cmp.Diff(state, &tt.expected)
diff := cmp.Diff(tt.state, tt.expected)
if diff != "" {
t.Fatalf("Data does not match: %s", diff)
}