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 1 commit
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
75 changes: 74 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_in_sec": {
Type: schema.TypeInt,
Optional: true,
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)

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,54 @@ func flattenApplicationGatewayBackendHTTPSettings(input *[]network.ApplicationGa
return results, nil
}

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

var connectionDraining *network.ApplicationGatewayConnectionDraining
if len(connectionsRaw) > 0 {
connectionRaw := connectionsRaw[0].(map[string]interface{})

enabled := connectionRaw["enabled"].(bool)
var drainTimeout int32

if enabled {
drainTimeout = int32(connectionRaw["drain_timeout_in_sec"].(int))
tombuildsstuff marked this conversation as resolved.
Show resolved Hide resolved
}

connectionDraining = &network.ApplicationGatewayConnectionDraining{
Enabled: utils.Bool(enabled),
DrainTimeoutInSec: utils.Int32(drainTimeout),
}
}

return connectionDraining
}

func flattenApplicationGatewayConnectionDraining(input *network.ApplicationGatewayBackendHTTPSettingsPropertiesFormat) []interface{} {
results := make([]interface{}, 0)

if connectionDraining := input.ConnectionDraining; connectionDraining != nil {
enabled := false
if connectionDraining.Enabled != nil {
enabled = *connectionDraining.Enabled
}

var drainTimeout int32
if enabled && connectionDraining.DrainTimeoutInSec != nil {
SteveByerly marked this conversation as resolved.
Show resolved Hide resolved
drainTimeout = *connectionDraining.DrainTimeoutInSec
}

var result = map[string]interface{}{
"enabled": enabled,
"drain_timeout_in_sec": drainTimeout,
}

results = append(results, result)
}

return results
}

func expandApplicationGatewaySslPolicy(d *schema.ResourceData) *network.ApplicationGatewaySslPolicy {
vs := d.Get("disabled_ssl_protocols").([]interface{})
results := make([]network.ApplicationGatewaySslProtocol, 0)
Expand Down
116 changes: 116 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,34 @@ 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.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_in_sec"),
resource.TestCheckResourceAttr(resourceName, "backend_http_settings.1.connection_draining.enabled", "true"),
resource.TestCheckResourceAttr(resourceName, "backend_http_settings.1.connection_draining.drain_timeout_in_sec", "127"),
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 +1348,94 @@ 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"
probe1_name = "${azurerm_virtual_network.test.name}-probe1"
probe2_name = "${azurerm_virtual_network.test.name}-probe2"
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"
port = 80
probe_name = "${local.probe1_name}"
protocol = "Http"
request_timeout = 1
}

backend_http_settings {
name = "${local.http_setting_name}-2"
cookie_based_affinity = "Disabled"
port = 8080
probe_name = "${local.probe2_name}"
protocol = "Http"
request_timeout = 1

connection_draining {
enabled = true
drain_timeout_in_sec = 127
}
}

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