diff --git a/aws/resource_aws_api_gateway_method.go b/aws/resource_aws_api_gateway_method.go index d35fb20ebc2..8989d060275 100644 --- a/aws/resource_aws_api_gateway_method.go +++ b/aws/resource_aws_api_gateway_method.go @@ -51,6 +51,13 @@ func resourceAwsApiGatewayMethod() *schema.Resource { Optional: true, }, + "authorization_scopes": &schema.Schema{ + Type: schema.TypeSet, + Elem: &schema.Schema{Type: schema.TypeString}, + Set: schema.HashString, + Optional: true, + }, + "api_key_required": &schema.Schema{ Type: schema.TypeBool, Optional: true, @@ -126,6 +133,10 @@ func resourceAwsApiGatewayMethodCreate(d *schema.ResourceData, meta interface{}) input.AuthorizerId = aws.String(v.(string)) } + if v, ok := d.GetOk("authorization_scopes"); ok { + input.AuthorizationScopes = expandStringList(v.(*schema.Set).List()) + } + if v, ok := d.GetOk("request_validator_id"); ok { input.RequestValidatorId = aws.String(v.(string)) } @@ -168,6 +179,10 @@ func resourceAwsApiGatewayMethodRead(d *schema.ResourceData, meta interface{}) e d.Set("request_models", aws.StringValueMap(out.RequestModels)) d.Set("request_validator_id", out.RequestValidatorId) + if err := d.Set("authorization_scopes", flattenStringList(out.AuthorizationScopes)); err != nil { + return fmt.Errorf("error setting authorization_scopes: %s", err) + } + return nil } @@ -229,6 +244,32 @@ func resourceAwsApiGatewayMethodUpdate(d *schema.ResourceData, meta interface{}) }) } + if d.HasChange("authorization_scopes") { + old, new := d.GetChange("authorization_scopes") + path := "/authorizationScopes" + + os := old.(*schema.Set) + ns := new.(*schema.Set) + + additionList := ns.Difference(os) + for _, v := range additionList.List() { + operations = append(operations, &apigateway.PatchOperation{ + Op: aws.String("add"), + Path: aws.String(path), + Value: aws.String(v.(string)), + }) + } + + removalList := os.Difference(ns) + for _, v := range removalList.List() { + operations = append(operations, &apigateway.PatchOperation{ + Op: aws.String("remove"), + Path: aws.String(path), + Value: aws.String(v.(string)), + }) + } + } + if d.HasChange("api_key_required") { operations = append(operations, &apigateway.PatchOperation{ Op: aws.String("replace"), diff --git a/aws/resource_aws_api_gateway_method_test.go b/aws/resource_aws_api_gateway_method_test.go index a6ca2d832d1..fc464425fe7 100644 --- a/aws/resource_aws_api_gateway_method_test.go +++ b/aws/resource_aws_api_gateway_method_test.go @@ -87,6 +87,52 @@ func TestAccAWSAPIGatewayMethod_customauthorizer(t *testing.T) { }) } +func TestAccAWSAPIGatewayMethod_cognitoauthorizer(t *testing.T) { + var conf apigateway.Method + rInt := acctest.RandInt() + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSAPIGatewayMethodDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSAPIGatewayMethodConfigWithCognitoAuthorizer(rInt), + 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", "COGNITO_USER_POOLS"), + resource.TestMatchResourceAttr( + "aws_api_gateway_method.test", "authorizer_id", regexp.MustCompile("^[a-z0-9]{6}$")), + resource.TestCheckResourceAttr( + "aws_api_gateway_method.test", "request_models.application/json", "Error"), + resource.TestCheckResourceAttr( + "aws_api_gateway_method.test", "authorization_scopes.#", "2"), + ), + }, + + { + Config: testAccAWSAPIGatewayMethodConfigWithCognitoAuthorizerUpdate(rInt), + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSAPIGatewayMethodExists("aws_api_gateway_method.test", &conf), + testAccCheckAWSAPIGatewayMethodAttributesUpdate(&conf), + resource.TestCheckResourceAttr( + "aws_api_gateway_method.test", "authorization", "COGNITO_USER_POOLS"), + resource.TestMatchResourceAttr( + "aws_api_gateway_method.test", "authorizer_id", regexp.MustCompile("^[a-z0-9]{6}$")), + resource.TestCheckResourceAttr( + "aws_api_gateway_method.test", "request_models.application/json", "Error"), + resource.TestCheckResourceAttr( + "aws_api_gateway_method.test", "authorization_scopes.#", "3"), + ), + }, + }, + }) +} + func TestAccAWSAPIGatewayMethod_customrequestvalidator(t *testing.T) { var conf apigateway.Method rInt := acctest.RandInt() @@ -130,7 +176,7 @@ func testAccCheckAWSAPIGatewayMethodAttributes(conf *apigateway.Method) resource if *conf.HttpMethod != "GET" { return fmt.Errorf("Wrong HttpMethod: %q", *conf.HttpMethod) } - if *conf.AuthorizationType != "NONE" && *conf.AuthorizationType != "CUSTOM" { + if *conf.AuthorizationType != "NONE" && *conf.AuthorizationType != "CUSTOM" && *conf.AuthorizationType != "COGNITO_USER_POOLS" { return fmt.Errorf("Wrong Authorization: %q", *conf.AuthorizationType) } @@ -337,6 +383,171 @@ resource "aws_api_gateway_method" "test" { }`, rInt, rInt, rInt, rInt, rInt) } +func testAccAWSAPIGatewayMethodConfigWithCognitoAuthorizer(rInt int) string { + return fmt.Sprintf(` +resource "aws_api_gateway_rest_api" "test" { + name = "tf-acc-test-cognito-auth-%d" +} + +resource "aws_iam_role" "invocation_role" { + name = "tf_acc_api_gateway_auth_invocation_role-%d" + path = "/" + assume_role_policy = <