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:
parent
18d3f4d1fb
commit
9bd1da7cee
26 changed files with 1011 additions and 146 deletions
|
|
@ -173,7 +173,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
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
|
|
@ -223,7 +223,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
|
||||
|
|
@ -258,7 +258,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
|
||||
|
|
@ -326,7 +326,7 @@ func (r *credentialResource) ImportState(ctx context.Context, req resource.Impor
|
|||
tflog.Info(ctx, "RabbitMQ credential state imported")
|
||||
}
|
||||
|
||||
func mapFields(credentialsResp *rabbitmq.CredentialsResponse, model *Model) error {
|
||||
func mapFields(ctx context.Context, credentialsResp *rabbitmq.CredentialsResponse, model *Model) error {
|
||||
if credentialsResp == nil {
|
||||
return fmt.Errorf("response input is nil")
|
||||
}
|
||||
|
|
@ -356,48 +356,66 @@ func mapFields(credentialsResp *rabbitmq.CredentialsResponse, model *Model) erro
|
|||
strings.Join(idParts, core.Separator),
|
||||
)
|
||||
model.CredentialId = types.StringValue(credentialId)
|
||||
|
||||
modelHosts, err := utils.ListValuetoStringSlice(model.Hosts)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
modelHttpApiUris, err := utils.ListValuetoStringSlice(model.HttpAPIURIs)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
modelUris, err := utils.ListValuetoStringSlice(model.Uris)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
model.Hosts = types.ListNull(types.StringType)
|
||||
model.Uris = types.ListNull(types.StringType)
|
||||
model.HttpAPIURIs = 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)
|
||||
if credentials.HttpApiUris != nil {
|
||||
var httpApiUris []attr.Value
|
||||
for _, httpApiUri := range *credentials.HttpApiUris {
|
||||
httpApiUris = append(httpApiUris, types.StringValue(httpApiUri))
|
||||
}
|
||||
httpApiUrisList, diags := types.ListValue(types.StringType, httpApiUris)
|
||||
respHttpApiUris := *credentials.HttpApiUris
|
||||
|
||||
reconciledHttpApiUris := utils.ReconcileStringSlices(modelHttpApiUris, respHttpApiUris)
|
||||
|
||||
httpApiUrisTF, diags := types.ListValueFrom(ctx, types.StringType, reconciledHttpApiUris)
|
||||
if diags.HasError() {
|
||||
return fmt.Errorf("failed to map httpApiUris: %w", core.DiagsToError(diags))
|
||||
}
|
||||
model.HttpAPIURIs = httpApiUrisList
|
||||
|
||||
model.HttpAPIURIs = httpApiUrisTF
|
||||
}
|
||||
|
||||
if credentials.Uris != nil {
|
||||
respUris := *credentials.Uris
|
||||
|
||||
reconciledUris := utils.ReconcileStringSlices(modelUris, respUris)
|
||||
|
||||
urisTF, diags := types.ListValueFrom(ctx, types.StringType, reconciledUris)
|
||||
if diags.HasError() {
|
||||
return fmt.Errorf("failed to map uris: %w", core.DiagsToError(diags))
|
||||
}
|
||||
|
||||
model.Uris = urisTF
|
||||
}
|
||||
|
||||
model.HttpAPIURI = types.StringPointerValue(credentials.HttpApiUri)
|
||||
model.Management = types.StringPointerValue(credentials.Management)
|
||||
model.Password = types.StringPointerValue(credentials.Password)
|
||||
model.Port = types.Int64PointerValue(credentials.Port)
|
||||
if credentials.Uris != nil {
|
||||
var uris []attr.Value
|
||||
for _, uri := range *credentials.Uris {
|
||||
uris = append(uris, types.StringValue(uri))
|
||||
}
|
||||
urisList, diags := types.ListValue(types.StringType, uris)
|
||||
if diags.HasError() {
|
||||
return fmt.Errorf("failed to map uris: %w", core.DiagsToError(diags))
|
||||
}
|
||||
model.Uris = urisList
|
||||
}
|
||||
model.Uri = types.StringPointerValue(credentials.Uri)
|
||||
model.Username = types.StringPointerValue(credentials.Username)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
package rabbitmq
|
||||
|
||||
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 *rabbitmq.CredentialsResponse
|
||||
expected Model
|
||||
isValid bool
|
||||
}{
|
||||
{
|
||||
"default_values",
|
||||
Model{
|
||||
InstanceId: types.StringValue("iid"),
|
||||
ProjectId: types.StringValue("pid"),
|
||||
},
|
||||
&rabbitmq.CredentialsResponse{
|
||||
Id: utils.Ptr("cid"),
|
||||
Raw: &rabbitmq.RawCredentials{},
|
||||
|
|
@ -43,6 +49,10 @@ func TestMapFields(t *testing.T) {
|
|||
},
|
||||
{
|
||||
"simple_values",
|
||||
Model{
|
||||
InstanceId: types.StringValue("iid"),
|
||||
ProjectId: types.StringValue("pid"),
|
||||
},
|
||||
&rabbitmq.CredentialsResponse{
|
||||
Id: utils.Ptr("cid"),
|
||||
Raw: &rabbitmq.RawCredentials{
|
||||
|
|
@ -96,8 +106,92 @@ func TestMapFields(t *testing.T) {
|
|||
},
|
||||
true,
|
||||
},
|
||||
{
|
||||
"hosts_uris_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"),
|
||||
}),
|
||||
Uris: types.ListValueMust(types.StringType, []attr.Value{
|
||||
types.StringValue("uri_2"),
|
||||
types.StringValue(""),
|
||||
types.StringValue("uri_1"),
|
||||
}),
|
||||
HttpAPIURIs: types.ListValueMust(types.StringType, []attr.Value{
|
||||
types.StringValue("http_api_uri_2"),
|
||||
types.StringValue(""),
|
||||
types.StringValue("http_api_uri_1"),
|
||||
}),
|
||||
},
|
||||
&rabbitmq.CredentialsResponse{
|
||||
Id: utils.Ptr("cid"),
|
||||
Raw: &rabbitmq.RawCredentials{
|
||||
Credentials: &rabbitmq.Credentials{
|
||||
Host: utils.Ptr("host"),
|
||||
Hosts: &[]string{
|
||||
"",
|
||||
"host_1",
|
||||
"host_2",
|
||||
},
|
||||
HttpApiUri: utils.Ptr("http"),
|
||||
HttpApiUris: &[]string{
|
||||
"",
|
||||
"http_api_uri_1",
|
||||
"http_api_uri_2",
|
||||
},
|
||||
Management: utils.Ptr("management"),
|
||||
Password: utils.Ptr("password"),
|
||||
Port: utils.Ptr(int64(1234)),
|
||||
Uri: utils.Ptr("uri"),
|
||||
Uris: &[]string{
|
||||
"",
|
||||
"uri_1",
|
||||
"uri_2",
|
||||
},
|
||||
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"),
|
||||
}),
|
||||
HttpAPIURI: types.StringValue("http"),
|
||||
HttpAPIURIs: types.ListValueMust(types.StringType, []attr.Value{
|
||||
types.StringValue("http_api_uri_2"),
|
||||
types.StringValue(""),
|
||||
types.StringValue("http_api_uri_1"),
|
||||
}),
|
||||
Management: types.StringValue("management"),
|
||||
Password: types.StringValue("password"),
|
||||
Port: types.Int64Value(1234),
|
||||
Uri: types.StringValue("uri"),
|
||||
Uris: types.ListValueMust(types.StringType, []attr.Value{
|
||||
types.StringValue("uri_2"),
|
||||
types.StringValue(""),
|
||||
types.StringValue("uri_1"),
|
||||
}),
|
||||
Username: types.StringValue("username"),
|
||||
},
|
||||
true,
|
||||
},
|
||||
{
|
||||
"null_fields_and_int_conversions",
|
||||
Model{
|
||||
InstanceId: types.StringValue("iid"),
|
||||
ProjectId: types.StringValue("pid"),
|
||||
},
|
||||
&rabbitmq.CredentialsResponse{
|
||||
Id: utils.Ptr("cid"),
|
||||
Raw: &rabbitmq.RawCredentials{
|
||||
|
|
@ -135,18 +229,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"),
|
||||
},
|
||||
&rabbitmq.CredentialsResponse{},
|
||||
Model{},
|
||||
false,
|
||||
},
|
||||
{
|
||||
"nil_raw_credential",
|
||||
Model{
|
||||
InstanceId: types.StringValue("iid"),
|
||||
ProjectId: types.StringValue("pid"),
|
||||
},
|
||||
&rabbitmq.CredentialsResponse{
|
||||
Id: utils.Ptr("cid"),
|
||||
},
|
||||
|
|
@ -156,11 +262,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")
|
||||
}
|
||||
|
|
@ -168,7 +270,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)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue