github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/aws/resource_aws_cloudwatch_metric_alarm_test.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/aws/aws-sdk-go/aws" 8 "github.com/aws/aws-sdk-go/service/cloudwatch" 9 "github.com/hashicorp/terraform/helper/resource" 10 "github.com/hashicorp/terraform/terraform" 11 ) 12 13 func TestAccAWSCloudWatchMetricAlarm_basic(t *testing.T) { 14 var alarm cloudwatch.MetricAlarm 15 16 resource.Test(t, resource.TestCase{ 17 PreCheck: func() { testAccPreCheck(t) }, 18 Providers: testAccProviders, 19 CheckDestroy: testAccCheckAWSCloudWatchMetricAlarmDestroy, 20 Steps: []resource.TestStep{ 21 resource.TestStep{ 22 Config: testAccAWSCloudWatchMetricAlarmConfig, 23 Check: resource.ComposeTestCheckFunc( 24 testAccCheckCloudWatchMetricAlarmExists("aws_cloudwatch_metric_alarm.foobar", &alarm), 25 resource.TestCheckResourceAttr("aws_cloudwatch_metric_alarm.foobar", "metric_name", "CPUUtilization"), 26 resource.TestCheckResourceAttr("aws_cloudwatch_metric_alarm.foobar", "statistic", "Average"), 27 ), 28 }, 29 }, 30 }) 31 } 32 33 func testAccCheckCloudWatchMetricAlarmExists(n string, alarm *cloudwatch.MetricAlarm) resource.TestCheckFunc { 34 return func(s *terraform.State) error { 35 rs, ok := s.RootModule().Resources[n] 36 if !ok { 37 return fmt.Errorf("Not found: %s", n) 38 } 39 40 conn := testAccProvider.Meta().(*AWSClient).cloudwatchconn 41 params := cloudwatch.DescribeAlarmsInput{ 42 AlarmNames: []*string{aws.String(rs.Primary.ID)}, 43 } 44 resp, err := conn.DescribeAlarms(¶ms) 45 if err != nil { 46 return err 47 } 48 if len(resp.MetricAlarms) == 0 { 49 return fmt.Errorf("Alarm not found") 50 } 51 *alarm = *resp.MetricAlarms[0] 52 53 return nil 54 } 55 } 56 57 func testAccCheckAWSCloudWatchMetricAlarmDestroy(s *terraform.State) error { 58 conn := testAccProvider.Meta().(*AWSClient).cloudwatchconn 59 60 for _, rs := range s.RootModule().Resources { 61 if rs.Type != "aws_cloudwatch_metric_alarm" { 62 continue 63 } 64 65 params := cloudwatch.DescribeAlarmsInput{ 66 AlarmNames: []*string{aws.String(rs.Primary.ID)}, 67 } 68 69 resp, err := conn.DescribeAlarms(¶ms) 70 71 if err == nil { 72 if len(resp.MetricAlarms) != 0 && 73 *resp.MetricAlarms[0].AlarmName == rs.Primary.ID { 74 return fmt.Errorf("Alarm Still Exists: %s", rs.Primary.ID) 75 } 76 } 77 } 78 79 return nil 80 } 81 82 var testAccAWSCloudWatchMetricAlarmConfig = fmt.Sprintf(` 83 resource "aws_cloudwatch_metric_alarm" "foobar" { 84 alarm_name = "terraform-test-foobar5" 85 comparison_operator = "GreaterThanOrEqualToThreshold" 86 evaluation_periods = "2" 87 metric_name = "CPUUtilization" 88 namespace = "AWS/EC2" 89 period = "120" 90 statistic = "Average" 91 threshold = "80" 92 alarm_description = "This metric monitor ec2 cpu utilization" 93 insufficient_data_actions = [] 94 } 95 `)