Skip to content

Commit 779881d

Browse files
Improve enterprise cost center resources UX
1 parent 97df975 commit 779881d

2 files changed

Lines changed: 28 additions & 9 deletions

File tree

github/resource_github_enterprise_cost_center_resources.go

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,19 @@ func resourceGithubEnterpriseCostCenterResources() *schema.Resource {
3535
},
3636
"users": {
3737
Type: schema.TypeSet,
38-
Required: true,
38+
Optional: true,
3939
Elem: &schema.Schema{Type: schema.TypeString},
4040
Description: "The usernames assigned to this cost center.",
4141
},
4242
"organizations": {
4343
Type: schema.TypeSet,
44-
Required: true,
44+
Optional: true,
4545
Elem: &schema.Schema{Type: schema.TypeString},
4646
Description: "The organization logins assigned to this cost center.",
4747
},
4848
"repositories": {
4949
Type: schema.TypeSet,
50-
Required: true,
50+
Optional: true,
5151
Elem: &schema.Schema{Type: schema.TypeString},
5252
Description: "The repositories (full name) assigned to this cost center.",
5353
},
@@ -100,15 +100,18 @@ func resourceGithubEnterpriseCostCenterResourcesUpdate(d *schema.ResourceData, m
100100

101101
cc, err := enterpriseCostCenterGet(ctx, client, enterpriseSlug, costCenterID)
102102
if err != nil {
103+
if is404(err) {
104+
return fmt.Errorf("cost center %q not found in enterprise %q (check enterprise_slug matches the cost center's enterprise)", costCenterID, enterpriseSlug)
105+
}
103106
return err
104107
}
105108
if strings.EqualFold(cc.State, "deleted") {
106109
return fmt.Errorf("cannot modify cost center %q resources because it is archived", costCenterID)
107110
}
108111

109-
desiredUsers := expandStringSet(d.Get("users").(*schema.Set))
110-
desiredOrgs := expandStringSet(d.Get("organizations").(*schema.Set))
111-
desiredRepos := expandStringSet(d.Get("repositories").(*schema.Set))
112+
desiredUsers := expandStringSet(getStringSetOrEmpty(d, "users"))
113+
desiredOrgs := expandStringSet(getStringSetOrEmpty(d, "organizations"))
114+
desiredRepos := expandStringSet(getStringSetOrEmpty(d, "repositories"))
112115

113116
currentUsers, currentOrgs, currentRepos := enterpriseCostCenterSplitResources(cc.Resources)
114117

@@ -204,6 +207,20 @@ func expandStringSet(set *schema.Set) []string {
204207
return out
205208
}
206209

210+
func getStringSetOrEmpty(d *schema.ResourceData, key string) *schema.Set {
211+
v, ok := d.GetOk(key)
212+
if !ok || v == nil {
213+
return schema.NewSet(schema.HashString, []any{})
214+
}
215+
216+
set, ok := v.(*schema.Set)
217+
if !ok || set == nil {
218+
return schema.NewSet(schema.HashString, []any{})
219+
}
220+
221+
return set
222+
}
223+
207224
func diffStringSlices(current []string, desired []string) (toAdd []string, toRemove []string) {
208225
cur := schema.NewSet(schema.HashString, stringSliceToAnySlice(current))
209226
des := schema.NewSet(schema.HashString, stringSliceToAnySlice(desired))

website/docs/r/enterprise_cost_center_resources.html.markdown

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ This resource allows you to manage which users, organizations, and repositories
1111

1212
The `users`, `organizations`, and `repositories` arguments are authoritative: on every apply, Terraform will add and remove assignments to match exactly what is configured.
1313

14+
Note: `enterprise_slug` must match the enterprise where the cost center was created. If they don't match, GitHub will return `404 Not Found` for the cost center ID.
15+
1416
## Example Usage
1517

1618
```
@@ -33,9 +35,9 @@ resource "github_enterprise_cost_center_resources" "example" {
3335

3436
* `enterprise_slug` - (Required) The slug of the enterprise.
3537
* `cost_center_id` - (Required) The cost center ID.
36-
* `users` - (Required) The usernames assigned to this cost center.
37-
* `organizations` - (Required) The organization logins assigned to this cost center.
38-
* `repositories` - (Required) The repositories (full name) assigned to this cost center.
38+
* `users` - (Optional) The usernames assigned to this cost center. Defaults to an empty list.
39+
* `organizations` - (Optional) The organization logins assigned to this cost center. Defaults to an empty list.
40+
* `repositories` - (Optional) The repositories (full name) assigned to this cost center. Defaults to an empty list.
3941

4042
## Import
4143

0 commit comments

Comments
 (0)