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 +``` diff --git a/internal/clients/retry_request.go b/internal/clients/retry_request.go index 0cd88181f..93bb3ebc4 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.(*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 +} 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()) }