forked from integrations/terraform-provider-github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresource_github_team.go
More file actions
419 lines (373 loc) · 11.7 KB
/
resource_github_team.go
File metadata and controls
419 lines (373 loc) · 11.7 KB
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
package github
import (
"context"
"log"
"net/http"
"strconv"
"github.com/google/go-github/v66/github"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/shurcooL/githubv4"
)
func resourceGithubTeam() *schema.Resource {
return &schema.Resource{
Create: resourceGithubTeamCreate,
Read: resourceGithubTeamRead,
Update: resourceGithubTeamUpdate,
Delete: resourceGithubTeamDelete,
Importer: &schema.ResourceImporter{
State: resourceGithubTeamImport,
},
CustomizeDiff: customdiff.Sequence(
customdiff.ComputedIf("slug", func(_ context.Context, d *schema.ResourceDiff, meta interface{}) bool {
return d.HasChange("name")
}),
),
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
Description: "The name of the team.",
},
"description": {
Type: schema.TypeString,
Optional: true,
Description: "A description of the team.",
},
"privacy": {
Type: schema.TypeString,
Optional: true,
Default: "secret",
Description: "The level of privacy for the team. Must be one of 'secret' or 'closed'.",
ValidateDiagFunc: validateValueFunc([]string{"secret", "closed"}),
},
"notification_setting": {
Type: schema.TypeString,
Optional: true,
Default: "notifications_enabled",
Description: "The notification setting for the team. Must be one of 'notifications_enabled' or 'notifications_disabled'.",
ValidateDiagFunc: validateValueFunc([]string{"notifications_enabled", "notifications_disabled"}),
},
"parent_team_id": {
Type: schema.TypeString,
Optional: true,
Default: "",
Description: "The ID or slug of the parent team, if this is a nested team.",
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
if d.Get("parent_team_id") == d.Get("parent_team_read_id") || d.Get("parent_team_id") == d.Get("parent_team_read_slug") {
return true
}
return false
},
},
"parent_team_read_id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "The id of the parent team read in Github.",
},
"parent_team_read_slug": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "The id of the parent team read in Github.",
},
"ldap_dn": {
Type: schema.TypeString,
Optional: true,
Description: "The LDAP Distinguished Name of the group where membership will be synchronized. Only available in GitHub Enterprise Server.",
},
"create_default_maintainer": {
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "Adds a default maintainer to the team. Adds the creating user to the team when 'true'.",
},
"slug": {
Type: schema.TypeString,
Computed: true,
Description: "The slug of the created team.",
},
"etag": {
Type: schema.TypeString,
Computed: true,
},
"node_id": {
Type: schema.TypeString,
Computed: true,
Description: "The Node ID of the created team.",
},
"members_count": {
Type: schema.TypeInt,
Computed: true,
},
},
}
}
func resourceGithubTeamCreate(d *schema.ResourceData, meta interface{}) error {
err := checkOrganization(meta)
if err != nil {
return err
}
client := meta.(*Owner).v3client
ownerName := meta.(*Owner).name
name := d.Get("name").(string)
newTeam := github.NewTeam{
Name: name,
Description: github.String(d.Get("description").(string)),
Privacy: github.String(d.Get("privacy").(string)),
NotificationSetting: github.String(d.Get("notification_setting").(string)),
}
if ldapDN := d.Get("ldap_dn").(string); ldapDN != "" {
newTeam.LDAPDN = &ldapDN
}
if parentTeamID, ok := d.GetOk("parent_team_id"); ok {
teamId, err := getTeamID(parentTeamID.(string), meta)
if err != nil {
return err
}
newTeam.ParentTeamID = &teamId
}
ctx := context.Background()
githubTeam, _, err := client.Teams.CreateTeam(ctx,
ownerName, newTeam)
if err != nil {
return err
}
/*
When using a GitHub App for authentication, `members:write` permissions on the App are needed.
However, when using a GitHub App, CreateTeam will not correctly nest the team under the parent,
if the parent team was created by someone else than the GitHub App. In that case, the response
object will contain a `nil` parent object.
This can be resolved by using an additional call to EditTeamByID. This will be able to set the
parent team correctly when using a GitHub App with `members:write` permissions.
Note that this is best-effort: when running this with a PAT that does not have admin permissions
on the parent team, the operation might still fail to set the parent team.
*/
if newTeam.ParentTeamID != nil && githubTeam.Parent == nil {
_, _, err := client.Teams.EditTeamByID(ctx,
*githubTeam.Organization.ID,
*githubTeam.ID,
newTeam,
false)
if err != nil {
return err
}
}
create_default_maintainer := d.Get("create_default_maintainer").(bool)
if !create_default_maintainer {
log.Printf("[DEBUG] Removing default maintainer from team: %s (%s)", name, ownerName)
if err := removeDefaultMaintainer(*githubTeam.Slug, meta); err != nil {
return err
}
}
d.SetId(strconv.FormatInt(githubTeam.GetID(), 10))
return resourceGithubTeamRead(d, meta)
}
func resourceGithubTeamRead(d *schema.ResourceData, meta interface{}) error {
err := checkOrganization(meta)
if err != nil {
return err
}
client := meta.(*Owner).v3client
orgId := meta.(*Owner).id
id, err := strconv.ParseInt(d.Id(), 10, 64)
if err != nil {
return unconvertibleIdErr(d.Id(), err)
}
ctx := context.WithValue(context.Background(), ctxId, d.Id())
if !d.IsNewResource() {
ctx = context.WithValue(ctx, ctxEtag, d.Get("etag").(string))
}
team, resp, err := client.Teams.GetTeamByID(ctx, orgId, id)
if err != nil {
if ghErr, ok := err.(*github.ErrorResponse); ok {
if ghErr.Response.StatusCode == http.StatusNotModified {
return nil
}
if ghErr.Response.StatusCode == http.StatusNotFound {
log.Printf("[INFO] Removing team %s from state because it no longer exists in GitHub",
d.Id())
d.SetId("")
return nil
}
}
return err
}
if err = d.Set("etag", resp.Header.Get("ETag")); err != nil {
return err
}
if err = d.Set("description", team.GetDescription()); err != nil {
return err
}
if err = d.Set("name", team.GetName()); err != nil {
return err
}
if err = d.Set("privacy", team.GetPrivacy()); err != nil {
return err
}
if err = d.Set("notification_setting", team.GetNotificationSetting()); err != nil {
return err
}
if parent := team.Parent; parent != nil {
if err = d.Set("parent_team_id", strconv.FormatInt(team.Parent.GetID(), 10)); err != nil {
return err
}
if err = d.Set("parent_team_read_id", strconv.FormatInt(team.Parent.GetID(), 10)); err != nil {
return err
}
if err = d.Set("parent_team_read_slug", parent.Slug); err != nil {
return err
}
} else {
if err = d.Set("parent_team_id", ""); err != nil {
return err
}
if err = d.Set("parent_team_read_id", ""); err != nil {
return err
}
if err = d.Set("parent_team_read_slug", ""); err != nil {
return err
}
}
if err = d.Set("ldap_dn", team.GetLDAPDN()); err != nil {
return err
}
if err = d.Set("slug", team.GetSlug()); err != nil {
return err
}
if err = d.Set("node_id", team.GetNodeID()); err != nil {
return err
}
if err = d.Set("members_count", team.GetMembersCount()); err != nil {
return err
}
return nil
}
func resourceGithubTeamUpdate(d *schema.ResourceData, meta interface{}) error {
err := checkOrganization(meta)
if err != nil {
return err
}
client := meta.(*Owner).v3client
orgId := meta.(*Owner).id
var removeParentTeam bool
editedTeam := github.NewTeam{
Name: d.Get("name").(string),
Description: github.String(d.Get("description").(string)),
Privacy: github.String(d.Get("privacy").(string)),
NotificationSetting: github.String(d.Get("notification_setting").(string)),
}
if parentTeamID, ok := d.GetOk("parent_team_id"); ok {
teamId, err := getTeamID(parentTeamID.(string), meta)
if err != nil {
return err
}
editedTeam.ParentTeamID = &teamId
removeParentTeam = false
} else {
removeParentTeam = true
}
teamId, err := strconv.ParseInt(d.Id(), 10, 64)
if err != nil {
return unconvertibleIdErr(d.Id(), err)
}
ctx := context.WithValue(context.Background(), ctxId, d.Id())
team, _, err := client.Teams.EditTeamByID(ctx, orgId, teamId, editedTeam, removeParentTeam)
if err != nil {
return err
}
if d.HasChange("ldap_dn") {
ldapDN := d.Get("ldap_dn").(string)
mapping := &github.TeamLDAPMapping{
LDAPDN: github.String(ldapDN),
}
_, _, err = client.Admin.UpdateTeamLDAPMapping(ctx, team.GetID(), mapping)
if err != nil {
return err
}
}
d.SetId(strconv.FormatInt(team.GetID(), 10))
return resourceGithubTeamRead(d, meta)
}
func resourceGithubTeamDelete(d *schema.ResourceData, meta interface{}) error {
err := checkOrganization(meta)
if err != nil {
return err
}
client := meta.(*Owner).v3client
orgId := meta.(*Owner).id
id, err := strconv.ParseInt(d.Id(), 10, 64)
if err != nil {
return unconvertibleIdErr(d.Id(), err)
}
ctx := context.WithValue(context.Background(), ctxId, d.Id())
_, err = client.Teams.DeleteTeamByID(ctx, orgId, id)
/*
When deleting a team and it failed, we need to check if it has already been deleted meanwhile.
This could be the case when deleting nested teams via Terraform by looping through a module
or resource and the parent team might have been deleted already. If the parent team had
been deleted already (via parallel runs), the child team is also already gone (deleted by
GitHub automatically).
So we're checking if it still exists and if not, simply remove it from TF state.
*/
if err != nil {
// Fetch the team in order to see if it exists or not (http 404)
_, _, err = client.Teams.GetTeamByID(ctx, orgId, id)
if err != nil {
if ghErr, ok := err.(*github.ErrorResponse); ok {
if ghErr.Response.StatusCode == http.StatusNotFound {
// If team we failed to delete does not exist, remove it from TF state.
log.Printf("[WARN] Removing team: %s from state because it no longer exists",
d.Id())
d.SetId("")
return nil
}
}
}
}
return err
}
func resourceGithubTeamImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
teamId, err := getTeamID(d.Id(), meta)
if err != nil {
return nil, err
}
d.SetId(strconv.FormatInt(teamId, 10))
if err = d.Set("create_default_maintainer", false); err != nil {
return nil, err
}
return []*schema.ResourceData{d}, nil
}
func removeDefaultMaintainer(teamSlug string, meta interface{}) error {
client := meta.(*Owner).v3client
orgName := meta.(*Owner).name
v4client := meta.(*Owner).v4client
type User struct {
Login githubv4.String
}
var query struct {
Organization struct {
Team struct {
Members struct {
Nodes []User
}
} `graphql:"team(slug:$slug)"`
} `graphql:"organization(login:$login)"`
}
variables := map[string]interface{}{
"slug": githubv4.String(teamSlug),
"login": githubv4.String(orgName),
}
err := v4client.Query(meta.(*Owner).StopContext, &query, variables)
if err != nil {
return err
}
for _, user := range query.Organization.Team.Members.Nodes {
_, err := client.Teams.RemoveTeamMembershipBySlug(meta.(*Owner).StopContext, orgName, teamSlug, string(user.Login))
if err != nil {
return err
}
}
return nil
}