Skip to content

Commit

Permalink
Issue hashicorp#2384 aws_cloudwatch_log_metric_filter: default_value …
Browse files Browse the repository at this point in the history
…is automatically set to 0
  • Loading branch information
Adam H. Leventhal committed Sep 19, 2018
1 parent 50ccc18 commit 7a35d05
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 12 deletions.
12 changes: 5 additions & 7 deletions aws/resource_aws_cloudwatch_log_metric_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package aws
import (
"fmt"
"log"
"math"
"strings"

"github.com/hashicorp/terraform/helper/resource"
Expand Down Expand Up @@ -73,6 +74,7 @@ func resourceAwsCloudWatchLogMetricFilter() *schema.Resource {
"default_value": {
Type: schema.TypeFloat,
Optional: true,
Default: math.NaN(),
},
},
},
Expand All @@ -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)
}
Expand Down Expand Up @@ -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
}
Expand Down
9 changes: 9 additions & 0 deletions aws/resource_aws_cloudwatch_log_metric_filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}),
),
},
Expand Down Expand Up @@ -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
}
}
Expand Down
13 changes: 8 additions & 5 deletions aws/structure.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"encoding/json"
"fmt"
"math"
"reflect"
"regexp"
"sort"
Expand Down Expand Up @@ -1777,29 +1778,31 @@ 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)

m["name"] = *ts[0].MetricName
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
}

Expand Down

0 comments on commit 7a35d05

Please sign in to comment.