From 858c38e3929b57d03d552c767ede81e42b0a990f Mon Sep 17 00:00:00 2001 From: mcharriere Date: Thu, 21 Mar 2019 15:25:57 -0300 Subject: [PATCH] azurerm_application_gateway: Support for Hostname (#2990) --- azurerm/resource_arm_application_gateway.go | 26 ++++ .../resource_arm_application_gateway_test.go | 117 ++++++++++++++++++ .../docs/r/application_gateway.html.markdown | 2 + 3 files changed, 145 insertions(+) diff --git a/azurerm/resource_arm_application_gateway.go b/azurerm/resource_arm_application_gateway.go index 5dc91d834362..bb820babc15e 100644 --- a/azurerm/resource_arm_application_gateway.go +++ b/azurerm/resource_arm_application_gateway.go @@ -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, @@ -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 @@ -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) @@ -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 } diff --git a/azurerm/resource_arm_application_gateway_test.go b/azurerm/resource_arm_application_gateway_test.go index 84ea780d11a2..7e4fd8a43d42 100644 --- a/azurerm/resource_arm_application_gateway_test.go +++ b/azurerm/resource_arm_application_gateway_test.go @@ -2,6 +2,7 @@ package azurerm import ( "fmt" + "regexp" "testing" "github.com/hashicorp/terraform/helper/resource" @@ -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() @@ -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(` diff --git a/website/docs/r/application_gateway.html.markdown b/website/docs/r/application_gateway.html.markdown index a0432ad23144..b38cdcf18f1b 100644 --- a/website/docs/r/application_gateway.html.markdown +++ b/website/docs/r/application_gateway.html.markdown @@ -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.