github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/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 runtime = "nodejs4.3" 265 } 266 267 resource "aws_api_gateway_authorizer" "test" { 268 name = "tf-acc-test-authorizer" 269 rest_api_id = "${aws_api_gateway_rest_api.test.id}" 270 authorizer_uri = "arn:aws:apigateway:region:lambda:path/2015-03-31/functions/${aws_lambda_function.authorizer.arn}/invocations" 271 authorizer_credentials = "${aws_iam_role.invocation_role.arn}" 272 } 273 274 resource "aws_api_gateway_resource" "test" { 275 rest_api_id = "${aws_api_gateway_rest_api.test.id}" 276 parent_id = "${aws_api_gateway_rest_api.test.root_resource_id}" 277 path_part = "test" 278 } 279 280 resource "aws_api_gateway_method" "test" { 281 rest_api_id = "${aws_api_gateway_rest_api.test.id}" 282 resource_id = "${aws_api_gateway_resource.test.id}" 283 http_method = "GET" 284 authorization = "CUSTOM" 285 authorizer_id = "${aws_api_gateway_authorizer.test.id}" 286 287 request_models = { 288 "application/json" = "Error" 289 } 290 291 request_parameters = { 292 "method.request.header.Content-Type" = false 293 "method.request.querystring.page" = true 294 } 295 } 296 ` 297 298 const testAccAWSAPIGatewayMethodConfig = ` 299 resource "aws_api_gateway_rest_api" "test" { 300 name = "test" 301 } 302 303 resource "aws_api_gateway_resource" "test" { 304 rest_api_id = "${aws_api_gateway_rest_api.test.id}" 305 parent_id = "${aws_api_gateway_rest_api.test.root_resource_id}" 306 path_part = "test" 307 } 308 309 resource "aws_api_gateway_method" "test" { 310 rest_api_id = "${aws_api_gateway_rest_api.test.id}" 311 resource_id = "${aws_api_gateway_resource.test.id}" 312 http_method = "GET" 313 authorization = "NONE" 314 315 request_models = { 316 "application/json" = "Error" 317 } 318 319 request_parameters = { 320 "method.request.header.Content-Type" = false, 321 "method.request.querystring.page" = true 322 } 323 } 324 ` 325 326 const testAccAWSAPIGatewayMethodConfigUpdate = ` 327 resource "aws_api_gateway_rest_api" "test" { 328 name = "test" 329 } 330 331 resource "aws_api_gateway_resource" "test" { 332 rest_api_id = "${aws_api_gateway_rest_api.test.id}" 333 parent_id = "${aws_api_gateway_rest_api.test.root_resource_id}" 334 path_part = "test" 335 } 336 337 resource "aws_api_gateway_method" "test" { 338 rest_api_id = "${aws_api_gateway_rest_api.test.id}" 339 resource_id = "${aws_api_gateway_resource.test.id}" 340 http_method = "GET" 341 authorization = "NONE" 342 343 request_models = { 344 "application/json" = "Error" 345 } 346 347 request_parameters = { 348 "method.request.querystring.page" = false 349 } 350 } 351 `