Skip to content

Commit

Permalink
More retries for CRM resources (project, org policies, folders)
Browse files Browse the repository at this point in the history
Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
emilymye authored and modular-magician committed Jul 24, 2019
1 parent d790acb commit 1b45f67
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 98 deletions.
46 changes: 32 additions & 14 deletions google/resource_google_folder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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())
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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
}

Expand Down
51 changes: 26 additions & 25 deletions google/resource_google_folder_organization_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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
})
}
45 changes: 26 additions & 19 deletions google/resource_google_organization_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}

Expand Down
57 changes: 41 additions & 16 deletions google/resource_google_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package google
import (
"fmt"
"log"
"net/http"
"regexp"
"strconv"
"strings"
Expand Down Expand Up @@ -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"+
Expand Down Expand Up @@ -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))
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
Expand All @@ -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")
}

Expand All @@ -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")
Expand All @@ -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")
Expand All @@ -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("")
Expand Down Expand Up @@ -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
}
Loading

0 comments on commit 1b45f67

Please sign in to comment.