Skip to content

Commit 6690cc5

Browse files
authored
Merge pull request #4 from casey-robertson-paypal/fix/address-stevehipwell-review
Address @stevehipwell review: code idioms, lint, docs
2 parents 83ef94a + 2645410 commit 6690cc5

10 files changed

Lines changed: 188 additions & 241 deletions

docs/resources/enterprise_security_configuration.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ This resource allows you to create and manage [code security configurations](htt
1313

1414
You must have enterprise admin access to use this resource.
1515

16-
~> **Note:** Some settings (such as `advanced_security` and `secret_scanning_generic_secrets`) require GitHub Advanced Security licensing.
16+
~> Some settings (such as `advanced_security` and `secret_scanning_generic_secrets`) require GitHub Advanced Security licensing.
1717

1818
## Example Usage
1919

@@ -98,7 +98,18 @@ Optional:
9898

9999
## Import
100100

101-
GitHub enterprise code security configurations can be imported using the enterprise slug and the configuration ID separated by a colon (`<enterprise_slug>:<configuration_id>`), for example:
101+
GitHub enterprise code security configurations can be imported using the enterprise slug and the configuration ID separated by a colon (`<enterprise_slug>:<configuration_id>`).
102+
103+
In Terraform v1.5.0 and later, the [`import` block](https://developer.hashicorp.com/terraform/language/import) can be used with the `id` attribute, for example:
104+
105+
```terraform
106+
import {
107+
to = github_enterprise_security_configuration.default
108+
id = "my-enterprise:123"
109+
}
110+
```
111+
112+
The [`terraform import` command](https://developer.hashicorp.com/terraform/cli/commands/import) can be used, for example:
102113

103114
```shell
104115
terraform import github_enterprise_security_configuration.default my-enterprise:123

docs/resources/organization_security_configuration.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ This resource allows you to create and manage [code security configurations](htt
1313

1414
You must have organization admin access to use this resource.
1515

16-
~> **Note:** Some settings (such as `advanced_security` and `secret_scanning_generic_secrets`) require GitHub Advanced Security licensing.
16+
~> Some settings (such as `advanced_security` and `secret_scanning_generic_secrets`) require GitHub Advanced Security licensing.
1717

1818
## Example Usage
1919

@@ -137,7 +137,18 @@ Required:
137137

138138
## Import
139139

140-
GitHub organization code security configurations can be imported using the configuration ID, for example:
140+
GitHub organization code security configurations can be imported using the configuration ID.
141+
142+
In Terraform v1.5.0 and later, the [`import` block](https://developer.hashicorp.com/terraform/language/import) can be used with the `id` attribute, for example:
143+
144+
```terraform
145+
import {
146+
to = github_organization_security_configuration.default
147+
id = "123"
148+
}
149+
```
150+
151+
The [`terraform import` command](https://developer.hashicorp.com/terraform/cli/commands/import) can be used, for example:
141152

142153
```shell
143154
terraform import github_organization_security_configuration.default 123
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import {
2+
to = github_enterprise_security_configuration.default
3+
id = "my-enterprise:123"
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import {
2+
to = github_organization_security_configuration.default
3+
id = "123"
4+
}

github/resource_github_enterprise_security_configuration.go

Lines changed: 41 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -264,25 +264,19 @@ func resourceGithubEnterpriseSecurityConfiguration() *schema.Resource {
264264
}
265265
}
266266

267-
func resourceGithubEnterpriseSecurityConfigurationCreate(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
268-
client := meta.(*Owner).v3client
269-
enterprise := d.Get("enterprise_slug").(string)
270-
name := d.Get("name").(string)
267+
func resourceGithubEnterpriseSecurityConfigurationCreate(ctx context.Context, d *schema.ResourceData, m any) diag.Diagnostics {
268+
meta, _ := m.(*Owner)
269+
client := meta.v3client
270+
enterprise, _ := d.Get("enterprise_slug").(string)
271+
name, _ := d.Get("name").(string)
271272

272-
tflog.Debug(ctx, "Creating enterprise code security configuration", map[string]any{
273-
"enterprise": enterprise,
274-
"name": name,
275-
})
273+
tflog.Debug(ctx, "Creating enterprise code security configuration", map[string]any{"enterprise": enterprise, "name": name})
276274

277275
config := expandCodeSecurityConfigurationCommon(d)
278276

279277
configuration, _, err := client.Enterprise.CreateCodeSecurityConfiguration(ctx, enterprise, config)
280278
if err != nil {
281-
tflog.Error(ctx, "Failed to create enterprise code security configuration", map[string]any{
282-
"enterprise": enterprise,
283-
"name": name,
284-
"error": err.Error(),
285-
})
279+
tflog.Error(ctx, "Failed to create enterprise code security configuration", map[string]any{"enterprise": enterprise, "name": name, "error": err.Error()})
286280
return diag.FromErr(err)
287281
}
288282

@@ -296,124 +290,86 @@ func resourceGithubEnterpriseSecurityConfigurationCreate(ctx context.Context, d
296290
return diags
297291
}
298292

299-
tflog.Info(ctx, "Created enterprise code security configuration", map[string]any{
300-
"enterprise": enterprise,
301-
"name": name,
302-
"id": configuration.GetID(),
303-
})
293+
tflog.Info(ctx, "Created enterprise code security configuration", map[string]any{"enterprise": enterprise, "name": name, "id": configuration.GetID()})
304294

305295
return nil
306296
}
307297

308-
func resourceGithubEnterpriseSecurityConfigurationRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
309-
client := meta.(*Owner).v3client
310-
enterprise := d.Get("enterprise_slug").(string)
311-
id := int64(d.Get("configuration_id").(int))
298+
func resourceGithubEnterpriseSecurityConfigurationRead(ctx context.Context, d *schema.ResourceData, m any) diag.Diagnostics {
299+
meta, _ := m.(*Owner)
300+
client := meta.v3client
301+
enterprise, _ := d.Get("enterprise_slug").(string)
302+
configID, _ := d.Get("configuration_id").(int)
303+
id := int64(configID)
312304

313-
tflog.Trace(ctx, "Reading enterprise code security configuration", map[string]any{
314-
"enterprise": enterprise,
315-
"id": id,
316-
})
305+
tflog.Trace(ctx, "Reading enterprise code security configuration", map[string]any{"enterprise": enterprise, "id": id})
317306

318307
configuration, _, err := client.Enterprise.GetCodeSecurityConfiguration(ctx, enterprise, id)
319308
if err != nil {
320-
var ghErr *github.ErrorResponse
321-
if errors.As(err, &ghErr) {
322-
if ghErr.Response.StatusCode == http.StatusNotFound {
323-
tflog.Info(ctx, "Removing enterprise code security configuration from state because it no longer exists in GitHub", map[string]any{
324-
"enterprise": enterprise,
325-
"id": id,
326-
})
327-
d.SetId("")
328-
return nil
329-
}
309+
if ghErr, ok := errors.AsType[*github.ErrorResponse](err); ok && ghErr.Response.StatusCode == http.StatusNotFound {
310+
tflog.Info(ctx, "Removing enterprise code security configuration from state because it no longer exists in GitHub", map[string]any{"enterprise": enterprise, "id": id})
311+
d.SetId("")
312+
return nil
330313
}
331-
tflog.Error(ctx, "Failed to read enterprise code security configuration", map[string]any{
332-
"enterprise": enterprise,
333-
"id": id,
334-
"error": err.Error(),
335-
})
314+
tflog.Error(ctx, "Failed to read enterprise code security configuration", map[string]any{"enterprise": enterprise, "id": id, "error": err.Error()})
336315
return diag.FromErr(err)
337316
}
338317

339318
if diags := setCodeSecurityConfigurationState(d, configuration); diags.HasError() {
340319
return diags
341320
}
342321

343-
tflog.Trace(ctx, "Successfully read enterprise code security configuration", map[string]any{
344-
"enterprise": enterprise,
345-
"id": id,
346-
})
322+
tflog.Trace(ctx, "Successfully read enterprise code security configuration", map[string]any{"enterprise": enterprise, "id": id})
347323

348324
return nil
349325
}
350326

351-
func resourceGithubEnterpriseSecurityConfigurationUpdate(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
352-
client := meta.(*Owner).v3client
353-
enterprise := d.Get("enterprise_slug").(string)
354-
id := int64(d.Get("configuration_id").(int))
327+
func resourceGithubEnterpriseSecurityConfigurationUpdate(ctx context.Context, d *schema.ResourceData, m any) diag.Diagnostics {
328+
meta, _ := m.(*Owner)
329+
client := meta.v3client
330+
enterprise, _ := d.Get("enterprise_slug").(string)
331+
configID, _ := d.Get("configuration_id").(int)
332+
id := int64(configID)
355333

356-
tflog.Debug(ctx, "Updating enterprise code security configuration", map[string]any{
357-
"enterprise": enterprise,
358-
"id": id,
359-
})
334+
tflog.Debug(ctx, "Updating enterprise code security configuration", map[string]any{"enterprise": enterprise, "id": id})
360335

361336
config := expandCodeSecurityConfigurationCommon(d)
362337

363338
configuration, _, err := client.Enterprise.UpdateCodeSecurityConfiguration(ctx, enterprise, id, config)
364339
if err != nil {
365-
tflog.Error(ctx, "Failed to update enterprise code security configuration", map[string]any{
366-
"enterprise": enterprise,
367-
"id": id,
368-
"error": err.Error(),
369-
})
340+
tflog.Error(ctx, "Failed to update enterprise code security configuration", map[string]any{"enterprise": enterprise, "id": id, "error": err.Error()})
370341
return diag.FromErr(err)
371342
}
372343

373344
if diags := setCodeSecurityConfigurationState(d, configuration); diags.HasError() {
374345
return diags
375346
}
376347

377-
tflog.Info(ctx, "Updated enterprise code security configuration", map[string]any{
378-
"enterprise": enterprise,
379-
"id": id,
380-
})
348+
tflog.Info(ctx, "Updated enterprise code security configuration", map[string]any{"enterprise": enterprise, "id": id})
381349

382350
return nil
383351
}
384352

385-
func resourceGithubEnterpriseSecurityConfigurationDelete(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
386-
client := meta.(*Owner).v3client
387-
enterprise := d.Get("enterprise_slug").(string)
388-
id := int64(d.Get("configuration_id").(int))
353+
func resourceGithubEnterpriseSecurityConfigurationDelete(ctx context.Context, d *schema.ResourceData, m any) diag.Diagnostics {
354+
meta, _ := m.(*Owner)
355+
client := meta.v3client
356+
enterprise, _ := d.Get("enterprise_slug").(string)
357+
configID, _ := d.Get("configuration_id").(int)
358+
id := int64(configID)
389359

390-
tflog.Debug(ctx, "Deleting enterprise code security configuration", map[string]any{
391-
"enterprise": enterprise,
392-
"id": id,
393-
})
360+
tflog.Debug(ctx, "Deleting enterprise code security configuration", map[string]any{"enterprise": enterprise, "id": id})
394361

395362
_, err := client.Enterprise.DeleteCodeSecurityConfiguration(ctx, enterprise, id)
396363
if err != nil {
397-
var ghErr *github.ErrorResponse
398-
if errors.As(err, &ghErr) && ghErr.Response.StatusCode == http.StatusNotFound {
399-
tflog.Info(ctx, "Enterprise code security configuration already deleted", map[string]any{
400-
"enterprise": enterprise,
401-
"id": id,
402-
})
364+
if ghErr, ok := errors.AsType[*github.ErrorResponse](err); ok && ghErr.Response.StatusCode == http.StatusNotFound {
365+
tflog.Info(ctx, "Enterprise code security configuration already deleted", map[string]any{"enterprise": enterprise, "id": id})
403366
return nil
404367
}
405-
tflog.Error(ctx, "Failed to delete enterprise code security configuration", map[string]any{
406-
"enterprise": enterprise,
407-
"id": id,
408-
"error": err.Error(),
409-
})
368+
tflog.Error(ctx, "Failed to delete enterprise code security configuration", map[string]any{"enterprise": enterprise, "id": id, "error": err.Error()})
410369
return diag.FromErr(err)
411370
}
412371

413-
tflog.Info(ctx, "Deleted enterprise code security configuration", map[string]any{
414-
"enterprise": enterprise,
415-
"id": id,
416-
})
372+
tflog.Info(ctx, "Deleted enterprise code security configuration", map[string]any{"enterprise": enterprise, "id": id})
417373

418374
return nil
419375
}
@@ -439,4 +395,3 @@ func resourceGithubEnterpriseSecurityConfigurationImport(_ context.Context, d *s
439395

440396
return []*schema.ResourceData{d}, nil
441397
}
442-

0 commit comments

Comments
 (0)