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

azurerm_application_gateway: Support for components in rewrite_rule_set.rewrite_rule.url #13899

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
47 changes: 39 additions & 8 deletions internal/services/network/application_gateway_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -1186,6 +1186,17 @@ func resourceApplicationGateway() *pluginsdk.Resource {
Type: pluginsdk.TypeString,
Optional: true,
},

"components": {
Type: pluginsdk.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validation.StringInSlice([]string{
"path_only",
"query_string_only",
}, false),
},

"reroute": {
Type: pluginsdk.TypeBool,
Optional: true,
Expand Down Expand Up @@ -3481,10 +3492,14 @@ func expandApplicationGatewayRewriteRuleSets(d *pluginsdk.ResourceData) (*[]netw
if c["path"] == nil && c["query_string"] == nil {
return nil, fmt.Errorf("At least one of `path` or `query_string` must be set")
}
if c["path"] != nil {
components := ""
if c["components"] != nil {
components = c["components"].(string)
}
if c["path"] != nil && components != "query_string_only" {
urlConfiguration.ModifiedPath = utils.String(c["path"].(string))
}
if c["query_string"] != nil {
if c["query_string"] != nil && components != "path_only" {
urlConfiguration.ModifiedQueryString = utils.String(c["query_string"].(string))
}
if c["reroute"] != nil {
Expand Down Expand Up @@ -3615,20 +3630,36 @@ func flattenApplicationGatewayRewriteRuleSets(input *[]network.ApplicationGatewa

if actionSet.URLConfiguration != nil {
config := *actionSet.URLConfiguration
urlConfig := map[string]interface{}{}

components := ""
path := ""
if config.ModifiedPath != nil {
urlConfig["path"] = *config.ModifiedPath
path = *config.ModifiedPath
}

queryString := ""
if config.ModifiedQueryString != nil {
urlConfig["query_string"] = *config.ModifiedQueryString
queryString = *config.ModifiedQueryString
}

if path != queryString {
if path != "" && queryString == "" {
components = "path_only"
} else if queryString != "" && path == "" {
components = "query_string_only"
}
}

reroute := false
if config.Reroute != nil {
urlConfig["reroute"] = *config.Reroute
reroute = *config.Reroute
}
urlConfigs = append(urlConfigs, urlConfig)

urlConfigs = append(urlConfigs, map[string]interface{}{
"components": components,
"query_string": queryString,
"path": path,
"reroute": reroute,
})
}
}
ruleOutput["request_header_configuration"] = requestConfigs
Expand Down
39 changes: 39 additions & 0 deletions internal/services/network/application_gateway_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,11 @@ func TestAccApplicationGateway_rewriteRuleSets_rewriteUrl(t *testing.T) {
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("rewrite_rule_set.0.name").Exists(),
check.That(data.ResourceName).Key("rewrite_rule_set.#").HasValue("2"),
check.That(data.ResourceName).Key("rewrite_rule_set.0.rewrite_rule.0.url.0.components").HasValue(""),
check.That(data.ResourceName).Key("rewrite_rule_set.1.rewrite_rule.#").HasValue("2"),
check.That(data.ResourceName).Key("rewrite_rule_set.1.rewrite_rule.0.url.0.components").HasValue("path_only"),
check.That(data.ResourceName).Key("rewrite_rule_set.1.rewrite_rule.1.url.0.components").HasValue("query_string_only"),
),
},
data.ImportStep(),
Expand Down Expand Up @@ -6001,6 +6006,40 @@ resource "azurerm_application_gateway" "test" {
}
}

rewrite_rule_set {
name = "${local.rewrite_rule_set_name}_1"

rewrite_rule {
name = "${local.rewrite_rule_name}_1"
rule_sequence = 1

condition {
variable = "var_uri_path"
pattern = ".*article/(.*)/(.*)"
}

url {
path = "/article.aspx"
components = "path_only"
}
}

rewrite_rule {
name = "${local.rewrite_rule_name}_2"
rule_sequence = 2

condition {
variable = "var_uri_path"
pattern = ".*article2/(.*)/(.*)"
}

url {
query_string = "id={var_uri_path_1}&title={var_uri_path_2}"
components = "query_string_only"
}
}
}

redirect_configuration {
name = local.redirect_configuration_name
redirect_type = "Temporary"
Expand Down
4 changes: 3 additions & 1 deletion website/docs/r/application_gateway.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -653,7 +653,9 @@ A `url` block supports the following:

* `query_string` - (Optional) The query string to rewrite.

~> **Note:** One or both of `path` and `query_string` must be specified.
* `components` - (Optional) The components used to rewrite the URL. Possible values are `path_only` and `query_string_only` to limit the rewrite to the URL Path or URL Query String only.

~> **Note:** One or both of `path` and `query_string` must be specified. If one of these is not specified, it means the value will be empty. If you only want to rewrite `path` or `query_string`, use `components`.

* `reroute` - (Optional) Whether the URL path map should be reevaluated after this rewrite has been applied. [More info on rewrite configutation](https://docs.microsoft.com/en-us/azure/application-gateway/rewrite-http-headers-url#rewrite-configuration)

Expand Down