From cfeeb5435d674a897c35b7accb164c4af569bd1a Mon Sep 17 00:00:00 2001 From: Riley Karson Date: Fri, 6 Sep 2019 16:59:23 +0000 Subject: [PATCH] Enable http timeout errors to be retried Signed-off-by: Modular Magician --- google/resource_logging_metric.go | 22 ------------ .../resource_logging_metric_generated_test.go | 1 - google/utils.go | 13 ++++++- google/utils_test.go | 34 +++++++++++++++++++ website/docs/r/logging_metric.html.markdown | 7 ---- 5 files changed, 46 insertions(+), 31 deletions(-) diff --git a/google/resource_logging_metric.go b/google/resource_logging_metric.go index bf641dc5a06..f2c6dbe2447 100644 --- a/google/resource_logging_metric.go +++ b/google/resource_logging_metric.go @@ -85,11 +85,6 @@ func resourceLoggingMetric() *schema.Resource { }, }, }, - "unit": { - Type: schema.TypeString, - Optional: true, - Default: "1", - }, }, }, }, @@ -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"] = @@ -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 } @@ -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 @@ -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 } diff --git a/google/resource_logging_metric_generated_test.go b/google/resource_logging_metric_generated_test.go index 91a2a1b2960..ca7407138b6 100644 --- a/google/resource_logging_metric_generated_test.go +++ b/google/resource_logging_metric_generated_test.go @@ -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" diff --git a/google/utils.go b/google/utils.go index a89207838ad..a2d154fb4f7 100644 --- a/google/utils.go +++ b/google/utils.go @@ -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) } @@ -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 diff --git a/google/utils_test.go b/google/utils_test.go index 25f3c11d5fc..87f5c5051e2 100644 --- a/google/utils_test.go +++ b/google/utils_test.go @@ -1,6 +1,7 @@ package google import ( + "net/url" "reflect" "strings" "testing" @@ -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) + } +} diff --git a/website/docs/r/logging_metric.html.markdown b/website/docs/r/logging_metric.html.markdown index 3a859be939c..7942092c421 100644 --- a/website/docs/r/logging_metric.html.markdown +++ b/website/docs/r/logging_metric.html.markdown @@ -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" @@ -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.