From 402e31878fbbae04bc5e36b83a7373605ca70912 Mon Sep 17 00:00:00 2001 From: John Jelinek IV Date: Tue, 17 May 2016 18:56:59 -0500 Subject: [PATCH] [WIP] AWS APIGateway Custom Authorizer --- .../resource_aws_api_gateway_authorizer.go | 7 +- .../aws/resource_aws_api_gateway_method.go | 6 + .../resource_aws_api_gateway_method_test.go | 137 +++++++++++++++++- .../aws/r/api_gateway_method.html.markdown | 4 +- 4 files changed, 151 insertions(+), 3 deletions(-) diff --git a/builtin/providers/aws/resource_aws_api_gateway_authorizer.go b/builtin/providers/aws/resource_aws_api_gateway_authorizer.go index 6b005181381b..8f881e185ccf 100644 --- a/builtin/providers/aws/resource_aws_api_gateway_authorizer.go +++ b/builtin/providers/aws/resource_aws_api_gateway_authorizer.go @@ -3,6 +3,7 @@ package aws import ( "fmt" "log" + "strings" "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/awserr" @@ -200,7 +201,11 @@ func resourceAwsApiGatewayAuthorizerDelete(d *schema.ResourceData, meta interfac log.Printf("[INFO] Deleting API Gateway Authorizer: %s", input) _, err := conn.DeleteAuthorizer(&input) if err != nil { - return fmt.Errorf("Deleting API Gateway Authorizer failed: %s", err) + // XXX: Figure out a way to delete the method that depends on the authorizer first + // otherwise the authorizer will be dangling until the API is deleted + if !strings.Contains(err.Error(), "ConflictException") { + return fmt.Errorf("Deleting API Gateway Authorizer failed: %s", err) + } } return nil diff --git a/builtin/providers/aws/resource_aws_api_gateway_method.go b/builtin/providers/aws/resource_aws_api_gateway_method.go index ad9b327af877..428f7988fe00 100644 --- a/builtin/providers/aws/resource_aws_api_gateway_method.go +++ b/builtin/providers/aws/resource_aws_api_gateway_method.go @@ -45,6 +45,11 @@ func resourceAwsApiGatewayMethod() *schema.Resource { Required: true, }, + "authorizer_id": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + }, + "api_key_required": &schema.Schema{ Type: schema.TypeBool, Optional: true, @@ -89,6 +94,7 @@ func resourceAwsApiGatewayMethodCreate(d *schema.ResourceData, meta interface{}) // TODO reimplement once [GH-2143](https://github.com/hashicorp/terraform/issues/2143) has been implemented RequestParameters: aws.BoolMap(parameters), ApiKeyRequired: aws.Bool(d.Get("api_key_required").(bool)), + AuthorizerId: aws.String(d.Get("authorizer_id").(string)), }) if err != nil { return fmt.Errorf("Error creating API Gateway Method: %s", err) diff --git a/builtin/providers/aws/resource_aws_api_gateway_method_test.go b/builtin/providers/aws/resource_aws_api_gateway_method_test.go index 23b2593ae362..c0b3fec40ee0 100644 --- a/builtin/providers/aws/resource_aws_api_gateway_method_test.go +++ b/builtin/providers/aws/resource_aws_api_gateway_method_test.go @@ -44,12 +44,45 @@ func TestAccAWSAPIGatewayMethod_basic(t *testing.T) { }) } +func TestAccAWSAPIGatewayMethod_customauthorizer(t *testing.T) { + var conf apigateway.Method + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSAPIGatewayMethodDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccAWSAPIGatewayMethodConfigWithCustomAuthorizer, + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSAPIGatewayMethodExists("aws_api_gateway_method.test", &conf), + testAccCheckAWSAPIGatewayMethodAttributes(&conf), + resource.TestCheckResourceAttr( + "aws_api_gateway_method.test", "http_method", "GET"), + resource.TestCheckResourceAttr( + "aws_api_gateway_method.test", "authorization", "CUSTOM"), + resource.TestCheckResourceAttr( + "aws_api_gateway_method.test", "request_models.application/json", "Error"), + ), + }, + + resource.TestStep{ + Config: testAccAWSAPIGatewayMethodConfigUpdate, + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSAPIGatewayMethodExists("aws_api_gateway_method.test", &conf), + testAccCheckAWSAPIGatewayMethodAttributesUpdate(&conf), + ), + }, + }, + }) +} + func testAccCheckAWSAPIGatewayMethodAttributes(conf *apigateway.Method) resource.TestCheckFunc { return func(s *terraform.State) error { if *conf.HttpMethod != "GET" { return fmt.Errorf("Wrong HttpMethod: %q", *conf.HttpMethod) } - if *conf.AuthorizationType != "NONE" { + if *conf.AuthorizationType != "NONE" && *conf.AuthorizationType != "CUSTOM" { return fmt.Errorf("Wrong Authorization: %q", *conf.AuthorizationType) } @@ -154,6 +187,108 @@ func testAccCheckAWSAPIGatewayMethodDestroy(s *terraform.State) error { return nil } +const testAccAWSAPIGatewayMethodConfigWithCustomAuthorizer = ` +resource "aws_api_gateway_rest_api" "test" { + name = "test" +} + +resource "aws_iam_role" "invocation_role" { + name = "tf_acc_api_gateway_auth_invocation_role" + path = "/" + assume_role_policy = <