github.com/jsoriano/terraform@v0.6.7-0.20151026070445-8b70867fdd95/builtin/providers/aws/resource_aws_sns_topic_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 TestAccAWSSNSTopic_basic(t *testing.T) {
    15  	resource.Test(t, resource.TestCase{
    16  		PreCheck:     func() { testAccPreCheck(t) },
    17  		Providers:    testAccProviders,
    18  		CheckDestroy: testAccCheckAWSSNSTopicDestroy,
    19  		Steps: []resource.TestStep{
    20  			resource.TestStep{
    21  				Config: testAccAWSSNSTopicConfig,
    22  				Check: resource.ComposeTestCheckFunc(
    23  					testAccCheckAWSSNSTopicExists("aws_sns_topic.test_topic"),
    24  				),
    25  			},
    26  		},
    27  	})
    28  }
    29  
    30  func testAccCheckAWSSNSTopicDestroy(s *terraform.State) error {
    31  	conn := testAccProvider.Meta().(*AWSClient).snsconn
    32  
    33  	for _, rs := range s.RootModule().Resources {
    34  		if rs.Type != "aws_sns_topic" {
    35  			continue
    36  		}
    37  
    38  		// Check if the topic exists by fetching its attributes
    39  		params := &sns.GetTopicAttributesInput{
    40  			TopicArn: aws.String(rs.Primary.ID),
    41  		}
    42  		_, err := conn.GetTopicAttributes(params)
    43  		if err == nil {
    44  			return fmt.Errorf("Topic exists when it should be destroyed!")
    45  		}
    46  
    47  		// Verify the error is an API error, not something else
    48  		_, ok := err.(awserr.Error)
    49  		if !ok {
    50  			return err
    51  		}
    52  	}
    53  
    54  	return nil
    55  }
    56  
    57  func testAccCheckAWSSNSTopicExists(n string) resource.TestCheckFunc {
    58  	return func(s *terraform.State) error {
    59  		rs, ok := s.RootModule().Resources[n]
    60  		if !ok {
    61  			return fmt.Errorf("Not found: %s", n)
    62  		}
    63  
    64  		if rs.Primary.ID == "" {
    65  			return fmt.Errorf("No SNS topic with that ARN exists")
    66  		}
    67  
    68  		conn := testAccProvider.Meta().(*AWSClient).snsconn
    69  
    70  		params := &sns.GetTopicAttributesInput{
    71  			TopicArn: aws.String(rs.Primary.ID),
    72  		}
    73  		_, err := conn.GetTopicAttributes(params)
    74  
    75  		if err != nil {
    76  			return err
    77  		}
    78  
    79  		return nil
    80  	}
    81  }
    82  
    83  const testAccAWSSNSTopicConfig = `
    84  resource "aws_sns_topic" "test_topic" {
    85      name = "terraform-test-topic"
    86  }
    87  `