Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a proper sorting of associate-role's permissions #551

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changes/unreleased/Fixed-20241217-190228.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
kind: Fixed
body: This adds a sorting of the permissions we get from the commercetools API response, so that it's identical to the one from the plan.
time: 2024-12-17T19:02:28.854328+01:00
38 changes: 37 additions & 1 deletion internal/resources/associate_role/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/booldefault"
"regexp"
"sort"
"time"

"github.com/hashicorp/terraform-plugin-framework-validators/listvalidator"
Expand Down Expand Up @@ -138,6 +139,35 @@ func (*associateRoleResource) Metadata(_ context.Context, req resource.MetadataR
resp.TypeName = req.ProviderTypeName + "_associate_role"
}

func resortPermissions(permissions, plan []platform.Permission) []platform.Permission {

// Build a map which maps the planned permissions to its index from the array
indexMap := make(map[platform.Permission]int)
for idx, p := range plan {
indexMap[p] = idx
}

// Build a map of the current permissions to the planned index
targetMap := make(map[platform.Permission]int)
for _, p := range permissions {
idx, ok := indexMap[p]
if ok {
targetMap[p] = idx
}
}

// Sort the target permission list by the index from the map
targetList := make([]platform.Permission, 0, len(targetMap))
for key := range targetMap {
targetList = append(targetList, key)
}
sort.SliceStable(targetList, func(i, j int) bool{
return targetMap[targetList[i]] < targetMap[targetList[j]]
})

return targetList
}

// Create implements resource.Resource.
func (r *associateRoleResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
var plan AssociateRole
Expand All @@ -163,6 +193,8 @@ func (r *associateRoleResource) Create(ctx context.Context, req resource.CreateR
return
}

associateRole.Permissions = resortPermissions(associateRole.Permissions, draft.Permissions);

current := NewAssociateRoleFromNative(associateRole)

diags = resp.State.Set(ctx, current)
Expand Down Expand Up @@ -228,6 +260,8 @@ func (r *associateRoleResource) Read(ctx context.Context, req resource.ReadReque
return
}

associateRole.Permissions = resortPermissions(associateRole.Permissions, state.draft().Permissions);

// Transform the remote platform associate role to the
// tf schema matching representation.
current := NewAssociateRoleFromNative(associateRole)
Expand Down Expand Up @@ -275,6 +309,8 @@ func (r *associateRoleResource) Update(ctx context.Context, req resource.UpdateR
return
}

associateRole.Permissions = resortPermissions(associateRole.Permissions, plan.draft().Permissions)

current := NewAssociateRoleFromNative(associateRole)

diags = resp.State.Set(ctx, current)
Expand All @@ -297,4 +333,4 @@ func (r *associateRoleResource) Configure(_ context.Context, req resource.Config
// ImportState implements resource.ResourceWithImportState.
func (*associateRoleResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp)
}
}