From 6daa134a0dc2a57a648bfe6f8125310b046cbb31 Mon Sep 17 00:00:00 2001 From: Chris Piraino Date: Tue, 12 Nov 2024 16:23:49 -0500 Subject: [PATCH 1/3] Retry billing update in project creation HCPF-2286 --- internal/clients/retry_request.go | 28 +++++++++++++++++++ .../resourcemanager/resource_project.go | 2 +- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/internal/clients/retry_request.go b/internal/clients/retry_request.go index 0cd88181f..5067b8735 100644 --- a/internal/clients/retry_request.go +++ b/internal/clients/retry_request.go @@ -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" ) @@ -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.(*project_service.ProjectServiceGetDefault) + 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 +} diff --git a/internal/provider/resourcemanager/resource_project.go b/internal/provider/resourcemanager/resource_project.go index c9ae1656a..4bab53ed2 100644 --- a/internal/provider/resourcemanager/resource_project.go +++ b/internal/provider/resourcemanager/resource_project.go @@ -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()) } From b0d4c7a3b408d9867ccce408fb4410a6d64ad0e0 Mon Sep 17 00:00:00 2001 From: Chris Piraino Date: Thu, 14 Nov 2024 09:41:04 -0500 Subject: [PATCH 2/3] Add changelog --- .changelog/1131.txt | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .changelog/1131.txt diff --git a/.changelog/1131.txt b/.changelog/1131.txt new file mode 100644 index 000000000..4fe4cc511 --- /dev/null +++ b/.changelog/1131.txt @@ -0,0 +1,3 @@ +```release-note:bug +Retry billing service update call when creating a project +``` From 69eb5d23b393fcfd50503d17b48fb72f74f0c232 Mon Sep 17 00:00:00 2001 From: Chris Piraino Date: Thu, 14 Nov 2024 11:07:27 -0500 Subject: [PATCH 3/3] Use correct error struct --- internal/clients/retry_request.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/clients/retry_request.go b/internal/clients/retry_request.go index 5067b8735..93bb3ebc4 100644 --- a/internal/clients/retry_request.go +++ b/internal/clients/retry_request.go @@ -115,7 +115,7 @@ func RetryBillingServiceUpdate(client *Client, params *billing.BillingAccountSer resp, err := client.Billing.BillingAccountServiceUpdate(params, nil) if err != nil { - serviceErr, ok := err.(*project_service.ProjectServiceGetDefault) + serviceErr, ok := err.(*billing.BillingAccountServiceUpdateDefault) if !ok { return nil, err }