From f900cc2d3c29423754f62a7d8b4b05d0d6d419bf Mon Sep 17 00:00:00 2001 From: tombuildsstuff Date: Thu, 12 Sep 2019 18:58:07 +0200 Subject: [PATCH] r/private_dns_zone_virtual_network_link: fixing tests / polling until it's gone ``` $ acctests azurerm TestAccAzureRMPrivateDnsZoneVirtualNetworkLink_ === RUN TestAccAzureRMPrivateDnsZoneVirtualNetworkLink_basic === PAUSE TestAccAzureRMPrivateDnsZoneVirtualNetworkLink_basic === RUN TestAccAzureRMPrivateDnsZoneVirtualNetworkLink_requiresImport --- SKIP: TestAccAzureRMPrivateDnsZoneVirtualNetworkLink_requiresImport (0.00s) resource_arm_private_dns_zone_virtual_network_link_test.go:42: Skipping since resources aren't required to be imported === RUN TestAccAzureRMPrivateDnsZoneVirtualNetworkLink_withTags === PAUSE TestAccAzureRMPrivateDnsZoneVirtualNetworkLink_withTags === CONT TestAccAzureRMPrivateDnsZoneVirtualNetworkLink_basic --- PASS: TestAccAzureRMPrivateDnsZoneVirtualNetworkLink_basic (226.26s) === CONT TestAccAzureRMPrivateDnsZoneVirtualNetworkLink_withTags --- PASS: TestAccAzureRMPrivateDnsZoneVirtualNetworkLink_withTags (372.72s) PASS ok github.com/terraform-providers/terraform-provider-azurerm/azurerm 599.035s ``` --- ...m_private_dns_zone_virtual_network_link.go | 46 +++++++++++++++---- ...vate_dns_zone_virtual_network_link_test.go | 23 +++++----- 2 files changed, 48 insertions(+), 21 deletions(-) diff --git a/azurerm/resource_arm_private_dns_zone_virtual_network_link.go b/azurerm/resource_arm_private_dns_zone_virtual_network_link.go index f815d98c1399..db6b3272d11b 100644 --- a/azurerm/resource_arm_private_dns_zone_virtual_network_link.go +++ b/azurerm/resource_arm_private_dns_zone_virtual_network_link.go @@ -2,8 +2,11 @@ package azurerm import ( "fmt" + "log" + "time" "github.com/Azure/azure-sdk-for-go/services/privatedns/mgmt/2018-09-01/privatedns" + "github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/helper/schema" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/response" @@ -50,7 +53,7 @@ func resourceArmPrivateDnsZoneVirtualNetworkLink() *schema.Resource { Default: false, }, - "resource_group_name": azure.SchemaResourceGroupName(), + "resource_group_name": azure.SchemaResourceGroupNameDiffSuppress(), "tags": tagsSchema(), }, @@ -117,7 +120,7 @@ func resourceArmPrivateDnsZoneVirtualNetworkLinkCreateUpdate(d *schema.ResourceD d.SetId(*resp.ID) - return resourceArmPrivateDnsZoneRead(d, meta) + return resourceArmPrivateDnsZoneVirtualNetworkLinkRead(d, meta) } func resourceArmPrivateDnsZoneVirtualNetworkLinkRead(d *schema.ResourceData, meta interface{}) error { @@ -173,19 +176,42 @@ func resourceArmPrivateDnsZoneVirtualNetworkLinkDelete(d *schema.ResourceData, m name := id.Path["virtualNetworkLinks"] etag := "" - future, err := client.Delete(ctx, resGroup, dnsZoneName, name, etag) - if err != nil { + if future, err := client.Delete(ctx, resGroup, dnsZoneName, name, etag); err != nil { if response.WasNotFound(future.Response()) { return nil } - return fmt.Errorf("error deleting Private DNS Zone Virtual network link %s (resource group %s): %+v", name, resGroup, err) + return fmt.Errorf("error deleting Virtual Network Link %q (Private DNS Zone %q / Resource Group %q): %+v", name, dnsZoneName, resGroup, err) } - if err = future.WaitForCompletionRef(ctx, client.Client); err != nil { - if response.WasNotFound(future.Response()) { - return nil - } - return fmt.Errorf("error waiting for deletion of Private DNS Zone Virtual network link %s (resource group %s): %+v", name, resGroup, err) + // whilst the Delete above returns a Future, the Azure API's broken such that even though it's marked as "gone" + // it's still kicking around - so we have to poll until this is actually gone + log.Printf("[DEBUG] Waiting for Virtual Network Link %q (Private DNS Zone %q / Resource Group %q) to be deleted", name, dnsZoneName, resGroup) + stateConf := &resource.StateChangeConf{ + Pending: []string{"Available"}, + Target: []string{"NotFound"}, + Refresh: func() (interface{}, string, error) { + log.Printf("[DEBUG] Checking to see if Virtual Network Link %q (Private DNS Zone %q / Resource Group %q) is available", name, dnsZoneName, resGroup) + resp, err := client.Get(ctx, resGroup, dnsZoneName, name) + if err != nil { + if utils.ResponseWasNotFound(resp.Response) { + log.Printf("[DEBUG] Virtual Network Link %q (Private DNS Zone %q / Resource Group %q) was not found", name, dnsZoneName, resGroup) + return "NotFound", "NotFound", nil + } + + return "", "error", err + } + + log.Printf("[DEBUG] Virtual Network Link %q (Private DNS Zone %q / Resource Group %q) still exists", name, dnsZoneName, resGroup) + return "Available", "Available", nil + }, + Timeout: 30 * time.Minute, + Delay: 30 * time.Second, + PollInterval: 10 * time.Second, + ContinuousTargetOccurence: 5, + } + + if _, err := stateConf.WaitForState(); err != nil { + return fmt.Errorf("error waiting for deletion of Virtual Network Link %q (Private DNS Zone %q / Resource Group %q): %+v", name, dnsZoneName, resGroup, err) } return nil diff --git a/azurerm/resource_arm_private_dns_zone_virtual_network_link_test.go b/azurerm/resource_arm_private_dns_zone_virtual_network_link_test.go index 785b5b7b63b6..5fd95d10ba09 100644 --- a/azurerm/resource_arm_private_dns_zone_virtual_network_link_test.go +++ b/azurerm/resource_arm_private_dns_zone_virtual_network_link_test.go @@ -8,6 +8,7 @@ import ( "github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/terraform" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" ) func TestAccAzureRMPrivateDnsZoneVirtualNetworkLink_basic(t *testing.T) { @@ -120,11 +121,11 @@ func testCheckAzureRMPrivateDnsZoneVirtualNetworkLinkExists(resourceName string) resp, err := client.Get(ctx, resourceGroup, dnsZoneName, name) if err != nil { - return fmt.Errorf("Bad: Get Private DNS zone virtual network link: %+v", err) - } + if utils.ResponseWasNotFound(resp.Response) { + return fmt.Errorf("Bad: virtual network link %q (Private DNS zone %q / resource group: %s) does not exist", name, dnsZoneName, resourceGroup) + } - if resp.StatusCode == http.StatusNotFound { - return fmt.Errorf("Bad: Private DNS zone virtual network link %s (resource group: %s) does not exist", name, resourceGroup) + return fmt.Errorf("Bad: Get Private DNS zone virtual network link: %+v", err) } return nil @@ -186,7 +187,7 @@ resource "azurerm_private_dns_zone" "test" { resource "azurerm_private_dns_zone_virtual_network_link" "test" { name = "acctest%d" private_dns_zone_name = "${azurerm_private_dns_zone.test.name}" - virtual_network_id = "${azurerm_virtual_network.test.id}" + virtual_network_id = "${azurerm_virtual_network.test.id}" resource_group_name = "${azurerm_resource_group.test.name}" } @@ -199,10 +200,10 @@ func testAccAzureRMPrivateDnsZoneVirtualNetworkLink_requiresImport(rInt int, loc %s resource "azurerm_private_dns_zone_virtual_network_link" "import" { - name = "${azurerm_private_dns_zone_virtual_network_link.name} - private_dns_zone_name = "${azurerm_private_dns_zone_virtual_network_link.private_dns_zone_name}" - virtual_network_id = "${azurerm_private_dns_zone_virtual_network_link.virtual_network_id}" - resource_group_name = "${azurerm_private_dns_zone_virtual_network_link.resource_group_name}" + name = "${azurerm_private_dns_zone_virtual_network_link.test.name} + private_dns_zone_name = "${azurerm_private_dns_zone_virtual_network_link.test.private_dns_zone_name}" + virtual_network_id = "${azurerm_private_dns_zone_virtual_network_link.test.virtual_network_id}" + resource_group_name = "${azurerm_private_dns_zone_virtual_network_link.test.resource_group_name}" } `, template) } @@ -234,7 +235,7 @@ resource "azurerm_private_dns_zone" "test" { resource "azurerm_private_dns_zone_virtual_network_link" "test" { name = "acctest%d" private_dns_zone_name = "${azurerm_private_dns_zone.test.name}" - virtual_network_id = "${azurerm_virtual_network.test.id}" + virtual_network_id = "${azurerm_virtual_network.test.id}" resource_group_name = "${azurerm_resource_group.test.name}" tags = { @@ -272,7 +273,7 @@ resource "azurerm_private_dns_zone" "test" { resource "azurerm_private_dns_zone_virtual_network_link" "test" { name = "acctestzone%d.com" private_dns_zone_name = "${azurerm_private_dns_zone.test.name}" - virtual_network_id = "${azurerm_virtual_network.test.id}" + virtual_network_id = "${azurerm_virtual_network.test.id}" resource_group_name = "${azurerm_resource_group.test.name}" tags = {