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 (r *credentialDataSource) Read(ctx context.Context, req datasource.ReadRequ
}
// Map response body to schema
err = mapFields(recordSetResp, &model)
err = mapFields(ctx, recordSetResp, &model)
if err != nil {
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading credential", fmt.Sprintf("Processing API payload: %v", err))
return

View file

@ -8,9 +8,9 @@ import (
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-log/tflog"
"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"
"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"
@ -211,7 +211,7 @@ func (r *credentialResource) Create(ctx context.Context, req resource.CreateRequ
}
// Map response body to schema
err = mapFields(waitResp, &model)
err = mapFields(ctx, waitResp, &model)
if err != nil {
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating credential", fmt.Sprintf("Processing API payload: %v", err))
return
@ -246,7 +246,7 @@ func (r *credentialResource) 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 credential", fmt.Sprintf("Processing API payload: %v", err))
return
@ -314,7 +314,7 @@ func (r *credentialResource) ImportState(ctx context.Context, req resource.Impor
tflog.Info(ctx, "Redis credential state imported")
}
func mapFields(credentialsResp *redis.CredentialsResponse, model *Model) error {
func mapFields(ctx context.Context, credentialsResp *redis.CredentialsResponse, model *Model) error {
if credentialsResp == nil {
return fmt.Errorf("response input is nil")
}
@ -343,19 +343,26 @@ func mapFields(credentialsResp *redis.CredentialsResponse, model *Model) error {
model.Id = types.StringValue(
strings.Join(idParts, core.Separator),
)
modelHosts, err := utils.ListValuetoStringSlice(model.Hosts)
if err != nil {
return err
}
model.CredentialId = types.StringValue(credentialId)
model.Hosts = types.ListNull(types.StringType)
if credentials != nil {
if credentials.Hosts != nil {
var hosts []attr.Value
for _, host := range *credentials.Hosts {
hosts = append(hosts, types.StringValue(host))
}
hostsList, diags := types.ListValue(types.StringType, hosts)
respHosts := *credentials.Hosts
reconciledHosts := utils.ReconcileStringSlices(modelHosts, respHosts)
hostsTF, diags := types.ListValueFrom(ctx, types.StringType, reconciledHosts)
if diags.HasError() {
return fmt.Errorf("failed to map hosts: %w", core.DiagsToError(diags))
}
model.Hosts = hostsList
model.Hosts = hostsTF
}
model.Host = types.StringPointerValue(credentials.Host)
model.LoadBalancedHost = types.StringPointerValue(credentials.LoadBalancedHost)

View file

@ -1,6 +1,7 @@
package redis
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 *redis.CredentialsResponse
expected Model
isValid bool
}{
{
"default_values",
Model{
InstanceId: types.StringValue("iid"),
ProjectId: types.StringValue("pid"),
},
&redis.CredentialsResponse{
Id: utils.Ptr("cid"),
Raw: &redis.RawCredentials{},
@ -40,6 +46,10 @@ func TestMapFields(t *testing.T) {
},
{
"simple_values",
Model{
InstanceId: types.StringValue("iid"),
ProjectId: types.StringValue("pid"),
},
&redis.CredentialsResponse{
Id: utils.Ptr("cid"),
Raw: &redis.RawCredentials{
@ -75,8 +85,60 @@ func TestMapFields(t *testing.T) {
},
true,
},
{
"hosts_unordered",
Model{
InstanceId: types.StringValue("iid"),
ProjectId: types.StringValue("pid"),
Hosts: types.ListValueMust(types.StringType, []attr.Value{
types.StringValue("host_2"),
types.StringValue(""),
types.StringValue("host_1"),
}),
},
&redis.CredentialsResponse{
Id: utils.Ptr("cid"),
Raw: &redis.RawCredentials{
Credentials: &redis.Credentials{
Host: utils.Ptr("host"),
Hosts: &[]string{
"",
"host_1",
"host_2",
},
LoadBalancedHost: utils.Ptr("load_balanced_host"),
Password: utils.Ptr("password"),
Port: utils.Ptr(int64(1234)),
Uri: utils.Ptr("uri"),
Username: utils.Ptr("username"),
},
},
},
Model{
Id: types.StringValue("pid,iid,cid"),
CredentialId: types.StringValue("cid"),
InstanceId: types.StringValue("iid"),
ProjectId: types.StringValue("pid"),
Host: types.StringValue("host"),
Hosts: types.ListValueMust(types.StringType, []attr.Value{
types.StringValue("host_2"),
types.StringValue(""),
types.StringValue("host_1"),
}),
LoadBalancedHost: types.StringValue("load_balanced_host"),
Password: types.StringValue("password"),
Port: types.Int64Value(1234),
Uri: types.StringValue("uri"),
Username: types.StringValue("username"),
},
true,
},
{
"null_fields_and_int_conversions",
Model{
InstanceId: types.StringValue("iid"),
ProjectId: types.StringValue("pid"),
},
&redis.CredentialsResponse{
Id: utils.Ptr("cid"),
Raw: &redis.RawCredentials{
@ -108,18 +170,30 @@ func TestMapFields(t *testing.T) {
},
{
"nil_response",
Model{
InstanceId: types.StringValue("iid"),
ProjectId: types.StringValue("pid"),
},
nil,
Model{},
false,
},
{
"no_resource_id",
Model{
InstanceId: types.StringValue("iid"),
ProjectId: types.StringValue("pid"),
},
&redis.CredentialsResponse{},
Model{},
false,
},
{
"nil_raw_credential",
Model{
InstanceId: types.StringValue("iid"),
ProjectId: types.StringValue("pid"),
},
&redis.CredentialsResponse{
Id: utils.Ptr("cid"),
},
@ -129,11 +203,7 @@ func TestMapFields(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.description, func(t *testing.T) {
model := &Model{
ProjectId: tt.expected.ProjectId,
InstanceId: tt.expected.InstanceId,
}
err := mapFields(tt.input, model)
err := mapFields(context.Background(), tt.input, &tt.state)
if !tt.isValid && err == nil {
t.Fatalf("Should have failed")
}
@ -141,7 +211,7 @@ func TestMapFields(t *testing.T) {
t.Fatalf("Should not have failed: %v", err)
}
if tt.isValid {
diff := cmp.Diff(model, &tt.expected)
diff := cmp.Diff(tt.state, tt.expected)
if diff != "" {
t.Fatalf("Data does not match: %s", diff)
}