github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/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 TestAccAWSSNSTopic_withIAMRole(t *testing.T) {
    31  	resource.Test(t, resource.TestCase{
    32  		PreCheck:     func() { testAccPreCheck(t) },
    33  		Providers:    testAccProviders,
    34  		CheckDestroy: testAccCheckAWSSNSTopicDestroy,
    35  		Steps: []resource.TestStep{
    36  			resource.TestStep{
    37  				Config: testAccAWSSNSTopicConfig_withIAMRole,
    38  				Check: resource.ComposeTestCheckFunc(
    39  					testAccCheckAWSSNSTopicExists("aws_sns_topic.test_topic"),
    40  				),
    41  			},
    42  		},
    43  	})
    44  }
    45  
    46  func testAccCheckAWSSNSTopicDestroy(s *terraform.State) error {
    47  	conn := testAccProvider.Meta().(*AWSClient).snsconn
    48  
    49  	for _, rs := range s.RootModule().Resources {
    50  		if rs.Type != "aws_sns_topic" {
    51  			continue
    52  		}
    53  
    54  		// Check if the topic exists by fetching its attributes
    55  		params := &sns.GetTopicAttributesInput{
    56  			TopicArn: aws.String(rs.Primary.ID),
    57  		}
    58  		_, err := conn.GetTopicAttributes(params)
    59  		if err == nil {
    60  			return fmt.Errorf("Topic exists when it should be destroyed!")
    61  		}
    62  
    63  		// Verify the error is an API error, not something else
    64  		_, ok := err.(awserr.Error)
    65  		if !ok {
    66  			return err
    67  		}
    68  	}
    69  
    70  	return nil
    71  }
    72  
    73  func testAccCheckAWSSNSTopicExists(n string) resource.TestCheckFunc {
    74  	return func(s *terraform.State) error {
    75  		rs, ok := s.RootModule().Resources[n]
    76  		if !ok {
    77  			return fmt.Errorf("Not found: %s", n)
    78  		}
    79  
    80  		if rs.Primary.ID == "" {
    81  			return fmt.Errorf("No SNS topic with that ARN exists")
    82  		}
    83  
    84  		conn := testAccProvider.Meta().(*AWSClient).snsconn
    85  
    86  		params := &sns.GetTopicAttributesInput{
    87  			TopicArn: aws.String(rs.Primary.ID),
    88  		}
    89  		_, err := conn.GetTopicAttributes(params)
    90  
    91  		if err != nil {
    92  			return err
    93  		}
    94  
    95  		return nil
    96  	}
    97  }
    98  
    99  const testAccAWSSNSTopicConfig = `
   100  resource "aws_sns_topic" "test_topic" {
   101      name = "terraform-test-topic"
   102  }
   103  `
   104  
   105  // Test for https://github.com/hashicorp/terraform/issues/3660
   106  const testAccAWSSNSTopicConfig_withIAMRole = `
   107  resource "aws_iam_role" "example" {
   108    name = "terraform_bug"
   109    path = "/test/"
   110    assume_role_policy = <<EOF
   111  {
   112    "Version": "2012-10-17",
   113    "Statement": [
   114      {
   115        "Action": "sts:AssumeRole",
   116        "Principal": {
   117          "Service": "ec2.amazonaws.com"
   118        },
   119        "Effect": "Allow",
   120        "Sid": ""
   121      }
   122    ]
   123  }
   124  EOF
   125  }
   126  
   127  resource "aws_sns_topic" "test_topic" {
   128    name = "example"
   129    policy = <<EOF
   130  {
   131    "Version": "2012-10-17",
   132    "Id": "Policy1445931846145",
   133    "Statement": [
   134      {
   135        "Sid": "Stmt1445931846145",
   136        "Effect": "Allow",
   137        "Principal": {
   138          "AWS": "${aws_iam_role.example.arn}"
   139  			},
   140        "Action": "sns:Publish",
   141        "Resource": "arn:aws:sns:us-west-2::example"
   142      }
   143    ]
   144  }
   145  EOF
   146  }
   147  `