Skip to content

Commit

Permalink
provider/aws: Add Support for aws_cloudwatch_metric_alarm extended
Browse files Browse the repository at this point in the history
statistic

Fixes: #11189

This introduces a new parameter and makes an existing parameter from
`required` to `optional` as both cannot be specified together

```
% make testacc TEST=./builtin/providers/aws TESTARGS='-run=TestAccAWSCloudWatchMetricAlarm_' 2 ↵ ✹ ✭
==> Checking that code complies with gofmt requirements...
go generate $(go list ./... | grep -v /terraform/vendor/)
2017/01/13 11:25:24 Generated command/internal_plugin_list.go
TF_ACC=1 go test ./builtin/providers/aws -v -run=TestAccAWSCloudWatchMetricAlarm_ -timeout 120m
=== RUN   TestAccAWSCloudWatchMetricAlarm_importBasic
--- PASS: TestAccAWSCloudWatchMetricAlarm_importBasic (19.80s)
=== RUN   TestAccAWSCloudWatchMetricAlarm_basic
--- PASS: TestAccAWSCloudWatchMetricAlarm_basic (20.42s)
=== RUN   TestAccAWSCloudWatchMetricAlarm_extendedStatistic
--- PASS: TestAccAWSCloudWatchMetricAlarm_extendedStatistic (18.92s)
PASS
```
  • Loading branch information
stack72 committed Jan 13, 2017
1 parent bad2000 commit 420b7b4
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 20 deletions.
46 changes: 29 additions & 17 deletions builtin/providers/aws/resource_aws_cloudwatch_metric_alarm.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,71 +21,75 @@ func resourceAwsCloudWatchMetricAlarm() *schema.Resource {
},

Schema: map[string]*schema.Schema{
"alarm_name": &schema.Schema{
"alarm_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"comparison_operator": &schema.Schema{
"comparison_operator": {
Type: schema.TypeString,
Required: true,
},
"evaluation_periods": &schema.Schema{
"evaluation_periods": {
Type: schema.TypeInt,
Required: true,
},
"metric_name": &schema.Schema{
"metric_name": {
Type: schema.TypeString,
Required: true,
},
"namespace": &schema.Schema{
"namespace": {
Type: schema.TypeString,
Required: true,
},
"period": &schema.Schema{
"period": {
Type: schema.TypeInt,
Required: true,
},
"statistic": &schema.Schema{
"statistic": {
Type: schema.TypeString,
Required: true,
Optional: true,
},
"threshold": &schema.Schema{
"threshold": {
Type: schema.TypeFloat,
Required: true,
},
"actions_enabled": &schema.Schema{
"actions_enabled": {
Type: schema.TypeBool,
Optional: true,
Default: true,
},
"alarm_actions": &schema.Schema{
"alarm_actions": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
},
"alarm_description": &schema.Schema{
"alarm_description": {
Type: schema.TypeString,
Optional: true,
},
"dimensions": &schema.Schema{
"dimensions": {
Type: schema.TypeMap,
Optional: true,
},
"insufficient_data_actions": &schema.Schema{
"insufficient_data_actions": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
},
"ok_actions": &schema.Schema{
"ok_actions": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
},
"unit": &schema.Schema{
"unit": {
Type: schema.TypeString,
Optional: true,
},
"extended_statistic": {
Type: schema.TypeString,
Optional: true,
},
Expand Down Expand Up @@ -147,6 +151,7 @@ func resourceAwsCloudWatchMetricAlarmRead(d *schema.ResourceData, meta interface
d.Set("statistic", a.Statistic)
d.Set("threshold", a.Threshold)
d.Set("unit", a.Unit)
d.Set("extended_statistic", a.ExtendedStatistic)

return nil
}
Expand Down Expand Up @@ -199,7 +204,6 @@ func getAwsCloudWatchPutMetricAlarmInput(d *schema.ResourceData) cloudwatch.PutM
MetricName: aws.String(d.Get("metric_name").(string)),
Namespace: aws.String(d.Get("namespace").(string)),
Period: aws.Int64(int64(d.Get("period").(int))),
Statistic: aws.String(d.Get("statistic").(string)),
Threshold: aws.Float64(d.Get("threshold").(float64)),
}

Expand All @@ -215,6 +219,14 @@ func getAwsCloudWatchPutMetricAlarmInput(d *schema.ResourceData) cloudwatch.PutM
params.Unit = aws.String(v.(string))
}

if v, ok := d.GetOk("statistic"); ok {
params.Statistic = aws.String(v.(string))
}

if v, ok := d.GetOk("extended_statistic"); ok {
params.ExtendedStatistic = aws.String(v.(string))
}

var alarmActions []*string
if v := d.Get("alarm_actions"); v != nil {
for _, v := range v.(*schema.Set).List() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func TestAccAWSCloudWatchMetricAlarm_basic(t *testing.T) {
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSCloudWatchMetricAlarmDestroy,
Steps: []resource.TestStep{
resource.TestStep{
{
Config: testAccAWSCloudWatchMetricAlarmConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckCloudWatchMetricAlarmExists("aws_cloudwatch_metric_alarm.foobar", &alarm),
Expand All @@ -32,6 +32,25 @@ func TestAccAWSCloudWatchMetricAlarm_basic(t *testing.T) {
})
}

func TestAccAWSCloudWatchMetricAlarm_extendedStatistic(t *testing.T) {
var alarm cloudwatch.MetricAlarm

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

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

var testAccAWSCloudWatchMetricAlarmConfigExtendedStatistic = fmt.Sprintf(`
resource "aws_cloudwatch_metric_alarm" "foobar" {
alarm_name = "terraform-test-foobar6"
comparison_operator = "GreaterThanOrEqualToThreshold"
evaluation_periods = "2"
metric_name = "CPUUtilization"
namespace = "AWS/EC2"
period = "120"
extended_statistic = "p88.0"
threshold = "80"
alarm_description = "This metric monitors ec2 cpu utilization"
insufficient_data_actions = []
dimensions {
InstanceId = "i-abc123"
}
}
`)
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ The following arguments are supported:
* `namespace` - (Required) The namespace for the alarm's associated metric. See docs for the [list of namespaces](https://docs.aws.amazon.com/AmazonCloudWatch/latest/DeveloperGuide/aws-namespaces.html).
See docs for [supported metrics](https://docs.aws.amazon.com/AmazonCloudWatch/latest/DeveloperGuide/CW_Support_For_AWS.html).
* `period` - (Required) The period in seconds over which the specified `statistic` is applied.
* `statistic` - (Required) The statistic to apply to the alarm's associated metric.
* `statistic` - (Optional) The statistic to apply to the alarm's associated metric.
Either of the following is supported: `SampleCount`, `Average`, `Sum`, `Minimum`, `Maximum`
* `threshold` - (Required) The value against which the specified statistic is compared.
* `actions_enabled` - (Optional) Indicates whether or not actions should be executed during any changes to the alarm's state. Defaults to `true`.
Expand All @@ -77,6 +77,7 @@ The following arguments are supported:
* `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).
* `unit` - (Optional) The unit for the alarm's associated metric.
* `extended_statistic` - (Optional) The percentile statistic for the metric associated with the alarm. Specify a value between p0.0 and p100. You cannot create a metric alarm made of both `statistic` and `extended_statistic` parameters.

## Attributes Reference

Expand All @@ -85,7 +86,6 @@ The following attributes are exported:
* `id` - The ID of the health check



## Import

Cloud Metric Alarms can be imported using the `alarm_name`, e.g.
Expand Down

0 comments on commit 420b7b4

Please sign in to comment.