diff --git a/provider/internal/msp/msp_tenant_users/resource.go b/provider/internal/msp/msp_tenant_users/resource.go index 0d77819..6bec76c 100644 --- a/provider/internal/msp/msp_tenant_users/resource.go +++ b/provider/internal/msp/msp_tenant_users/resource.go @@ -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{} } @@ -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.", @@ -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)...) } @@ -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)...) }