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