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

Application gateway: Autoscale configuration #3353

Merged
merged 13 commits into from
May 7, 2019
72 changes: 66 additions & 6 deletions azurerm/resource_arm_application_gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,25 @@ func resourceArmApplicationGateway() *schema.Resource {
},
},
},

"autoscale_configuration": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"min_capacity": {
timja marked this conversation as resolved.
Show resolved Hide resolved
Type: schema.TypeInt,
Required: true,
ValidateFunc: validation.IntBetween(2, 10),
},
"max_capacity": {
Type: schema.TypeInt,
Optional: true,
ValidateFunc: validation.IntBetween(2, 100),
},
},
},
},
"sku": {
Type: schema.TypeList,
Required: true,
Expand Down Expand Up @@ -583,7 +601,7 @@ func resourceArmApplicationGateway() *schema.Resource {

"capacity": {
Type: schema.TypeInt,
Required: true,
Optional: true,
ValidateFunc: validation.IntBetween(1, 10),
},
},
Expand Down Expand Up @@ -997,6 +1015,7 @@ func resourceArmApplicationGatewayCreateUpdate(d *schema.ResourceData, meta inte
requestRoutingRules := expandApplicationGatewayRequestRoutingRules(d, gatewayID)
redirectConfigurations := expandApplicationGatewayRedirectConfigurations(d, gatewayID)
sku := expandApplicationGatewaySku(d)
autoscaleConfiguration := expandAutoscaleConfiguration(d)
sslCertificates := expandApplicationGatewaySslCertificates(d)
sslPolicy := expandApplicationGatewaySslPolicy(d)
customErrorConfigurations := expandApplicationGatewayCustomErrorConfigurations(d.Get("custom_error_configuration").([]interface{}))
Expand Down Expand Up @@ -1025,6 +1044,7 @@ func resourceArmApplicationGatewayCreateUpdate(d *schema.ResourceData, meta inte
SslPolicy: sslPolicy,
CustomErrorConfigurations: customErrorConfigurations,
URLPathMaps: urlPathMaps,
AutoscaleConfiguration: autoscaleConfiguration,
},
}

Expand Down Expand Up @@ -1177,6 +1197,10 @@ func resourceArmApplicationGatewayRead(d *schema.ResourceData, meta interface{})
return fmt.Errorf("Error setting `sku`: %+v", setErr)
}

if setErr := d.Set("autoscale_configuration", flattenAutoscaleConfiguration(props.AutoscaleConfiguration)); setErr != nil {
return fmt.Errorf("Error setting `autoscale_configuration`: %+v", setErr)
}

if setErr := d.Set("ssl_certificate", flattenApplicationGatewaySslCertificates(props.SslCertificates, d)); setErr != nil {
return fmt.Errorf("Error setting `ssl_certificate`: %+v", setErr)
}
Expand Down Expand Up @@ -2282,6 +2306,37 @@ func flattenApplicationGatewayRedirectConfigurations(input *[]network.Applicatio
return results, nil
}

func expandAutoscaleConfiguration(d *schema.ResourceData) *network.ApplicationGatewayAutoscaleConfiguration {
timja marked this conversation as resolved.
Show resolved Hide resolved
vs := d.Get("autoscale_configuration").([]interface{})
v := vs[0].(map[string]interface{})
timja marked this conversation as resolved.
Show resolved Hide resolved

minCapacity := int32(v["min_capacity"].(int))
maxCapacity := int32(v["max_capacity"].(int))

configuration := network.ApplicationGatewayAutoscaleConfiguration{
MinCapacity: utils.Int32(minCapacity),
}

if maxCapacity != 0 {
configuration.MaxCapacity = utils.Int32(maxCapacity)
}

return &configuration
}

func flattenAutoscaleConfiguration(input *network.ApplicationGatewayAutoscaleConfiguration) []interface{} {
timja marked this conversation as resolved.
Show resolved Hide resolved
result := make(map[string]interface{})

timja marked this conversation as resolved.
Show resolved Hide resolved
if v := input.MinCapacity; v != nil {
result["min_capacity"] = *v
}
if input.MaxCapacity != nil {
result["max_capacity"] = *input.MaxCapacity
}

return []interface{}{result}
}

func expandApplicationGatewaySku(d *schema.ResourceData) *network.ApplicationGatewaySku {
vs := d.Get("sku").([]interface{})
v := vs[0].(map[string]interface{})
Expand All @@ -2290,11 +2345,16 @@ func expandApplicationGatewaySku(d *schema.ResourceData) *network.ApplicationGat
tier := v["tier"].(string)
capacity := int32(v["capacity"].(int))

return &network.ApplicationGatewaySku{
Name: network.ApplicationGatewaySkuName(name),
Tier: network.ApplicationGatewayTier(tier),
Capacity: utils.Int32(capacity),
sku := network.ApplicationGatewaySku{
Name: network.ApplicationGatewaySkuName(name),
Tier: network.ApplicationGatewayTier(tier),
}

if capacity != 0 {
sku.Capacity = utils.Int32(capacity)
}

return &sku
}

func flattenApplicationGatewaySku(input *network.ApplicationGatewaySku) []interface{} {
Expand Down
233 changes: 233 additions & 0 deletions azurerm/resource_arm_application_gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,74 @@ func TestAccAzureRMApplicationGateway_basic(t *testing.T) {
})
}

func TestAccAzureRMApplicationGateway_autoscaleConfiguration(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_autoscaleConfiguration(ri, testLocation(), 2, 10),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMApplicationGatewayExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "sku.0.name", "Standard_v2"),
resource.TestCheckResourceAttr(resourceName, "sku.0.tier", "Standard_v2"),
resource.TestCheckResourceAttr(resourceName, "autoscale_configuration.0.min_capacity", "2"),
resource.TestCheckResourceAttr(resourceName, "autoscale_configuration.0.max_capacity", "10"),
resource.TestCheckResourceAttr(resourceName, "waf_configuration.#", "0"),
),
},
timja marked this conversation as resolved.
Show resolved Hide resolved
{
Config: testAccAzureRMApplicationGateway_autoscaleConfiguration(ri, testLocation(), 4, 12),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMApplicationGatewayExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "sku.0.name", "Standard_v2"),
resource.TestCheckResourceAttr(resourceName, "sku.0.tier", "Standard_v2"),
resource.TestCheckResourceAttr(resourceName, "autoscale_configuration.0.min_capacity", "4"),
resource.TestCheckResourceAttr(resourceName, "autoscale_configuration.0.max_capacity", "12"),
resource.TestCheckResourceAttr(resourceName, "waf_configuration.#", "0"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func TestAccAzureRMApplicationGateway_autoscaleConfigurationNoMaxCapacity(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_autoscaleConfigurationNoMaxCapacity(ri, testLocation()),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMApplicationGatewayExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "sku.0.name", "Standard_v2"),
resource.TestCheckResourceAttr(resourceName, "sku.0.tier", "Standard_v2"),
resource.TestCheckResourceAttr(resourceName, "autoscale_configuration.0.min_capacity", "2"),
resource.TestCheckResourceAttr(resourceName, "waf_configuration.#", "0"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func TestAccAzureRMApplicationGateway_zones(t *testing.T) {
resourceName := "azurerm_application_gateway.test"
ri := tf.AccRandTimeInt()
Expand Down Expand Up @@ -744,6 +812,171 @@ resource "azurerm_application_gateway" "test" {
`, template, rInt, rInt)
}

func testAccAzureRMApplicationGateway_autoscaleConfiguration(rInt int, location string, minCapacity int, maxCapacity int) 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_public_ip" "test_standard" {
name = "acctest-pubip-%d-standard"
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"
sku = "Standard"
allocation_method = "Static"
}

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_v2"
tier = "Standard_v2"
}

autoscale_configuration {
min_capacity = %d
max_capacity = %d
}

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_standard.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
}

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, rInt, minCapacity, maxCapacity)
}

func testAccAzureRMApplicationGateway_autoscaleConfigurationNoMaxCapacity(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_public_ip" "test_standard" {
name = "acctest-pubip-%d-standard"
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"
sku = "Standard"
allocation_method = "Static"
}

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_v2"
tier = "Standard_v2"
}

autoscale_configuration {
min_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_standard.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
}

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

func testAccAzureRMApplicationGateway_overridePath(rInt int, location string) string {
template := testAccAzureRMApplicationGateway_template(rInt, location)
return fmt.Sprintf(`
Expand Down
Loading