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: add connection draining #2778

Merged
merged 7 commits into from
Feb 28, 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
58 changes: 57 additions & 1 deletion azurerm/resource_arm_application_gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,26 @@ func resourceArmApplicationGateway() *schema.Resource {
},
},

"connection_draining": {
Type: schema.TypeList,
MaxItems: 1,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"enabled": {
Type: schema.TypeBool,
Required: true,
},

"drain_timeout_sec": {
Type: schema.TypeInt,
Required: true,
SteveByerly marked this conversation as resolved.
Show resolved Hide resolved
ValidateFunc: validation.IntBetween(1, 3600),
},
SteveByerly marked this conversation as resolved.
Show resolved Hide resolved
},
},
},

"probe_name": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -1139,6 +1159,7 @@ func expandApplicationGatewayBackendHTTPSettings(d *schema.ResourceData, gateway
Port: utils.Int32(port),
Protocol: network.ApplicationGatewayProtocol(protocol),
RequestTimeout: utils.Int32(requestTimeout),
ConnectionDraining: expandApplicationGatewayConnectionDraining(v),
},
}

Expand Down Expand Up @@ -1193,13 +1214,17 @@ func flattenApplicationGatewayBackendHTTPSettings(input *[]network.ApplicationGa

if props := v.ApplicationGatewayBackendHTTPSettingsPropertiesFormat; props != nil {
output["cookie_based_affinity"] = string(props.CookieBasedAffinity)
output["protocol"] = string(props.Protocol)
output["connection_draining"] = flattenApplicationGatewayConnectionDraining(props.ConnectionDraining)

if port := props.Port; port != nil {
output["port"] = int(*port)
}

if pickHostNameFromBackendAddress := props.PickHostNameFromBackendAddress; pickHostNameFromBackendAddress != nil {
output["pick_host_name_from_backend_address"] = *pickHostNameFromBackendAddress
}
output["protocol"] = string(props.Protocol)
tombuildsstuff marked this conversation as resolved.
Show resolved Hide resolved

if timeout := props.RequestTimeout; timeout != nil {
output["request_timeout"] = int(*timeout)
}
Expand Down Expand Up @@ -1245,6 +1270,37 @@ func flattenApplicationGatewayBackendHTTPSettings(input *[]network.ApplicationGa
return results, nil
}

func expandApplicationGatewayConnectionDraining(d map[string]interface{}) *network.ApplicationGatewayConnectionDraining {
connectionsRaw := d["connection_draining"].([]interface{})

if len(connectionsRaw) <= 0 {
return nil
}

connectionRaw := connectionsRaw[0].(map[string]interface{})

return &network.ApplicationGatewayConnectionDraining{
Enabled: utils.Bool(connectionRaw["enabled"].(bool)),
DrainTimeoutInSec: utils.Int32(int32(connectionRaw["drain_timeout_sec"].(int))),
}
}

func flattenApplicationGatewayConnectionDraining(input *network.ApplicationGatewayConnectionDraining) []interface{} {
result := map[string]interface{}{}
if input == nil {
return []interface{}{}
}

if v := input.Enabled; v != nil {
result["enabled"] = *v
}
if v := input.DrainTimeoutInSec; v != nil {
result["drain_timeout_sec"] = *v
}

return []interface{}{result}
}

func expandApplicationGatewaySslPolicy(d *schema.ResourceData) *network.ApplicationGatewaySslPolicy {
vs := d.Get("disabled_ssl_protocols").([]interface{})
results := make([]network.ApplicationGatewaySslProtocol, 0)
Expand Down
119 changes: 119 additions & 0 deletions azurerm/resource_arm_application_gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,48 @@ func TestAccAzureRMApplicationGateway_webApplicationFirewall(t *testing.T) {
})
}

func TestAccAzureRMApplicationGateway_connectionDraining(t *testing.T) {
resourceName := "azurerm_application_gateway.test"
ri := tf.AccRandTimeInt()

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMApplicationGatewayDestroy,
Steps: []resource.TestStep{
{
Config: testAccAzureRMApplicationGateway_connectionDraining(ri, testLocation()),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMApplicationGatewayExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "backend_http_settings.0.connection_draining.0.enabled", "true"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccAzureRMApplicationGateway_basic(ri, testLocation()),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMApplicationGatewayExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "sku.0.name", "Standard_Small"),
resource.TestCheckResourceAttr(resourceName, "sku.0.tier", "Standard"),
resource.TestCheckResourceAttr(resourceName, "sku.0.capacity", "2"),
resource.TestCheckResourceAttr(resourceName, "waf_configuration.#", "0"),
resource.TestCheckNoResourceAttr(resourceName, "backend_http_settings.0.connection_draining.enabled"),
SteveByerly marked this conversation as resolved.
Show resolved Hide resolved
resource.TestCheckNoResourceAttr(resourceName, "backend_http_settings.0.connection_draining.drain_timeout_sec"),
SteveByerly marked this conversation as resolved.
Show resolved Hide resolved
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
SteveByerly marked this conversation as resolved.
Show resolved Hide resolved
})
}

func testCheckAzureRMApplicationGatewayExists(resourceName string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[resourceName]
Expand Down Expand Up @@ -1320,6 +1362,83 @@ resource "azurerm_application_gateway" "test" {
`, template, rInt)
}

func testAccAzureRMApplicationGateway_connectionDraining(rInt int, location 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}"
enable_http2 = true

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"
port = 80
protocol = "Http"
request_timeout = 1

connection_draining {
enabled = true
drain_timeout_sec = 1984
}
}

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

func testAccAzureRMApplicationGateway_template(rInt int, location string) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {
Expand Down
11 changes: 11 additions & 0 deletions website/docs/r/application_gateway.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,19 @@ A `backend_http_settings` block supports the following:

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

* `connection_draining` - (Optional) One or more `connection_draining` blocks.
SteveByerly marked this conversation as resolved.
Show resolved Hide resolved

---

A `connection_draining` block supports the following:

* `enabled` - (Required) If connection draining is enabled or not.

* `drain_timeout_sec` - (Required) The number of seconds connection draining is active. Acceptable values are from `1` second to `3600` seconds.

---


A `frontend_ip_configuration` block supports the following:

* `name` - (Required) The name of the Frontend IP Configuration.
Expand Down