Skip to content

Commit

Permalink
azurerm_application_gateway - fix error when firewall_policy_id i…
Browse files Browse the repository at this point in the history
…s removed (#23682)

* fix update

* fix

* fix lint
  • Loading branch information
teowa authored Nov 2, 2023
1 parent 233c40d commit b8ee9b0
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 2 deletions.
8 changes: 6 additions & 2 deletions internal/services/network/application_gateway_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -1987,8 +1987,12 @@ func resourceApplicationGatewayUpdate(d *pluginsdk.ResourceData, meta interface{
}

if d.HasChange("firewall_policy_id") {
applicationGateway.ApplicationGatewayPropertiesFormat.FirewallPolicy = &network.SubResource{
ID: utils.String(d.Get("firewall_policy_id").(string)),
if d.Get("firewall_policy_id").(string) != "" {
applicationGateway.ApplicationGatewayPropertiesFormat.FirewallPolicy = &network.SubResource{
ID: utils.String(d.Get("firewall_policy_id").(string)),
}
} else {
applicationGateway.ApplicationGatewayPropertiesFormat.FirewallPolicy = nil
}
}

Expand Down
123 changes: 123 additions & 0 deletions internal/services/network/application_gateway_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1287,6 +1287,28 @@ func TestAccApplicationGateway_withoutRequestTimeout(t *testing.T) {
})
}

func TestAccApplicationGateway_removeFirewallPolicy(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_application_gateway", "test")
r := ApplicationGatewayResource{}

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.basic_wafv2(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
{
Config: r.basic_v2(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
})
}

func (t ApplicationGatewayResource) Exists(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) (*bool, error) {
id, err := parse.ApplicationGatewayID(state.ID)
if err != nil {
Expand Down Expand Up @@ -1371,6 +1393,107 @@ resource "azurerm_application_gateway" "test" {
`, r.template(data), data.RandomInteger)
}

func (r ApplicationGatewayResource) basic_wafv2(data acceptance.TestData) string {
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-standard-%d"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
allocation_method = "Static"
sku = "Standard"
}
resource "azurerm_web_application_firewall_policy" "test" {
name = "acctest-fwp-%[2]d"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
managed_rules {
managed_rule_set {
type = "OWASP"
version = "3.1"
}
}
policy_settings {
enabled = true
mode = "Prevention"
request_body_check = true
file_upload_limit_in_mb = 250
max_request_body_size_in_kb = 128
}
lifecycle {
create_before_destroy = true
}
}
resource "azurerm_application_gateway" "test" {
name = "acctestag-%d"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
firewall_policy_id = azurerm_web_application_firewall_policy.test.id
sku {
name = "WAF_v2"
tier = "WAF_v2"
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
priority = 10
}
}
`, r.template(data), data.RandomInteger, data.RandomInteger)
}

func (r ApplicationGatewayResource) basic_v2(data acceptance.TestData) string {
return fmt.Sprintf(`
%s
Expand Down

0 comments on commit b8ee9b0

Please sign in to comment.