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 Hostname #2990

Merged
merged 4 commits into from
Mar 21, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
21 changes: 21 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,
},
tombuildsstuff marked this conversation as resolved.
Show resolved Hide resolved

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

for _, backendHttpSettings := range *backendHTTPSettingsCollection {
backendHttpSettingsProperties := *backendHttpSettings.ApplicationGatewayBackendHTTPSettingsPropertiesFormat
hostName := *backendHttpSettingsProperties.HostName
mcharriere marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -1255,6 +1270,7 @@ func expandApplicationGatewayBackendHTTPSettings(d *schema.ResourceData, gateway
port := int32(v["port"].(int))
protocol := v["protocol"].(string)
cookieBasedAffinity := v["cookie_based_affinity"].(string)
hostName := v["host_name"].(string)
mcharriere marked this conversation as resolved.
Show resolved Hide resolved
pickHostNameFromBackendAddress := v["pick_host_name_from_backend_address"].(bool)
requestTimeout := int32(v["request_timeout"].(int))

Expand All @@ -1263,6 +1279,7 @@ func expandApplicationGatewayBackendHTTPSettings(d *schema.ResourceData, gateway
ApplicationGatewayBackendHTTPSettingsPropertiesFormat: &network.ApplicationGatewayBackendHTTPSettingsPropertiesFormat{
CookieBasedAffinity: network.ApplicationGatewayCookieBasedAffinity(cookieBasedAffinity),
Path: utils.String(path),
HostName: utils.String(hostName),
mcharriere marked this conversation as resolved.
Show resolved Hide resolved
PickHostNameFromBackendAddress: utils.Bool(pickHostNameFromBackendAddress),
Port: utils.Int32(port),
Protocol: network.ApplicationGatewayProtocol(protocol),
Expand Down Expand Up @@ -1332,6 +1349,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
98 changes: 98 additions & 0 deletions azurerm/resource_arm_application_gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,32 @@ func TestAccAzureRMApplicationGateway_probesPickHostNameFromBackendHTTPSettings(
})
}

func TestAccAzureRMApplicationGateway_backendHttpSettingsHostName(t *testing.T) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we get another test with 3 steps:

  • set pick true
  • set hostname
  • unset hostname

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a doubt regarding the test. If I set pick true and then I try to set hostname it will throw an error. Until here it's fine, i can check for that error. But I dont understand the following step. I think that unsetting hostname won't do anything because of the previous error.

Should I add a test just to check for that error (both pick and host_name set)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@katbyte I've added the test TestAccAzureRMApplicationGateway_backendHttpSettingsHostNameAndPick checking for the error and it's passing.
Please let me know if you think there is something else to check for.

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),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMApplicationGatewayExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "backend_http_settings.0.host_name", hostName),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

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

func testAccAzureRMApplicationGateway_backendHttpSettingsHostName(rInt int, location string, hostName string) 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
}

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)
}

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`.
katbyte marked this conversation as resolved.
Show resolved Hide resolved

* `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