Skip to content

Commit

Permalink
Merge pull request #13833 from ewbankkit/b-aws_apigatewayv2_route-upd…
Browse files Browse the repository at this point in the history
…ate-route-key

r/aws_apigatewayv2_route: Update route key
  • Loading branch information
breathingdust authored Jul 22, 2020
2 parents 910e8e9 + 38b9aae commit 8c7e4b1
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 18 deletions.
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) + `
resource "aws_apigatewayv2_route" "test" {
Expand Down Expand Up @@ -457,7 +504,7 @@ func testAccAWSAPIGatewayV2RouteConfig_jwtAuthorization(rName string) string {
return testAccAWSAPIGatewayV2AuthorizerConfig_jwt(rName) + `
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 All @@ -471,7 +518,7 @@ func testAccAWSAPIGatewayV2RouteConfig_jwtAuthorizationUpdated(rName string) str
return testAccAWSAPIGatewayV2AuthorizerConfig_jwt(rName) + `
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) + `
Expand Down

0 comments on commit 8c7e4b1

Please sign in to comment.