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 custom plan modifier for groups field in the MQ User resource #283

Closed
wants to merge 1 commit into from
Closed
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
44 changes: 44 additions & 0 deletions internal/service/mq/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package mq
import (
"context"
"fmt"
"sort"
"strings"

"github.com/aws/aws-sdk-go-v2/service/mq"
Expand Down Expand Up @@ -72,6 +73,9 @@ func (r *resourceUser) Schema(ctx context.Context, request resource.SchemaReques
"groups": schema.ListAttribute{
ElementType: types.StringType,
Optional: true,
PlanModifiers: []planmodifier.List{
listSortModifier{},
},
},
"id": framework.IDAttribute(),
"password": schema.StringAttribute{
Expand Down Expand Up @@ -285,3 +289,43 @@ func userHasChanges(plan, state resourceUserData) bool {
!plan.Password.Equal(state.Password) ||
!plan.ReplicationUser.Equal(state.ReplicationUser)
}

// Custom Plan Modifier: Sorts list items
type listSortModifier struct{}

func (m listSortModifier) PlanModifyList(ctx context.Context, req planmodifier.ListRequest, resp *planmodifier.ListResponse) {
// Get plan value
planValue := req.PlanValue

// If plan value is null or unknown, do nothing
if planValue.IsNull() || planValue.IsUnknown() {
return
}

// Convert plan value to []string
var groups []string
diags := planValue.ElementsAs(ctx, &groups, false)
if diags.HasError() {
resp.Diagnostics.Append(diags...)
return
}

sort.Strings(groups)

// Write sorted value back to plan
sortedList, diags := types.ListValueFrom(ctx, types.StringType, groups)
if diags.HasError() {
resp.Diagnostics.Append(diags...)
return
}

resp.PlanValue = sortedList
}

func (m listSortModifier) Description(ctx context.Context) string {
return "Sorts the list elements alphabetically."
}

func (m listSortModifier) MarkdownDescription(ctx context.Context) string {
return "Sorts the list elements alphabetically."
}
Loading