From 7a35d0569d3919e777e6641a451dc79f4b84454f Mon Sep 17 00:00:00 2001 From: "Adam H. Leventhal" Date: Wed, 19 Sep 2018 15:16:35 -0700 Subject: [PATCH] Issue #2384 aws_cloudwatch_log_metric_filter: default_value is automatically set to 0 --- aws/resource_aws_cloudwatch_log_metric_filter.go | 12 +++++------- ...esource_aws_cloudwatch_log_metric_filter_test.go | 9 +++++++++ aws/structure.go | 13 ++++++++----- 3 files changed, 22 insertions(+), 12 deletions(-) diff --git a/aws/resource_aws_cloudwatch_log_metric_filter.go b/aws/resource_aws_cloudwatch_log_metric_filter.go index 7abaec78f36..4a335c90cf6 100644 --- a/aws/resource_aws_cloudwatch_log_metric_filter.go +++ b/aws/resource_aws_cloudwatch_log_metric_filter.go @@ -3,6 +3,7 @@ package aws import ( "fmt" "log" + "math" "strings" "github.com/hashicorp/terraform/helper/resource" @@ -73,6 +74,7 @@ func resourceAwsCloudWatchLogMetricFilter() *schema.Resource { "default_value": { Type: schema.TypeFloat, Optional: true, + Default: math.NaN(), }, }, }, @@ -92,14 +94,10 @@ func resourceAwsCloudWatchLogMetricFilterUpdate(d *schema.ResourceData, meta int transformations := d.Get("metric_transformation").([]interface{}) o := transformations[0].(map[string]interface{}) - metricsTransformations, err := expandCloudWachLogMetricTransformations(o) - if err != nil { - return err - } - input.MetricTransformations = metricsTransformations + input.MetricTransformations = expandCloudWatchLogMetricTransformations(o) log.Printf("[DEBUG] Creating/Updating CloudWatch Log Metric Filter: %s", input) - _, err = conn.PutMetricFilter(&input) + _, err := conn.PutMetricFilter(&input) if err != nil { return fmt.Errorf("Creating/Updating CloudWatch Log Metric Filter failed: %s", err) } @@ -130,7 +128,7 @@ func resourceAwsCloudWatchLogMetricFilterRead(d *schema.ResourceData, meta inter d.Set("name", mf.FilterName) d.Set("pattern", mf.FilterPattern) - d.Set("metric_transformation", flattenCloudWachLogMetricTransformations(mf.MetricTransformations)) + d.Set("metric_transformation", flattenCloudWatchLogMetricTransformations(mf.MetricTransformations)) return nil } diff --git a/aws/resource_aws_cloudwatch_log_metric_filter_test.go b/aws/resource_aws_cloudwatch_log_metric_filter_test.go index 8a955648d81..5084472401b 100644 --- a/aws/resource_aws_cloudwatch_log_metric_filter_test.go +++ b/aws/resource_aws_cloudwatch_log_metric_filter_test.go @@ -56,6 +56,7 @@ func TestAccAWSCloudWatchLogMetricFilter_basic(t *testing.T) { MetricName: aws.String("AccessDeniedCount"), MetricNamespace: aws.String("MyNamespace"), MetricValue: aws.String("2"), + DefaultValue: aws.Float64(1), }), ), }, @@ -109,6 +110,14 @@ func testAccCheckCloudWatchLogMetricFilterTransformation(mf *cloudwatchlogs.Metr *expected.MetricValue, *given.MetricValue) } + if (given.DefaultValue != nil) != (expected.DefaultValue != nil) { + return fmt.Errorf("Expected default value to be present: %t, received: %t", + expected.DefaultValue != nil, given.DefaultValue != nil) + } else if (given.DefaultValue != nil) && *given.DefaultValue != *expected.DefaultValue { + return fmt.Errorf("Expected metric value: %g, received: %g", + *expected.DefaultValue, *given.DefaultValue) + } + return nil } } diff --git a/aws/structure.go b/aws/structure.go index 266da93e6b1..bf4109d994e 100644 --- a/aws/structure.go +++ b/aws/structure.go @@ -4,6 +4,7 @@ import ( "bytes" "encoding/json" "fmt" + "math" "reflect" "regexp" "sort" @@ -1777,21 +1778,21 @@ func expandApiGatewayStageKeyOperations(d *schema.ResourceData) []*apigateway.Pa return operations } -func expandCloudWachLogMetricTransformations(m map[string]interface{}) ([]*cloudwatchlogs.MetricTransformation, error) { +func expandCloudWatchLogMetricTransformations(m map[string]interface{}) []*cloudwatchlogs.MetricTransformation { transformation := cloudwatchlogs.MetricTransformation{ MetricName: aws.String(m["name"].(string)), MetricNamespace: aws.String(m["namespace"].(string)), MetricValue: aws.String(m["value"].(string)), } - if m["default_value"] != "" { + if !math.IsNaN(m["default_value"].(float64)) { transformation.DefaultValue = aws.Float64(m["default_value"].(float64)) } - return []*cloudwatchlogs.MetricTransformation{&transformation}, nil + return []*cloudwatchlogs.MetricTransformation{&transformation} } -func flattenCloudWachLogMetricTransformations(ts []*cloudwatchlogs.MetricTransformation) []interface{} { +func flattenCloudWatchLogMetricTransformations(ts []*cloudwatchlogs.MetricTransformation) []interface{} { mts := make([]interface{}, 0) m := make(map[string]interface{}, 0) @@ -1799,7 +1800,9 @@ func flattenCloudWachLogMetricTransformations(ts []*cloudwatchlogs.MetricTransfo m["namespace"] = *ts[0].MetricNamespace m["value"] = *ts[0].MetricValue - if ts[0].DefaultValue != nil { + if ts[0].DefaultValue == nil { + m["default_value"] = math.NaN() + } else { m["default_value"] = *ts[0].DefaultValue }