github.com/bendemaree/terraform@v0.5.4-0.20150613200311-f50d97d6eee6/builtin/providers/aws/resource_aws_sns_topic_subscription_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/sns"
     9  	"github.com/hashicorp/terraform/helper/resource"
    10  	"github.com/hashicorp/terraform/terraform"
    11  	"github.com/aws/aws-sdk-go/aws/awserr"
    12  )
    13  
    14  func TestAccAWSSNSTopicSubscription_basic(t *testing.T) {
    15  	resource.Test(t, resource.TestCase{
    16  		PreCheck:     func() { testAccPreCheck(t) },
    17  		Providers:    testAccProviders,
    18  		CheckDestroy: testAccCheckAWSSNSTopicSubscriptionDestroy,
    19  		Steps: []resource.TestStep{
    20  			resource.TestStep{
    21  				Config: testAccAWSSNSTopicSubscriptionConfig,
    22  				Check: resource.ComposeTestCheckFunc(
    23  					testAccCheckAWSSNSTopicExists("aws_sns_topic.test_topic"),
    24  					testAccCheckAWSSNSTopicSubscriptionExists("aws_sns_topic_subscription.test_subscription"),
    25  				),
    26  			},
    27  		},
    28  	})
    29  }
    30  
    31  
    32  func testAccCheckAWSSNSTopicSubscriptionDestroy(s *terraform.State) error {
    33  	conn := testAccProvider.Meta().(*AWSClient).snsconn
    34  
    35  	for _, rs := range s.RootModule().Resources {
    36  		if rs.Type != "aws_sns_topic" {
    37  			continue
    38  		}
    39  
    40  		// Try to find key pair
    41  		req := &sns.GetSubscriptionAttributesInput{
    42  			SubscriptionARN: aws.String(rs.Primary.ID),
    43  		}
    44  
    45  
    46  		_, err := conn.GetSubscriptionAttributes(req)
    47  
    48  		if err == nil {
    49  			return fmt.Errorf("Subscription still exists, can't continue.")
    50  		}
    51  
    52  		// Verify the error is an API error, not something else
    53  		_, ok := err.(awserr.Error)
    54  		if !ok {
    55  			return err
    56  		}
    57  	}
    58  
    59  	return nil
    60  }
    61  
    62  
    63  func testAccCheckAWSSNSTopicSubscriptionExists(n string) resource.TestCheckFunc {
    64  	return func(s *terraform.State) error {
    65  		rs, ok := s.RootModule().Resources[n]
    66  		if !ok {
    67  			return fmt.Errorf("Not found: %s", n)
    68  		}
    69  
    70  		if rs.Primary.ID == "" {
    71  			return fmt.Errorf("No SNS subscription with that ARN exists")
    72  		}
    73  
    74  		conn := testAccProvider.Meta().(*AWSClient).snsconn
    75  
    76  		params := &sns.GetSubscriptionAttributesInput{
    77  			SubscriptionARN: aws.String(rs.Primary.ID),
    78  		}
    79  		_, err := conn.GetSubscriptionAttributes(params)
    80  
    81  		if err != nil {
    82  			return err
    83  		}
    84  
    85  		return nil
    86  	}
    87  }
    88  
    89  const testAccAWSSNSTopicSubscriptionConfig = `
    90  resource "aws_sns_topic" "test_topic" {
    91      name = "terraform-test-topic"
    92  }
    93  
    94  resource "aws_sqs_queue" "test_queue" {
    95  	name = "terraform-subscription-test-queue"
    96  }
    97  
    98  resource "aws_sns_topic_subscription" "test_subscription" {
    99      topic_arn = "${aws_sns_topic.test_topic.arn}"
   100      protocol = "sqs"
   101      endpoint = "${aws_sqs_queue.test_queue.arn}"
   102  }
   103  `