Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Retry billing update in project creation #1131

Merged
merged 5 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/1131.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
Retry billing service update call when creating a project
```
28 changes: 28 additions & 0 deletions internal/clients/retry_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"time"

billing "github.com/hashicorp/hcp-sdk-go/clients/cloud-billing/preview/2020-11-05/client/billing_account_service"
"github.com/hashicorp/hcp-sdk-go/clients/cloud-resource-manager/stable/2019-12-10/client/organization_service"
"github.com/hashicorp/hcp-sdk-go/clients/cloud-resource-manager/stable/2019-12-10/client/project_service"
)
Expand Down Expand Up @@ -108,3 +109,30 @@ func RetryProjectServiceGet(client *Client, params *project_service.ProjectServi
}
return resp, err
}

// Wraps the BillingServiceUpdate function in a loop that supports retrying the PUT request
func RetryBillingServiceUpdate(client *Client, params *billing.BillingAccountServiceUpdateParams) (*billing.BillingAccountServiceUpdateOK, error) {
resp, err := client.Billing.BillingAccountServiceUpdate(params, nil)

if err != nil {
serviceErr, ok := err.(*billing.BillingAccountServiceUpdateDefault)
if !ok {
return nil, err
}

counter := counterStart
for shouldRetryErrorCode(serviceErr.Code(), []int{503}) && counter < retryCount {
resp, err = client.Billing.BillingAccountServiceUpdate(params, nil)
if err == nil {
break
}
// Avoid wasting time if we're not going to retry next loop cycle
if (counter + 1) != retryCount {
fmt.Printf("Error trying to update billing account. Retrying in %d seconds...", retryDelay*counter)
time.Sleep(time.Duration(retryDelay*counter) * time.Second)
}
counter++
}
}
return resp, err
}
2 changes: 1 addition & 1 deletion internal/provider/resourcemanager/resource_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func (r *resourceProject) addToBillingAccount(ctx context.Context, projectID str

updateReq.Body.ProjectIds = append(updateReq.Body.ProjectIds, projectID)

_, err = r.client.Billing.BillingAccountServiceUpdate(updateReq, nil)
_, err = clients.RetryBillingServiceUpdate(r.client, updateReq)
if err != nil {
return fmt.Errorf("updating billing account failed: %v", err.Error())
}
Expand Down