github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/aws/resource_aws_api_gateway_method_test.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"regexp"
     6  	"testing"
     7  
     8  	"github.com/aws/aws-sdk-go/aws"
     9  	"github.com/aws/aws-sdk-go/aws/awserr"
    10  	"github.com/aws/aws-sdk-go/service/apigateway"
    11  	"github.com/hashicorp/terraform/helper/acctest"
    12  	"github.com/hashicorp/terraform/helper/resource"
    13  	"github.com/hashicorp/terraform/terraform"
    14  )
    15  
    16  func TestAccAWSAPIGatewayMethod_basic(t *testing.T) {
    17  	var conf apigateway.Method
    18  	rInt := acctest.RandInt()
    19  
    20  	resource.Test(t, resource.TestCase{
    21  		PreCheck:     func() { testAccPreCheck(t) },
    22  		Providers:    testAccProviders,
    23  		CheckDestroy: testAccCheckAWSAPIGatewayMethodDestroy,
    24  		Steps: []resource.TestStep{
    25  			{
    26  				Config: testAccAWSAPIGatewayMethodConfig(rInt),
    27  				Check: resource.ComposeTestCheckFunc(
    28  					testAccCheckAWSAPIGatewayMethodExists("aws_api_gateway_method.test", &conf),
    29  					testAccCheckAWSAPIGatewayMethodAttributes(&conf),
    30  					resource.TestCheckResourceAttr(
    31  						"aws_api_gateway_method.test", "http_method", "GET"),
    32  					resource.TestCheckResourceAttr(
    33  						"aws_api_gateway_method.test", "authorization", "NONE"),
    34  					resource.TestCheckResourceAttr(
    35  						"aws_api_gateway_method.test", "request_models.application/json", "Error"),
    36  				),
    37  			},
    38  
    39  			{
    40  				Config: testAccAWSAPIGatewayMethodConfigUpdate(rInt),
    41  				Check: resource.ComposeTestCheckFunc(
    42  					testAccCheckAWSAPIGatewayMethodExists("aws_api_gateway_method.test", &conf),
    43  					testAccCheckAWSAPIGatewayMethodAttributesUpdate(&conf),
    44  				),
    45  			},
    46  		},
    47  	})
    48  }
    49  
    50  func TestAccAWSAPIGatewayMethod_customauthorizer(t *testing.T) {
    51  	var conf apigateway.Method
    52  	rInt := acctest.RandInt()
    53  
    54  	resource.Test(t, resource.TestCase{
    55  		PreCheck:     func() { testAccPreCheck(t) },
    56  		Providers:    testAccProviders,
    57  		CheckDestroy: testAccCheckAWSAPIGatewayMethodDestroy,
    58  		Steps: []resource.TestStep{
    59  			{
    60  				Config: testAccAWSAPIGatewayMethodConfigWithCustomAuthorizer(rInt),
    61  				Check: resource.ComposeTestCheckFunc(
    62  					testAccCheckAWSAPIGatewayMethodExists("aws_api_gateway_method.test", &conf),
    63  					testAccCheckAWSAPIGatewayMethodAttributes(&conf),
    64  					resource.TestCheckResourceAttr(
    65  						"aws_api_gateway_method.test", "http_method", "GET"),
    66  					resource.TestCheckResourceAttr(
    67  						"aws_api_gateway_method.test", "authorization", "CUSTOM"),
    68  					resource.TestMatchResourceAttr(
    69  						"aws_api_gateway_method.test", "authorizer_id", regexp.MustCompile("^[a-z0-9]{6}$")),
    70  					resource.TestCheckResourceAttr(
    71  						"aws_api_gateway_method.test", "request_models.application/json", "Error"),
    72  				),
    73  			},
    74  
    75  			{
    76  				Config: testAccAWSAPIGatewayMethodConfigUpdate(rInt),
    77  				Check: resource.ComposeTestCheckFunc(
    78  					testAccCheckAWSAPIGatewayMethodExists("aws_api_gateway_method.test", &conf),
    79  					testAccCheckAWSAPIGatewayMethodAttributesUpdate(&conf),
    80  					resource.TestCheckResourceAttr(
    81  						"aws_api_gateway_method.test", "authorization", "NONE"),
    82  					resource.TestCheckResourceAttr(
    83  						"aws_api_gateway_method.test", "authorizer_id", ""),
    84  				),
    85  			},
    86  		},
    87  	})
    88  }
    89  
    90  func testAccCheckAWSAPIGatewayMethodAttributes(conf *apigateway.Method) resource.TestCheckFunc {
    91  	return func(s *terraform.State) error {
    92  		if *conf.HttpMethod != "GET" {
    93  			return fmt.Errorf("Wrong HttpMethod: %q", *conf.HttpMethod)
    94  		}
    95  		if *conf.AuthorizationType != "NONE" && *conf.AuthorizationType != "CUSTOM" {
    96  			return fmt.Errorf("Wrong Authorization: %q", *conf.AuthorizationType)
    97  		}
    98  
    99  		if val, ok := conf.RequestParameters["method.request.header.Content-Type"]; !ok {
   100  			return fmt.Errorf("missing Content-Type RequestParameters")
   101  		} else {
   102  			if *val != false {
   103  				return fmt.Errorf("wrong Content-Type RequestParameters value")
   104  			}
   105  		}
   106  		if val, ok := conf.RequestParameters["method.request.querystring.page"]; !ok {
   107  			return fmt.Errorf("missing page RequestParameters")
   108  		} else {
   109  			if *val != true {
   110  				return fmt.Errorf("wrong query string RequestParameters value")
   111  			}
   112  		}
   113  
   114  		return nil
   115  	}
   116  }
   117  
   118  func testAccCheckAWSAPIGatewayMethodAttributesUpdate(conf *apigateway.Method) resource.TestCheckFunc {
   119  	return func(s *terraform.State) error {
   120  		if *conf.HttpMethod != "GET" {
   121  			return fmt.Errorf("Wrong HttpMethod: %q", *conf.HttpMethod)
   122  		}
   123  		if conf.RequestParameters["method.request.header.Content-Type"] != nil {
   124  			return fmt.Errorf("Content-Type RequestParameters shouldn't exist")
   125  		}
   126  		if val, ok := conf.RequestParameters["method.request.querystring.page"]; !ok {
   127  			return fmt.Errorf("missing updated page RequestParameters")
   128  		} else {
   129  			if *val != false {
   130  				return fmt.Errorf("wrong query string RequestParameters updated value")
   131  			}
   132  		}
   133  
   134  		return nil
   135  	}
   136  }
   137  
   138  func testAccCheckAWSAPIGatewayMethodExists(n string, res *apigateway.Method) resource.TestCheckFunc {
   139  	return func(s *terraform.State) error {
   140  		rs, ok := s.RootModule().Resources[n]
   141  		if !ok {
   142  			return fmt.Errorf("Not found: %s", n)
   143  		}
   144  
   145  		if rs.Primary.ID == "" {
   146  			return fmt.Errorf("No API Gateway Method ID is set")
   147  		}
   148  
   149  		conn := testAccProvider.Meta().(*AWSClient).apigateway
   150  
   151  		req := &apigateway.GetMethodInput{
   152  			HttpMethod: aws.String("GET"),
   153  			ResourceId: aws.String(s.RootModule().Resources["aws_api_gateway_resource.test"].Primary.ID),
   154  			RestApiId:  aws.String(s.RootModule().Resources["aws_api_gateway_rest_api.test"].Primary.ID),
   155  		}
   156  		describe, err := conn.GetMethod(req)
   157  		if err != nil {
   158  			return err
   159  		}
   160  
   161  		*res = *describe
   162  
   163  		return nil
   164  	}
   165  }
   166  
   167  func testAccCheckAWSAPIGatewayMethodDestroy(s *terraform.State) error {
   168  	conn := testAccProvider.Meta().(*AWSClient).apigateway
   169  
   170  	for _, rs := range s.RootModule().Resources {
   171  		if rs.Type != "aws_api_gateway_method" {
   172  			continue
   173  		}
   174  
   175  		req := &apigateway.GetMethodInput{
   176  			HttpMethod: aws.String("GET"),
   177  			ResourceId: aws.String(s.RootModule().Resources["aws_api_gateway_resource.test"].Primary.ID),
   178  			RestApiId:  aws.String(s.RootModule().Resources["aws_api_gateway_rest_api.test"].Primary.ID),
   179  		}
   180  		_, err := conn.GetMethod(req)
   181  
   182  		if err == nil {
   183  			return fmt.Errorf("API Gateway Method still exists")
   184  		}
   185  
   186  		aws2err, ok := err.(awserr.Error)
   187  		if !ok {
   188  			return err
   189  		}
   190  		if aws2err.Code() != "NotFoundException" {
   191  			return err
   192  		}
   193  
   194  		return nil
   195  	}
   196  
   197  	return nil
   198  }
   199  
   200  func testAccAWSAPIGatewayMethodConfigWithCustomAuthorizer(rInt int) string {
   201  	return fmt.Sprintf(`
   202  resource "aws_api_gateway_rest_api" "test" {
   203    name = "tf-acc-test-custom-auth-%d"
   204  }
   205  
   206  resource "aws_iam_role" "invocation_role" {
   207    name = "tf_acc_api_gateway_auth_invocation_role-%d"
   208    path = "/"
   209    assume_role_policy = <<EOF
   210  {
   211    "Version": "2012-10-17",
   212    "Statement": [
   213      {
   214        "Action": "sts:AssumeRole",
   215        "Principal": {
   216          "Service": "apigateway.amazonaws.com"
   217        },
   218        "Effect": "Allow",
   219        "Sid": ""
   220      }
   221    ]
   222  }
   223  EOF
   224  }
   225  
   226  resource "aws_iam_role_policy" "invocation_policy" {
   227    name = "tf-acc-api-gateway-%d"
   228    role = "${aws_iam_role.invocation_role.id}"
   229    policy = <<EOF
   230  {
   231    "Version": "2012-10-17",
   232    "Statement": [
   233      {
   234        "Action": "lambda:InvokeFunction",
   235        "Effect": "Allow",
   236        "Resource": "${aws_lambda_function.authorizer.arn}"
   237      }
   238    ]
   239  }
   240  EOF
   241  }
   242  
   243  resource "aws_iam_role" "iam_for_lambda" {
   244    name = "tf_acc_iam_for_lambda_api_gateway_authorizer-%d"
   245    assume_role_policy = <<EOF
   246  {
   247    "Version": "2012-10-17",
   248    "Statement": [
   249      {
   250        "Action": "sts:AssumeRole",
   251        "Principal": {
   252          "Service": "lambda.amazonaws.com"
   253        },
   254        "Effect": "Allow",
   255        "Sid": ""
   256      }
   257    ]
   258  }
   259  EOF
   260  }
   261  
   262  resource "aws_lambda_function" "authorizer" {
   263    filename = "test-fixtures/lambdatest.zip"
   264    source_code_hash = "${base64sha256(file("test-fixtures/lambdatest.zip"))}"
   265    function_name = "tf_acc_api_gateway_authorizer_%d"
   266    role = "${aws_iam_role.iam_for_lambda.arn}"
   267    handler = "exports.example"
   268  	runtime = "nodejs4.3"
   269  }
   270  
   271  resource "aws_api_gateway_authorizer" "test" {
   272    name = "tf-acc-test-authorizer"
   273    rest_api_id = "${aws_api_gateway_rest_api.test.id}"
   274    authorizer_uri = "arn:aws:apigateway:region:lambda:path/2015-03-31/functions/${aws_lambda_function.authorizer.arn}/invocations"
   275    authorizer_credentials = "${aws_iam_role.invocation_role.arn}"
   276  }
   277  
   278  resource "aws_api_gateway_resource" "test" {
   279    rest_api_id = "${aws_api_gateway_rest_api.test.id}"
   280    parent_id = "${aws_api_gateway_rest_api.test.root_resource_id}"
   281    path_part = "test"
   282  }
   283  
   284  resource "aws_api_gateway_method" "test" {
   285    rest_api_id = "${aws_api_gateway_rest_api.test.id}"
   286    resource_id = "${aws_api_gateway_resource.test.id}"
   287    http_method = "GET"
   288    authorization = "CUSTOM"
   289    authorizer_id = "${aws_api_gateway_authorizer.test.id}"
   290  
   291    request_models = {
   292      "application/json" = "Error"
   293    }
   294  
   295    request_parameters = {
   296      "method.request.header.Content-Type" = false
   297  	  "method.request.querystring.page" = true
   298    }
   299  }`, rInt, rInt, rInt, rInt, rInt)
   300  }
   301  
   302  func testAccAWSAPIGatewayMethodConfig(rInt int) string {
   303  	return fmt.Sprintf(`
   304  resource "aws_api_gateway_rest_api" "test" {
   305    name = "tf-acc-test-apig-method-%d"
   306  }
   307  
   308  resource "aws_api_gateway_resource" "test" {
   309    rest_api_id = "${aws_api_gateway_rest_api.test.id}"
   310    parent_id = "${aws_api_gateway_rest_api.test.root_resource_id}"
   311    path_part = "test"
   312  }
   313  
   314  resource "aws_api_gateway_method" "test" {
   315    rest_api_id = "${aws_api_gateway_rest_api.test.id}"
   316    resource_id = "${aws_api_gateway_resource.test.id}"
   317    http_method = "GET"
   318    authorization = "NONE"
   319  
   320    request_models = {
   321      "application/json" = "Error"
   322    }
   323  
   324    request_parameters = {
   325      "method.request.header.Content-Type" = false,
   326  	  "method.request.querystring.page" = true
   327    }
   328  }
   329  `, rInt)
   330  }
   331  
   332  func testAccAWSAPIGatewayMethodConfigUpdate(rInt int) string {
   333  	return fmt.Sprintf(`
   334  resource "aws_api_gateway_rest_api" "test" {
   335    name = "tf-acc-test-apig-method-%d"
   336  }
   337  
   338  resource "aws_api_gateway_resource" "test" {
   339    rest_api_id = "${aws_api_gateway_rest_api.test.id}"
   340    parent_id = "${aws_api_gateway_rest_api.test.root_resource_id}"
   341    path_part = "test"
   342  }
   343  
   344  resource "aws_api_gateway_method" "test" {
   345    rest_api_id = "${aws_api_gateway_rest_api.test.id}"
   346    resource_id = "${aws_api_gateway_resource.test.id}"
   347    http_method = "GET"
   348    authorization = "NONE"
   349  
   350    request_models = {
   351      "application/json" = "Error"
   352    }
   353  
   354    request_parameters = {
   355  	  "method.request.querystring.page" = false
   356    }
   357  }
   358  `, rInt)
   359  }