Skip to content

Commit

Permalink
Merge pull request #1578 from stack72/b-aws-logs-filter-1563
Browse files Browse the repository at this point in the history
resource/aws_cloudwatch_log_metric_filter: Add support for DefaultValue
  • Loading branch information
Ninir authored Oct 30, 2017
2 parents cf4e1ba + f955381 commit 2d165c7
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 11 deletions.
26 changes: 17 additions & 9 deletions aws/resource_aws_cloudwatch_log_metric_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ func resourceAwsCloudWatchLogMetricFilter() *schema.Resource {
Delete: resourceAwsCloudWatchLogMetricFilterDelete,

Schema: map[string]*schema.Schema{
"name": &schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateLogMetricFilterName,
},

"pattern": &schema.Schema{
"pattern": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validateMaxLength(512),
Expand All @@ -41,34 +41,38 @@ func resourceAwsCloudWatchLogMetricFilter() *schema.Resource {
},
},

"log_group_name": &schema.Schema{
"log_group_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateLogGroupName,
},

"metric_transformation": &schema.Schema{
"metric_transformation": {
Type: schema.TypeList,
Required: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": &schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validateLogMetricFilterTransformationName,
},
"namespace": &schema.Schema{
"namespace": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validateLogMetricFilterTransformationName,
},
"value": &schema.Schema{
"value": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validateMaxLength(100),
},
"default_value": {
Type: schema.TypeFloat,
Optional: true,
},
},
},
},
Expand All @@ -87,10 +91,14 @@ func resourceAwsCloudWatchLogMetricFilterUpdate(d *schema.ResourceData, meta int

transformations := d.Get("metric_transformation").([]interface{})
o := transformations[0].(map[string]interface{})
input.MetricTransformations = expandCloudWachLogMetricTransformations(o)
metricsTransformations, err := expandCloudWachLogMetricTransformations(o)
if err != nil {
return err
}
input.MetricTransformations = metricsTransformations

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
2 changes: 2 additions & 0 deletions aws/resource_aws_cloudwatch_log_metric_filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func TestAccAWSCloudWatchLogMetricFilter_basic(t *testing.T) {
resource.TestCheckResourceAttr("aws_cloudwatch_log_metric_filter.foobar", "metric_transformation.0.name", "AccessDeniedCount"),
resource.TestCheckResourceAttr("aws_cloudwatch_log_metric_filter.foobar", "metric_transformation.0.namespace", "MyNamespace"),
resource.TestCheckResourceAttr("aws_cloudwatch_log_metric_filter.foobar", "metric_transformation.0.value", "2"),
resource.TestCheckResourceAttr("aws_cloudwatch_log_metric_filter.foobar", "metric_transformation.0.default_value", "1"),
testAccCheckCloudWatchLogMetricFilterTransformation(&mf, &cloudwatchlogs.MetricTransformation{
MetricName: aws.String("AccessDeniedCount"),
MetricNamespace: aws.String("MyNamespace"),
Expand Down Expand Up @@ -181,6 +182,7 @@ PATTERN
name = "AccessDeniedCount"
namespace = "MyNamespace"
value = "2"
default_value = "1"
}
}
Expand Down
12 changes: 10 additions & 2 deletions aws/structure.go
Original file line number Diff line number Diff line change
Expand Up @@ -1503,14 +1503,18 @@ func expandApiGatewayStageKeyOperations(d *schema.ResourceData) []*apigateway.Pa
return operations
}

func expandCloudWachLogMetricTransformations(m map[string]interface{}) []*cloudwatchlogs.MetricTransformation {
func expandCloudWachLogMetricTransformations(m map[string]interface{}) ([]*cloudwatchlogs.MetricTransformation, error) {
transformation := cloudwatchlogs.MetricTransformation{
MetricName: aws.String(m["name"].(string)),
MetricNamespace: aws.String(m["namespace"].(string)),
MetricValue: aws.String(m["value"].(string)),
}

return []*cloudwatchlogs.MetricTransformation{&transformation}
if m["default_value"] != "" {
transformation.DefaultValue = aws.Float64(m["default_value"].(float64))
}

return []*cloudwatchlogs.MetricTransformation{&transformation}, nil
}

func flattenCloudWachLogMetricTransformations(ts []*cloudwatchlogs.MetricTransformation) []interface{} {
Expand All @@ -1521,6 +1525,10 @@ func flattenCloudWachLogMetricTransformations(ts []*cloudwatchlogs.MetricTransfo
m["namespace"] = *ts[0].MetricNamespace
m["value"] = *ts[0].MetricValue

if ts[0].DefaultValue != nil {
m["default_value"] = *ts[0].DefaultValue
}

mts = append(mts, m)

return mts
Expand Down
1 change: 1 addition & 0 deletions website/docs/r/cloudwatch_log_metric_filter.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ The `metric_transformation` block supports the following arguments:
* `name` - (Required) The name of the CloudWatch metric to which the monitored log information should be published (e.g. `ErrorCount`)
* `namespace` - (Required) The destination namespace of the CloudWatch metric.
* `value` - (Required) What to publish to the metric. For example, if you're counting the occurrences of a particular term like "Error", the value will be "1" for each occurrence. If you're counting the bytes transferred the published value will be the value in the log event.
* `default_value` - (Optional) The value to emit when a filter pattern does not match a log event.

## Attributes Reference

Expand Down

0 comments on commit 2d165c7

Please sign in to comment.