github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/builtin/providers/aws/resource_aws_cloudwatch_event_rule_test.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/aws/aws-sdk-go/aws"
     8  	events "github.com/aws/aws-sdk-go/service/cloudwatchevents"
     9  	"github.com/hashicorp/terraform/helper/resource"
    10  	"github.com/hashicorp/terraform/terraform"
    11  )
    12  
    13  func TestAccAWSCloudWatchEventRule_basic(t *testing.T) {
    14  	var rule events.DescribeRuleOutput
    15  
    16  	resource.Test(t, resource.TestCase{
    17  		PreCheck:     func() { testAccPreCheck(t) },
    18  		Providers:    testAccProviders,
    19  		CheckDestroy: testAccCheckAWSCloudWatchEventRuleDestroy,
    20  		Steps: []resource.TestStep{
    21  			resource.TestStep{
    22  				Config: testAccAWSCloudWatchEventRuleConfig,
    23  				Check: resource.ComposeTestCheckFunc(
    24  					testAccCheckCloudWatchEventRuleExists("aws_cloudwatch_event_rule.foo", &rule),
    25  					resource.TestCheckResourceAttr("aws_cloudwatch_event_rule.foo", "name", "tf-acc-cw-event-rule"),
    26  				),
    27  			},
    28  			resource.TestStep{
    29  				Config: testAccAWSCloudWatchEventRuleConfigModified,
    30  				Check: resource.ComposeTestCheckFunc(
    31  					testAccCheckCloudWatchEventRuleExists("aws_cloudwatch_event_rule.foo", &rule),
    32  					resource.TestCheckResourceAttr("aws_cloudwatch_event_rule.foo", "name", "tf-acc-cw-event-rule-mod"),
    33  				),
    34  			},
    35  		},
    36  	})
    37  }
    38  
    39  func TestAccAWSCloudWatchEventRule_full(t *testing.T) {
    40  	var rule events.DescribeRuleOutput
    41  
    42  	resource.Test(t, resource.TestCase{
    43  		PreCheck:     func() { testAccPreCheck(t) },
    44  		Providers:    testAccProviders,
    45  		CheckDestroy: testAccCheckAWSCloudWatchEventRuleDestroy,
    46  		Steps: []resource.TestStep{
    47  			resource.TestStep{
    48  				Config: testAccAWSCloudWatchEventRuleConfig_full,
    49  				Check: resource.ComposeTestCheckFunc(
    50  					testAccCheckCloudWatchEventRuleExists("aws_cloudwatch_event_rule.moobar", &rule),
    51  					resource.TestCheckResourceAttr("aws_cloudwatch_event_rule.moobar", "name", "tf-acc-cw-event-rule-full"),
    52  					resource.TestCheckResourceAttr("aws_cloudwatch_event_rule.moobar", "schedule_expression", "rate(5 minutes)"),
    53  					resource.TestCheckResourceAttr("aws_cloudwatch_event_rule.moobar", "event_pattern", "{\"source\":[\"aws.ec2\"]}"),
    54  					resource.TestCheckResourceAttr("aws_cloudwatch_event_rule.moobar", "description", "He's not dead, he's just resting!"),
    55  					resource.TestCheckResourceAttr("aws_cloudwatch_event_rule.moobar", "role_arn", ""),
    56  					testAccCheckCloudWatchEventRuleEnabled("aws_cloudwatch_event_rule.moobar", "DISABLED", &rule),
    57  				),
    58  			},
    59  		},
    60  	})
    61  }
    62  
    63  func TestAccAWSCloudWatchEventRule_enable(t *testing.T) {
    64  	var rule events.DescribeRuleOutput
    65  
    66  	resource.Test(t, resource.TestCase{
    67  		PreCheck:     func() { testAccPreCheck(t) },
    68  		Providers:    testAccProviders,
    69  		CheckDestroy: testAccCheckAWSCloudWatchEventRuleDestroy,
    70  		Steps: []resource.TestStep{
    71  			resource.TestStep{
    72  				Config: testAccAWSCloudWatchEventRuleConfigEnabled,
    73  				Check: resource.ComposeTestCheckFunc(
    74  					testAccCheckCloudWatchEventRuleExists("aws_cloudwatch_event_rule.moo", &rule),
    75  					testAccCheckCloudWatchEventRuleEnabled("aws_cloudwatch_event_rule.moo", "ENABLED", &rule),
    76  				),
    77  			},
    78  			resource.TestStep{
    79  				Config: testAccAWSCloudWatchEventRuleConfigDisabled,
    80  				Check: resource.ComposeTestCheckFunc(
    81  					testAccCheckCloudWatchEventRuleExists("aws_cloudwatch_event_rule.moo", &rule),
    82  					testAccCheckCloudWatchEventRuleEnabled("aws_cloudwatch_event_rule.moo", "DISABLED", &rule),
    83  				),
    84  			},
    85  			resource.TestStep{
    86  				Config: testAccAWSCloudWatchEventRuleConfigEnabled,
    87  				Check: resource.ComposeTestCheckFunc(
    88  					testAccCheckCloudWatchEventRuleExists("aws_cloudwatch_event_rule.moo", &rule),
    89  					testAccCheckCloudWatchEventRuleEnabled("aws_cloudwatch_event_rule.moo", "ENABLED", &rule),
    90  				),
    91  			},
    92  		},
    93  	})
    94  }
    95  
    96  func testAccCheckCloudWatchEventRuleExists(n string, rule *events.DescribeRuleOutput) resource.TestCheckFunc {
    97  	return func(s *terraform.State) error {
    98  		rs, ok := s.RootModule().Resources[n]
    99  		if !ok {
   100  			return fmt.Errorf("Not found: %s", n)
   101  		}
   102  
   103  		conn := testAccProvider.Meta().(*AWSClient).cloudwatcheventsconn
   104  		params := events.DescribeRuleInput{
   105  			Name: aws.String(rs.Primary.ID),
   106  		}
   107  		resp, err := conn.DescribeRule(&params)
   108  		if err != nil {
   109  			return err
   110  		}
   111  		if resp == nil {
   112  			return fmt.Errorf("Rule not found")
   113  		}
   114  
   115  		*rule = *resp
   116  
   117  		return nil
   118  	}
   119  }
   120  
   121  func testAccCheckCloudWatchEventRuleEnabled(n string, desired string, rule *events.DescribeRuleOutput) resource.TestCheckFunc {
   122  	return func(s *terraform.State) error {
   123  		rs, ok := s.RootModule().Resources[n]
   124  		if !ok {
   125  			return fmt.Errorf("Not found: %s", n)
   126  		}
   127  
   128  		conn := testAccProvider.Meta().(*AWSClient).cloudwatcheventsconn
   129  		params := events.DescribeRuleInput{
   130  			Name: aws.String(rs.Primary.ID),
   131  		}
   132  		resp, err := conn.DescribeRule(&params)
   133  
   134  		if err != nil {
   135  			return err
   136  		}
   137  		if *resp.State != desired {
   138  			return fmt.Errorf("Expected state %q, given %q", desired, *resp.State)
   139  		}
   140  
   141  		return nil
   142  	}
   143  }
   144  
   145  func testAccCheckAWSCloudWatchEventRuleDestroy(s *terraform.State) error {
   146  	conn := testAccProvider.Meta().(*AWSClient).cloudwatcheventsconn
   147  
   148  	for _, rs := range s.RootModule().Resources {
   149  		if rs.Type != "aws_cloudwatch_event_rule" {
   150  			continue
   151  		}
   152  
   153  		params := events.DescribeRuleInput{
   154  			Name: aws.String(rs.Primary.ID),
   155  		}
   156  
   157  		resp, err := conn.DescribeRule(&params)
   158  
   159  		if err == nil {
   160  			return fmt.Errorf("CloudWatch Event Rule %q still exists: %s",
   161  				rs.Primary.ID, resp)
   162  		}
   163  	}
   164  
   165  	return nil
   166  }
   167  
   168  var testAccAWSCloudWatchEventRuleConfig = `
   169  resource "aws_cloudwatch_event_rule" "foo" {
   170      name = "tf-acc-cw-event-rule"
   171      schedule_expression = "rate(1 hour)"
   172  }
   173  `
   174  
   175  var testAccAWSCloudWatchEventRuleConfigEnabled = `
   176  resource "aws_cloudwatch_event_rule" "moo" {
   177      name = "tf-acc-cw-event-rule-state"
   178      schedule_expression = "rate(1 hour)"
   179  }
   180  `
   181  var testAccAWSCloudWatchEventRuleConfigDisabled = `
   182  resource "aws_cloudwatch_event_rule" "moo" {
   183      name = "tf-acc-cw-event-rule-state"
   184      schedule_expression = "rate(1 hour)"
   185      is_enabled = false
   186  }
   187  `
   188  
   189  var testAccAWSCloudWatchEventRuleConfigModified = `
   190  resource "aws_cloudwatch_event_rule" "foo" {
   191      name = "tf-acc-cw-event-rule-mod"
   192      schedule_expression = "rate(1 hour)"
   193  }
   194  `
   195  
   196  var testAccAWSCloudWatchEventRuleConfig_full = `
   197  resource "aws_cloudwatch_event_rule" "moobar" {
   198      name = "tf-acc-cw-event-rule-full"
   199      schedule_expression = "rate(5 minutes)"
   200  	event_pattern = <<PATTERN
   201  { "source": ["aws.ec2"] }
   202  PATTERN
   203  	description = "He's not dead, he's just resting!"
   204  	is_enabled = false
   205  }
   206  `
   207  
   208  // TODO: Figure out example with IAM Role