2015-05-15 01:17:18 +02:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
2015-06-03 20:36:57 +02:00
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
2015-06-24 07:31:24 +02:00
|
|
|
"github.com/aws/aws-sdk-go/aws/awserr"
|
2015-06-03 20:36:57 +02:00
|
|
|
"github.com/aws/aws-sdk-go/service/sns"
|
2015-05-15 01:17:18 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
2015-06-08 01:18:14 +02:00
|
|
|
func TestAccAWSSNSTopic_basic(t *testing.T) {
|
2015-05-15 01:17:18 +02:00
|
|
|
resource.Test(t, resource.TestCase{
|
2016-04-21 17:18:04 +02:00
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
IDRefreshName: "aws_sns_topic.test_topic",
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckAWSSNSTopicDestroy,
|
2015-05-15 01:17:18 +02:00
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccAWSSNSTopicConfig,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckAWSSNSTopicExists("aws_sns_topic.test_topic"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-10-30 17:53:59 +01:00
|
|
|
func TestAccAWSSNSTopic_withIAMRole(t *testing.T) {
|
|
|
|
resource.Test(t, resource.TestCase{
|
2016-04-21 17:18:04 +02:00
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
IDRefreshName: "aws_sns_topic.test_topic",
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckAWSSNSTopicDestroy,
|
2015-10-30 17:53:59 +01:00
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccAWSSNSTopicConfig_withIAMRole,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckAWSSNSTopicExists("aws_sns_topic.test_topic"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-05-15 01:17:18 +02:00
|
|
|
func testAccCheckAWSSNSTopicDestroy(s *terraform.State) error {
|
|
|
|
conn := testAccProvider.Meta().(*AWSClient).snsconn
|
|
|
|
|
|
|
|
for _, rs := range s.RootModule().Resources {
|
|
|
|
if rs.Type != "aws_sns_topic" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the topic exists by fetching its attributes
|
|
|
|
params := &sns.GetTopicAttributesInput{
|
2015-08-17 20:27:16 +02:00
|
|
|
TopicArn: aws.String(rs.Primary.ID),
|
2015-05-15 01:17:18 +02:00
|
|
|
}
|
|
|
|
_, err := conn.GetTopicAttributes(params)
|
|
|
|
if err == nil {
|
|
|
|
return fmt.Errorf("Topic exists when it should be destroyed!")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify the error is an API error, not something else
|
2015-05-23 06:12:25 +02:00
|
|
|
_, ok := err.(awserr.Error)
|
2015-05-15 01:17:18 +02:00
|
|
|
if !ok {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckAWSSNSTopicExists(n string) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
rs, ok := s.RootModule().Resources[n]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Not found: %s", n)
|
|
|
|
}
|
|
|
|
|
|
|
|
if rs.Primary.ID == "" {
|
|
|
|
return fmt.Errorf("No SNS topic with that ARN exists")
|
|
|
|
}
|
|
|
|
|
|
|
|
conn := testAccProvider.Meta().(*AWSClient).snsconn
|
|
|
|
|
|
|
|
params := &sns.GetTopicAttributesInput{
|
2015-08-17 20:27:16 +02:00
|
|
|
TopicArn: aws.String(rs.Primary.ID),
|
2015-05-15 01:17:18 +02:00
|
|
|
}
|
|
|
|
_, err := conn.GetTopicAttributes(params)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const testAccAWSSNSTopicConfig = `
|
|
|
|
resource "aws_sns_topic" "test_topic" {
|
|
|
|
name = "terraform-test-topic"
|
|
|
|
}
|
2015-06-03 20:36:57 +02:00
|
|
|
`
|
2015-10-30 17:53:59 +01:00
|
|
|
|
|
|
|
// Test for https://github.com/hashicorp/terraform/issues/3660
|
|
|
|
const testAccAWSSNSTopicConfig_withIAMRole = `
|
|
|
|
resource "aws_iam_role" "example" {
|
|
|
|
name = "terraform_bug"
|
|
|
|
path = "/test/"
|
|
|
|
assume_role_policy = <<EOF
|
|
|
|
{
|
|
|
|
"Version": "2012-10-17",
|
|
|
|
"Statement": [
|
|
|
|
{
|
|
|
|
"Action": "sts:AssumeRole",
|
|
|
|
"Principal": {
|
|
|
|
"Service": "ec2.amazonaws.com"
|
|
|
|
},
|
|
|
|
"Effect": "Allow",
|
|
|
|
"Sid": ""
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
EOF
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_sns_topic" "test_topic" {
|
|
|
|
name = "example"
|
|
|
|
policy = <<EOF
|
|
|
|
{
|
|
|
|
"Statement": [
|
|
|
|
{
|
|
|
|
"Sid": "Stmt1445931846145",
|
|
|
|
"Effect": "Allow",
|
|
|
|
"Principal": {
|
|
|
|
"AWS": "${aws_iam_role.example.arn}"
|
|
|
|
},
|
|
|
|
"Action": "sns:Publish",
|
|
|
|
"Resource": "arn:aws:sns:us-west-2::example"
|
|
|
|
}
|
2016-04-08 20:55:50 +02:00
|
|
|
],
|
|
|
|
"Version": "2012-10-17",
|
|
|
|
"Id": "Policy1445931846145"
|
2015-10-30 17:53:59 +01:00
|
|
|
}
|
|
|
|
EOF
|
|
|
|
}
|
|
|
|
`
|