/
resource_github_organization_settings.go
467 lines (446 loc) · 22.7 KB
/
resource_github_organization_settings.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
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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
package github
import (
"context"
"log"
"strconv"
"github.com/google/go-github/v57/github"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
)
func resourceGithubOrganizationSettings() *schema.Resource {
return &schema.Resource{
Create: resourceGithubOrganizationSettingsCreateOrUpdate,
Read: resourceGithubOrganizationSettingsRead,
Update: resourceGithubOrganizationSettingsCreateOrUpdate,
Delete: resourceGithubOrganizationSettingsDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"billing_email": {
Type: schema.TypeString,
Required: true,
Description: "The billing email address for the organization.",
},
"company": {
Type: schema.TypeString,
Optional: true,
Description: "The company name for the organization.",
},
"email": {
Type: schema.TypeString,
Optional: true,
Description: "The email address for the organization.",
},
"twitter_username": {
Type: schema.TypeString,
Optional: true,
Description: "The Twitter username for the organization.",
},
"location": {
Type: schema.TypeString,
Optional: true,
Description: "The location for the organization.",
},
"name": {
Type: schema.TypeString,
Optional: true,
Description: "The name for the organization.",
},
"description": {
Type: schema.TypeString,
Optional: true,
Description: "The description for the organization.",
},
"has_organization_projects": {
Type: schema.TypeBool,
Optional: true,
Default: true,
Description: "Whether or not organization projects are enabled for the organization.",
},
"has_repository_projects": {
Type: schema.TypeBool,
Optional: true,
Default: true,
Description: "Whether or not repository projects are enabled for the organization.",
},
"default_repository_permission": {
Type: schema.TypeString,
Optional: true,
Default: "read",
Description: "The default permission for organization members to create new repositories. Can be one of 'read', 'write', 'admin' or 'none'.",
ValidateFunc: validation.StringInSlice([]string{"read", "write", "admin", "none"}, false),
},
"members_can_create_repositories": {
Type: schema.TypeBool,
Optional: true,
Default: true,
Description: "Whether or not organization members can create new repositories.",
},
"members_can_create_internal_repositories": {
Type: schema.TypeBool,
Optional: true,
Description: "Whether or not organization members can create new internal repositories. For Enterprise Organizations only.",
},
"members_can_create_private_repositories": {
Type: schema.TypeBool,
Optional: true,
Default: true,
Description: "Whether or not organization members can create new private repositories.",
},
"members_can_create_public_repositories": {
Type: schema.TypeBool,
Optional: true,
Default: true,
Description: "Whether or not organization members can create new public repositories.",
},
"members_can_create_pages": {
Type: schema.TypeBool,
Optional: true,
Default: true,
Description: "Whether or not organization members can create new pages.",
},
"members_can_create_public_pages": {
Type: schema.TypeBool,
Optional: true,
Default: true,
Description: "Whether or not organization members can create new public pages.",
},
"members_can_create_private_pages": {
Type: schema.TypeBool,
Optional: true,
Default: true,
Description: "Whether or not organization members can create new private pages.",
},
"members_can_fork_private_repositories": {
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "Whether or not organization members can fork private repositories.",
},
"web_commit_signoff_required": {
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "Whether or not commit signatures are required for commits to the organization.",
},
"blog": {
Type: schema.TypeString,
Optional: true,
Description: "The blog URL for the organization.",
},
"advanced_security_enabled_for_new_repositories": {
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: " Whether or not advanced security is enabled for new repositories.",
},
"dependabot_alerts_enabled_for_new_repositories": {
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "Whether or not dependabot alerts are enabled for new repositories.",
},
"dependabot_security_updates_enabled_for_new_repositories": {
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: " Whether or not dependabot security updates are enabled for new repositories.",
},
"dependency_graph_enabled_for_new_repositories": {
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "Whether or not dependency graph is enabled for new repositories.",
},
"secret_scanning_enabled_for_new_repositories": {
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "Whether or not secret scanning is enabled for new repositories.",
},
"secret_scanning_push_protection_enabled_for_new_repositories": {
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "Whether or not secret scanning push protection is enabled for new repositories.",
},
},
}
}
func resourceGithubOrganizationSettingsCreateOrUpdate(d *schema.ResourceData, meta interface{}) error {
err := checkOrganization(meta)
if err != nil {
return err
}
client := meta.(*Owner).v3client
ctx := context.WithValue(context.Background(), ctxId, d.Id())
org := meta.(*Owner).name
settings := github.Organization{
BillingEmail: github.String(d.Get("billing_email").(string)),
Company: github.String(d.Get("company").(string)),
Email: github.String(d.Get("email").(string)),
TwitterUsername: github.String(d.Get("twitter_username").(string)),
Location: github.String(d.Get("location").(string)),
Name: github.String(d.Get("name").(string)),
Description: github.String(d.Get("description").(string)),
HasOrganizationProjects: github.Bool(d.Get("has_organization_projects").(bool)),
HasRepositoryProjects: github.Bool(d.Get("has_repository_projects").(bool)),
DefaultRepoPermission: github.String(d.Get("default_repository_permission").(string)),
MembersCanCreateRepos: github.Bool(d.Get("members_can_create_repositories").(bool)),
MembersCanCreatePrivateRepos: github.Bool(d.Get("members_can_create_private_repositories").(bool)),
MembersCanCreatePublicRepos: github.Bool(d.Get("members_can_create_public_repositories").(bool)),
MembersCanCreatePages: github.Bool(d.Get("members_can_create_pages").(bool)),
MembersCanCreatePublicPages: github.Bool(d.Get("members_can_create_public_pages").(bool)),
MembersCanCreatePrivatePages: github.Bool(d.Get("members_can_create_private_pages").(bool)),
MembersCanForkPrivateRepos: github.Bool(d.Get("members_can_fork_private_repositories").(bool)),
WebCommitSignoffRequired: github.Bool(d.Get("web_commit_signoff_required").(bool)),
Blog: github.String(d.Get("blog").(string)),
AdvancedSecurityEnabledForNewRepos: github.Bool(d.Get("advanced_security_enabled_for_new_repositories").(bool)),
DependabotAlertsEnabledForNewRepos: github.Bool(d.Get("dependabot_alerts_enabled_for_new_repositories").(bool)),
DependabotSecurityUpdatesEnabledForNewRepos: github.Bool(d.Get("dependabot_security_updates_enabled_for_new_repositories").(bool)),
DependencyGraphEnabledForNewRepos: github.Bool(d.Get("dependency_graph_enabled_for_new_repositories").(bool)),
SecretScanningEnabledForNewRepos: github.Bool(d.Get("secret_scanning_enabled_for_new_repositories").(bool)),
SecretScanningPushProtectionEnabledForNewRepos: github.Bool(d.Get("secret_scanning_push_protection_enabled_for_new_repositories").(bool)),
}
enterpriseSettings := github.Organization{
BillingEmail: github.String(d.Get("billing_email").(string)),
Company: github.String(d.Get("company").(string)),
Email: github.String(d.Get("email").(string)),
TwitterUsername: github.String(d.Get("twitter_username").(string)),
Location: github.String(d.Get("location").(string)),
Name: github.String(d.Get("name").(string)),
Description: github.String(d.Get("description").(string)),
HasOrganizationProjects: github.Bool(d.Get("has_organization_projects").(bool)),
HasRepositoryProjects: github.Bool(d.Get("has_repository_projects").(bool)),
DefaultRepoPermission: github.String(d.Get("default_repository_permission").(string)),
MembersCanCreateRepos: github.Bool(d.Get("members_can_create_repositories").(bool)),
MembersCanCreateInternalRepos: github.Bool(d.Get("members_can_create_internal_repositories").(bool)),
MembersCanCreatePrivateRepos: github.Bool(d.Get("members_can_create_private_repositories").(bool)),
MembersCanCreatePublicRepos: github.Bool(d.Get("members_can_create_public_repositories").(bool)),
MembersCanCreatePages: github.Bool(d.Get("members_can_create_pages").(bool)),
MembersCanCreatePublicPages: github.Bool(d.Get("members_can_create_public_pages").(bool)),
MembersCanCreatePrivatePages: github.Bool(d.Get("members_can_create_private_pages").(bool)),
MembersCanForkPrivateRepos: github.Bool(d.Get("members_can_fork_private_repositories").(bool)),
WebCommitSignoffRequired: github.Bool(d.Get("web_commit_signoff_required").(bool)),
Blog: github.String(d.Get("blog").(string)),
AdvancedSecurityEnabledForNewRepos: github.Bool(d.Get("advanced_security_enabled_for_new_repositories").(bool)),
DependabotAlertsEnabledForNewRepos: github.Bool(d.Get("dependabot_alerts_enabled_for_new_repositories").(bool)),
DependabotSecurityUpdatesEnabledForNewRepos: github.Bool(d.Get("dependabot_security_updates_enabled_for_new_repositories").(bool)),
DependencyGraphEnabledForNewRepos: github.Bool(d.Get("dependency_graph_enabled_for_new_repositories").(bool)),
SecretScanningEnabledForNewRepos: github.Bool(d.Get("secret_scanning_enabled_for_new_repositories").(bool)),
SecretScanningPushProtectionEnabledForNewRepos: github.Bool(d.Get("secret_scanning_push_protection_enabled_for_new_repositories").(bool)),
}
enterpriseSettingsNoFork := github.Organization{
BillingEmail: github.String(d.Get("billing_email").(string)),
Company: github.String(d.Get("company").(string)),
Email: github.String(d.Get("email").(string)),
TwitterUsername: github.String(d.Get("twitter_username").(string)),
Location: github.String(d.Get("location").(string)),
Name: github.String(d.Get("name").(string)),
Description: github.String(d.Get("description").(string)),
HasOrganizationProjects: github.Bool(d.Get("has_organization_projects").(bool)),
HasRepositoryProjects: github.Bool(d.Get("has_repository_projects").(bool)),
DefaultRepoPermission: github.String(d.Get("default_repository_permission").(string)),
MembersCanCreateRepos: github.Bool(d.Get("members_can_create_repositories").(bool)),
MembersCanCreateInternalRepos: github.Bool(d.Get("members_can_create_internal_repositories").(bool)),
MembersCanCreatePrivateRepos: github.Bool(d.Get("members_can_create_private_repositories").(bool)),
MembersCanCreatePublicRepos: github.Bool(d.Get("members_can_create_public_repositories").(bool)),
MembersCanCreatePages: github.Bool(d.Get("members_can_create_pages").(bool)),
MembersCanCreatePublicPages: github.Bool(d.Get("members_can_create_public_pages").(bool)),
MembersCanCreatePrivatePages: github.Bool(d.Get("members_can_create_private_pages").(bool)),
WebCommitSignoffRequired: github.Bool(d.Get("web_commit_signoff_required").(bool)),
Blog: github.String(d.Get("blog").(string)),
AdvancedSecurityEnabledForNewRepos: github.Bool(d.Get("advanced_security_enabled_for_new_repositories").(bool)),
DependabotAlertsEnabledForNewRepos: github.Bool(d.Get("dependabot_alerts_enabled_for_new_repositories").(bool)),
DependabotSecurityUpdatesEnabledForNewRepos: github.Bool(d.Get("dependabot_security_updates_enabled_for_new_repositories").(bool)),
DependencyGraphEnabledForNewRepos: github.Bool(d.Get("dependency_graph_enabled_for_new_repositories").(bool)),
SecretScanningEnabledForNewRepos: github.Bool(d.Get("secret_scanning_enabled_for_new_repositories").(bool)),
SecretScanningPushProtectionEnabledForNewRepos: github.Bool(d.Get("secret_scanning_push_protection_enabled_for_new_repositories").(bool)),
}
orgPlan, _, err := client.Organizations.Edit(ctx, org, nil)
if err != nil {
return err
}
if orgPlan.GetPlan().GetName() == "enterprise" {
if _, ok := d.GetOk("members_can_fork_private_repositories"); !ok {
orgSettings, _, err := client.Organizations.Edit(ctx, org, &enterpriseSettingsNoFork)
if err != nil {
return err
}
id := strconv.FormatInt(orgSettings.GetID(), 10)
d.SetId(id)
} else if _, ok := d.GetOk("members_can_fork_private_repositories"); ok {
orgSettings, _, err := client.Organizations.Edit(ctx, org, &enterpriseSettings)
if err != nil {
return err
}
id := strconv.FormatInt(orgSettings.GetID(), 10)
d.SetId(id)
}
} else {
orgSettings, _, err := client.Organizations.Edit(ctx, org, &settings)
if err != nil {
return err
}
id := strconv.FormatInt(orgSettings.GetID(), 10)
d.SetId(id)
}
return resourceGithubOrganizationSettingsRead(d, meta)
}
func resourceGithubOrganizationSettingsRead(d *schema.ResourceData, meta interface{}) error {
err := checkOrganization(meta)
if err != nil {
return err
}
client := meta.(*Owner).v3client
ctx := context.Background()
org := meta.(*Owner).name
orgSettings, _, err := client.Organizations.Get(ctx, org)
if err != nil {
return err
}
d.Set("billing_email", orgSettings.GetBillingEmail())
d.Set("company", orgSettings.GetCompany())
d.Set("email", orgSettings.GetEmail())
d.Set("twitter_username", orgSettings.GetTwitterUsername())
d.Set("location", orgSettings.GetLocation())
d.Set("name", orgSettings.GetName())
d.Set("description", orgSettings.GetDescription())
d.Set("has_organization_projects", orgSettings.GetHasOrganizationProjects())
d.Set("has_repository_projects", orgSettings.GetHasRepositoryProjects())
d.Set("default_repository_permission", orgSettings.GetDefaultRepoPermission())
d.Set("members_can_create_repositories", orgSettings.GetMembersCanCreateRepos())
d.Set("members_can_create_internal_repositories", orgSettings.GetMembersCanCreateInternalRepos())
d.Set("members_can_create_private_repositories", orgSettings.GetMembersCanCreatePrivateRepos())
d.Set("members_can_create_public_repositories", orgSettings.GetMembersCanCreatePublicRepos())
d.Set("members_can_create_pages", orgSettings.GetMembersCanCreatePages())
d.Set("members_can_create_public_pages", orgSettings.GetMembersCanCreatePublicPages())
d.Set("members_can_create_private_pages", orgSettings.GetMembersCanCreatePrivatePages())
d.Set("members_can_fork_private_repositories", orgSettings.GetMembersCanForkPrivateRepos())
d.Set("web_commit_signoff_required", orgSettings.GetWebCommitSignoffRequired())
d.Set("blog", orgSettings.GetBlog())
d.Set("advanced_security_enabled_for_new_repositories", orgSettings.GetAdvancedSecurityEnabledForNewRepos())
d.Set("dependabot_alerts_enabled_for_new_repositories", orgSettings.GetDependabotAlertsEnabledForNewRepos())
d.Set("dependabot_security_updates_enabled_for_new_repositories", orgSettings.GetDependabotSecurityUpdatesEnabledForNewRepos())
d.Set("dependency_graph_enabled_for_new_repositories", orgSettings.GetDependencyGraphEnabledForNewRepos())
d.Set("secret_scanning_enabled_for_new_repositories", orgSettings.GetSecretScanningEnabledForNewRepos())
d.Set("secret_scanning_push_protection_enabled_for_new_repositories", orgSettings.GetSecretScanningPushProtectionEnabledForNewRepos())
return nil
}
func resourceGithubOrganizationSettingsDelete(d *schema.ResourceData, meta interface{}) error {
err := checkOrganization(meta)
if err != nil {
return err
}
client := meta.(*Owner).v3client
ctx := context.WithValue(context.Background(), ctxId, d.Id())
org := meta.(*Owner).name
// This will set org settings to default values
settings := github.Organization{
BillingEmail: github.String("email@example.com"),
Company: github.String(""),
Email: github.String(""),
TwitterUsername: github.String(""),
Location: github.String(""),
Name: github.String(""),
Description: github.String(""),
HasOrganizationProjects: github.Bool(true),
HasRepositoryProjects: github.Bool(true),
DefaultRepoPermission: github.String("read"),
MembersCanCreateRepos: github.Bool(true),
MembersCanCreatePrivateRepos: github.Bool(true),
MembersCanCreatePublicRepos: github.Bool(true),
MembersCanCreatePages: github.Bool(false),
MembersCanCreatePublicPages: github.Bool(true),
MembersCanCreatePrivatePages: github.Bool(true),
MembersCanForkPrivateRepos: github.Bool(false),
WebCommitSignoffRequired: github.Bool(false),
Blog: github.String(""),
AdvancedSecurityEnabledForNewRepos: github.Bool(false),
DependabotAlertsEnabledForNewRepos: github.Bool(false),
DependabotSecurityUpdatesEnabledForNewRepos: github.Bool(false),
DependencyGraphEnabledForNewRepos: github.Bool(false),
SecretScanningEnabledForNewRepos: github.Bool(false),
SecretScanningPushProtectionEnabledForNewRepos: github.Bool(false),
}
enterpriseSettings := github.Organization{
BillingEmail: github.String("email@example.com"),
Company: github.String(""),
Email: github.String(""),
TwitterUsername: github.String(""),
Location: github.String(""),
Name: github.String(""),
Description: github.String(""),
HasOrganizationProjects: github.Bool(true),
HasRepositoryProjects: github.Bool(true),
DefaultRepoPermission: github.String("read"),
MembersCanCreateRepos: github.Bool(true),
MembersCanCreatePrivateRepos: github.Bool(true),
MembersCanCreateInternalRepos: github.Bool(true),
MembersCanCreatePublicRepos: github.Bool(true),
MembersCanCreatePages: github.Bool(false),
MembersCanCreatePublicPages: github.Bool(true),
MembersCanCreatePrivatePages: github.Bool(true),
MembersCanForkPrivateRepos: github.Bool(false),
WebCommitSignoffRequired: github.Bool(false),
Blog: github.String(""),
AdvancedSecurityEnabledForNewRepos: github.Bool(false),
DependabotAlertsEnabledForNewRepos: github.Bool(false),
DependabotSecurityUpdatesEnabledForNewRepos: github.Bool(false),
DependencyGraphEnabledForNewRepos: github.Bool(false),
SecretScanningEnabledForNewRepos: github.Bool(false),
SecretScanningPushProtectionEnabledForNewRepos: github.Bool(false),
}
enterpriseSettingsNoFork := github.Organization{
BillingEmail: github.String("email@example.com"),
Company: github.String(""),
Email: github.String(""),
TwitterUsername: github.String(""),
Location: github.String(""),
Name: github.String(""),
Description: github.String(""),
HasOrganizationProjects: github.Bool(true),
HasRepositoryProjects: github.Bool(true),
DefaultRepoPermission: github.String("read"),
MembersCanCreateRepos: github.Bool(true),
MembersCanCreatePrivateRepos: github.Bool(true),
MembersCanCreateInternalRepos: github.Bool(true),
MembersCanCreatePublicRepos: github.Bool(true),
MembersCanCreatePages: github.Bool(false),
MembersCanCreatePublicPages: github.Bool(true),
MembersCanCreatePrivatePages: github.Bool(true),
WebCommitSignoffRequired: github.Bool(false),
Blog: github.String(""),
AdvancedSecurityEnabledForNewRepos: github.Bool(false),
DependabotAlertsEnabledForNewRepos: github.Bool(false),
DependabotSecurityUpdatesEnabledForNewRepos: github.Bool(false),
DependencyGraphEnabledForNewRepos: github.Bool(false),
SecretScanningEnabledForNewRepos: github.Bool(false),
SecretScanningPushProtectionEnabledForNewRepos: github.Bool(false),
}
log.Printf("[DEBUG] Reverting Organization Settings to default values: %s", org)
orgPlan, _, err := client.Organizations.Edit(ctx, org, nil)
if err != nil {
return err
}
if orgPlan.GetPlan().GetName() == "enterprise" {
if _, ok := d.GetOk("members_can_fork_private_repositories"); !ok {
_, _, err := client.Organizations.Edit(ctx, org, &enterpriseSettingsNoFork)
if err != nil {
return err
}
} else if _, ok := d.GetOk("members_can_fork_private_repositories"); ok {
_, _, err := client.Organizations.Edit(ctx, org, &enterpriseSettings)
if err != nil {
return err
}
}
} else {
_, _, err := client.Organizations.Edit(ctx, org, &settings)
if err != nil {
return err
}
}
return nil
}