Skip to content

Commit 702d395

Browse files
Retry cost center assignments and improve docs
1 parent 779881d commit 702d395

3 files changed

Lines changed: 48 additions & 13 deletions

File tree

github/resource_github_enterprise_cost_center_resources.go

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,15 @@ package github
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
67
"log"
78
"sort"
89
"strings"
10+
"time"
911

12+
"github.com/google/go-github/v67/github"
13+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
1014
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1115
)
1216

@@ -121,10 +125,19 @@ func resourceGithubEnterpriseCostCenterResourcesUpdate(d *schema.ResourceData, m
121125

122126
if len(toRemoveUsers)+len(toRemoveOrgs)+len(toRemoveRepos) > 0 {
123127
log.Printf("[INFO] Removing enterprise cost center resources: %s/%s", enterpriseSlug, costCenterID)
124-
_, err := enterpriseCostCenterRemoveResources(ctx, client, enterpriseSlug, costCenterID, enterpriseCostCenterResourcesRequest{
125-
Users: toRemoveUsers,
126-
Organizations: toRemoveOrgs,
127-
Repositories: toRemoveRepos,
128+
err := resource.RetryContext(ctx, 30*time.Second, func() *resource.RetryError {
129+
_, err := enterpriseCostCenterRemoveResources(ctx, client, enterpriseSlug, costCenterID, enterpriseCostCenterResourcesRequest{
130+
Users: toRemoveUsers,
131+
Organizations: toRemoveOrgs,
132+
Repositories: toRemoveRepos,
133+
})
134+
if err == nil {
135+
return nil
136+
}
137+
if isRetryableGithubResponseError(err) {
138+
return resource.RetryableError(err)
139+
}
140+
return resource.NonRetryableError(err)
128141
})
129142
if err != nil {
130143
return err
@@ -133,10 +146,19 @@ func resourceGithubEnterpriseCostCenterResourcesUpdate(d *schema.ResourceData, m
133146

134147
if len(toAddUsers)+len(toAddOrgs)+len(toAddRepos) > 0 {
135148
log.Printf("[INFO] Assigning enterprise cost center resources: %s/%s", enterpriseSlug, costCenterID)
136-
_, err := enterpriseCostCenterAssignResources(ctx, client, enterpriseSlug, costCenterID, enterpriseCostCenterResourcesRequest{
137-
Users: toAddUsers,
138-
Organizations: toAddOrgs,
139-
Repositories: toAddRepos,
149+
err := resource.RetryContext(ctx, 30*time.Second, func() *resource.RetryError {
150+
_, err := enterpriseCostCenterAssignResources(ctx, client, enterpriseSlug, costCenterID, enterpriseCostCenterResourcesRequest{
151+
Users: toAddUsers,
152+
Organizations: toAddOrgs,
153+
Repositories: toAddRepos,
154+
})
155+
if err == nil {
156+
return nil
157+
}
158+
if isRetryableGithubResponseError(err) {
159+
return resource.RetryableError(err)
160+
}
161+
return resource.NonRetryableError(err)
140162
})
141163
if err != nil {
142164
return err
@@ -221,6 +243,19 @@ func getStringSetOrEmpty(d *schema.ResourceData, key string) *schema.Set {
221243
return set
222244
}
223245

246+
func isRetryableGithubResponseError(err error) bool {
247+
var ghErr *github.ErrorResponse
248+
if errors.As(err, &ghErr) && ghErr.Response != nil {
249+
switch ghErr.Response.StatusCode {
250+
case 404, 409, 500, 502, 503, 504:
251+
return true
252+
default:
253+
return false
254+
}
255+
}
256+
return false
257+
}
258+
224259
func diffStringSlices(current []string, desired []string) (toAdd []string, toRemove []string) {
225260
cur := schema.NewSet(schema.HashString, stringSliceToAnySlice(current))
226261
des := schema.NewSet(schema.HashString, stringSliceToAnySlice(desired))

website/docs/r/enterprise_cost_center.html.markdown

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,6 @@ The following additional attributes are exported:
4141
GitHub Enterprise Cost Center can be imported using the `enterprise_slug` and the `cost_center_id`, separated by a `/` character.
4242

4343
```
44-
$ terraform import github_enterprise_cost_center.example example-enterprise/cc_123456
44+
$ terraform import github_enterprise_cost_center.example example-enterprise/<cost_center_id>
4545
```
4646

website/docs/r/enterprise_cost_center_resources.html.markdown

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,15 @@ resource "github_enterprise_cost_center_resources" "example" {
3535

3636
* `enterprise_slug` - (Required) The slug of the enterprise.
3737
* `cost_center_id` - (Required) The cost center ID.
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.
38+
* `users` - (Optional) The usernames assigned to this cost center. If omitted, treated as an empty set.
39+
* `organizations` - (Optional) The organization logins assigned to this cost center. If omitted, treated as an empty set.
40+
* `repositories` - (Optional) The repositories (full name) assigned to this cost center. If omitted, treated as an empty set.
4141

4242
## Import
4343

4444
GitHub Enterprise Cost Center Resources can be imported using the `enterprise_slug` and the `cost_center_id`, separated by a `/` character.
4545

4646
```
47-
$ terraform import github_enterprise_cost_center_resources.example example-enterprise/cc_123456
47+
$ terraform import github_enterprise_cost_center_resources.example example-enterprise/<cost_center_id>
4848
```
4949

0 commit comments

Comments
 (0)