Skip to content

Commit

Permalink
Use errwrap in operations, unwrap errors to check retryable errors
Browse files Browse the repository at this point in the history
Signed-off-by: Modular Magician <magic-modules@google.com>
  • Loading branch information
emilymye authored and modular-magician committed May 31, 2019
1 parent 1b1cd11 commit 269989f
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 11 deletions.
7 changes: 4 additions & 3 deletions google/common_operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"log"
"time"

"github.com/hashicorp/errwrap"
"github.com/hashicorp/terraform/helper/resource"
cloudresourcemanager "google.golang.org/api/cloudresourcemanager/v1"
"google.golang.org/api/googleapi"
Expand Down Expand Up @@ -102,11 +103,11 @@ func CommonRefreshFunc(w Waiter) resource.StateRefreshFunc {
return op, "done: false", nil
}

return nil, "", fmt.Errorf("error while retrieving operation: %s", err)
return nil, "", errwrap.Wrapf("error while retrieving operation: {{err}}", err)
}

if err = w.SetOp(op); err != nil {
return nil, "", fmt.Errorf("Cannot continue, unable to use operation: %s", err)
return nil, "", errwrap.Wrapf("Cannot continue, unable to use operation: {{err}}", err)
}

if err = w.Error(); err != nil {
Expand Down Expand Up @@ -135,7 +136,7 @@ func OperationWait(w Waiter, activity string, timeoutMinutes int) error {
}
opRaw, err := c.WaitForState()
if err != nil {
return fmt.Errorf("Error waiting for %s: %s", activity, err)
return errwrap.Wrapf(fmt.Sprintf("Error waiting for %s: {{err}}", activity), err)
}

err = w.SetOp(opRaw)
Expand Down
7 changes: 4 additions & 3 deletions google/resource_dns_record_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"strings"

"github.com/hashicorp/errwrap"
"github.com/hashicorp/terraform/helper/schema"
"google.golang.org/api/dns/v1"
"net"
Expand Down Expand Up @@ -138,7 +139,7 @@ func resourceDnsRecordSetCreate(d *schema.ResourceData, meta interface{}) error
}
_, err = w.Conf().WaitForState()
if err != nil {
return fmt.Errorf("Error waiting for Google DNS change: %s", err)
return errwrap.Wrapf("Error waiting for Google DNS change: {{err}}", err)
}

return resourceDnsRecordSetRead(d, meta)
Expand Down Expand Up @@ -237,7 +238,7 @@ func resourceDnsRecordSetDelete(d *schema.ResourceData, meta interface{}) error
}
_, err = w.Conf().WaitForState()
if err != nil {
return fmt.Errorf("Error waiting for Google DNS change: %s", err)
return errwrap.Wrapf("Error waiting for Google DNS change: {{err}}", err)
}

d.SetId("")
Expand Down Expand Up @@ -298,7 +299,7 @@ func resourceDnsRecordSetUpdate(d *schema.ResourceData, meta interface{}) error
ManagedZone: zone,
}
if _, err = w.Conf().WaitForState(); err != nil {
return fmt.Errorf("Error waiting for Google DNS change: %s", err)
return errwrap.Wrapf("Error waiting for Google DNS change: {{err}}", err)
}

return resourceDnsRecordSetRead(d, meta)
Expand Down
5 changes: 3 additions & 2 deletions google/service_account_waiter.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package google

import (
"fmt"
"time"

"fmt"
"github.com/hashicorp/errwrap"
"github.com/hashicorp/terraform/helper/resource"
"google.golang.org/api/googleapi"
"google.golang.org/api/iam/v1"
Expand Down Expand Up @@ -49,7 +50,7 @@ func serviceAccountKeyWaitTime(client *iam.ProjectsServiceAccountsKeysService, k
}
_, err := c.WaitForState()
if err != nil {
return fmt.Errorf("Error waiting for %s: %s", activity, err)
return errwrap.Wrapf(fmt.Sprintf("Error waiting for %s: {{err}}", activity), err)
}

return nil
Expand Down
5 changes: 5 additions & 0 deletions google/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,11 @@ func retryTimeDuration(retryFunc func() error, duration time.Duration) error {
}

func isRetryableError(err error) bool {
// Unwrap google error if possible (for example, wrapped operation errors)
if gErr := errwrap.GetType(err, &googleapi.Error{}); gErr != nil {
err = gErr
}

if gerr, ok := err.(*googleapi.Error); ok && (gerr.Code == 429 || gerr.Code == 500 || gerr.Code == 502 || gerr.Code == 503) {
log.Printf("[DEBUG] Dismissed an error as retryable based on error code: %s", err)
return true
Expand Down
6 changes: 3 additions & 3 deletions website/docs/getting_started.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ resource "google_compute_instance" "vm_instance" {
network_interface {
# A default network is created for all GCP projects
network = "default"
access_config = {
access_config {
}
}
}
Expand Down Expand Up @@ -135,7 +135,7 @@ network_interface {
- # A default network is created for all GCP projects
- network = "default"
+ network = "${google_compute_network.vpc_network.self_link}"
access_config = {
access_config {
```

This means that when we create the VM instance, it will use
Expand Down Expand Up @@ -190,7 +190,7 @@ resource "google_compute_instance" "vm_instance" {
network_interface {
# A default network is created for all GCP projects
network = "${google_compute_network.vpc_network.self_link}"
access_config = {
access_config {
}
}
}
Expand Down

0 comments on commit 269989f

Please sign in to comment.