Skip to content

Commit

Permalink
Enable http timeout errors to be retried (hashicorp#1120)
Browse files Browse the repository at this point in the history
Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
modular-magician authored and rileykarson committed Sep 9, 2019
1 parent cda02e5 commit 4737279
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 31 deletions.
22 changes: 0 additions & 22 deletions google-beta/resource_logging_metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,6 @@ func resourceLoggingMetric() *schema.Resource {
},
},
},
"unit": {
Type: schema.TypeString,
Optional: true,
Default: "1",
},
},
},
},
Expand Down Expand Up @@ -461,8 +456,6 @@ func flattenLoggingMetricMetricDescriptor(v interface{}, d *schema.ResourceData)
return nil
}
transformed := make(map[string]interface{})
transformed["unit"] =
flattenLoggingMetricMetricDescriptorUnit(original["unit"], d)
transformed["value_type"] =
flattenLoggingMetricMetricDescriptorValueType(original["valueType"], d)
transformed["metric_kind"] =
Expand All @@ -471,10 +464,6 @@ func flattenLoggingMetricMetricDescriptor(v interface{}, d *schema.ResourceData)
flattenLoggingMetricMetricDescriptorLabels(original["labels"], d)
return []interface{}{transformed}
}
func flattenLoggingMetricMetricDescriptorUnit(v interface{}, d *schema.ResourceData) interface{} {
return v
}

func flattenLoggingMetricMetricDescriptorValueType(v interface{}, d *schema.ResourceData) interface{} {
return v
}
Expand Down Expand Up @@ -663,13 +652,6 @@ func expandLoggingMetricMetricDescriptor(v interface{}, d TerraformResourceData,
original := raw.(map[string]interface{})
transformed := make(map[string]interface{})

transformedUnit, err := expandLoggingMetricMetricDescriptorUnit(original["unit"], d, config)
if err != nil {
return nil, err
} else if val := reflect.ValueOf(transformedUnit); val.IsValid() && !isEmptyValue(val) {
transformed["unit"] = transformedUnit
}

transformedValueType, err := expandLoggingMetricMetricDescriptorValueType(original["value_type"], d, config)
if err != nil {
return nil, err
Expand All @@ -694,10 +676,6 @@ func expandLoggingMetricMetricDescriptor(v interface{}, d TerraformResourceData,
return transformed, nil
}

func expandLoggingMetricMetricDescriptorUnit(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}

func expandLoggingMetricMetricDescriptorValueType(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}
Expand Down
1 change: 0 additions & 1 deletion google-beta/resource_logging_metric_generated_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ resource "google_logging_metric" "logging_metric" {
metric_descriptor {
metric_kind = "DELTA"
value_type = "DISTRIBUTION"
unit = "1"
labels {
key = "mass"
value_type = "STRING"
Expand Down
13 changes: 12 additions & 1 deletion google-beta/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ func retryTimeDuration(retryFunc func() error, duration time.Duration, errorRetr
if err == nil {
return nil
}
for _, e := range errwrap.GetAllType(err, &googleapi.Error{}) {
for _, e := range getAllTypes(err, &googleapi.Error{}, &url.Error{}) {
if isRetryableError(e, errorRetryPredicates) {
return resource.RetryableError(e)
}
Expand All @@ -370,6 +370,17 @@ func retryTimeDuration(retryFunc func() error, duration time.Duration, errorRetr
})
}

func getAllTypes(err error, args ...interface{}) []error {
var result []error
for _, v := range args {
subResult := errwrap.GetAllType(err, v)
if subResult != nil {
result = append(result, subResult...)
}
}
return result
}

func isRetryableError(err error, retryPredicates []func(e error) (bool, string)) bool {

// These operations are always hitting googleapis.com - they should rarely
Expand Down
34 changes: 34 additions & 0 deletions google-beta/utils_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package google

import (
"net/url"
"reflect"
"strings"
"testing"
Expand Down Expand Up @@ -528,3 +529,36 @@ func TestRetryTimeDuration_noretry(t *testing.T) {
t.Errorf("expected error function to be called exactly once, but was called %d times", i)
}
}

type TimeoutError struct {
timeout bool
}

func (e *TimeoutError) Timeout() bool {
return e.timeout
}

func (e *TimeoutError) Error() string {
return "timeout error"
}

func TestRetryTimeDuration_URLTimeoutsShouldRetry(t *testing.T) {
runCount := 0
retryFunc := func() error {
runCount++
if runCount == 1 {
return &url.Error{
Err: &TimeoutError{timeout: true},
}
}
return nil
}
err := retryTimeDuration(retryFunc, 1*time.Minute)
if err != nil {
t.Errorf("unexpected error: got '%v' want 'nil'", err)
}
expectedRunCount := 2
if runCount != expectedRunCount {
t.Errorf("expected the retryFunc to be called %v time(s), instead was called %v time(s)", expectedRunCount, runCount)
}
}
7 changes: 0 additions & 7 deletions website/docs/r/logging_metric.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ resource "google_logging_metric" "logging_metric" {
metric_descriptor {
metric_kind = "DELTA"
value_type = "DISTRIBUTION"
unit = "1"
labels {
key = "mass"
value_type = "STRING"
Expand Down Expand Up @@ -134,12 +133,6 @@ The following arguments are supported:

The `metric_descriptor` block supports:

* `unit` -
(Optional)
The unit in which the metric value is reported. It is only applicable if the valueType is
`INT64`, `DOUBLE`, or `DISTRIBUTION`. The supported units are a subset of
[The Unified Code for Units of Measure](http://unitsofmeasure.org/ucum.html) standard

* `value_type` -
(Required)
Whether the measurement is an integer, a floating-point number, etc.
Expand Down

0 comments on commit 4737279

Please sign in to comment.