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

In resource_google_project: Fix one bug with retries to Projects.Update(...) and add retries to GetBillingInfo(...) requests #5578

Merged
merged 1 commit into from
Feb 7, 2020
Merged
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
46 changes: 28 additions & 18 deletions google/resource_google_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,11 +293,8 @@ func resourceGoogleProjectUpdate(d *schema.ResourceData, meta interface{}) error
if ok := d.HasChange("name"); ok {
p.Name = project_name
// Do update on project
if err = retryTimeDuration(func() (updateErr error) {
p, updateErr = config.clientResourceManager.Projects.Update(p.ProjectId, p).Do()
return updateErr
}, d.Timeout(schema.TimeoutUpdate)); err != nil {
return fmt.Errorf("Error updating project %q: %s", project_name, err)
if p, err = updateProject(config, d, project_name, p); err != nil {
return err
}

d.SetPartial("name")
Expand All @@ -310,11 +307,8 @@ func resourceGoogleProjectUpdate(d *schema.ResourceData, meta interface{}) error
}

// Do update on project
if err = retryTimeDuration(func() (updateErr error) {
p, updateErr = config.clientResourceManager.Projects.Update(p.ProjectId, p).Do()
return updateErr
}, d.Timeout(schema.TimeoutUpdate)); err != nil {
return fmt.Errorf("Error updating project %q: %s", project_name, err)
if p, err = updateProject(config, d, project_name, p); err != nil {
return err
}
d.SetPartial("org_id")
d.SetPartial("folder_id")
Expand All @@ -333,11 +327,8 @@ func resourceGoogleProjectUpdate(d *schema.ResourceData, meta interface{}) error
p.Labels = expandLabels(d)

// Do Update on project
if err = retryTimeDuration(func() (updateErr error) {
p, updateErr = config.clientResourceManager.Projects.Update(p.ProjectId, p).Do()
return updateErr
}, d.Timeout(schema.TimeoutUpdate)); err != nil {
return fmt.Errorf("Error updating project %q: %s", project_name, err)
if p, err = updateProject(config, d, project_name, p); err != nil {
return err
}
d.SetPartial("labels")
}
Expand All @@ -346,6 +337,17 @@ func resourceGoogleProjectUpdate(d *schema.ResourceData, meta interface{}) error
return resourceGoogleProjectRead(d, meta)
}

func updateProject(config *Config, d *schema.ResourceData, projectName string, desiredProject *cloudresourcemanager.Project) (*cloudresourcemanager.Project, error) {
var newProj *cloudresourcemanager.Project
if err := retryTimeDuration(func() (updateErr error) {
newProj, updateErr = config.clientResourceManager.Projects.Update(desiredProject.ProjectId, desiredProject).Do()
return updateErr
}, d.Timeout(schema.TimeoutUpdate)); err != nil {
return nil, fmt.Errorf("Error updating project %q: %s", projectName, err)
}
return newProj, nil
}

func resourceGoogleProjectDelete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
// Only delete projects if skip_delete isn't set
Expand Down Expand Up @@ -429,7 +431,11 @@ func updateProjectBillingAccount(d *schema.ResourceData, config *Config) error {
if name != "" {
ba.BillingAccountName = "billingAccounts/" + name
}
_, err := config.clientBilling.Projects.UpdateBillingInfo(prefixedProject(pid), ba).Do()
updateBillingInfoFunc := func() error {
_, err := config.clientBilling.Projects.UpdateBillingInfo(prefixedProject(pid), ba).Do()
return err
}
err := retryTimeDuration(updateBillingInfoFunc, d.Timeout(schema.TimeoutUpdate))
if err != nil {
d.Set("billing_account", "")
if _err, ok := err.(*googleapi.Error); ok {
Expand All @@ -438,9 +444,13 @@ func updateProjectBillingAccount(d *schema.ResourceData, config *Config) error {
return fmt.Errorf("Error setting billing account %q for project %q: %v", name, prefixedProject(pid), err)
}
for retries := 0; retries < 3; retries++ {
ba, err = config.clientBilling.Projects.GetBillingInfo(prefixedProject(pid)).Do()
var ba *cloudbilling.ProjectBillingInfo
err = retryTimeDuration(func() (reqErr error) {
ba, reqErr = config.clientBilling.Projects.GetBillingInfo(prefixedProject(pid)).Do()
return reqErr
}, d.Timeout(schema.TimeoutRead))
if err != nil {
return err
return fmt.Errorf("Error getting billing info for project %q: %v", prefixedProject(pid), err)
rileykarson marked this conversation as resolved.
Show resolved Hide resolved
}
baName := strings.TrimPrefix(ba.BillingAccountName, "billingAccounts/")
if baName == name {
Expand Down