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