Skip to content

Commit

Permalink
azurerm_application_gateway: can combine include_path with targ… (#6175)
Browse files Browse the repository at this point in the history
* azurerm_application_gateway: can combine include_path with target_url

Redirecting say http://foo/path?query to http://bar/
include_path controls whether it will go to
	http://bar/path
or
	http://bar/

This is a perfectly valid option in a redirect_configuration in
combination with target_url. Currently the code gives an error if you
use them in combination and omits it from the request if target_url is
set.

Omitting include_path has strange results in the Azure Portal
(no radio button selected) and "null" defaults to true whereas the
documentation says include_path should default to false.

This change makes the behavior consistent with the documentation.

* Update resource_arm_application_gateway_test.go

Co-authored-by: Emanuel Rietveld <[email protected]>
Co-authored-by: Matthew Frahry <[email protected]>
  • Loading branch information
3 people authored Apr 2, 2020
1 parent 030e9fe commit 69cdfbf
Show file tree
Hide file tree
Showing 2 changed files with 150 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3044,6 +3044,7 @@ func expandApplicationGatewayRedirectConfigurations(d *schema.ResourceData, gate
ApplicationGatewayRedirectConfigurationPropertiesFormat: &network.ApplicationGatewayRedirectConfigurationPropertiesFormat{
RedirectType: network.ApplicationGatewayRedirectType(redirectType),
IncludeQueryString: utils.Bool(includeQueryString),
IncludePath: utils.Bool(includePath),
},
}

Expand All @@ -3055,16 +3056,11 @@ func expandApplicationGatewayRedirectConfigurations(d *schema.ResourceData, gate
return nil, fmt.Errorf("Conflict between `target_listener_name` and `target_url` (redirection is either to URL or target listener)")
}

if targetUrl != "" && includePath {
return nil, fmt.Errorf("`include_path` is not a valid option when `target_url` is set")
}

if targetListenerName != "" {
targetListenerID := fmt.Sprintf("%s/httpListeners/%s", gatewayID, targetListenerName)
output.ApplicationGatewayRedirectConfigurationPropertiesFormat.TargetListener = &network.SubResource{
ID: utils.String(targetListenerID),
}
output.ApplicationGatewayRedirectConfigurationPropertiesFormat.IncludePath = utils.Bool(includePath)
}

if targetUrl != "" {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -933,6 +933,25 @@ func TestAccAzureRMApplicationGateway_V2SKUCapacity(t *testing.T) {
})
}

func TestAccAzureRMApplicationGateway_IncludePathWithTargetURL(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_application_gateway", "test")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acceptance.PreCheck(t) },
Providers: acceptance.SupportedProviders,
CheckDestroy: testCheckAzureRMApplicationGatewayDestroy,
Steps: []resource.TestStep{
{
Config: testAccAzureRMApplicationGateway_includePathWithTargetURL(data),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMApplicationGatewayExists(data.ResourceName),
),
},
data.ImportStep(),
},
})
}

func testCheckAzureRMApplicationGatewayExists(resourceName string) resource.TestCheckFunc {
return func(s *terraform.State) error {
client := acceptance.AzureProvider.Meta().(*clients.Client).Network.ApplicationGatewaysClient
Expand Down Expand Up @@ -4719,3 +4738,133 @@ resource "azurerm_application_gateway" "test" {
}
`, template, data.RandomInteger, data.RandomInteger)
}

func testAccAzureRMApplicationGateway_includePathWithTargetURL(data acceptance.TestData) string {
template := testAccAzureRMApplicationGateway_template(data)
return fmt.Sprintf(`
%s
# since these variables are re-used - a locals block makes this more maintainable
locals {
backend_address_pool_name = "${azurerm_virtual_network.test.name}-beap"
frontend_port_name = "${azurerm_virtual_network.test.name}-feport"
frontend_port_name2 = "${azurerm_virtual_network.test.name}-feport2"
frontend_ip_configuration_name = "${azurerm_virtual_network.test.name}-feip"
http_setting_name = "${azurerm_virtual_network.test.name}-be-htst"
listener_name = "${azurerm_virtual_network.test.name}-httplstn"
target_listener_name = "${azurerm_virtual_network.test.name}-trgthttplstn"
request_routing_rule_name = "${azurerm_virtual_network.test.name}-rqrt"
path_rule_name = "${azurerm_virtual_network.test.name}-pathrule1"
path_rule_name2 = "${azurerm_virtual_network.test.name}-pathrule2"
url_path_map_name = "${azurerm_virtual_network.test.name}-urlpath1"
redirect_configuration_name = "${azurerm_virtual_network.test.name}-PathRedirect"
redirect_configuration_name2 = "${azurerm_virtual_network.test.name}-PathRedirect2"
target_url = "http://www.example.com"
}
resource "azurerm_application_gateway" "test" {
name = "acctestag-%d"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
sku {
name = "Standard_Small"
tier = "Standard"
capacity = 2
}
gateway_ip_configuration {
name = "my-gateway-ip-configuration"
subnet_id = azurerm_subnet.test.id
}
frontend_port {
name = local.frontend_port_name
port = 80
}
frontend_port {
name = local.frontend_port_name2
port = 8888
}
frontend_ip_configuration {
name = local.frontend_ip_configuration_name
public_ip_address_id = azurerm_public_ip.test.id
}
backend_address_pool {
name = local.backend_address_pool_name
}
backend_http_settings {
name = local.http_setting_name
cookie_based_affinity = "Disabled"
port = 80
protocol = "Http"
request_timeout = 1
}
http_listener {
name = local.listener_name
frontend_ip_configuration_name = local.frontend_ip_configuration_name
frontend_port_name = local.frontend_port_name
protocol = "Http"
}
http_listener {
name = local.target_listener_name
frontend_ip_configuration_name = local.frontend_ip_configuration_name
frontend_port_name = local.frontend_port_name2
protocol = "Http"
}
request_routing_rule {
name = local.request_routing_rule_name
rule_type = "PathBasedRouting"
url_path_map_name = local.url_path_map_name
http_listener_name = local.listener_name
}
url_path_map {
name = local.url_path_map_name
default_backend_address_pool_name = local.backend_address_pool_name
default_backend_http_settings_name = local.http_setting_name
path_rule {
name = local.path_rule_name
redirect_configuration_name = local.redirect_configuration_name
paths = [
"/test",
]
}
path_rule {
name = local.path_rule_name2
redirect_configuration_name = local.redirect_configuration_name2
paths = [
"/test2",
]
}
}
redirect_configuration {
name = local.redirect_configuration_name
redirect_type = "Found"
target_url = local.target_url
include_query_string = true
include_path = true
}
redirect_configuration {
name = local.redirect_configuration_name2
redirect_type = "Permanent"
target_listener_name = local.target_listener_name
include_path = false
include_query_string = false
}
}
`, template, data.RandomInteger)
}

0 comments on commit 69cdfbf

Please sign in to comment.