From 59ca5de205cfa15eb4f5bcc7052bfcacb48c5f44 Mon Sep 17 00:00:00 2001 From: tombuildsstuff Date: Wed, 18 Jul 2018 20:01:00 +0200 Subject: [PATCH] Polling for the Notification Hub Namespace ourselves since the Future's broken ``` $ acctests azurerm TestAccAzureRMNotificationHubNamespace_importFree === RUN TestAccAzureRMNotificationHubNamespace_importFree --- PASS: TestAccAzureRMNotificationHubNamespace_importFree (129.94s) PASS ok github.com/terraform-providers/terraform-provider-azurerm/azurerm 129.989s ``` --- ...resource_arm_notification_hub_namespace.go | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/azurerm/resource_arm_notification_hub_namespace.go b/azurerm/resource_arm_notification_hub_namespace.go index c5791196bba83..f6c9e0594d862 100644 --- a/azurerm/resource_arm_notification_hub_namespace.go +++ b/azurerm/resource_arm_notification_hub_namespace.go @@ -1,10 +1,16 @@ package azurerm import ( + "context" "fmt" "log" + "time" + + "strconv" + "github.com/Azure/azure-sdk-for-go/services/notificationhubs/mgmt/2017-04-01/notificationhubs" + "github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/helper/schema" "github.com/hashicorp/terraform/helper/validation" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/response" @@ -179,6 +185,19 @@ func resourceArmNotificationHubNamespaceDelete(d *schema.ResourceData, meta inte } } + // the future returned from the Delete method is broken 50% of the time - let's poll ourselves for now + // TODO: BUG + log.Printf("[DEBUG] Waiting for Notification Hub Namespace %q (Resource Group %q) to be deleted", name, resourceGroup) + stateConf := &resource.StateChangeConf{ + Pending: []string{"404"}, + Target: []string{"200"}, + Refresh: notificationHubNamespaceStateRefreshFunc(ctx, client, resourceGroup, name), + Timeout: 10 * time.Minute, + } + if _, err := stateConf.WaitForState(); err != nil { + return fmt.Errorf("Error waiting for Notification Hub %q (Resource Group %q) to be deleted: %s", name, resourceGroup, err) + } + err = future.WaitForCompletionRef(ctx, client.Client) if err != nil { return fmt.Errorf("Error waiting for the deletion of Notification Hub Namespace %q (Resource Group %q): %+v", name, resourceGroup, err) @@ -209,3 +228,14 @@ func flattenNotificationHubNamespacesSku(input *notificationhubs.Sku) []interfac outputs = append(outputs, output) return outputs } + +func notificationHubNamespaceStateRefreshFunc(ctx context.Context, client notificationhubs.NamespacesClient, resourceGroupName string, name string) resource.StateRefreshFunc { + return func() (interface{}, string, error) { + res, err := client.Get(ctx, resourceGroupName, name) + if err != nil { + return nil, "", fmt.Errorf("Error retrieving Notification Hub Namespace %q (Resource Group %q): %s", name, resourceGroupName, err) + } + + return res, strconv.Itoa(res.StatusCode), nil + } +}