github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/aws/resource_aws_sns_topic_subscription_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/acctest"
    11  	"github.com/hashicorp/terraform/helper/resource"
    12  	"github.com/hashicorp/terraform/terraform"
    13  )
    14  
    15  func TestAccAWSSNSTopicSubscription_basic(t *testing.T) {
    16  	ri := acctest.RandInt()
    17  
    18  	resource.Test(t, resource.TestCase{
    19  		PreCheck:     func() { testAccPreCheck(t) },
    20  		Providers:    testAccProviders,
    21  		CheckDestroy: testAccCheckAWSSNSTopicSubscriptionDestroy,
    22  		Steps: []resource.TestStep{
    23  			resource.TestStep{
    24  				Config: testAccAWSSNSTopicSubscriptionConfig(ri),
    25  				Check: resource.ComposeTestCheckFunc(
    26  					testAccCheckAWSSNSTopicExists("aws_sns_topic.test_topic"),
    27  					testAccCheckAWSSNSTopicSubscriptionExists("aws_sns_topic_subscription.test_subscription"),
    28  				),
    29  			},
    30  		},
    31  	})
    32  }
    33  
    34  func TestAccAWSSNSTopicSubscription_autoConfirmingEndpoint(t *testing.T) {
    35  	ri := acctest.RandInt()
    36  
    37  	resource.Test(t, resource.TestCase{
    38  		PreCheck:     func() { testAccPreCheck(t) },
    39  		Providers:    testAccProviders,
    40  		CheckDestroy: testAccCheckAWSSNSTopicSubscriptionDestroy,
    41  		Steps: []resource.TestStep{
    42  			resource.TestStep{
    43  				Config: testAccAWSSNSTopicSubscriptionConfig_autoConfirmingEndpoint(ri),
    44  				Check: resource.ComposeTestCheckFunc(
    45  					testAccCheckAWSSNSTopicExists("aws_sns_topic.test_topic"),
    46  					testAccCheckAWSSNSTopicSubscriptionExists("aws_sns_topic_subscription.test_subscription"),
    47  				),
    48  			},
    49  		},
    50  	})
    51  }
    52  
    53  func testAccCheckAWSSNSTopicSubscriptionDestroy(s *terraform.State) error {
    54  	conn := testAccProvider.Meta().(*AWSClient).snsconn
    55  
    56  	for _, rs := range s.RootModule().Resources {
    57  		if rs.Type != "aws_sns_topic" {
    58  			continue
    59  		}
    60  
    61  		// Try to find key pair
    62  		req := &sns.GetSubscriptionAttributesInput{
    63  			SubscriptionArn: aws.String(rs.Primary.ID),
    64  		}
    65  
    66  		_, err := conn.GetSubscriptionAttributes(req)
    67  
    68  		if err == nil {
    69  			return fmt.Errorf("Subscription still exists, can't continue.")
    70  		}
    71  
    72  		// Verify the error is an API error, not something else
    73  		_, ok := err.(awserr.Error)
    74  		if !ok {
    75  			return err
    76  		}
    77  	}
    78  
    79  	return nil
    80  }
    81  
    82  func testAccCheckAWSSNSTopicSubscriptionExists(n string) resource.TestCheckFunc {
    83  	return func(s *terraform.State) error {
    84  		rs, ok := s.RootModule().Resources[n]
    85  		if !ok {
    86  			return fmt.Errorf("Not found: %s", n)
    87  		}
    88  
    89  		if rs.Primary.ID == "" {
    90  			return fmt.Errorf("No SNS subscription with that ARN exists")
    91  		}
    92  
    93  		conn := testAccProvider.Meta().(*AWSClient).snsconn
    94  
    95  		params := &sns.GetSubscriptionAttributesInput{
    96  			SubscriptionArn: aws.String(rs.Primary.ID),
    97  		}
    98  		_, err := conn.GetSubscriptionAttributes(params)
    99  
   100  		if err != nil {
   101  			return err
   102  		}
   103  
   104  		return nil
   105  	}
   106  }
   107  
   108  func testAccAWSSNSTopicSubscriptionConfig(i int) string {
   109  	return fmt.Sprintf(`
   110  resource "aws_sns_topic" "test_topic" {
   111      name = "terraform-test-topic"
   112  }
   113  
   114  resource "aws_sqs_queue" "test_queue" {
   115  	name = "terraform-subscription-test-queue-%d"
   116  }
   117  
   118  resource "aws_sns_topic_subscription" "test_subscription" {
   119      topic_arn = "${aws_sns_topic.test_topic.arn}"
   120      protocol = "sqs"
   121      endpoint = "${aws_sqs_queue.test_queue.arn}"
   122  }
   123  `, i)
   124  }
   125  
   126  func testAccAWSSNSTopicSubscriptionConfig_autoConfirmingEndpoint(i int) string {
   127  	return fmt.Sprintf(`
   128  resource "aws_sns_topic" "test_topic" {
   129    name = "tf-acc-test-sns-%d"
   130  }
   131  
   132  resource "aws_api_gateway_rest_api" "test" {
   133    name        = "tf-acc-test-sns-%d"
   134    description = "Terraform Acceptance test for SNS subscription"
   135  }
   136  
   137  resource "aws_api_gateway_method" "test" {
   138    rest_api_id   = "${aws_api_gateway_rest_api.test.id}"
   139    resource_id   = "${aws_api_gateway_rest_api.test.root_resource_id}"
   140    http_method   = "POST"
   141    authorization = "NONE"
   142  }
   143  
   144  resource "aws_api_gateway_method_response" "test" {
   145    rest_api_id = "${aws_api_gateway_rest_api.test.id}"
   146    resource_id = "${aws_api_gateway_rest_api.test.root_resource_id}"
   147    http_method = "${aws_api_gateway_method.test.http_method}"
   148    status_code = "200"
   149  
   150    response_parameters {
   151      "method.response.header.Access-Control-Allow-Origin" = true
   152    }
   153  }
   154  
   155  resource "aws_api_gateway_integration" "test" {
   156    rest_api_id             = "${aws_api_gateway_rest_api.test.id}"
   157    resource_id             = "${aws_api_gateway_rest_api.test.root_resource_id}"
   158    http_method             = "${aws_api_gateway_method.test.http_method}"
   159    integration_http_method = "POST"
   160    type                    = "AWS"
   161    uri                     = "${aws_lambda_function.lambda.invoke_arn}"
   162  }
   163  
   164  resource "aws_api_gateway_integration_response" "test" {
   165    depends_on  = ["aws_api_gateway_integration.test"]
   166    rest_api_id = "${aws_api_gateway_rest_api.test.id}"
   167    resource_id = "${aws_api_gateway_rest_api.test.root_resource_id}"
   168    http_method = "${aws_api_gateway_method.test.http_method}"
   169    status_code = "${aws_api_gateway_method_response.test.status_code}"
   170  
   171    response_parameters {
   172      "method.response.header.Access-Control-Allow-Origin" = "'*'"
   173    }
   174  }
   175  
   176  resource "aws_iam_role" "iam_for_lambda" {
   177    name = "tf-acc-test-sns-%d"
   178  
   179    assume_role_policy = <<EOF
   180  {
   181    "Version": "2012-10-17",
   182    "Statement": [
   183      {
   184        "Action": "sts:AssumeRole",
   185        "Principal": {
   186          "Service": "lambda.amazonaws.com"
   187        },
   188        "Effect": "Allow",
   189        "Sid": ""
   190      }
   191    ]
   192  }
   193  EOF
   194  }
   195  
   196  resource "aws_iam_role_policy" "policy" {
   197    name = "tf-acc-test-sns-%d"
   198    role = "${aws_iam_role.iam_for_lambda.id}"
   199  
   200    policy = <<EOF
   201  {
   202    "Version": "2012-10-17",
   203    "Statement": [
   204      {
   205        "Action": [
   206          "logs:*"
   207        ],
   208        "Effect": "Allow",
   209        "Resource": "*"
   210      }
   211    ]
   212  }
   213  EOF
   214  }
   215  
   216  resource "aws_lambda_permission" "apigw_lambda" {
   217    statement_id  = "AllowExecutionFromAPIGateway"
   218    action        = "lambda:InvokeFunction"
   219    function_name = "${aws_lambda_function.lambda.arn}"
   220    principal     = "apigateway.amazonaws.com"
   221    source_arn    = "${aws_api_gateway_deployment.test.execution_arn}/*"
   222  }
   223  
   224  resource "aws_lambda_function" "lambda" {
   225    filename         = "test-fixtures/lambda_confirm_sns.zip"
   226    function_name    = "tf-acc-test-sns-%d"
   227    role             = "${aws_iam_role.iam_for_lambda.arn}"
   228    handler          = "main.confirm_subscription"
   229    source_code_hash = "${base64sha256(file("test-fixtures/lambda_confirm_sns.zip"))}"
   230    runtime          = "python3.6"
   231  }
   232  
   233  resource "aws_api_gateway_deployment" "test" {
   234    depends_on  = ["aws_api_gateway_integration_response.test"]
   235    rest_api_id = "${aws_api_gateway_rest_api.test.id}"
   236    stage_name  = "acctest"
   237  }
   238  
   239  resource "aws_sns_topic_subscription" "test_subscription" {
   240    depends_on             = ["aws_lambda_permission.apigw_lambda"]
   241    topic_arn              = "${aws_sns_topic.test_topic.arn}"
   242    protocol               = "https"
   243    endpoint               = "${aws_api_gateway_deployment.test.invoke_url}"
   244    endpoint_auto_confirms = true
   245  }
   246  `, i, i, i, i, i)
   247  }