github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/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/aws/awserr"
     9  	"github.com/aws/aws-sdk-go/service/sns"
    10  	"github.com/hashicorp/terraform/helper/resource"
    11  	"github.com/hashicorp/terraform/terraform"
    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  func testAccCheckAWSSNSTopicSubscriptionDestroy(s *terraform.State) error {
    32  	conn := testAccProvider.Meta().(*AWSClient).snsconn
    33  
    34  	for _, rs := range s.RootModule().Resources {
    35  		if rs.Type != "aws_sns_topic" {
    36  			continue
    37  		}
    38  
    39  		// Try to find key pair
    40  		req := &sns.GetSubscriptionAttributesInput{
    41  			SubscriptionArn: aws.String(rs.Primary.ID),
    42  		}
    43  
    44  		_, err := conn.GetSubscriptionAttributes(req)
    45  
    46  		if err == nil {
    47  			return fmt.Errorf("Subscription still exists, can't continue.")
    48  		}
    49  
    50  		// Verify the error is an API error, not something else
    51  		_, ok := err.(awserr.Error)
    52  		if !ok {
    53  			return err
    54  		}
    55  	}
    56  
    57  	return nil
    58  }
    59  
    60  func testAccCheckAWSSNSTopicSubscriptionExists(n string) resource.TestCheckFunc {
    61  	return func(s *terraform.State) error {
    62  		rs, ok := s.RootModule().Resources[n]
    63  		if !ok {
    64  			return fmt.Errorf("Not found: %s", n)
    65  		}
    66  
    67  		if rs.Primary.ID == "" {
    68  			return fmt.Errorf("No SNS subscription with that ARN exists")
    69  		}
    70  
    71  		conn := testAccProvider.Meta().(*AWSClient).snsconn
    72  
    73  		params := &sns.GetSubscriptionAttributesInput{
    74  			SubscriptionArn: aws.String(rs.Primary.ID),
    75  		}
    76  		_, err := conn.GetSubscriptionAttributes(params)
    77  
    78  		if err != nil {
    79  			return err
    80  		}
    81  
    82  		return nil
    83  	}
    84  }
    85  
    86  const testAccAWSSNSTopicSubscriptionConfig = `
    87  resource "aws_sns_topic" "test_topic" {
    88      name = "terraform-test-topic"
    89  }
    90  
    91  resource "aws_sqs_queue" "test_queue" {
    92  	name = "terraform-subscription-test-queue"
    93  }
    94  
    95  resource "aws_sns_topic_subscription" "test_subscription" {
    96      topic_arn = "${aws_sns_topic.test_topic.arn}"
    97      protocol = "sqs"
    98      endpoint = "${aws_sqs_queue.test_queue.arn}"
    99  }
   100  `