Merge pull request #9029 from hashicorp/b-cloudwatch-dimensions-read
provider/aws: Fix reading dimensions on cloudwatch alarms
This commit is contained in:
commit
01cef1a63c
|
@ -129,7 +129,9 @@ func resourceAwsCloudWatchMetricAlarmRead(d *schema.ResourceData, meta interface
|
||||||
d.Set("alarm_description", a.AlarmDescription)
|
d.Set("alarm_description", a.AlarmDescription)
|
||||||
d.Set("alarm_name", a.AlarmName)
|
d.Set("alarm_name", a.AlarmName)
|
||||||
d.Set("comparison_operator", a.ComparisonOperator)
|
d.Set("comparison_operator", a.ComparisonOperator)
|
||||||
d.Set("dimensions", a.Dimensions)
|
if err := d.Set("dimensions", flattenDimensions(a.Dimensions)); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
d.Set("evaluation_periods", a.EvaluationPeriods)
|
d.Set("evaluation_periods", a.EvaluationPeriods)
|
||||||
|
|
||||||
if err := d.Set("insufficient_data_actions", _strArrPtrToList(a.InsufficientDataActions)); err != nil {
|
if err := d.Set("insufficient_data_actions", _strArrPtrToList(a.InsufficientDataActions)); err != nil {
|
||||||
|
@ -282,3 +284,11 @@ func _strArrPtrToList(strArrPtr []*string) []string {
|
||||||
}
|
}
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func flattenDimensions(dims []*cloudwatch.Dimension) map[string]interface{} {
|
||||||
|
flatDims := make(map[string]interface{})
|
||||||
|
for _, d := range dims {
|
||||||
|
flatDims[*d.Name] = *d.Value
|
||||||
|
}
|
||||||
|
return flatDims
|
||||||
|
}
|
||||||
|
|
|
@ -24,12 +24,32 @@ func TestAccAWSCloudWatchMetricAlarm_basic(t *testing.T) {
|
||||||
testAccCheckCloudWatchMetricAlarmExists("aws_cloudwatch_metric_alarm.foobar", &alarm),
|
testAccCheckCloudWatchMetricAlarmExists("aws_cloudwatch_metric_alarm.foobar", &alarm),
|
||||||
resource.TestCheckResourceAttr("aws_cloudwatch_metric_alarm.foobar", "metric_name", "CPUUtilization"),
|
resource.TestCheckResourceAttr("aws_cloudwatch_metric_alarm.foobar", "metric_name", "CPUUtilization"),
|
||||||
resource.TestCheckResourceAttr("aws_cloudwatch_metric_alarm.foobar", "statistic", "Average"),
|
resource.TestCheckResourceAttr("aws_cloudwatch_metric_alarm.foobar", "statistic", "Average"),
|
||||||
|
testAccCheckCloudWatchMetricAlarmDimension(
|
||||||
|
"aws_cloudwatch_metric_alarm.foobar", "InstanceId", "i-abc123"),
|
||||||
),
|
),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func testAccCheckCloudWatchMetricAlarmDimension(n, k, v string) resource.TestCheckFunc {
|
||||||
|
return func(s *terraform.State) error {
|
||||||
|
rs, ok := s.RootModule().Resources[n]
|
||||||
|
if !ok {
|
||||||
|
return fmt.Errorf("Not found: %s", n)
|
||||||
|
}
|
||||||
|
key := fmt.Sprintf("dimensions.%s", k)
|
||||||
|
val, ok := rs.Primary.Attributes[key]
|
||||||
|
if !ok {
|
||||||
|
return fmt.Errorf("Could not find dimension: %s", k)
|
||||||
|
}
|
||||||
|
if val != v {
|
||||||
|
return fmt.Errorf("Expected dimension %s => %s; got: %s", k, v, val)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func testAccCheckCloudWatchMetricAlarmExists(n string, alarm *cloudwatch.MetricAlarm) resource.TestCheckFunc {
|
func testAccCheckCloudWatchMetricAlarmExists(n string, alarm *cloudwatch.MetricAlarm) resource.TestCheckFunc {
|
||||||
return func(s *terraform.State) error {
|
return func(s *terraform.State) error {
|
||||||
rs, ok := s.RootModule().Resources[n]
|
rs, ok := s.RootModule().Resources[n]
|
||||||
|
@ -89,7 +109,10 @@ resource "aws_cloudwatch_metric_alarm" "foobar" {
|
||||||
period = "120"
|
period = "120"
|
||||||
statistic = "Average"
|
statistic = "Average"
|
||||||
threshold = "80"
|
threshold = "80"
|
||||||
alarm_description = "This metric monitor ec2 cpu utilization"
|
alarm_description = "This metric monitors ec2 cpu utilization"
|
||||||
insufficient_data_actions = []
|
insufficient_data_actions = []
|
||||||
|
dimensions {
|
||||||
|
InstanceId = "i-abc123"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
`)
|
`)
|
||||||
|
|
Loading…
Reference in New Issue