diff --git a/google/resource_google_folder.go b/google/resource_google_folder.go index d9079be4b0e..01a92d6d292 100644 --- a/google/resource_google_folder.go +++ b/google/resource_google_folder.go @@ -55,10 +55,14 @@ func resourceGoogleFolderCreate(d *schema.ResourceData, meta interface{}) error displayName := d.Get("display_name").(string) parent := d.Get("parent").(string) - op, err := config.clientResourceManagerV2Beta1.Folders.Create(&resourceManagerV2Beta1.Folder{ - DisplayName: displayName, - }).Parent(parent).Do() - + var op *resourceManagerV2Beta1.Operation + err := retry(func() error { + var reqErr error + op, reqErr = config.clientResourceManagerV2Beta1.Folders.Create(&resourceManagerV2Beta1.Folder{ + DisplayName: displayName, + }).Parent(parent).Do() + return reqErr + }) if err != nil { return fmt.Errorf("Error creating folder '%s' in '%s': %s", displayName, parent, err) } @@ -95,7 +99,12 @@ func resourceGoogleFolderCreate(d *schema.ResourceData, meta interface{}) error func resourceGoogleFolderRead(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) - folder, err := config.clientResourceManagerV2Beta1.Folders.Get(d.Id()).Do() + var folder *resourceManagerV2Beta1.Folder + err := retry(func() error { + var reqErr error + folder, reqErr = config.clientResourceManagerV2Beta1.Folders.Get(d.Id()).Do() + return reqErr + }) if err != nil { return handleNotFoundError(err, d, d.Id()) } @@ -115,10 +124,12 @@ func resourceGoogleFolderUpdate(d *schema.ResourceData, meta interface{}) error d.Partial(true) if d.HasChange("display_name") { - _, err := config.clientResourceManagerV2Beta1.Folders.Patch(d.Id(), &resourceManagerV2Beta1.Folder{ - DisplayName: displayName, - }).Do() - + err := retry(func() error { + _, reqErr := config.clientResourceManagerV2Beta1.Folders.Patch(d.Id(), &resourceManagerV2Beta1.Folder{ + DisplayName: displayName, + }).Do() + return reqErr + }) if err != nil { return fmt.Errorf("Error updating display_name to '%s': %s", displayName, err) } @@ -128,10 +139,15 @@ func resourceGoogleFolderUpdate(d *schema.ResourceData, meta interface{}) error if d.HasChange("parent") { newParent := d.Get("parent").(string) - op, err := config.clientResourceManagerV2Beta1.Folders.Move(d.Id(), &resourceManagerV2Beta1.MoveFolderRequest{ - DestinationParent: newParent, - }).Do() + var op *resourceManagerV2Beta1.Operation + err := retry(func() error { + var reqErr error + op, reqErr = config.clientResourceManagerV2Beta1.Folders.Move(d.Id(), &resourceManagerV2Beta1.MoveFolderRequest{ + DestinationParent: newParent, + }).Do() + return reqErr + }) if err != nil { return fmt.Errorf("Error moving folder '%s' to '%s': %s", displayName, newParent, err) } @@ -158,11 +174,13 @@ func resourceGoogleFolderDelete(d *schema.ResourceData, meta interface{}) error config := meta.(*Config) displayName := d.Get("display_name").(string) - _, err := config.clientResourceManagerV2Beta1.Folders.Delete(d.Id()).Do() + err := retry(func() error { + _, reqErr := config.clientResourceManagerV2Beta1.Folders.Delete(d.Id()).Do() + return reqErr + }) if err != nil { return fmt.Errorf("Error deleting folder '%s': %s", displayName, err) } - return nil } diff --git a/google/resource_google_folder_organization_policy.go b/google/resource_google_folder_organization_policy.go index ebf367fdc0f..b4c6dd23676 100644 --- a/google/resource_google_folder_organization_policy.go +++ b/google/resource_google_folder_organization_policy.go @@ -68,10 +68,13 @@ func resourceGoogleFolderOrganizationPolicyRead(d *schema.ResourceData, meta int config := meta.(*Config) folder := canonicalFolderId(d.Get("folder").(string)) - policy, err := config.clientResourceManager.Folders.GetOrgPolicy(folder, &cloudresourcemanager.GetOrgPolicyRequest{ - Constraint: canonicalOrgPolicyConstraint(d.Get("constraint").(string)), - }).Do() - + var policy *cloudresourcemanager.OrgPolicy + err := retry(func() (getErr error) { + policy, getErr = config.clientResourceManager.Folders.GetOrgPolicy(folder, &cloudresourcemanager.GetOrgPolicyRequest{ + Constraint: canonicalOrgPolicyConstraint(d.Get("constraint").(string)), + }).Do() + return getErr + }) if err != nil { return handleNotFoundError(err, d, fmt.Sprintf("Organization policy for %s", folder)) } @@ -103,15 +106,12 @@ func resourceGoogleFolderOrganizationPolicyDelete(d *schema.ResourceData, meta i config := meta.(*Config) folder := canonicalFolderId(d.Get("folder").(string)) - _, err := config.clientResourceManager.Folders.ClearOrgPolicy(folder, &cloudresourcemanager.ClearOrgPolicyRequest{ - Constraint: canonicalOrgPolicyConstraint(d.Get("constraint").(string)), - }).Do() - - if err != nil { - return err - } - - return nil + return retry(func() (delErr error) { + _, delErr = config.clientResourceManager.Folders.ClearOrgPolicy(folder, &cloudresourcemanager.ClearOrgPolicyRequest{ + Constraint: canonicalOrgPolicyConstraint(d.Get("constraint").(string)), + }).Do() + return delErr + }) } func setFolderOrganizationPolicy(d *schema.ResourceData, meta interface{}) error { @@ -128,16 +128,17 @@ func setFolderOrganizationPolicy(d *schema.ResourceData, meta interface{}) error return err } - _, err = config.clientResourceManager.Folders.SetOrgPolicy(folder, &cloudresourcemanager.SetOrgPolicyRequest{ - Policy: &cloudresourcemanager.OrgPolicy{ - Constraint: canonicalOrgPolicyConstraint(d.Get("constraint").(string)), - BooleanPolicy: expandBooleanOrganizationPolicy(d.Get("boolean_policy").([]interface{})), - ListPolicy: listPolicy, - RestoreDefault: restoreDefault, - Version: int64(d.Get("version").(int)), - Etag: d.Get("etag").(string), - }, - }).Do() - - return err + return retry(func() (setErr error) { + _, setErr = config.clientResourceManager.Folders.SetOrgPolicy(folder, &cloudresourcemanager.SetOrgPolicyRequest{ + Policy: &cloudresourcemanager.OrgPolicy{ + Constraint: canonicalOrgPolicyConstraint(d.Get("constraint").(string)), + BooleanPolicy: expandBooleanOrganizationPolicy(d.Get("boolean_policy").([]interface{})), + ListPolicy: listPolicy, + RestoreDefault: restoreDefault, + Version: int64(d.Get("version").(int)), + Etag: d.Get("etag").(string), + }, + }).Do() + return setErr + }) } diff --git a/google/resource_google_organization_policy.go b/google/resource_google_organization_policy.go index 2c7f96e185e..7dbdf19971c 100644 --- a/google/resource_google_organization_policy.go +++ b/google/resource_google_organization_policy.go @@ -161,10 +161,13 @@ func resourceGoogleOrganizationPolicyRead(d *schema.ResourceData, meta interface config := meta.(*Config) org := "organizations/" + d.Get("org_id").(string) - policy, err := config.clientResourceManager.Organizations.GetOrgPolicy(org, &cloudresourcemanager.GetOrgPolicyRequest{ - Constraint: canonicalOrgPolicyConstraint(d.Get("constraint").(string)), - }).Do() - + var policy *cloudresourcemanager.OrgPolicy + err := retry(func() (readErr error) { + policy, readErr = config.clientResourceManager.Organizations.GetOrgPolicy(org, &cloudresourcemanager.GetOrgPolicyRequest{ + Constraint: canonicalOrgPolicyConstraint(d.Get("constraint").(string)), + }).Do() + return readErr + }) if err != nil { return handleNotFoundError(err, d, fmt.Sprintf("Organization policy for %s", org)) } @@ -196,10 +199,12 @@ func resourceGoogleOrganizationPolicyDelete(d *schema.ResourceData, meta interfa config := meta.(*Config) org := "organizations/" + d.Get("org_id").(string) - _, err := config.clientResourceManager.Organizations.ClearOrgPolicy(org, &cloudresourcemanager.ClearOrgPolicyRequest{ - Constraint: canonicalOrgPolicyConstraint(d.Get("constraint").(string)), - }).Do() - + err := retry(func() error { + _, dErr := config.clientResourceManager.Organizations.ClearOrgPolicy(org, &cloudresourcemanager.ClearOrgPolicyRequest{ + Constraint: canonicalOrgPolicyConstraint(d.Get("constraint").(string)), + }).Do() + return dErr + }) if err != nil { return err } @@ -246,17 +251,19 @@ func setOrganizationPolicy(d *schema.ResourceData, meta interface{}) error { return err } - _, err = config.clientResourceManager.Organizations.SetOrgPolicy(org, &cloudresourcemanager.SetOrgPolicyRequest{ - Policy: &cloudresourcemanager.OrgPolicy{ - Constraint: canonicalOrgPolicyConstraint(d.Get("constraint").(string)), - BooleanPolicy: expandBooleanOrganizationPolicy(d.Get("boolean_policy").([]interface{})), - ListPolicy: listPolicy, - RestoreDefault: restoreDefault, - Version: int64(d.Get("version").(int)), - Etag: d.Get("etag").(string), - }, - }).Do() - + err = retry(func() (setErr error) { + _, setErr = config.clientResourceManager.Organizations.SetOrgPolicy(org, &cloudresourcemanager.SetOrgPolicyRequest{ + Policy: &cloudresourcemanager.OrgPolicy{ + Constraint: canonicalOrgPolicyConstraint(d.Get("constraint").(string)), + BooleanPolicy: expandBooleanOrganizationPolicy(d.Get("boolean_policy").([]interface{})), + ListPolicy: listPolicy, + RestoreDefault: restoreDefault, + Version: int64(d.Get("version").(int)), + Etag: d.Get("etag").(string), + }, + }).Do() + return setErr + }) return err } diff --git a/google/resource_google_project.go b/google/resource_google_project.go index 8a25b6e2190..608ec504027 100644 --- a/google/resource_google_project.go +++ b/google/resource_google_project.go @@ -3,7 +3,6 @@ package google import ( "fmt" "log" - "net/http" "regexp" "strconv" "strings" @@ -216,7 +215,11 @@ func resourceGoogleProjectCreate(d *schema.ResourceData, meta interface{}) error project.Labels = expandLabels(d) } - op, err := config.clientResourceManager.Projects.Create(project).Do() + var op *cloudresourcemanager.Operation + err = retry(func() (reqErr error) { + op, reqErr = config.clientResourceManager.Projects.Create(project).Do() + return reqErr + }) if err != nil { return fmt.Errorf("error creating project %s (%s): %s. "+ "If you received a 403 error, make sure you have the"+ @@ -273,8 +276,7 @@ func resourceGoogleProjectRead(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) pid := d.Id() - // Read the project - p, err := config.clientResourceManager.Projects.Get(pid).Do() + p, err := readGoogleProject(d, config) if err != nil { return handleNotFoundError(err, d, fmt.Sprintf("Project %q", pid)) } @@ -306,8 +308,12 @@ func resourceGoogleProjectRead(d *schema.ResourceData, meta interface{}) error { } } + var ba *cloudbilling.ProjectBillingInfo + err = retry(func() (reqErr error) { + ba, reqErr = config.clientBilling.Projects.GetBillingInfo(prefixedProject(pid)).Do() + return reqErr + }) // Read the billing account - ba, err := config.clientBilling.Projects.GetBillingInfo(prefixedProject(pid)).Do() if err != nil && !isApiNotEnabledError(err) { return fmt.Errorf("Error reading billing account for project %q: %v", prefixedProject(pid), err) } else if isApiNotEnabledError(err) { @@ -374,9 +380,9 @@ func resourceGoogleProjectUpdate(d *schema.ResourceData, meta interface{}) error // Read the project // we need the project even though refresh has already been called // because the API doesn't support patch, so we need the actual object - p, err := config.clientResourceManager.Projects.Get(pid).Do() + p, err := readGoogleProject(d, config) if err != nil { - if v, ok := err.(*googleapi.Error); ok && v.Code == http.StatusNotFound { + if isGoogleApiErrorWithCode(err, 404) { return fmt.Errorf("Project %q does not exist.", pid) } return fmt.Errorf("Error checking project %q: %s", pid, err) @@ -388,10 +394,13 @@ func resourceGoogleProjectUpdate(d *schema.ResourceData, meta interface{}) error if ok := d.HasChange("name"); ok { p.Name = project_name // Do update on project - p, err = config.clientResourceManager.Projects.Update(p.ProjectId, p).Do() - if err != nil { + if err = retry(func() (updateErr error) { + p, updateErr = config.clientResourceManager.Projects.Update(p.ProjectId, p).Do() + return updateErr + }); err != nil { return fmt.Errorf("Error updating project %q: %s", project_name, err) } + d.SetPartial("name") } @@ -402,8 +411,10 @@ func resourceGoogleProjectUpdate(d *schema.ResourceData, meta interface{}) error } // Do update on project - p, err = config.clientResourceManager.Projects.Update(p.ProjectId, p).Do() - if err != nil { + if err = retry(func() (updateErr error) { + p, updateErr = config.clientResourceManager.Projects.Update(p.ProjectId, p).Do() + return updateErr + }); err != nil { return fmt.Errorf("Error updating project %q: %s", project_name, err) } d.SetPartial("org_id") @@ -423,8 +434,10 @@ func resourceGoogleProjectUpdate(d *schema.ResourceData, meta interface{}) error p.Labels = expandLabels(d) // Do Update on project - p, err = config.clientResourceManager.Projects.Update(p.ProjectId, p).Do() - if err != nil { + if err = retry(func() (updateErr error) { + p, updateErr = config.clientResourceManager.Projects.Update(p.ProjectId, p).Do() + return updateErr + }); err != nil { return fmt.Errorf("Error updating project %q: %s", project_name, err) } d.SetPartial("labels") @@ -439,9 +452,11 @@ func resourceGoogleProjectDelete(d *schema.ResourceData, meta interface{}) error // Only delete projects if skip_delete isn't set if !d.Get("skip_delete").(bool) { pid := d.Id() - _, err := config.clientResourceManager.Projects.Delete(pid).Do() - if err != nil { - return fmt.Errorf("Error deleting project %q: %s", pid, err) + if err := retry(func() error { + _, delErr := config.clientResourceManager.Projects.Delete(pid).Do() + return delErr + }); err != nil { + return handleNotFoundError(err, d, fmt.Sprintf("Project %s", pid)) } } d.SetId("") @@ -542,3 +557,13 @@ func deleteComputeNetwork(project, network string, config *Config) error { } return nil } + +func readGoogleProject(d *schema.ResourceData, config *Config) (*cloudresourcemanager.Project, error) { + var p *cloudresourcemanager.Project + // Read the project + err := retry(func() (reqErr error) { + p, reqErr = config.clientResourceManager.Projects.Get(d.Id()).Do() + return reqErr + }) + return p, err +} diff --git a/google/resource_google_project_organization_policy.go b/google/resource_google_project_organization_policy.go index f509782ab28..3b3c4e89f6e 100644 --- a/google/resource_google_project_organization_policy.go +++ b/google/resource_google_project_organization_policy.go @@ -67,10 +67,13 @@ func resourceGoogleProjectOrganizationPolicyRead(d *schema.ResourceData, meta in config := meta.(*Config) project := prefixedProject(d.Get("project").(string)) - policy, err := config.clientResourceManager.Projects.GetOrgPolicy(project, &cloudresourcemanager.GetOrgPolicyRequest{ - Constraint: canonicalOrgPolicyConstraint(d.Get("constraint").(string)), - }).Do() - + var policy *cloudresourcemanager.OrgPolicy + err := retry(func() (readErr error) { + policy, readErr = config.clientResourceManager.Projects.GetOrgPolicy(project, &cloudresourcemanager.GetOrgPolicyRequest{ + Constraint: canonicalOrgPolicyConstraint(d.Get("constraint").(string)), + }).Do() + return readErr + }) if err != nil { return handleNotFoundError(err, d, fmt.Sprintf("Organization policy for %s", project)) } @@ -102,15 +105,12 @@ func resourceGoogleProjectOrganizationPolicyDelete(d *schema.ResourceData, meta config := meta.(*Config) project := prefixedProject(d.Get("project").(string)) - _, err := config.clientResourceManager.Projects.ClearOrgPolicy(project, &cloudresourcemanager.ClearOrgPolicyRequest{ - Constraint: canonicalOrgPolicyConstraint(d.Get("constraint").(string)), - }).Do() - - if err != nil { + return retry(func() error { + _, err := config.clientResourceManager.Projects.ClearOrgPolicy(project, &cloudresourcemanager.ClearOrgPolicyRequest{ + Constraint: canonicalOrgPolicyConstraint(d.Get("constraint").(string)), + }).Do() return err - } - - return nil + }) } func setProjectOrganizationPolicy(d *schema.ResourceData, meta interface{}) error { @@ -127,16 +127,17 @@ func setProjectOrganizationPolicy(d *schema.ResourceData, meta interface{}) erro return err } - _, err = config.clientResourceManager.Projects.SetOrgPolicy(project, &cloudresourcemanager.SetOrgPolicyRequest{ - Policy: &cloudresourcemanager.OrgPolicy{ - Constraint: canonicalOrgPolicyConstraint(d.Get("constraint").(string)), - BooleanPolicy: expandBooleanOrganizationPolicy(d.Get("boolean_policy").([]interface{})), - ListPolicy: listPolicy, - RestoreDefault: restore_default, - Version: int64(d.Get("version").(int)), - Etag: d.Get("etag").(string), - }, - }).Do() - - return err + return retry(func() error { + _, err := config.clientResourceManager.Projects.SetOrgPolicy(project, &cloudresourcemanager.SetOrgPolicyRequest{ + Policy: &cloudresourcemanager.OrgPolicy{ + Constraint: canonicalOrgPolicyConstraint(d.Get("constraint").(string)), + BooleanPolicy: expandBooleanOrganizationPolicy(d.Get("boolean_policy").([]interface{})), + ListPolicy: listPolicy, + RestoreDefault: restore_default, + Version: int64(d.Get("version").(int)), + Etag: d.Get("etag").(string), + }, + }).Do() + return err + }) }