Skip to content

Commit

Permalink
Application gateway: Autoscale configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
timja committed May 1, 2019
1 parent 8a9f79b commit f44bbab
Show file tree
Hide file tree
Showing 2 changed files with 169 additions and 6 deletions.
63 changes: 57 additions & 6 deletions azurerm/resource_arm_application_gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,23 @@ func resourceArmApplicationGateway() *schema.Resource {
},
},
},

"autoscale_configuration": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"min_capacity": {
Type: schema.TypeInt,
Required: true,
},
"max_capacity": {
Type: schema.TypeInt,
Optional: true,
},
},
},
},
"sku": {
Type: schema.TypeList,
Required: true,
Expand Down Expand Up @@ -583,7 +599,7 @@ func resourceArmApplicationGateway() *schema.Resource {

"capacity": {
Type: schema.TypeInt,
Required: true,
Optional: true,
ValidateFunc: validation.IntBetween(1, 10),
},
},
Expand Down Expand Up @@ -997,6 +1013,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 +1042,7 @@ func resourceArmApplicationGatewayCreateUpdate(d *schema.ResourceData, meta inte
SslPolicy: sslPolicy,
CustomErrorConfigurations: customErrorConfigurations,
URLPathMaps: urlPathMaps,
AutoscaleConfiguration: autoscaleConfiguration,
},
}

Expand Down Expand Up @@ -1177,6 +1195,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 +2304,30 @@ func flattenApplicationGatewayRedirectConfigurations(input *[]network.Applicatio
return results, nil
}

func expandAutoscaleConfiguration(d *schema.ResourceData) *network.ApplicationGatewayAutoscaleConfiguration {
vs := d.Get("autoscale_configuration").([]interface{})
v := vs[0].(map[string]interface{})

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

return &network.ApplicationGatewayAutoscaleConfiguration{
MinCapacity: utils.Int32(minCapacity),
MaxCapacity: utils.Int32(maxCapacity),
}
}

func flattenAutoscaleConfiguration(input *network.ApplicationGatewayAutoscaleConfiguration) []interface{} {
result := make(map[string]interface{})

result["min_capacity"] = int32(*input.MinCapacity)
if input.MaxCapacity != nil {
result["max_capacity"] = int32(*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 +2336,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
112 changes: 112 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,35 @@ 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()),
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"),
),
},
{
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 +773,89 @@ resource "azurerm_application_gateway" "test" {
`, template, rInt, rInt)
}

func testAccAzureRMApplicationGateway_autoscaleConfiguration(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
max_capacity = 10
}
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

0 comments on commit f44bbab

Please sign in to comment.