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

r/notification_hub_namespace: polling to handle eventual consistency #2701

Merged
merged 2 commits into from
Jan 17, 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
34 changes: 34 additions & 0 deletions azurerm/resource_arm_notification_hub.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package azurerm

import (
"context"
"fmt"
"log"
"strconv"
"time"

"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/utils"
Expand Down Expand Up @@ -156,10 +160,25 @@ func resourceArmNotificationHubCreateUpdate(d *schema.ResourceData, meta interfa
return fmt.Errorf("Error creating Notification Hub %q (Namespace %q / Resource Group %q): %+v", name, namespaceName, resourceGroup, err)
}

// Notification Hubs are eventually consistent
log.Printf("[DEBUG] Waiting for Notification Hub %q to become available", name)
stateConf := &resource.StateChangeConf{
Pending: []string{"404"},
Target: []string{"200"},
Refresh: notificationHubStateRefreshFunc(ctx, client, resourceGroup, namespaceName, name),
Timeout: 10 * time.Minute,
MinTimeout: 15 * time.Second,
ContinuousTargetOccurence: 10,
}
if _, err2 := stateConf.WaitForState(); err2 != nil {
return fmt.Errorf("Error waiting for Notification Hub %q to become available: %s", name, err2)
}

read, err := client.Get(ctx, resourceGroup, namespaceName, name)
if err != nil {
return fmt.Errorf("Error retrieving Notification Hub %q (Namespace %q / Resource Group %q): %+v", name, namespaceName, resourceGroup, err)
}

if read.ID == nil {
return fmt.Errorf("Cannot read Notification Hub %q (Namespace %q / Resource Group %q) ID", name, namespaceName, resourceGroup)
}
Expand All @@ -169,6 +188,21 @@ func resourceArmNotificationHubCreateUpdate(d *schema.ResourceData, meta interfa
return resourceArmNotificationHubRead(d, meta)
}

func notificationHubStateRefreshFunc(ctx context.Context, client notificationhubs.Client, resourceGroup, namespaceName, name string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
res, err := client.Get(ctx, resourceGroup, namespaceName, name)
if err != nil {
if utils.ResponseWasNotFound(res.Response) {
return nil, "404", nil
}

return nil, "", fmt.Errorf("Error retrieving Notification Hub %q (Namespace %q / Resource Group %q): %+v", name, namespaceName, resourceGroup, err)
}

return res, strconv.Itoa(res.StatusCode), nil
}
}

func resourceArmNotificationHubRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).notificationHubsClient
ctx := meta.(*ArmClient).StopContext
Expand Down
30 changes: 29 additions & 1 deletion azurerm/resource_arm_notification_hub_namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,19 @@ func resourceArmNotificationHubNamespaceCreateUpdate(d *schema.ResourceData, met
return fmt.Errorf("Error creating/updating Notification Hub Namesapce %q (Resource Group %q): %+v", name, resourceGroup, err)
}

log.Printf("[DEBUG] Waiting for Notification Hub Namespace %q (Resource Group %q) to be created", name, resourceGroup)
stateConf := &resource.StateChangeConf{
Pending: []string{"404"},
Target: []string{"200"},
Refresh: notificationHubNamespaceStateRefreshFunc(ctx, client, resourceGroup, name),
Timeout: 10 * time.Minute,
MinTimeout: 15 * time.Second,
ContinuousTargetOccurence: 10,
}
if _, err := stateConf.WaitForState(); err != nil {
return fmt.Errorf("Error waiting for Notification Hub %q (Resource Group %q) to finish replicating: %s", name, resourceGroup, err)
}

read, err := client.Get(ctx, resourceGroup, name)
if err != nil {
return fmt.Errorf("Error retrieving Notification Hub Namesapce %q (Resource Group %q): %+v", name, resourceGroup, err)
Expand Down Expand Up @@ -190,7 +203,7 @@ func resourceArmNotificationHubNamespaceDelete(d *schema.ResourceData, meta inte
stateConf := &resource.StateChangeConf{
Pending: []string{"200", "202"},
Target: []string{"404"},
Refresh: notificationHubNamespaceStateRefreshFunc(ctx, client, resourceGroup, name),
Refresh: notificationHubNamespaceDeleteStateRefreshFunc(ctx, client, resourceGroup, name),
Timeout: 10 * time.Minute,
}
if _, err := stateConf.WaitForState(); err != nil {
Expand Down Expand Up @@ -224,6 +237,21 @@ func flattenNotificationHubNamespacesSku(input *notificationhubs.Sku) []interfac
}

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 {
if utils.ResponseWasNotFound(res.Response) {
return nil, "404", 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
}
}

func notificationHubNamespaceDeleteStateRefreshFunc(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 {
Expand Down