Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

r/aws_apigatewayv2_route: Update route key #13833

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions aws/resource_aws_apigatewayv2_route.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,9 @@ func resourceAwsApiGatewayV2RouteUpdate(d *schema.ResourceData, meta interface{}
if d.HasChange("request_models") {
req.RequestModels = stringMapToPointers(d.Get("request_models").(map[string]interface{}))
}
if d.HasChange("route_key") {
req.RouteKey = aws.String(d.Get("route_key").(string))
}
if d.HasChange("route_response_selection_expression") {
req.RouteResponseSelectionExpression = aws.String(d.Get("route_response_selection_expression").(string))
}
Expand Down
94 changes: 76 additions & 18 deletions aws/resource_aws_apigatewayv2_route_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func TestAccAWSAPIGatewayV2Route_disappears(t *testing.T) {
Config: testAccAWSAPIGatewayV2RouteConfig_basic(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSAPIGatewayV2RouteExists(resourceName, &apiId, &v),
testAccCheckAWSAPIGatewayV2RouteDisappears(&apiId, &v),
testAccCheckResourceDisappears(testAccProvider, resourceAwsApiGatewayV2Route(), resourceName),
),
ExpectNonEmptyPlan: true,
},
Expand Down Expand Up @@ -148,7 +148,7 @@ func TestAccAWSAPIGatewayV2Route_JwtAuthorization(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "model_selection_expression", ""),
resource.TestCheckResourceAttr(resourceName, "operation_name", ""),
resource.TestCheckResourceAttr(resourceName, "request_models.%", "0"),
resource.TestCheckResourceAttr(resourceName, "route_key", "$connect"),
resource.TestCheckResourceAttr(resourceName, "route_key", "GET /test"),
resource.TestCheckResourceAttr(resourceName, "route_response_selection_expression", ""),
resource.TestCheckResourceAttr(resourceName, "target", ""),
),
Expand All @@ -170,7 +170,7 @@ func TestAccAWSAPIGatewayV2Route_JwtAuthorization(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "model_selection_expression", ""),
resource.TestCheckResourceAttr(resourceName, "operation_name", ""),
resource.TestCheckResourceAttr(resourceName, "request_models.%", "0"),
resource.TestCheckResourceAttr(resourceName, "route_key", "$connect"),
resource.TestCheckResourceAttr(resourceName, "route_key", "GET /test"),
resource.TestCheckResourceAttr(resourceName, "route_response_selection_expression", ""),
resource.TestCheckResourceAttr(resourceName, "target", ""),
),
Expand Down Expand Up @@ -322,6 +322,57 @@ func TestAccAWSAPIGatewayV2Route_Target(t *testing.T) {
})
}

func TestAccAWSAPIGatewayV2Route_UpdateRouteKey(t *testing.T) {
var apiId string
var v apigatewayv2.GetRouteOutput
resourceName := "aws_apigatewayv2_route.test"
rName := acctest.RandomWithPrefix("tf-acc-test")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSAPIGatewayV2RouteDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSAPIGatewayV2RouteConfig_routeKey(rName, "GET /path"),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSAPIGatewayV2RouteExists(resourceName, &apiId, &v),
resource.TestCheckResourceAttr(resourceName, "api_key_required", "false"),
resource.TestCheckResourceAttr(resourceName, "authorization_type", apigatewayv2.AuthorizationTypeNone),
resource.TestCheckResourceAttr(resourceName, "authorizer_id", ""),
resource.TestCheckResourceAttr(resourceName, "model_selection_expression", ""),
resource.TestCheckResourceAttr(resourceName, "operation_name", ""),
resource.TestCheckResourceAttr(resourceName, "request_models.%", "0"),
resource.TestCheckResourceAttr(resourceName, "route_key", "GET /path"),
resource.TestCheckResourceAttr(resourceName, "route_response_selection_expression", ""),
resource.TestCheckResourceAttr(resourceName, "target", ""),
),
},
{
Config: testAccAWSAPIGatewayV2RouteConfig_routeKey(rName, "POST /new/path"),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSAPIGatewayV2RouteExists(resourceName, &apiId, &v),
resource.TestCheckResourceAttr(resourceName, "api_key_required", "false"),
resource.TestCheckResourceAttr(resourceName, "authorization_type", apigatewayv2.AuthorizationTypeNone),
resource.TestCheckResourceAttr(resourceName, "authorizer_id", ""),
resource.TestCheckResourceAttr(resourceName, "model_selection_expression", ""),
resource.TestCheckResourceAttr(resourceName, "operation_name", ""),
resource.TestCheckResourceAttr(resourceName, "request_models.%", "0"),
resource.TestCheckResourceAttr(resourceName, "route_key", "POST /new/path"),
resource.TestCheckResourceAttr(resourceName, "route_response_selection_expression", ""),
resource.TestCheckResourceAttr(resourceName, "target", ""),
),
},
{
ResourceName: resourceName,
ImportStateIdFunc: testAccAWSAPIGatewayV2RouteImportStateIdFunc(resourceName),
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testAccCheckAWSAPIGatewayV2RouteDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).apigatewayv2conn

Expand All @@ -347,19 +398,6 @@ func testAccCheckAWSAPIGatewayV2RouteDestroy(s *terraform.State) error {
return nil
}

func testAccCheckAWSAPIGatewayV2RouteDisappears(apiId *string, v *apigatewayv2.GetRouteOutput) resource.TestCheckFunc {
return func(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).apigatewayv2conn

_, err := conn.DeleteRoute(&apigatewayv2.DeleteRouteInput{
ApiId: apiId,
RouteId: v.RouteId,
})

return err
}
}

func testAccCheckAWSAPIGatewayV2RouteExists(n string, vApiId *string, v *apigatewayv2.GetRouteOutput) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
Expand Down Expand Up @@ -421,6 +459,15 @@ resource "aws_apigatewayv2_api" "test" {
`, rName)
}

func testAccAWSAPIGatewayV2RouteConfig_apiHttp(rName string) string {
return fmt.Sprintf(`
resource "aws_apigatewayv2_api" "test" {
name = %[1]q
protocol_type = "HTTP"
}
`, rName)
}

func testAccAWSAPIGatewayV2RouteConfig_basic(rName string) string {
return testAccAWSAPIGatewayV2RouteConfig_apiWebSocket(rName) + fmt.Sprintf(`
resource "aws_apigatewayv2_route" "test" {
Expand Down Expand Up @@ -457,7 +504,7 @@ func testAccAWSAPIGatewayV2RouteConfig_jwtAuthorization(rName string) string {
return testAccAWSAPIGatewayV2AuthorizerConfig_jwt(rName) + fmt.Sprintf(`
resource "aws_apigatewayv2_route" "test" {
api_id = "${aws_apigatewayv2_api.test.id}"
route_key = "$connect"
route_key = "GET /test"
Copy link
Contributor Author

@ewbankkit ewbankkit Jun 18, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Required or else

--- FAIL: TestAccAWSAPIGatewayV2Route_JwtAuthorization (39.56s)
    testing.go:684: Step 0 error: errors during apply:
        
        Error: error creating API Gateway v2 route: BadRequestException: The provided route key is not formatted properly for HTTP protocol. Format should be "[HTTP METHOD] /[RESOURCE PATH]" or "$default"
        
          on /tmp/tf-test641911466/main.tf line 155:
          (source code not available)


authorization_type = "JWT"
authorizer_id = "${aws_apigatewayv2_authorizer.test.id}"
Expand All @@ -471,7 +518,7 @@ func testAccAWSAPIGatewayV2RouteConfig_jwtAuthorizationUpdated(rName string) str
return testAccAWSAPIGatewayV2AuthorizerConfig_jwt(rName) + fmt.Sprintf(`
resource "aws_apigatewayv2_route" "test" {
api_id = "${aws_apigatewayv2_api.test.id}"
route_key = "$connect"
route_key = "GET /test"

authorization_type = "JWT"
authorizer_id = "${aws_apigatewayv2_authorizer.test.id}"
Expand Down Expand Up @@ -509,6 +556,17 @@ resource "aws_apigatewayv2_route" "test" {
`)
}

func testAccAWSAPIGatewayV2RouteConfig_routeKey(rName, routeKey string) string {
return composeConfig(
testAccAWSAPIGatewayV2RouteConfig_apiHttp(rName),
fmt.Sprintf(`
resource "aws_apigatewayv2_route" "test" {
api_id = "${aws_apigatewayv2_api.test.id}"
route_key = %[1]q
}
`, routeKey))
}

// Simple attributes - No authorization, models or targets.
func testAccAWSAPIGatewayV2RouteConfig_simpleAttributes(rName string) string {
return testAccAWSAPIGatewayV2RouteConfig_apiWebSocket(rName) + fmt.Sprintf(`
Expand Down