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