Skip to content

Commit

Permalink
r/aws_cloudwatch_metric_alarm: Support optional datapoints_to_alarm c…
Browse files Browse the repository at this point in the history
…onfiguration (#2609)

* r/aws_cloudwatch_metric_alarm: Support optional datapoints_to_alarm configuration

* r/aws_cloudwatch_metric_alarm: Fix testing go vet error with fmt.Sprintf typing

* r/aws_cloudwatch_metric_alarm: #2609 PR feedback

* Use rInt and fix indentation in new datapoints_to_alarm test for consistency
* Remove extraneous period in datapoints_to_alarm documentation
  • Loading branch information
bflad authored and Ninir committed Jan 3, 2018
1 parent 97b0ce3 commit 4e8f6e5
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
9 changes: 9 additions & 0 deletions aws/resource_aws_cloudwatch_metric_alarm.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ func resourceAwsCloudWatchMetricAlarm() *schema.Resource {
Type: schema.TypeString,
Optional: true,
},
"datapoints_to_alarm": {
Type: schema.TypeInt,
Optional: true,
},
"dimensions": {
Type: schema.TypeMap,
Optional: true,
Expand Down Expand Up @@ -158,6 +162,7 @@ func resourceAwsCloudWatchMetricAlarmRead(d *schema.ResourceData, meta interface
d.Set("alarm_description", a.AlarmDescription)
d.Set("alarm_name", a.AlarmName)
d.Set("comparison_operator", a.ComparisonOperator)
d.Set("datapoints_to_alarm", a.DatapointsToAlarm)
if err := d.Set("dimensions", flattenDimensions(a.Dimensions)); err != nil {
return err
}
Expand Down Expand Up @@ -243,6 +248,10 @@ func getAwsCloudWatchPutMetricAlarmInput(d *schema.ResourceData) cloudwatch.PutM
params.AlarmDescription = aws.String(v.(string))
}

if v, ok := d.GetOk("datapoints_to_alarm"); ok {
params.DatapointsToAlarm = aws.Int64(int64(v.(int)))
}

if v, ok := d.GetOk("unit"); ok {
params.Unit = aws.String(v.(string))
}
Expand Down
40 changes: 40 additions & 0 deletions aws/resource_aws_cloudwatch_metric_alarm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,26 @@ func TestAccAWSCloudWatchMetricAlarm_basic(t *testing.T) {
})
}

func TestAccAWSCloudWatchMetricAlarm_datapointsToAlarm(t *testing.T) {
var alarm cloudwatch.MetricAlarm
rInt := acctest.RandInt()

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSCloudWatchMetricAlarmDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSCloudWatchMetricAlarmConfigDatapointsToAlarm(rInt),
Check: resource.ComposeTestCheckFunc(
testAccCheckCloudWatchMetricAlarmExists("aws_cloudwatch_metric_alarm.foobar", &alarm),
resource.TestCheckResourceAttr("aws_cloudwatch_metric_alarm.foobar", "datapoints_to_alarm", "2"),
),
},
},
})
}

func TestAccAWSCloudWatchMetricAlarm_treatMissingData(t *testing.T) {
var alarm cloudwatch.MetricAlarm
rInt := acctest.RandInt()
Expand Down Expand Up @@ -209,6 +229,26 @@ resource "aws_cloudwatch_metric_alarm" "foobar" {
}`, rInt)
}

func testAccAWSCloudWatchMetricAlarmConfigDatapointsToAlarm(rInt int) string {
return fmt.Sprintf(`
resource "aws_cloudwatch_metric_alarm" "foobar" {
alarm_name = "terraform-test-foobar%d"
comparison_operator = "GreaterThanOrEqualToThreshold"
datapoints_to_alarm = "2"
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"
}
}`, rInt)
}

func testAccAWSCloudWatchMetricAlarmConfigTreatMissingData(rInt int) string {
return fmt.Sprintf(`
resource "aws_cloudwatch_metric_alarm" "foobar" {
Expand Down
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 @@ -81,6 +81,7 @@ The following arguments are supported:
* `actions_enabled` - (Optional) Indicates whether or not actions should be executed during any changes to the alarm's state. Defaults to `true`.
* `alarm_actions` - (Optional) The list of actions to execute when this alarm transitions into an ALARM state from any other state. Each action is specified as an Amazon Resource Number (ARN).
* `alarm_description` - (Optional) The description for the alarm.
* `datapoints_to_alarm` - (Optional) The number of datapoints that must be breaching to trigger the alarm.
* `dimensions` - (Optional) The dimensions for the alarm's associated metric. For the list of available dimensions see the AWS documentation [here](http://docs.aws.amazon.com/AmazonCloudWatch/latest/DeveloperGuide/CW_Support_For_AWS.html).
* `insufficient_data_actions` - (Optional) The list of actions to execute when this alarm transitions into an INSUFFICIENT_DATA state from any other state. Each action is specified as an Amazon Resource Number (ARN).
* `ok_actions` - (Optional) The list of actions to execute when this alarm transitions into an OK state from any other state. Each action is specified as an Amazon Resource Number (ARN).
Expand Down

0 comments on commit 4e8f6e5

Please sign in to comment.