这是indexloc提供的服务,不要输入任何密码
Skip to content
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
2 changes: 1 addition & 1 deletion docs/resources/usergroup.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
9 changes: 5 additions & 4 deletions internal/provider/resource_usergroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"`
}

Expand Down Expand Up @@ -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.",
},
Expand Down
2 changes: 1 addition & 1 deletion internal/provider/resource_usergroup_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion internal/provider/usergroup_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
}
}
Expand Down
20 changes: 19 additions & 1 deletion internal/provider/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{})
Expand All @@ -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)
}