From c632d7a3e36b91b938c16a27f4386ef76bceb141 Mon Sep 17 00:00:00 2001 From: atoni <50518795+bosc0@users.noreply.github.com> Date: Thu, 3 Apr 2025 10:37:19 +0200 Subject: [PATCH] feat: change users attribute from List to Set in user group resource --- docs/resources/usergroup.md | 2 +- internal/provider/resource_usergroup.go | 9 +++++---- internal/provider/resource_usergroup_mock.go | 2 +- internal/provider/usergroup_service.go | 2 +- internal/provider/utils.go | 20 +++++++++++++++++++- 5 files changed, 27 insertions(+), 8 deletions(-) diff --git a/docs/resources/usergroup.md b/docs/resources/usergroup.md index a4aff48..c1d7e42 100644 --- a/docs/resources/usergroup.md +++ b/docs/resources/usergroup.md @@ -32,7 +32,7 @@ This resource requires the following scopes: - `channels` (List of String) Channels shared by the user group. - `description` (String) - `prevent_conflicts` (Boolean) If true, the plan fails if there's an enabled user group with the same name or handle. -- `users` (List of String) List of user IDs in the user group. +- `users` (Set of String) List of user IDs in the user group. ### Read-Only diff --git a/internal/provider/resource_usergroup.go b/internal/provider/resource_usergroup.go index 4bccffe..806042e 100644 --- a/internal/provider/resource_usergroup.go +++ b/internal/provider/resource_usergroup.go @@ -14,6 +14,7 @@ import ( "github.com/hashicorp/terraform-plugin-framework/resource/schema/booldefault" "github.com/hashicorp/terraform-plugin-framework/resource/schema/listdefault" "github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier" + "github.com/hashicorp/terraform-plugin-framework/resource/schema/setdefault" "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringdefault" "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier" "github.com/hashicorp/terraform-plugin-framework/types" @@ -41,7 +42,7 @@ type UserGroupResourceModel struct { Description types.String `tfsdk:"description"` Handle types.String `tfsdk:"handle"` Channels types.List `tfsdk:"channels"` - Users types.List `tfsdk:"users"` + Users types.Set `tfsdk:"users"` PreventConflicts types.Bool `tfsdk:"prevent_conflicts"` } @@ -84,12 +85,12 @@ This resource requires the following scopes: types.ListValueMust(types.StringType, []attr.Value{}), ), }, - "users": schema.ListAttribute{ + "users": schema.SetAttribute{ ElementType: types.StringType, Optional: true, Computed: true, - Default: listdefault.StaticValue( - types.ListValueMust(types.StringType, []attr.Value{}), + Default: setdefault.StaticValue( + types.SetValueMust(types.StringType, []attr.Value{}), ), Description: "List of user IDs in the user group.", }, diff --git a/internal/provider/resource_usergroup_mock.go b/internal/provider/resource_usergroup_mock.go index 9db224c..27643f6 100644 --- a/internal/provider/resource_usergroup_mock.go +++ b/internal/provider/resource_usergroup_mock.go @@ -55,7 +55,7 @@ func (b *UserGroupResourceModelBuilder) WithUsers(value []string) *UserGroupReso for i, v := range value { attrValues[i] = types.StringValue(v) } - b.result.Users = types.ListValueMust(types.StringType, attrValues) + b.result.Users = types.SetValueMust(types.StringType, attrValues) return b } diff --git a/internal/provider/usergroup_service.go b/internal/provider/usergroup_service.go index 394c030..cef55c1 100644 --- a/internal/provider/usergroup_service.go +++ b/internal/provider/usergroup_service.go @@ -51,7 +51,7 @@ func toPlan(m *UserGroupResourceModel) *UserGroupPlan { Description: m.Description.ValueString(), Handle: m.Handle.ValueString(), Channels: listToStringSlice(m.Channels), - Users: listToStringSlice(m.Users), + Users: setToStringSlice(m.Users), PreventConflicts: m.PreventConflicts.ValueBool(), } } diff --git a/internal/provider/utils.go b/internal/provider/utils.go index b388e31..4cab0ad 100644 --- a/internal/provider/utils.go +++ b/internal/provider/utils.go @@ -23,6 +23,16 @@ func listToStringSlice(l types.List) []string { return result } +func setToStringSlice(s types.Set) []string { + res := make([]string, 0, len(s.Elements())) + for _, e := range s.Elements() { + if str, ok := e.(types.String); ok { + res = append(res, str.ValueString()) + } + } + return res +} + func stringSliceToList(list []string) types.List { if len(list) == 0 { emptyVal, _ := types.ListValue(types.StringType, []attr.Value{}) @@ -39,11 +49,19 @@ func stringSliceToList(list []string) types.List { return res } +func stringSliceToSet(list []string) types.Set { + attrValues := make([]attr.Value, len(list)) + for i, v := range list { + attrValues[i] = types.StringValue(v) + } + return types.SetValueMust(types.StringType, attrValues) +} + func (m *UserGroupResourceModel) UpdateFromUserGroup(ug *slack.UserGroup) { m.ID = types.StringValue(ug.ID) m.Name = types.StringValue(ug.Name) m.Description = types.StringValue(ug.Description) m.Handle = types.StringValue(ug.Handle) m.Channels = stringSliceToList(ug.Prefs.Channels) - m.Users = stringSliceToList(ug.Users) + m.Users = stringSliceToSet(ug.Users) }