diff --git a/azurerm/resource_arm_firewall.go b/azurerm/resource_arm_firewall.go index ef19e7ec7b04..e66ee51997f9 100644 --- a/azurerm/resource_arm_firewall.go +++ b/azurerm/resource_arm_firewall.go @@ -127,6 +127,23 @@ func resourceArmFirewallCreateUpdate(d *schema.ResourceData, meta interface{}) e }, } + if !d.IsNewResource() { + exists, err := client.Get(ctx, resourceGroup, name) + if err != nil { + if utils.ResponseWasNotFound(exists.Response) { + return fmt.Errorf("Error retrieving existing Firewall %q (Resource Group %q): firewall not found in resource group", name, resourceGroup) + } + return fmt.Errorf("Error retrieving existing Firewall %q (Resource Group %q): %s", name, resourceGroup, err) + } + if exists.AzureFirewallPropertiesFormat == nil { + return fmt.Errorf("Error retrieving existing rules (Firewall %q / Resource Group %q): `props` was nil", name, resourceGroup) + } + props := *exists.AzureFirewallPropertiesFormat + parameters.AzureFirewallPropertiesFormat.ApplicationRuleCollections = props.ApplicationRuleCollections + parameters.AzureFirewallPropertiesFormat.NetworkRuleCollections = props.NetworkRuleCollections + parameters.AzureFirewallPropertiesFormat.NatRuleCollections = props.NatRuleCollections + } + future, err := client.CreateOrUpdate(ctx, resourceGroup, name, parameters) if err != nil { return fmt.Errorf("Error creating/updating Azure Firewall %q (Resource Group %q): %+v", name, resourceGroup, err) diff --git a/azurerm/resource_arm_firewall_application_rule_collection_test.go b/azurerm/resource_arm_firewall_application_rule_collection_test.go index 79bc3fe40056..61018058e75c 100644 --- a/azurerm/resource_arm_firewall_application_rule_collection_test.go +++ b/azurerm/resource_arm_firewall_application_rule_collection_test.go @@ -379,6 +379,43 @@ func TestAccAzureRMFirewallApplicationRuleCollection_updateProtocols(t *testing. }) } +func TestAccAzureRMFirewallApplicationRuleCollection_updateFirewallTags(t *testing.T) { + resourceName := "azurerm_firewall_application_rule_collection.test" + ri := tf.AccRandTimeInt() + + location := testLocation() + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testCheckAzureRMFirewallDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAzureRMFirewallApplicationRuleCollection_basic(ri, location), + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMFirewallApplicationRuleCollectionExists(resourceName), + resource.TestCheckResourceAttr(resourceName, "name", "acctestarc"), + resource.TestCheckResourceAttr(resourceName, "priority", "100"), + resource.TestCheckResourceAttr(resourceName, "action", "Allow"), + resource.TestCheckResourceAttr(resourceName, "rule.#", "1"), + resource.TestCheckResourceAttr(resourceName, "rule.0.name", "rule1"), + ), + }, + { + Config: testAccAzureRMFirewallApplicationRuleCollection_updateFirewallTags(ri, location), + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMFirewallApplicationRuleCollectionExists(resourceName), + resource.TestCheckResourceAttr(resourceName, "name", "acctestarc"), + resource.TestCheckResourceAttr(resourceName, "priority", "100"), + resource.TestCheckResourceAttr(resourceName, "action", "Allow"), + resource.TestCheckResourceAttr(resourceName, "rule.#", "1"), + resource.TestCheckResourceAttr(resourceName, "rule.0.name", "rule1"), + ), + }, + }, + }) +} + func testCheckAzureRMFirewallApplicationRuleCollectionExists(resourceName string) resource.TestCheckFunc { return func(s *terraform.State) error { // Ensure we have enough information in state to look up in API @@ -814,3 +851,35 @@ resource "azurerm_firewall_application_rule_collection" "test" { } `, template) } + +func testAccAzureRMFirewallApplicationRuleCollection_updateFirewallTags(rInt int, location string) string { + template := testAccAzureRMFirewall_withTags(rInt, location) + return fmt.Sprintf(` +%s + +resource "azurerm_firewall_application_rule_collection" "test" { + name = "acctestarc" + azure_firewall_name = "${azurerm_firewall.test.name}" + resource_group_name = "${azurerm_resource_group.test.name}" + priority = 100 + action = "Allow" + + rule { + name = "rule1" + + source_addresses = [ + "10.0.0.0/16", + ] + + target_fqdns = [ + "*.google.com", + ] + + protocol { + port = 443 + type = "Https" + } + } +} +`, template) +} diff --git a/azurerm/resource_arm_firewall_network_rule_collection_test.go b/azurerm/resource_arm_firewall_network_rule_collection_test.go index 6c0de54b01de..592ae30c2380 100644 --- a/azurerm/resource_arm_firewall_network_rule_collection_test.go +++ b/azurerm/resource_arm_firewall_network_rule_collection_test.go @@ -271,6 +271,40 @@ func TestAccAzureRMFirewallNetworkRuleCollection_multipleRules(t *testing.T) { }) } +func TestAccAzureRMFirewallNetworkRuleCollection_updateFirewallTags(t *testing.T) { + resourceName := "azurerm_firewall_network_rule_collection.test" + ri := tf.AccRandTimeInt() + location := testLocation() + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testCheckAzureRMFirewallDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAzureRMFirewallNetworkRuleCollection_basic(ri, location), + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMFirewallNetworkRuleCollectionExists(resourceName), + resource.TestCheckResourceAttr(resourceName, "name", "acctestnrc"), + resource.TestCheckResourceAttr(resourceName, "priority", "100"), + resource.TestCheckResourceAttr(resourceName, "action", "Allow"), + resource.TestCheckResourceAttr(resourceName, "rule.#", "1"), + ), + }, + { + Config: testAccAzureRMFirewallNetworkRuleCollection_updateFirewallTags(ri, location), + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMFirewallNetworkRuleCollectionExists(resourceName), + resource.TestCheckResourceAttr(resourceName, "name", "acctestnrc"), + resource.TestCheckResourceAttr(resourceName, "priority", "100"), + resource.TestCheckResourceAttr(resourceName, "action", "Allow"), + resource.TestCheckResourceAttr(resourceName, "rule.#", "1"), + ), + }, + }, + }) +} + func testCheckAzureRMFirewallNetworkRuleCollectionExists(resourceName string) resource.TestCheckFunc { return func(s *terraform.State) error { // Ensure we have enough information in state to look up in API @@ -661,3 +695,38 @@ resource "azurerm_firewall_network_rule_collection" "test" { } `, template) } + +func testAccAzureRMFirewallNetworkRuleCollection_updateFirewallTags(rInt int, location string) string { + template := testAccAzureRMFirewall_withTags(rInt, location) + return fmt.Sprintf(` +%s + +resource "azurerm_firewall_network_rule_collection" "test" { + name = "acctestnrc" + azure_firewall_name = "${azurerm_firewall.test.name}" + resource_group_name = "${azurerm_resource_group.test.name}" + priority = 100 + action = "Allow" + + rule { + name = "rule1" + + source_addresses = [ + "10.0.0.0/16", + ] + + destination_ports = [ + "53", + ] + + destination_addresses = [ + "8.8.8.8", + ] + + protocols = [ + "Any", + ] + } +} +`, template) +}