Skip to content

Commit

Permalink
fix(lh-87768): sort users returned by SCC API to prevent inconsistent…
Browse files Browse the repository at this point in the history
… plan errors (#151)
  • Loading branch information
siddhuwarrier authored Nov 13, 2024
1 parent 7789968 commit 09f3ed5
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions provider/internal/msp/msp_tenant_users/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
"sort"
)

func NewMspManagedTenantUsersResource() resource.Resource { return &MspManagedTenantUsersResource{} }
Expand All @@ -23,6 +24,19 @@ type MspManagedTenantUsersResource struct {
client *cdoClient.Client
}

func sortUsersToOrderInPlanData(users []User, planData *MspManagedTenantUsersResourceModel) *[]User {
userOrder := make(map[string]int)
for i, user := range planData.Users {
userOrder[user.Username.ValueString()] = i
}

sort.Slice(users, func(i, j int) bool {
return userOrder[users[i].Username.ValueString()] < userOrder[users[j].Username.ValueString()]
})

return &users
}

func (resource *MspManagedTenantUsersResource) Schema(ctx context.Context, request resource.SchemaRequest, response *resource.SchemaResponse) {
response.Schema = schema.Schema{
MarkdownDescription: "Provides a resource to add users to an MSP managed tenant.",
Expand Down Expand Up @@ -85,7 +99,7 @@ func (resource *MspManagedTenantUsersResource) Create(ctx context.Context, reque
return
}

planData.Users = *resource.transformApiResponseToPlan(createdUserDetails)
planData.Users = *sortUsersToOrderInPlanData(*resource.transformApiResponseToPlan(createdUserDetails), &planData)

response.Diagnostics.Append(response.State.Set(ctx, &planData)...)
}
Expand All @@ -101,7 +115,7 @@ func (resource *MspManagedTenantUsersResource) Read(ctx context.Context, request
return
}

stateData.Users = *resource.transformApiResponseToPlan(userDetails)
stateData.Users = *sortUsersToOrderInPlanData(*resource.transformApiResponseToPlan(userDetails), &stateData)
response.Diagnostics.Append(response.State.Set(ctx, &stateData)...)
}

Expand Down

0 comments on commit 09f3ed5

Please sign in to comment.