github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/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  					testAccCheckCloudWatchMetricAlarmDimension(
    28  						"aws_cloudwatch_metric_alarm.foobar", "InstanceId", "i-abc123"),
    29  				),
    30  			},
    31  		},
    32  	})
    33  }
    34  
    35  func testAccCheckCloudWatchMetricAlarmDimension(n, k, v string) resource.TestCheckFunc {
    36  	return func(s *terraform.State) error {
    37  		rs, ok := s.RootModule().Resources[n]
    38  		if !ok {
    39  			return fmt.Errorf("Not found: %s", n)
    40  		}
    41  		key := fmt.Sprintf("dimensions.%s", k)
    42  		val, ok := rs.Primary.Attributes[key]
    43  		if !ok {
    44  			return fmt.Errorf("Could not find dimension: %s", k)
    45  		}
    46  		if val != v {
    47  			return fmt.Errorf("Expected dimension %s => %s; got: %s", k, v, val)
    48  		}
    49  		return nil
    50  	}
    51  }
    52  
    53  func testAccCheckCloudWatchMetricAlarmExists(n string, alarm *cloudwatch.MetricAlarm) resource.TestCheckFunc {
    54  	return func(s *terraform.State) error {
    55  		rs, ok := s.RootModule().Resources[n]
    56  		if !ok {
    57  			return fmt.Errorf("Not found: %s", n)
    58  		}
    59  
    60  		conn := testAccProvider.Meta().(*AWSClient).cloudwatchconn
    61  		params := cloudwatch.DescribeAlarmsInput{
    62  			AlarmNames: []*string{aws.String(rs.Primary.ID)},
    63  		}
    64  		resp, err := conn.DescribeAlarms(&params)
    65  		if err != nil {
    66  			return err
    67  		}
    68  		if len(resp.MetricAlarms) == 0 {
    69  			return fmt.Errorf("Alarm not found")
    70  		}
    71  		*alarm = *resp.MetricAlarms[0]
    72  
    73  		return nil
    74  	}
    75  }
    76  
    77  func testAccCheckAWSCloudWatchMetricAlarmDestroy(s *terraform.State) error {
    78  	conn := testAccProvider.Meta().(*AWSClient).cloudwatchconn
    79  
    80  	for _, rs := range s.RootModule().Resources {
    81  		if rs.Type != "aws_cloudwatch_metric_alarm" {
    82  			continue
    83  		}
    84  
    85  		params := cloudwatch.DescribeAlarmsInput{
    86  			AlarmNames: []*string{aws.String(rs.Primary.ID)},
    87  		}
    88  
    89  		resp, err := conn.DescribeAlarms(&params)
    90  
    91  		if err == nil {
    92  			if len(resp.MetricAlarms) != 0 &&
    93  				*resp.MetricAlarms[0].AlarmName == rs.Primary.ID {
    94  				return fmt.Errorf("Alarm Still Exists: %s", rs.Primary.ID)
    95  			}
    96  		}
    97  	}
    98  
    99  	return nil
   100  }
   101  
   102  var testAccAWSCloudWatchMetricAlarmConfig = fmt.Sprintf(`
   103  resource "aws_cloudwatch_metric_alarm" "foobar" {
   104    alarm_name                = "terraform-test-foobar5"
   105    comparison_operator       = "GreaterThanOrEqualToThreshold"
   106    evaluation_periods        = "2"
   107    metric_name               = "CPUUtilization"
   108    namespace                 = "AWS/EC2"
   109    period                    = "120"
   110    statistic                 = "Average"
   111    threshold                 = "80"
   112    alarm_description         = "This metric monitors ec2 cpu utilization"
   113    insufficient_data_actions = []
   114    dimensions {
   115      InstanceId = "i-abc123"
   116    }
   117  }
   118  `)