Skip to content

Commit

Permalink
firewall service tests - refactor to CheckWithClient / DisappearsStep (
Browse files Browse the repository at this point in the history
  • Loading branch information
jackofallops authored Jan 13, 2021
1 parent 6a538ed commit 258739b
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/acceptance"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/acceptance/check"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/firewall/parse"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

Expand Down Expand Up @@ -125,7 +126,7 @@ func TestAccFirewallNetworkRuleCollection_multipleRuleCollections(t *testing.T)
check.That(data.ResourceName).Key("priority").HasValue("100"),
check.That(data.ResourceName).Key("action").HasValue("Allow"),
check.That(data.ResourceName).Key("rule.#").HasValue("1"),
testCheckFirewallNetworkRuleCollectionDoesNotExist("azurerm_firewall.test", "acctestnrc_add"),
data.CheckWithClient(r.checkFirewallNetworkRuleCollectionDoesNotExist("acctestnrc_add")),
),
},
})
Expand Down Expand Up @@ -183,10 +184,12 @@ func TestAccFirewallNetworkRuleCollection_disappears(t *testing.T) {
check.That(data.ResourceName).Key("priority").HasValue("100"),
check.That(data.ResourceName).Key("action").HasValue("Allow"),
check.That(data.ResourceName).Key("rule.#").HasValue("1"),
testCheckFirewallNetworkRuleCollectionDisappears(data.ResourceName),
),
ExpectNonEmptyPlan: true,
},
data.DisappearsStep(acceptance.DisappearsStepData{
Config: r.basic,
TestResource: r,
}),
})
}

Expand Down Expand Up @@ -360,21 +363,18 @@ func (FirewallNetworkRuleCollectionResource) Exists(ctx context.Context, clients
return utils.Bool(false), nil
}

func testCheckFirewallNetworkRuleCollectionDoesNotExist(resourceName string, collectionName string) resource.TestCheckFunc {
return func(s *terraform.State) error {
client := acceptance.AzureProvider.Meta().(*clients.Client).Firewall.AzureFirewallsClient
ctx := acceptance.AzureProvider.Meta().(*clients.Client).StopContext

func (r FirewallNetworkRuleCollectionResource) checkFirewallNetworkRuleCollectionDoesNotExist(collectionName string) acceptance.ClientCheckFunc {
return func(ctx context.Context, clients *clients.Client, state *terraform.InstanceState) error {
// Ensure we have enough information in state to look up in API
rs, ok := s.RootModule().Resources[resourceName]
if !ok {
return fmt.Errorf("Not found: %s", resourceName)
id, err := parse.FirewallNetworkRuleCollectionID(state.ID)
if err != nil {
return err
}

firewallName := rs.Primary.Attributes["name"]
resourceGroup := rs.Primary.Attributes["resource_group_name"]
firewallName := id.AzureFirewallName
resourceGroup := id.ResourceGroup

read, err := client.Get(ctx, resourceGroup, firewallName)
read, err := clients.Firewall.AzureFirewallsClient.Get(ctx, resourceGroup, firewallName)
if err != nil {
return err
}
Expand All @@ -389,47 +389,42 @@ func testCheckFirewallNetworkRuleCollectionDoesNotExist(resourceName string, col
}
}

func testCheckFirewallNetworkRuleCollectionDisappears(resourceName string) resource.TestCheckFunc {
return func(s *terraform.State) error {
client := acceptance.AzureProvider.Meta().(*clients.Client).Firewall.AzureFirewallsClient
ctx := acceptance.AzureProvider.Meta().(*clients.Client).StopContext
func (FirewallNetworkRuleCollectionResource) Destroy(ctx context.Context, clients *clients.Client, state *terraform.InstanceState) (*bool, error) {
id, err := parse.FirewallNetworkRuleCollectionID(state.ID)
if err != nil {
return nil, err
}

// Ensure we have enough information in state to look up in API
rs, ok := s.RootModule().Resources[resourceName]
if !ok {
return fmt.Errorf("Not found: %s", resourceName)
}
name := id.NetworkRuleCollectionName
firewallName := id.AzureFirewallName
resourceGroup := id.ResourceGroup

name := rs.Primary.Attributes["name"]
firewallName := rs.Primary.Attributes["azure_firewall_name"]
resourceGroup := rs.Primary.Attributes["resource_group_name"]
read, err := clients.Firewall.AzureFirewallsClient.Get(ctx, resourceGroup, firewallName)
if err != nil {
return utils.Bool(false), err
}

read, err := client.Get(ctx, resourceGroup, firewallName)
if err != nil {
return err
rules := make([]network.AzureFirewallNetworkRuleCollection, 0)
for _, collection := range *read.AzureFirewallPropertiesFormat.NetworkRuleCollections {
if *collection.Name != name {
rules = append(rules, collection)
}
}

rules := make([]network.AzureFirewallNetworkRuleCollection, 0)
for _, collection := range *read.AzureFirewallPropertiesFormat.NetworkRuleCollections {
if *collection.Name != name {
rules = append(rules, collection)
}
}
read.AzureFirewallPropertiesFormat.NetworkRuleCollections = &rules

read.AzureFirewallPropertiesFormat.NetworkRuleCollections = &rules
future, err := clients.Firewall.AzureFirewallsClient.CreateOrUpdate(ctx, resourceGroup, firewallName, read)
if err != nil {
return utils.Bool(false), fmt.Errorf("Error removing Network Rule Collection from Firewall: %+v", err)
}

future, err := client.CreateOrUpdate(ctx, resourceGroup, firewallName, read)
if err != nil {
return fmt.Errorf("Error removing Network Rule Collection from Firewall: %+v", err)
}
if err = future.WaitForCompletionRef(ctx, clients.Firewall.AzureFirewallsClient.Client); err != nil {
return utils.Bool(false), fmt.Errorf("Error waiting for the removal of Network Rule Collection from Firewall: %+v", err)
}

if err = future.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("Error waiting for the removal of Network Rule Collection from Firewall: %+v", err)
}
_, err = clients.Firewall.AzureFirewallsClient.Get(ctx, resourceGroup, firewallName)

_, err = client.Get(ctx, resourceGroup, firewallName)
return err
}
return utils.Bool(err == nil), err
}

func (FirewallNetworkRuleCollectionResource) basic(data acceptance.TestData) string {
Expand Down
49 changes: 15 additions & 34 deletions azurerm/internal/services/firewall/firewall_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/acceptance"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/acceptance/check"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/firewall/parse"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

Expand Down Expand Up @@ -201,14 +202,10 @@ func TestAccFirewall_disappears(t *testing.T) {
r := FirewallResource{}

data.ResourceTest(t, r, []resource.TestStep{
{
Config: r.basic(data),
Check: resource.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
testCheckFirewallDisappears(data.ResourceName),
),
ExpectNonEmptyPlan: true,
},
data.DisappearsStep(acceptance.DisappearsStepData{
Config: r.basic,
TestResource: r,
}),
})
}

Expand Down Expand Up @@ -285,33 +282,17 @@ func (FirewallResource) Exists(ctx context.Context, clients *clients.Client, sta
return utils.Bool(resp.AzureFirewallPropertiesFormat != nil), nil
}

func testCheckFirewallDisappears(resourceName string) resource.TestCheckFunc {
return func(s *terraform.State) error {
client := acceptance.AzureProvider.Meta().(*clients.Client).Firewall.AzureFirewallsClient
ctx := acceptance.AzureProvider.Meta().(*clients.Client).StopContext

// Ensure we have enough information in state to look up in API
rs, ok := s.RootModule().Resources[resourceName]
if !ok {
return fmt.Errorf("Not found: %s", resourceName)
}

name := rs.Primary.Attributes["name"]
resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"]
if !hasResourceGroup {
return fmt.Errorf("Bad: no resource group found in state for Azure Firewall: %q", name)
}

future, err := client.Delete(ctx, resourceGroup, name)
if err != nil {
return fmt.Errorf("Bad: Delete on azureFirewallsClient: %+v", err)
}
if err = future.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("Bad: waiting for Deletion on azureFirewallsClient: %+v", err)
}

return nil
func (FirewallResource) Destroy(ctx context.Context, clients *clients.Client, state *terraform.InstanceState) (*bool, error) {
id, err := parse.FirewallID(state.ID)
if err != nil {
return nil, err
}

if _, err := clients.Firewall.AzureFirewallsClient.Delete(ctx, id.ResourceGroup, id.AzureFirewallName); err != nil {
return nil, fmt.Errorf("deleting Azure Firewall %q: %+v", id.AzureFirewallName, err)
}

return utils.Bool(true), nil
}

func (FirewallResource) basic(data acceptance.TestData) string {
Expand Down

0 comments on commit 258739b

Please sign in to comment.