Skip to content

Commit

Permalink
resource/aws_cloudwatch_metric_alarm: Refactor to use keyvaluetags pa…
Browse files Browse the repository at this point in the history
…ckage and common test practices (#11372)

Output from acceptance testing:

```
--- PASS: TestAccAWSCloudWatchMetricAlarm_missingStatistic (6.51s)
--- PASS: TestAccAWSCloudWatchMetricAlarm_datapointsToAlarm (10.68s)
--- PASS: TestAccAWSCloudWatchMetricAlarm_extendedStatistic (11.03s)
--- PASS: TestAccAWSCloudWatchMetricAlarm_basic (11.76s)
--- PASS: TestAccAWSCloudWatchMetricAlarm_AlarmActions_SNSTopic (11.92s)
--- PASS: TestAccAWSCloudWatchMetricAlarm_AlarmActions_SWFAction (13.40s)
--- PASS: TestAccAWSCloudWatchMetricAlarm_treatMissingData (16.07s)
--- PASS: TestAccAWSCloudWatchMetricAlarm_evaluateLowSampleCountPercentiles (16.61s)
--- PASS: TestAccAWSCloudWatchMetricAlarm_tags (21.31s)
--- PASS: TestAccAWSCloudWatchMetricAlarm_expression (34.27s)
--- PASS: TestAccAWSCloudWatchMetricAlarm_AlarmActions_EC2Automate (119.43s)
```
  • Loading branch information
DrFaust92 authored and bflad committed Dec 19, 2019
1 parent 799e1f5 commit 224ce1a
Show file tree
Hide file tree
Showing 4 changed files with 181 additions and 409 deletions.
83 changes: 48 additions & 35 deletions aws/resource_aws_cloudwatch_metric_alarm.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import (
"fmt"
"log"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/cloudwatch"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/keyvaluetags"
)

func resourceAwsCloudWatchMetricAlarm() *schema.Resource {
Expand Down Expand Up @@ -249,41 +249,44 @@ func resourceAwsCloudWatchMetricAlarmCreate(d *schema.ResourceData, meta interfa
}

func resourceAwsCloudWatchMetricAlarmRead(d *schema.ResourceData, meta interface{}) error {
a, err := getAwsCloudWatchMetricAlarm(d, meta)
conn := meta.(*AWSClient).cloudwatchconn

resp, err := getAwsCloudWatchMetricAlarm(d, meta)
if err != nil {
return err
}
if a == nil {
if resp == nil {
d.SetId("")
return nil
}

log.Printf("[DEBUG] Reading CloudWatch Metric Alarm: %s", d.Get("alarm_name"))

d.Set("actions_enabled", a.ActionsEnabled)
d.Set("actions_enabled", resp.ActionsEnabled)

if err := d.Set("alarm_actions", _strArrPtrToList(a.AlarmActions)); err != nil {
if err := d.Set("alarm_actions", _strArrPtrToList(resp.AlarmActions)); err != nil {
log.Printf("[WARN] Error setting Alarm Actions: %s", err)
}
d.Set("alarm_description", a.AlarmDescription)
d.Set("alarm_name", a.AlarmName)
d.Set("arn", a.AlarmArn)
d.Set("comparison_operator", a.ComparisonOperator)
d.Set("datapoints_to_alarm", a.DatapointsToAlarm)
if err := d.Set("dimensions", flattenDimensions(a.Dimensions)); err != nil {
arn := *resp.AlarmArn
d.Set("alarm_description", resp.AlarmDescription)
d.Set("alarm_name", resp.AlarmName)
d.Set("arn", arn)
d.Set("comparison_operator", resp.ComparisonOperator)
d.Set("datapoints_to_alarm", resp.DatapointsToAlarm)
if err := d.Set("dimensions", flattenDimensions(resp.Dimensions)); err != nil {
return err
}
d.Set("evaluation_periods", a.EvaluationPeriods)
d.Set("evaluation_periods", resp.EvaluationPeriods)

if err := d.Set("insufficient_data_actions", _strArrPtrToList(a.InsufficientDataActions)); err != nil {
if err := d.Set("insufficient_data_actions", _strArrPtrToList(resp.InsufficientDataActions)); err != nil {
log.Printf("[WARN] Error setting Insufficient Data Actions: %s", err)
}
d.Set("metric_name", a.MetricName)
d.Set("namespace", a.Namespace)
d.Set("metric_name", resp.MetricName)
d.Set("namespace", resp.Namespace)

if a.Metrics != nil && len(a.Metrics) > 0 {
metricQueries := make([]interface{}, len(a.Metrics))
for i, mq := range a.Metrics {
if resp.Metrics != nil && len(resp.Metrics) > 0 {
metricQueries := make([]interface{}, len(resp.Metrics))
for i, mq := range resp.Metrics {
metricQuery := map[string]interface{}{
"expression": aws.StringValue(mq.Expression),
"id": aws.StringValue(mq.Id),
Expand All @@ -308,19 +311,25 @@ func resourceAwsCloudWatchMetricAlarmRead(d *schema.ResourceData, meta interface
}
}

if err := d.Set("ok_actions", _strArrPtrToList(a.OKActions)); err != nil {
if err := d.Set("ok_actions", _strArrPtrToList(resp.OKActions)); err != nil {
log.Printf("[WARN] Error setting OK Actions: %s", err)
}
d.Set("period", a.Period)
d.Set("statistic", a.Statistic)
d.Set("threshold", a.Threshold)
d.Set("threshold_metric_id", a.ThresholdMetricId)
d.Set("unit", a.Unit)
d.Set("extended_statistic", a.ExtendedStatistic)
d.Set("treat_missing_data", a.TreatMissingData)
d.Set("evaluate_low_sample_count_percentiles", a.EvaluateLowSampleCountPercentile)
d.Set("period", resp.Period)
d.Set("statistic", resp.Statistic)
d.Set("threshold", resp.Threshold)
d.Set("threshold_metric_id", resp.ThresholdMetricId)
d.Set("unit", resp.Unit)
d.Set("extended_statistic", resp.ExtendedStatistic)
d.Set("treat_missing_data", resp.TreatMissingData)
d.Set("evaluate_low_sample_count_percentiles", resp.EvaluateLowSampleCountPercentile)

tags, err := keyvaluetags.CloudwatchListTags(conn, arn)

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

if err := d.Set("tags", tags.IgnoreAws().Map()); err != nil {
return fmt.Errorf("error setting tags: %s", err)
}

Expand All @@ -338,20 +347,24 @@ 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)
arn := d.Get("arn").(string)
if d.HasChange("tags") {
o, n := d.GetChange("tags")

if err := keyvaluetags.CloudwatchUpdateTags(conn, arn, o, n); err != nil {
return fmt.Errorf("error updating CloudWatch Metric Alarm (%s) tags: %s", arn, err)
}
}

return resourceAwsCloudWatchMetricAlarmRead(d, meta)
}

func resourceAwsCloudWatchMetricAlarmDelete(d *schema.ResourceData, meta interface{}) error {
p, err := getAwsCloudWatchMetricAlarm(d, meta)
resp, err := getAwsCloudWatchMetricAlarm(d, meta)
if err != nil {
return err
}
if p == nil {
if resp == nil {
log.Printf("[DEBUG] CloudWatch Metric Alarm %s is already gone", d.Id())
return nil
}
Expand All @@ -377,7 +390,7 @@ func getAwsCloudWatchPutMetricAlarmInput(d *schema.ResourceData) cloudwatch.PutM
ComparisonOperator: aws.String(d.Get("comparison_operator").(string)),
EvaluationPeriods: aws.Int64(int64(d.Get("evaluation_periods").(int))),
TreatMissingData: aws.String(d.Get("treat_missing_data").(string)),
Tags: tagsFromMapCloudWatch(d.Get("tags").(map[string]interface{})),
Tags: keyvaluetags.New(d.Get("tags").(map[string]interface{})).IgnoreAws().CloudwatchTags(),
}

if v := d.Get("actions_enabled"); v != nil {
Expand Down
Loading

0 comments on commit 224ce1a

Please sign in to comment.