Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix: allow updates to metric_query on aws_cloudwatch_metric_alarm #8085

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion aws/resource_aws_cloudwatch_metric_alarm.go
Original file line number Diff line number Diff line change
Expand Up @@ -422,8 +422,12 @@ func getAwsCloudWatchPutMetricAlarmInput(d *schema.ResourceData) cloudwatch.PutM
if v := d.Get("metric_query"); v != nil {
for _, v := range v.(*schema.Set).List() {
metricQueryResource := v.(map[string]interface{})
id := metricQueryResource["id"].(string)
if id == "" {
continue
}
metricQuery := cloudwatch.MetricDataQuery{
Id: aws.String(metricQueryResource["id"].(string)),
Id: aws.String(id),
}
if v, ok := metricQueryResource["expression"]; ok && v.(string) != "" {
metricQuery.Expression = aws.String(v.(string))
Expand Down
45 changes: 45 additions & 0 deletions aws/resource_aws_cloudwatch_metric_alarm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,20 @@ func TestAccAWSCloudWatchMetricAlarm_expression(t *testing.T) {
resource.TestCheckResourceAttr("aws_cloudwatch_metric_alarm.foobar", "metric_query.#", "3"),
),
},
{
Config: testAccAWSCloudWatchMetricAlarmConfigWithExpression(rInt),
Check: resource.ComposeTestCheckFunc(
testAccCheckCloudWatchMetricAlarmExists("aws_cloudwatch_metric_alarm.foobar", &alarm),
resource.TestCheckResourceAttr("aws_cloudwatch_metric_alarm.foobar", "metric_query.#", "2"),
),
},
{
Config: testAccAWSCloudWatchMetricAlarmConfigWithExpressionWithQueryUpdated(rInt),
Check: resource.ComposeTestCheckFunc(
testAccCheckCloudWatchMetricAlarmExists("aws_cloudwatch_metric_alarm.foobar", &alarm),
resource.TestCheckResourceAttr("aws_cloudwatch_metric_alarm.foobar", "metric_query.#", "2"),
),
},
{
ResourceName: "aws_cloudwatch_metric_alarm.foobar",
ImportState: true,
Expand Down Expand Up @@ -577,6 +591,37 @@ resource "aws_cloudwatch_metric_alarm" "foobar" {
}`, rInt)
}

func testAccAWSCloudWatchMetricAlarmConfigWithExpressionWithQueryUpdated(rInt int) string {
return fmt.Sprintf(`
resource "aws_cloudwatch_metric_alarm" "foobar" {
alarm_name = "terraform-test-foobar%d"
comparison_operator = "GreaterThanOrEqualToThreshold"
evaluation_periods = "2"
threshold = "80"
alarm_description = "This metric monitors ec2 cpu utilization"
insufficient_data_actions = []
metric_query {
id = "e1"
expression = "m1"
label = "cat"
return_data = "true"
}
metric_query {
id = "m1"
metric {
metric_name = "CPUUtilization"
namespace = "AWS/EC2"
period = "120"
stat = "Maximum"
unit = "Count"
dimensions = {
InstanceId = "i-abc123"
}
}
}
}`, rInt)
}

func testAccAWSCloudWatchMetricAlarmConfigWithBadExpression(rInt int) string {
return fmt.Sprintf(`
resource "aws_cloudwatch_metric_alarm" "foobar" {
Expand Down