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

iothub_resourcewait LRO #15698

Merged
merged 2 commits into from
Mar 4, 2022
Merged
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
93 changes: 8 additions & 85 deletions internal/services/iothub/iothub_resource.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package iothub

import (
"context"
"fmt"
"log"
"net/url"
"regexp"
"sort"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -790,23 +788,13 @@ func resourceIotHubCreateUpdate(d *pluginsdk.ResourceData, meta interface{}) err
props.Properties.MinTLSVersion = utils.String(v.(string))
}

if _, err = client.CreateOrUpdate(ctx, id.ResourceGroup, id.Name, props, ""); err != nil {
future, err := client.CreateOrUpdate(ctx, id.ResourceGroup, id.Name, props, "")
if err != nil {
return fmt.Errorf("creating/updating %s: %+v", id, err)
}

timeout := pluginsdk.TimeoutUpdate
if d.IsNewResource() {
timeout = pluginsdk.TimeoutCreate
}
stateConf := &pluginsdk.StateChangeConf{
Pending: []string{"Activating", "Transitioning"},
Target: []string{"Succeeded"},
Refresh: iothubStateRefreshFunc(ctx, client, id.ResourceGroup, id.Name),
Timeout: d.Timeout(timeout),
}

if _, err := stateConf.WaitForStateContext(ctx); err != nil {
return fmt.Errorf("waiting for the completion of the creating/updating of %s: %+v", id, err)
if err := future.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("waiting for creation/update of %q: %+v", id, err)
}

d.SetId(id.ID())
Expand Down Expand Up @@ -954,82 +942,17 @@ func resourceIotHubDelete(d *pluginsdk.ResourceData, meta interface{}) error {
locks.ByName(id.Name, IothubResourceName)
defer locks.UnlockByName(id.Name, IothubResourceName)

// when running acctest of `azurerm_iot_security_solution`, we found after delete the iot security solution, the iothub provisionState is `Transitioning`
// if we delete directly, the func `client.Delete` will throw error
// so first wait for the iotHub state become succeed
stateConf := &pluginsdk.StateChangeConf{
Pending: []string{"Activating", "Transitioning"},
Target: []string{"Succeeded"},
Refresh: iothubStateRefreshFunc(ctx, client, id.ResourceGroup, id.Name),
Timeout: d.Timeout(pluginsdk.TimeoutDelete),
}

if _, err := stateConf.WaitForStateContext(ctx); err != nil {
return fmt.Errorf("waiting for ProvisioningState of %s to become `Succeeded`: %+v", id, err)
}

if _, err := client.Delete(ctx, id.ResourceGroup, id.Name); err != nil {
future, err := client.Delete(ctx, id.ResourceGroup, id.Name)
if err != nil {
return err
}

return waitForIotHubToBeDeleted(ctx, client, id.ResourceGroup, id.Name, d)
}

func waitForIotHubToBeDeleted(ctx context.Context, client *devices.IotHubResourceClient, resourceGroup, name string, d *pluginsdk.ResourceData) error {
// we can't use the Waiter here since the API returns a 404 once it's deleted which is considered a polling status code..
log.Printf("[DEBUG] Waiting for IotHub (%q in Resource Group %q) to be deleted", name, resourceGroup)
stateConf := &pluginsdk.StateChangeConf{
Pending: []string{"200"},
Target: []string{"404"},
Refresh: iothubStateStatusCodeRefreshFunc(ctx, client, resourceGroup, name),
Timeout: d.Timeout(pluginsdk.TimeoutDelete),
}

if _, err := stateConf.WaitForStateContext(ctx); err != nil {
return fmt.Errorf("waiting for IotHub (%q in Resource Group %q) to be deleted: %+v", name, resourceGroup, err)
if err := future.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("waiting for creation/update of %q: %+v", id, err)
}

return nil
}

func iothubStateRefreshFunc(ctx context.Context, client *devices.IotHubResourceClient, resourceGroup, name string) pluginsdk.StateRefreshFunc {
return func() (interface{}, string, error) {
res, err := client.Get(ctx, resourceGroup, name)

log.Printf("Retrieving IoTHub %q (Resource Group %q) returned Status %d", resourceGroup, name, res.StatusCode)

if err != nil {
if utils.ResponseWasNotFound(res.Response) {
return res, "NotFound", nil
}
return nil, "", fmt.Errorf("polling for the Provisioning State of the IotHub %q (RG: %q): %+v", name, resourceGroup, err)
}

if res.Properties == nil || res.Properties.ProvisioningState == nil {
return res, "", fmt.Errorf("polling for the Provisioning State of the IotHub %q (RG: %q): %+v", name, resourceGroup, err)
}

return res, *res.Properties.ProvisioningState, nil
}
}

func iothubStateStatusCodeRefreshFunc(ctx context.Context, client *devices.IotHubResourceClient, resourceGroup, name string) pluginsdk.StateRefreshFunc {
return func() (interface{}, string, error) {
res, err := client.Get(ctx, resourceGroup, name)

log.Printf("Retrieving IoTHub %q (Resource Group %q) returned Status %d", resourceGroup, name, res.StatusCode)

if err != nil {
if utils.ResponseWasNotFound(res.Response) {
return res, strconv.Itoa(res.StatusCode), nil
}
return nil, "", fmt.Errorf("polling for the status of the IotHub %q (RG: %q): %+v", name, resourceGroup, err)
}

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

func expandIoTHubRoutes(d *pluginsdk.ResourceData) *[]devices.RouteProperties {
routeList := d.Get("route").([]interface{})

Expand Down