github.com/hobbeswalsh/terraform@v0.3.7-0.20150619183303-ad17cf55a0fa/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/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 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  
    31  func testAccCheckAWSSNSTopicDestroy(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  		// Check if the topic exists by fetching its attributes
    40  		params := &sns.GetTopicAttributesInput{
    41  			TopicARN: aws.String(rs.Primary.ID),
    42  		}
    43  		_, err := conn.GetTopicAttributes(params)
    44  		if err == nil {
    45  			return fmt.Errorf("Topic exists when it should be destroyed!")
    46  		}
    47  
    48  		// Verify the error is an API error, not something else
    49  		_, ok := err.(awserr.Error)
    50  		if !ok {
    51  			return err
    52  		}
    53  	}
    54  
    55  	return nil
    56  }
    57  
    58  
    59  func testAccCheckAWSSNSTopicExists(n string) resource.TestCheckFunc {
    60  	return func(s *terraform.State) error {
    61  		rs, ok := s.RootModule().Resources[n]
    62  		if !ok {
    63  			return fmt.Errorf("Not found: %s", n)
    64  		}
    65  
    66  		if rs.Primary.ID == "" {
    67  			return fmt.Errorf("No SNS topic with that ARN exists")
    68  		}
    69  
    70  		conn := testAccProvider.Meta().(*AWSClient).snsconn
    71  
    72  		params := &sns.GetTopicAttributesInput{
    73  			TopicARN: aws.String(rs.Primary.ID),
    74  		}
    75  		_, err := conn.GetTopicAttributes(params)
    76  
    77  		if err != nil {
    78  			return err
    79  		}
    80  
    81  		return nil
    82  	}
    83  }
    84  
    85  const testAccAWSSNSTopicConfig = `
    86  resource "aws_sns_topic" "test_topic" {
    87      name = "terraform-test-topic"
    88  }
    89  `