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

check for rules on the firewall and include in updates #2663

Merged
merged 2 commits into from
Jan 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
17 changes: 17 additions & 0 deletions azurerm/resource_arm_firewall.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
tombuildsstuff marked this conversation as resolved.
Show resolved Hide resolved

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)
Expand Down
69 changes: 69 additions & 0 deletions azurerm/resource_arm_firewall_application_rule_collection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
69 changes: 69 additions & 0 deletions azurerm/resource_arm_firewall_network_rule_collection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}