Skip to content

Commit

Permalink
Add path object to rewrite on gateway route
Browse files Browse the repository at this point in the history
  • Loading branch information
markitovtr1 authored and Marcos Romero committed Jul 19, 2023
1 parent 1aab36e commit 4f4d289
Show file tree
Hide file tree
Showing 4 changed files with 236 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .changelog/32448.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/aws_gateway_route: Add `path` structure to `rewrite` in `http_route` and `http2_route`
```
38 changes: 38 additions & 0 deletions internal/service/appmesh/gateway_route.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,27 @@ func resourceGatewayRouteSpecSchema() *schema.Schema {
},
},
AtLeastOneOf: []string{
fmt.Sprintf("spec.0.%s.0.action.0.rewrite.0.path", attrName),
fmt.Sprintf("spec.0.%s.0.action.0.rewrite.0.prefix", attrName),
fmt.Sprintf("spec.0.%s.0.action.0.rewrite.0.hostname", attrName),
},
},
"path": {
Type: schema.TypeList,
Optional: true,
MinItems: 1,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"exact": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringLenBetween(1, 255),
},
},
},
AtLeastOneOf: []string{
fmt.Sprintf("spec.0.%s.0.action.0.rewrite.0.path", attrName),
fmt.Sprintf("spec.0.%s.0.action.0.rewrite.0.prefix", attrName),
fmt.Sprintf("spec.0.%s.0.action.0.rewrite.0.hostname", attrName),
},
Expand Down Expand Up @@ -191,6 +212,7 @@ func resourceGatewayRouteSpecSchema() *schema.Schema {
},
},
AtLeastOneOf: []string{
fmt.Sprintf("spec.0.%s.0.action.0.rewrite.0.path", attrName),
fmt.Sprintf("spec.0.%s.0.action.0.rewrite.0.prefix", attrName),
fmt.Sprintf("spec.0.%s.0.action.0.rewrite.0.hostname", attrName),
},
Expand Down Expand Up @@ -783,6 +805,15 @@ func expandHTTPGatewayRouteRewrite(vHttpRouteRewrite []interface{}) *appmesh.Htt
routeRewrite.Hostname = routeHostnameRewrite
}

if vRoutePathRewrite, ok := mRouteRewrite["path"].([]interface{}); ok && len(vRoutePathRewrite) > 0 && vRoutePathRewrite[0] != nil {
mRoutePathRewrite := vRoutePathRewrite[0].(map[string]interface{})
routePathRewrite := &appmesh.HttpGatewayRoutePathRewrite{}
if vExact, ok := mRoutePathRewrite["exact"].(string); ok && vExact != "" {
routePathRewrite.Exact = aws.String(vExact)
}
routeRewrite.Path = routePathRewrite
}

if vRoutePrefixRewrite, ok := mRouteRewrite["prefix"].([]interface{}); ok && len(vRoutePrefixRewrite) > 0 && vRoutePrefixRewrite[0] != nil {
mRoutePrefixRewrite := vRoutePrefixRewrite[0].(map[string]interface{})
routePrefixRewrite := &appmesh.HttpGatewayRoutePrefixRewrite{}
Expand Down Expand Up @@ -1136,6 +1167,13 @@ func flattenHTTPGatewayRouteRewrite(routeRewrite *appmesh.HttpGatewayRouteRewrit
mRouteRewrite["hostname"] = []interface{}{mRewriteHostname}
}

if rewritePath := routeRewrite.Path; rewritePath != nil {
mRewritePath := map[string]interface{}{
"exact": aws.StringValue(rewritePath.Exact),
}
mRouteRewrite["path"] = []interface{}{mRewritePath}
}

if rewritePrefix := routeRewrite.Prefix; rewritePrefix != nil {
mRewritePrefix := map[string]interface{}{
"default_prefix": aws.StringValue(rewritePrefix.DefaultPrefix),
Expand Down
190 changes: 190 additions & 0 deletions internal/service/appmesh/gateway_route_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,37 @@ func testAccGatewayRoute_httpRoute(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "tags.%", "0"),
),
},
{
Config: testAccGatewayRouteConfig_httpRouteRewriteWithPath(meshName, vgName, grName),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckGatewayRouteExists(ctx, resourceName, &v),
resource.TestCheckResourceAttr(resourceName, "mesh_name", meshName),
acctest.CheckResourceAttrAccountID(resourceName, "mesh_owner"),
resource.TestCheckResourceAttr(resourceName, "name", grName),
resource.TestCheckResourceAttr(resourceName, "spec.#", "1"),
resource.TestCheckResourceAttr(resourceName, "spec.0.grpc_route.#", "0"),
resource.TestCheckResourceAttr(resourceName, "spec.0.http2_route.#", "0"),
resource.TestCheckResourceAttr(resourceName, "spec.0.http_route.#", "1"),
resource.TestCheckResourceAttr(resourceName, "spec.0.http_route.0.action.#", "1"),
resource.TestCheckResourceAttr(resourceName, "spec.0.http_route.0.action.0.target.#", "1"),
resource.TestCheckResourceAttr(resourceName, "spec.0.http_route.0.action.0.target.0.virtual_service.#", "1"),
resource.TestCheckResourceAttrPair(resourceName, "spec.0.http_route.0.action.0.target.0.virtual_service.0.virtual_service_name", vs1ResourceName, "name"),
resource.TestCheckResourceAttr(resourceName, "spec.0.http_route.0.action.0.rewrite.#", "1"),
resource.TestCheckResourceAttr(resourceName, "spec.0.http_route.0.action.0.rewrite.0.hostname.#", "0"),
resource.TestCheckResourceAttr(resourceName, "spec.0.http_route.0.action.0.rewrite.0.prefix.#", "0"),
resource.TestCheckResourceAttr(resourceName, "spec.0.http_route.0.action.0.rewrite.0.path.#", "1"),
resource.TestCheckResourceAttr(resourceName, "spec.0.http_route.0.action.0.rewrite.0.path.0.exact", "/rewrite_path"),
resource.TestCheckResourceAttr(resourceName, "spec.0.http_route.0.match.#", "1"),
resource.TestCheckResourceAttr(resourceName, "spec.0.http_route.0.match.0.path.#", "1"),
resource.TestCheckResourceAttr(resourceName, "spec.0.http_route.0.match.0.path.0.exact", "/match_path"),
resource.TestCheckResourceAttr(resourceName, "virtual_gateway_name", vgName),
resource.TestCheckResourceAttrSet(resourceName, "created_date"),
resource.TestCheckResourceAttrSet(resourceName, "last_updated_date"),
acctest.CheckResourceAttrAccountID(resourceName, "resource_owner"),
acctest.CheckResourceAttrRegionalARN(resourceName, "arn", "appmesh", fmt.Sprintf("mesh/%s/virtualGateway/%s/gatewayRoute/%s", meshName, vgName, grName)),
resource.TestCheckResourceAttr(resourceName, "tags.%", "0"),
),
},
{
ResourceName: resourceName,
ImportStateIdFunc: testAccGatewayRouteImportStateIdFunc(resourceName),
Expand Down Expand Up @@ -733,6 +764,37 @@ func testAccGatewayRoute_httpRouteWithPort(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "tags.%", "0"),
),
},
{
Config: testAccGatewayRouteConfig_httpRouteRewriteWithPath(meshName, vgName, grName),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckGatewayRouteExists(ctx, resourceName, &v),
resource.TestCheckResourceAttr(resourceName, "mesh_name", meshName),
acctest.CheckResourceAttrAccountID(resourceName, "mesh_owner"),
resource.TestCheckResourceAttr(resourceName, "name", grName),
resource.TestCheckResourceAttr(resourceName, "spec.#", "1"),
resource.TestCheckResourceAttr(resourceName, "spec.0.grpc_route.#", "0"),
resource.TestCheckResourceAttr(resourceName, "spec.0.http2_route.#", "0"),
resource.TestCheckResourceAttr(resourceName, "spec.0.http_route.#", "1"),
resource.TestCheckResourceAttr(resourceName, "spec.0.http_route.0.action.#", "1"),
resource.TestCheckResourceAttr(resourceName, "spec.0.http_route.0.action.0.target.#", "1"),
resource.TestCheckResourceAttr(resourceName, "spec.0.http_route.0.action.0.target.0.virtual_service.#", "1"),
resource.TestCheckResourceAttrPair(resourceName, "spec.0.http_route.0.action.0.target.0.virtual_service.0.virtual_service_name", vs1ResourceName, "name"),
resource.TestCheckResourceAttr(resourceName, "spec.0.http_route.0.action.0.rewrite.#", "1"),
resource.TestCheckResourceAttr(resourceName, "spec.0.http_route.0.action.0.rewrite.0.hostname.#", "0"),
resource.TestCheckResourceAttr(resourceName, "spec.0.http_route.0.action.0.rewrite.0.prefix.#", "0"),
resource.TestCheckResourceAttr(resourceName, "spec.0.http_route.0.action.0.rewrite.0.path.#", "1"),
resource.TestCheckResourceAttr(resourceName, "spec.0.http_route.0.action.0.rewrite.0.path.0.exact", "/rewrite_path"),
resource.TestCheckResourceAttr(resourceName, "spec.0.http_route.0.match.#", "1"),
resource.TestCheckResourceAttr(resourceName, "spec.0.http_route.0.match.0.path.#", "1"),
resource.TestCheckResourceAttr(resourceName, "spec.0.http_route.0.match.0.path.0.exact", "/match_path"),
resource.TestCheckResourceAttr(resourceName, "virtual_gateway_name", vgName),
resource.TestCheckResourceAttrSet(resourceName, "created_date"),
resource.TestCheckResourceAttrSet(resourceName, "last_updated_date"),
acctest.CheckResourceAttrAccountID(resourceName, "resource_owner"),
acctest.CheckResourceAttrRegionalARN(resourceName, "arn", "appmesh", fmt.Sprintf("mesh/%s/virtualGateway/%s/gatewayRoute/%s", meshName, vgName, grName)),
resource.TestCheckResourceAttr(resourceName, "tags.%", "0"),
),
},
{
ResourceName: resourceName,
ImportStateIdFunc: testAccGatewayRouteImportStateIdFunc(resourceName),
Expand Down Expand Up @@ -893,6 +955,37 @@ func testAccGatewayRoute_http2Route(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "tags.%", "0"),
),
},
{
Config: testAccGatewayRouteConfig_http2RouteRewriteWithPath(meshName, vgName, grName),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckGatewayRouteExists(ctx, resourceName, &v),
resource.TestCheckResourceAttr(resourceName, "mesh_name", meshName),
acctest.CheckResourceAttrAccountID(resourceName, "mesh_owner"),
resource.TestCheckResourceAttr(resourceName, "name", grName),
resource.TestCheckResourceAttr(resourceName, "spec.#", "1"),
resource.TestCheckResourceAttr(resourceName, "spec.0.grpc_route.#", "0"),
resource.TestCheckResourceAttr(resourceName, "spec.0.http2_route.#", "1"),
resource.TestCheckResourceAttr(resourceName, "spec.0.http2_route.0.action.#", "1"),
resource.TestCheckResourceAttr(resourceName, "spec.0.http2_route.0.action.0.target.#", "1"),
resource.TestCheckResourceAttr(resourceName, "spec.0.http2_route.0.action.0.target.0.virtual_service.#", "1"),
resource.TestCheckResourceAttrPair(resourceName, "spec.0.http2_route.0.action.0.target.0.virtual_service.0.virtual_service_name", vs1ResourceName, "name"),
resource.TestCheckResourceAttr(resourceName, "spec.0.http2_route.0.action.0.rewrite.#", "1"),
resource.TestCheckResourceAttr(resourceName, "spec.0.http2_route.0.action.0.rewrite.0.hostname.#", "0"),
resource.TestCheckResourceAttr(resourceName, "spec.0.http2_route.0.action.0.rewrite.0.prefix.#", "0"),
resource.TestCheckResourceAttr(resourceName, "spec.0.http2_route.0.action.0.rewrite.0.path.#", "1"),
resource.TestCheckResourceAttr(resourceName, "spec.0.http2_route.0.action.0.rewrite.0.path.0.exact", "/rewrite_path"),
resource.TestCheckResourceAttr(resourceName, "spec.0.http2_route.0.match.#", "1"),
resource.TestCheckResourceAttr(resourceName, "spec.0.http2_route.0.match.0.path.#", "1"),
resource.TestCheckResourceAttr(resourceName, "spec.0.http2_route.0.match.0.path.0.exact", "/match_path"),
resource.TestCheckResourceAttr(resourceName, "spec.0.http_route.#", "0"),
resource.TestCheckResourceAttr(resourceName, "virtual_gateway_name", vgName),
resource.TestCheckResourceAttrSet(resourceName, "created_date"),
resource.TestCheckResourceAttrSet(resourceName, "last_updated_date"),
acctest.CheckResourceAttrAccountID(resourceName, "resource_owner"),
acctest.CheckResourceAttrRegionalARN(resourceName, "arn", "appmesh", fmt.Sprintf("mesh/%s/virtualGateway/%s/gatewayRoute/%s", meshName, vgName, grName)),
resource.TestCheckResourceAttr(resourceName, "tags.%", "0"),
),
},
{
ResourceName: resourceName,
ImportStateIdFunc: testAccGatewayRouteImportStateIdFunc(resourceName),
Expand Down Expand Up @@ -1104,6 +1197,37 @@ func testAccGatewayRoute_http2RouteWithPort(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "tags.%", "0"),
),
},
{
Config: testAccGatewayRouteConfig_http2RouteRewriteWithPath(meshName, vgName, grName),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckGatewayRouteExists(ctx, resourceName, &v),
resource.TestCheckResourceAttr(resourceName, "mesh_name", meshName),
acctest.CheckResourceAttrAccountID(resourceName, "mesh_owner"),
resource.TestCheckResourceAttr(resourceName, "name", grName),
resource.TestCheckResourceAttr(resourceName, "spec.#", "1"),
resource.TestCheckResourceAttr(resourceName, "spec.0.grpc_route.#", "0"),
resource.TestCheckResourceAttr(resourceName, "spec.0.http2_route.#", "1"),
resource.TestCheckResourceAttr(resourceName, "spec.0.http2_route.0.action.#", "1"),
resource.TestCheckResourceAttr(resourceName, "spec.0.http2_route.0.action.0.target.#", "1"),
resource.TestCheckResourceAttr(resourceName, "spec.0.http2_route.0.action.0.target.0.virtual_service.#", "1"),
resource.TestCheckResourceAttrPair(resourceName, "spec.0.http2_route.0.action.0.target.0.virtual_service.0.virtual_service_name", vs1ResourceName, "name"),
resource.TestCheckResourceAttr(resourceName, "spec.0.http2_route.0.action.0.rewrite.#", "1"),
resource.TestCheckResourceAttr(resourceName, "spec.0.http2_route.0.action.0.rewrite.0.hostname.#", "0"),
resource.TestCheckResourceAttr(resourceName, "spec.0.http2_route.0.action.0.rewrite.0.prefix.#", "0"),
resource.TestCheckResourceAttr(resourceName, "spec.0.http2_route.0.action.0.rewrite.0.path.#", "1"),
resource.TestCheckResourceAttr(resourceName, "spec.0.http2_route.0.action.0.rewrite.0.path.0.exact", "/rewrite_path"),
resource.TestCheckResourceAttr(resourceName, "spec.0.http2_route.0.match.#", "1"),
resource.TestCheckResourceAttr(resourceName, "spec.0.http2_route.0.match.0.path.#", "1"),
resource.TestCheckResourceAttr(resourceName, "spec.0.http2_route.0.match.0.path.0.exact", "/match_path"),
resource.TestCheckResourceAttr(resourceName, "spec.0.http_route.#", "0"),
resource.TestCheckResourceAttr(resourceName, "virtual_gateway_name", vgName),
resource.TestCheckResourceAttrSet(resourceName, "created_date"),
resource.TestCheckResourceAttrSet(resourceName, "last_updated_date"),
acctest.CheckResourceAttrAccountID(resourceName, "resource_owner"),
acctest.CheckResourceAttrRegionalARN(resourceName, "arn", "appmesh", fmt.Sprintf("mesh/%s/virtualGateway/%s/gatewayRoute/%s", meshName, vgName, grName)),
resource.TestCheckResourceAttr(resourceName, "tags.%", "0"),
),
},
{
ResourceName: resourceName,
ImportStateIdFunc: testAccGatewayRouteImportStateIdFunc(resourceName),
Expand Down Expand Up @@ -1786,6 +1910,39 @@ resource "aws_appmesh_gateway_route" "test" {
`, grName))
}

func testAccGatewayRouteConfig_httpRouteRewriteWithPath(meshName, vgName, grName string) string {
return acctest.ConfigCompose(testAccGatewayRouteConfig_base(meshName, vgName, "http"), fmt.Sprintf(`
resource "aws_appmesh_gateway_route" "test" {
name = %[1]q
mesh_name = aws_appmesh_mesh.test.name
virtual_gateway_name = aws_appmesh_virtual_gateway.test.name
spec {
http_route {
action {
target {
virtual_service {
virtual_service_name = aws_appmesh_virtual_service.test[0].name
}
}
rewrite {
path {
exact = "/rewrite_path"
}
}
}
match {
path {
exact = "/match_path"
}
}
}
}
}
`, grName))
}

func testAccGatewayRouteConfig_http2Route(meshName, vgName, grName string) string {
return acctest.ConfigCompose(testAccGatewayRouteConfig_base(meshName, vgName, "http2"), fmt.Sprintf(`
resource "aws_appmesh_gateway_route" "test" {
Expand Down Expand Up @@ -2069,6 +2226,39 @@ resource "aws_appmesh_gateway_route" "test" {
`, grName))
}

func testAccGatewayRouteConfig_http2RouteRewriteWithPath(meshName, vgName, grName string) string {
return acctest.ConfigCompose(testAccGatewayRouteConfig_base(meshName, vgName, "http2"), fmt.Sprintf(`
resource "aws_appmesh_gateway_route" "test" {
name = %[1]q
mesh_name = aws_appmesh_mesh.test.name
virtual_gateway_name = aws_appmesh_virtual_gateway.test.name
spec {
http2_route {
action {
target {
virtual_service {
virtual_service_name = aws_appmesh_virtual_service.test[0].name
}
}
rewrite {
path {
exact = "/rewrite_path"
}
}
}
match {
path {
exact = "/match_path"
}
}
}
}
}
`, grName))
}

func testAccGatewayRouteConfig_tags1(meshName, vgName, grName, tagKey1, tagValue1 string) string {
return acctest.ConfigCompose(testAccGatewayRouteConfig_base(meshName, vgName, "http"), fmt.Sprintf(`
resource "aws_appmesh_gateway_route" "test" {
Expand Down
5 changes: 5 additions & 0 deletions website/docs/r/appmesh_gateway_route.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,17 @@ The `http_route` and `http2_route`'s `action` object additionally supports the f
The `rewrite` object supports the following:

* `hostname` - (Optional) Host name to rewrite.
* `path` - (Optional) Exact path to rewrite.
* `prefix` - (Optional) Specified beginning characters to rewrite.

The `hostname` object supports the following:

* `default_target_hostname` - (Required) Default target host name to write to. Valid values: `ENABLED`, `DISABLED`.

The `path` object supports the following:

* `exact` - (Required) Value used to replace matched path.

The `prefix` object supports the following:

* `default_prefix` - (Optional) Default prefix used to replace the incoming route prefix when rewritten. Valid values: `ENABLED`, `DISABLED`.
Expand Down

0 comments on commit 4f4d289

Please sign in to comment.