Skip to content

Commit

Permalink
Add tags attribute for aws_cloudwatch_metric_alarm
Browse files Browse the repository at this point in the history
  • Loading branch information
teraken0509 committed Apr 4, 2019
1 parent 1ec9034 commit 749bee2
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 0 deletions.
12 changes: 12 additions & 0 deletions aws/resource_aws_cloudwatch_metric_alarm.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,8 @@ func resourceAwsCloudWatchMetricAlarm() *schema.Resource {
Computed: true,
ValidateFunc: validation.StringInSlice([]string{"evaluate", "ignore"}, true),
},

"tags": tagsSchema(),
},
}
}
Expand Down Expand Up @@ -310,6 +312,10 @@ func resourceAwsCloudWatchMetricAlarmRead(d *schema.ResourceData, meta interface
d.Set("treat_missing_data", a.TreatMissingData)
d.Set("evaluate_low_sample_count_percentiles", a.EvaluateLowSampleCountPercentile)

if err := saveTagsCloudWatch(meta.(*AWSClient).cloudwatchconn, d, aws.StringValue(a.AlarmArn)); err != nil {
return fmt.Errorf("error setting tags: %s", err)
}

return nil
}

Expand All @@ -324,6 +330,11 @@ func resourceAwsCloudWatchMetricAlarmUpdate(d *schema.ResourceData, meta interfa
}
log.Println("[INFO] CloudWatch Metric Alarm updated")

// Tags are cannot update by PutMetricAlarm.
if err := setTagsCloudWatch(conn, d, d.Get("arn").(string)); err != nil {
return fmt.Errorf("error updating tags for %s: %s", d.Id(), err)
}

return resourceAwsCloudWatchMetricAlarmRead(d, meta)
}

Expand Down Expand Up @@ -359,6 +370,7 @@ func getAwsCloudWatchPutMetricAlarmInput(d *schema.ResourceData) cloudwatch.PutM
EvaluationPeriods: aws.Int64(int64(d.Get("evaluation_periods").(int))),
Threshold: aws.Float64(d.Get("threshold").(float64)),
TreatMissingData: aws.String(d.Get("treat_missing_data").(string)),
Tags: tagsFromMapCloudWatch(d.Get("tags").(map[string]interface{})),
}

if v := d.Get("actions_enabled"); v != nil {
Expand Down
121 changes: 121 additions & 0 deletions aws/resource_aws_cloudwatch_metric_alarm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,55 @@ func TestAccAWSCloudWatchMetricAlarm_missingStatistic(t *testing.T) {
})
}

func TestAccAWSCloudWatchMetricAlarm_tags(t *testing.T) {
var alarm cloudwatch.MetricAlarm
resourceName := "aws_cloudwatch_metric_alarm.foobar"
rInt := acctest.RandInt()

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSCloudWatchMetricAlarmDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSCloudWatchMetricAlarmConfigTags(rInt),
Check: resource.ComposeTestCheckFunc(
testAccCheckCloudWatchMetricAlarmExists(resourceName, &alarm),
resource.TestCheckResourceAttr(resourceName, "tags.%", "3"),
resource.TestCheckResourceAttr(resourceName, "tags.Name", fmt.Sprintf("terraform-test-foobar%d", rInt)),
resource.TestCheckResourceAttr(resourceName, "tags.fizz", "buzz"),
resource.TestCheckResourceAttr(resourceName, "tags.foo", "bar"),
),
},
{
Config: testAccAWSCloudWatchMetricAlarmConfigUpdateTags(rInt),
Check: resource.ComposeTestCheckFunc(
testAccCheckCloudWatchMetricAlarmExists(resourceName, &alarm),
resource.TestCheckResourceAttr(resourceName, "tags.%", "4"),
resource.TestCheckResourceAttr(resourceName, "tags.Name", fmt.Sprintf("terraform-test-foobar%d", rInt)),
resource.TestCheckResourceAttr(resourceName, "tags.fizz", "buzz"),
resource.TestCheckResourceAttr(resourceName, "tags.foo", "bar2"),
resource.TestCheckResourceAttr(resourceName, "tags.good", "bad"),
),
},
{
Config: testAccAWSCloudWatchMetricAlarmConfigRemoveTags(rInt),
Check: resource.ComposeTestCheckFunc(
testAccCheckCloudWatchMetricAlarmExists(resourceName, &alarm),
resource.TestCheckResourceAttr(resourceName, "tags.%", "2"),
resource.TestCheckResourceAttr(resourceName, "tags.Name", fmt.Sprintf("terraform-test-foobar%d", rInt)),
resource.TestCheckResourceAttr(resourceName, "tags.fizz", "buzz"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testAccCheckCloudWatchMetricAlarmDimension(n, k, v string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
Expand Down Expand Up @@ -771,3 +820,75 @@ resource "aws_cloudwatch_metric_alarm" "test" {
}
`, rName)
}

func testAccAWSCloudWatchMetricAlarmConfigTags(rInt int) string {
return fmt.Sprintf(`
resource "aws_cloudwatch_metric_alarm" "foobar" {
alarm_name = "terraform-test-foobar%[1]d"
comparison_operator = "GreaterThanOrEqualToThreshold"
evaluation_periods = "2"
metric_name = "CPUUtilization"
namespace = "AWS/EC2"
period = "120"
statistic = "Average"
threshold = "80"
alarm_description = "This metric monitors ec2 cpu utilization"
insufficient_data_actions = []
dimensions = {
InstanceId = "i-abc123"
}
tags = {
Name = "terraform-test-foobar%[1]d"
fizz = "buzz"
foo = "bar"
}
}`, rInt)
}

func testAccAWSCloudWatchMetricAlarmConfigUpdateTags(rInt int) string {
return fmt.Sprintf(`
resource "aws_cloudwatch_metric_alarm" "foobar" {
alarm_name = "terraform-test-foobar%[1]d"
comparison_operator = "GreaterThanOrEqualToThreshold"
evaluation_periods = "2"
metric_name = "CPUUtilization"
namespace = "AWS/EC2"
period = "120"
statistic = "Average"
threshold = "80"
alarm_description = "This metric monitors ec2 cpu utilization"
insufficient_data_actions = []
dimensions = {
InstanceId = "i-abc123"
}
tags = {
Name = "terraform-test-foobar%[1]d"
fizz = "buzz"
foo = "bar2"
good = "bad"
}
}`, rInt)
}

func testAccAWSCloudWatchMetricAlarmConfigRemoveTags(rInt int) string {
return fmt.Sprintf(`
resource "aws_cloudwatch_metric_alarm" "foobar" {
alarm_name = "terraform-test-foobar%[1]d"
comparison_operator = "GreaterThanOrEqualToThreshold"
evaluation_periods = "2"
metric_name = "CPUUtilization"
namespace = "AWS/EC2"
period = "120"
statistic = "Average"
threshold = "80"
alarm_description = "This metric monitors ec2 cpu utilization"
insufficient_data_actions = []
dimensions = {
InstanceId = "i-abc123"
}
tags = {
Name = "terraform-test-foobar%[1]d"
fizz = "buzz"
}
}`, rInt)
}
1 change: 1 addition & 0 deletions website/docs/r/cloudwatch_metric_alarm.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ If you specify `evaluate` or omit this parameter, the alarm will always be
evaluated and possibly change state no matter how many data points are available.
The following values are supported: `ignore`, and `evaluate`.
* `metric_query` (Optional) Enables you to create an alarm based on a metric math expression. You may specify at most 20.
* `tags` - (Optional) A mapping of tags to assign to the resource.

~> **NOTE:** If you specify at least one `metric_query`, you may not specify a `metric_name`, `namespace`, `period` or `statistic`. If you do not specify a `metric_query`, you must specify each of these (although you may use `extended_statistic` instead of `statistic`).

Expand Down

0 comments on commit 749bee2

Please sign in to comment.