Skip to content

Commit

Permalink
azurerm_application_gateway: Support for Hostname (#2990)
Browse files Browse the repository at this point in the history
  • Loading branch information
mcharriere authored and katbyte committed Mar 21, 2019
1 parent cb17865 commit 858c38e
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 0 deletions.
26 changes: 26 additions & 0 deletions azurerm/resource_arm_application_gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,11 @@ func resourceArmApplicationGateway() *schema.Resource {
}, true),
},

"host_name": {
Type: schema.TypeString,
Optional: true,
},

"pick_host_name_from_backend_address": {
Type: schema.TypeBool,
Optional: true,
Expand Down Expand Up @@ -927,6 +932,18 @@ func resourceArmApplicationGatewayCreateUpdate(d *schema.ResourceData, meta inte
},
}

for _, backendHttpSettings := range *backendHTTPSettingsCollection {
backendHttpSettingsProperties := *backendHttpSettings.ApplicationGatewayBackendHTTPSettingsPropertiesFormat
if backendHttpSettingsProperties.HostName != nil {
hostName := *backendHttpSettingsProperties.HostName
pick := *backendHttpSettingsProperties.PickHostNameFromBackendAddress

if hostName != "" && pick {
return fmt.Errorf("Only one of `host_name` or `pick_host_name_from_backend_address` can be set")
}
}
}

for _, probe := range *probes {
probeProperties := *probe.ApplicationGatewayProbePropertiesFormat
host := *probeProperties.Host
Expand Down Expand Up @@ -1285,6 +1302,11 @@ func expandApplicationGatewayBackendHTTPSettings(d *schema.ResourceData, gateway
},
}

hostName := v["host_name"].(string)
if hostName != "" {
setting.ApplicationGatewayBackendHTTPSettingsPropertiesFormat.HostName = utils.String(hostName)
}

if v["authentication_certificate"] != nil {
authCerts := v["authentication_certificate"].([]interface{})
authCertSubResources := make([]network.SubResource, 0)
Expand Down Expand Up @@ -1346,6 +1368,10 @@ func flattenApplicationGatewayBackendHTTPSettings(input *[]network.ApplicationGa
output["port"] = int(*port)
}

if hostName := props.HostName; hostName != nil {
output["host_name"] = *hostName
}

if pickHostNameFromBackendAddress := props.PickHostNameFromBackendAddress; pickHostNameFromBackendAddress != nil {
output["pick_host_name_from_backend_address"] = *pickHostNameFromBackendAddress
}
Expand Down
117 changes: 117 additions & 0 deletions azurerm/resource_arm_application_gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package azurerm

import (
"fmt"
"regexp"
"testing"

"github.com/hashicorp/terraform/helper/resource"
Expand Down Expand Up @@ -260,6 +261,49 @@ func TestAccAzureRMApplicationGateway_probesPickHostNameFromBackendHTTPSettings(
})
}

func TestAccAzureRMApplicationGateway_backendHttpSettingsHostName(t *testing.T) {
resourceName := "azurerm_application_gateway.test"
ri := tf.AccRandTimeInt()
hostName := "example.com"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMApplicationGatewayDestroy,
Steps: []resource.TestStep{
{
Config: testAccAzureRMApplicationGateway_backendHttpSettingsHostName(ri, testLocation(), hostName, false),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMApplicationGatewayExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "backend_http_settings.0.host_name", hostName),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func TestAccAzureRMApplicationGateway_backendHttpSettingsHostNameAndPick(t *testing.T) {
ri := tf.AccRandTimeInt()
hostName := "example.com"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMApplicationGatewayDestroy,
Steps: []resource.TestStep{
{
Config: testAccAzureRMApplicationGateway_backendHttpSettingsHostName(ri, testLocation(), hostName, true),
ExpectError: regexp.MustCompile("Only one of `host_name` or `pick_host_name_from_backend_address` can be set"),
},
},
})
}

func TestAccAzureRMApplicationGateway_settingsPickHostNameFromBackendAddress(t *testing.T) {
resourceName := "azurerm_application_gateway.test"
ri := tf.AccRandTimeInt()
Expand Down Expand Up @@ -1171,6 +1215,79 @@ resource "azurerm_application_gateway" "test" {
`, template, rInt)
}

func testAccAzureRMApplicationGateway_backendHttpSettingsHostName(rInt int, location string, hostName string, pick bool) string {
template := testAccAzureRMApplicationGateway_template(rInt, location)
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_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"
request_routing_rule_name = "${azurerm_virtual_network.test.name}-rqrt"
}
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_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"
host_name = "%s"
port = 80
protocol = "Http"
request_timeout = 1
pick_host_name_from_backend_address = %t
}
http_listener {
name = "${local.listener_name}"
frontend_ip_configuration_name = "${local.frontend_ip_configuration_name}"
frontend_port_name = "${local.frontend_port_name}"
protocol = "Http"
}
request_routing_rule {
name = "${local.request_routing_rule_name}"
rule_type = "Basic"
http_listener_name = "${local.listener_name}"
backend_address_pool_name = "${local.backend_address_pool_name}"
backend_http_settings_name = "${local.http_setting_name}"
}
}
`, template, rInt, hostName, pick)
}

func testAccAzureRMApplicationGateway_settingsPickHostNameFromBackendAddress(rInt int, location string) string {
template := testAccAzureRMApplicationGateway_template(rInt, location)
return fmt.Sprintf(`
Expand Down
2 changes: 2 additions & 0 deletions website/docs/r/application_gateway.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,8 @@ A `backend_http_settings` block supports the following:

* `request_timeout` - (Required) The request timeout in seconds, which must be between 1 and 86400 seconds.

* `host_name` - (Optional) Host header to be sent to the backend servers. Cannot be set if `pick_host_name_from_backend_address` is set to `true`.

* `pick_host_name_from_backend_address` - (Optional) Whether host header should be picked from the host name of the backend server. Defaults to `false`.

* `authentication_certificate` - (Optional) One or more `authentication_certificate` blocks.
Expand Down

0 comments on commit 858c38e

Please sign in to comment.