-
Notifications
You must be signed in to change notification settings - Fork 34
/
resource_usergroup.go
233 lines (202 loc) · 6.67 KB
/
resource_usergroup.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package slack
import (
"context"
"fmt"
"strings"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/slack-go/slack"
)
func resourceSlackUserGroup() *schema.Resource {
return &schema.Resource{
ReadContext: resourceSlackUserGroupRead,
CreateContext: resourceSlackUserGroupCreate,
UpdateContext: resourceSlackUserGroupUpdate,
DeleteContext: resourceSlackUserGroupDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"channels": {
Type: schema.TypeSet,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Set: schema.HashString,
Optional: true,
},
"description": {
Type: schema.TypeString,
Optional: true,
},
"handle": {
Type: schema.TypeString,
Optional: true,
},
"users": {
Type: schema.TypeSet,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Set: schema.HashString,
Optional: true,
},
},
}
}
func resourceSlackUserGroupCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(*slack.Client)
name := d.Get("name").(string)
description := d.Get("description").(string)
handle := d.Get("handle").(string)
channels := d.Get("channels").(*schema.Set)
users := d.Get("users").(*schema.Set)
userGroup := slack.UserGroup{
Name: name,
Description: description,
Handle: handle,
Prefs: slack.UserGroupPrefs{
Channels: schemaSetToSlice(channels),
},
}
createdUserGroup, err := client.CreateUserGroupContext(ctx, userGroup)
if err != nil {
if err.Error() != "name_already_exists" && err.Error() != "handle_already_exists" {
return diag.Errorf("could not create usergroup %s: %s", name, err)
}
group, err := findUserGroupByName(ctx, name, true, m)
if err != nil {
return diag.Errorf("could not find usergroup %s: %s", name, err)
}
_, err = client.EnableUserGroupContext(ctx, group.ID)
if err != nil {
if err.Error() != "already_enabled" {
return diag.Errorf("could not enable usergroup %s (%s): %s", name, group.ID, err)
}
}
_, err = client.UpdateUserGroupContext(ctx, group.ID)
if err != nil {
return diag.Errorf("could not update usergroup %s (%s): %s", name, group.ID, err)
}
d.SetId(group.ID)
} else {
d.SetId(createdUserGroup.ID)
}
if users.Len() > 0 {
_, err := client.UpdateUserGroupMembersContext(ctx, d.Id(), strings.Join(schemaSetToSlice(users), ","))
if err != nil {
return diag.Errorf("could not update usergroup members %s: %s", name, err)
}
schemaSetToSlice(users)
}
return resourceSlackUserGroupRead(ctx, d, m)
}
func resourceSlackUserGroupRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(*slack.Client)
id := d.Id()
var diags diag.Diagnostics
userGroups, err := client.GetUserGroupsContext(ctx, slack.GetUserGroupsOptionIncludeUsers(true))
if err != nil {
return diag.FromErr(fmt.Errorf("couldn't get usergroups: %w", err))
}
for _, userGroup := range userGroups {
if userGroup.ID == id {
return updateUserGroupData(d, userGroup)
}
}
diags = append(diags, diag.Diagnostic{
Severity: diag.Warning,
Summary: fmt.Sprintf("usergroup with ID %s not found, removing from state", id),
})
d.SetId("")
return diags
}
func findUserGroupByName(ctx context.Context, name string, includeDisabled bool, m interface{}) (slack.UserGroup, error) {
client := m.(*slack.Client)
userGroups, err := client.GetUserGroupsContext(ctx, slack.GetUserGroupsOptionIncludeDisabled(includeDisabled), slack.GetUserGroupsOptionIncludeUsers(true))
if err != nil {
return slack.UserGroup{}, err
}
for _, userGroup := range userGroups {
if userGroup.Name == name {
return userGroup, nil
}
}
return slack.UserGroup{}, fmt.Errorf("could not find usergroup %s", name)
}
func findUserGroupByID(ctx context.Context, id string, includeDisabled bool, m interface{}) (slack.UserGroup, error) {
client := m.(*slack.Client)
userGroups, err := client.GetUserGroupsContext(ctx, slack.GetUserGroupsOptionIncludeDisabled(includeDisabled), slack.GetUserGroupsOptionIncludeUsers(true))
if err != nil {
return slack.UserGroup{}, err
}
for _, userGroup := range userGroups {
if userGroup.ID == id {
return userGroup, nil
}
}
return slack.UserGroup{}, fmt.Errorf("could not find usergroup %s", id)
}
func resourceSlackUserGroupUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(*slack.Client)
id := d.Id()
name := d.Get("name").(string)
description := d.Get("description").(string)
handle := d.Get("handle").(string)
channels := d.Get("channels").(*schema.Set)
users := d.Get("users").(*schema.Set)
updateUserGroupOptions := []slack.UpdateUserGroupsOption{
slack.UpdateUserGroupsOptionName(name),
slack.UpdateUserGroupsOptionChannels(schemaSetToSlice(channels)),
slack.UpdateUserGroupsOptionDescription(&description),
slack.UpdateUserGroupsOptionHandle(handle),
}
_, err := client.UpdateUserGroupContext(ctx, id, updateUserGroupOptions...)
if err != nil {
return diag.Errorf("could not update usergroup %s: %s", name, err)
}
if d.HasChanges("users") {
_, err := client.UpdateUserGroupMembersContext(ctx, id, strings.Join(schemaSetToSlice(users), ","))
if err != nil {
return diag.Errorf("could not update usergroup members %s: %s", name, err)
}
schemaSetToSlice(users)
}
return resourceSlackUserGroupRead(ctx, d, m)
}
func resourceSlackUserGroupDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
var diags diag.Diagnostics
client := m.(*slack.Client)
id := d.Id()
_, err := client.DisableUserGroupContext(ctx, id)
if err != nil {
return diag.FromErr(err)
}
return diags
}
func updateUserGroupData(d *schema.ResourceData, userGroup slack.UserGroup) diag.Diagnostics {
if userGroup.ID == "" {
return diag.Errorf("error setting id: returned usergroup does not have an id")
}
d.SetId(userGroup.ID)
if err := d.Set("name", userGroup.Name); err != nil {
return diag.Errorf("error setting name: %s", err)
}
if err := d.Set("handle", userGroup.Handle); err != nil {
return diag.Errorf("error setting handle: %s", err)
}
if err := d.Set("description", userGroup.Description); err != nil {
return diag.Errorf("error setting description: %s", err)
}
if err := d.Set("channels", userGroup.Prefs.Channels); err != nil {
return diag.Errorf("error setting channels: %s", err)
}
if err := d.Set("users", userGroup.Users); err != nil {
return diag.Errorf("error setting users: %s", err)
}
return nil
}