From c27dd33aac03c5f8df59f309dea10b1b83a38855 Mon Sep 17 00:00:00 2001 From: magodo Date: Thu, 21 Sep 2023 16:04:08 +0800 Subject: [PATCH 1/2] `azurerm_management_lock` - Add post-create/delete polling to tolerate RP propagation --- .../resource/management_lock_resource.go | 41 ++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/internal/services/resource/management_lock_resource.go b/internal/services/resource/management_lock_resource.go index c083a0f08995..27abf000373c 100644 --- a/internal/services/resource/management_lock_resource.go +++ b/internal/services/resource/management_lock_resource.go @@ -4,6 +4,7 @@ package resource import ( + "context" "fmt" "time" @@ -32,7 +33,6 @@ func resourceManagementLock() *pluginsdk.Resource { Timeouts: &pluginsdk.ResourceTimeout{ Create: pluginsdk.DefaultTimeout(30 * time.Minute), Read: pluginsdk.DefaultTimeout(5 * time.Minute), - Update: pluginsdk.DefaultTimeout(30 * time.Minute), Delete: pluginsdk.DefaultTimeout(30 * time.Minute), }, @@ -100,6 +100,19 @@ func resourceManagementLockCreate(d *pluginsdk.ResourceData, meta interface{}) e return fmt.Errorf("creating %s: %+v", id, err) } + stateConf := &pluginsdk.StateChangeConf{ + Target: []string{ + "OK", + }, + Refresh: managementLockStateRefreshFunc(ctx, client, id), + MinTimeout: 10 * time.Second, + ContinuousTargetOccurence: 12, + Timeout: d.Timeout(pluginsdk.TimeoutUpdate), + } + if _, err := stateConf.WaitForStateContext(ctx); err != nil { + return fmt.Errorf("waiting for %s to finish create replication", id) + } + d.SetId(id.ID()) return resourceManagementLockRead(d, meta) } @@ -155,5 +168,31 @@ func resourceManagementLockDelete(d *pluginsdk.ResourceData, meta interface{}) e return fmt.Errorf("deleting %s: %+v", *id, err) } + stateConf := &pluginsdk.StateChangeConf{ + Target: []string{ + "NotFound", + }, + Refresh: managementLockStateRefreshFunc(ctx, client, *id), + MinTimeout: 10 * time.Second, + ContinuousTargetOccurence: 12, + Timeout: d.Timeout(pluginsdk.TimeoutUpdate), + } + if _, err := stateConf.WaitForStateContext(ctx); err != nil { + return fmt.Errorf("waiting for %s to finish delete replication", id) + } + return nil } + +func managementLockStateRefreshFunc(ctx context.Context, client *managementlocks.ManagementLocksClient, id managementlocks.ScopedLockId) pluginsdk.StateRefreshFunc { + return func() (interface{}, string, error) { + resp, err := client.GetByScope(ctx, id) + if err != nil { + if response.WasNotFound(resp.HttpResponse) { + return resp, "NotFound", nil + } + return nil, "Error", err + } + return "OK", "OK", nil + } +} From 562a0709615088ab795c3dc3141f153e44227291 Mon Sep 17 00:00:00 2001 From: magodo Date: Fri, 22 Sep 2023 10:04:57 +0800 Subject: [PATCH 2/2] Using ctx deadline --- .../services/resource/management_lock_resource.go | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/internal/services/resource/management_lock_resource.go b/internal/services/resource/management_lock_resource.go index 27abf000373c..08ea57aa062f 100644 --- a/internal/services/resource/management_lock_resource.go +++ b/internal/services/resource/management_lock_resource.go @@ -100,6 +100,11 @@ func resourceManagementLockCreate(d *pluginsdk.ResourceData, meta interface{}) e return fmt.Errorf("creating %s: %+v", id, err) } + deadline, ok := ctx.Deadline() + if !ok { + return fmt.Errorf("internal-error: context was missing a deadline") + } + stateConf := &pluginsdk.StateChangeConf{ Target: []string{ "OK", @@ -107,7 +112,7 @@ func resourceManagementLockCreate(d *pluginsdk.ResourceData, meta interface{}) e Refresh: managementLockStateRefreshFunc(ctx, client, id), MinTimeout: 10 * time.Second, ContinuousTargetOccurence: 12, - Timeout: d.Timeout(pluginsdk.TimeoutUpdate), + Timeout: time.Until(deadline), } if _, err := stateConf.WaitForStateContext(ctx); err != nil { return fmt.Errorf("waiting for %s to finish create replication", id) @@ -168,6 +173,11 @@ func resourceManagementLockDelete(d *pluginsdk.ResourceData, meta interface{}) e return fmt.Errorf("deleting %s: %+v", *id, err) } + deadline, ok := ctx.Deadline() + if !ok { + return fmt.Errorf("internal-error: context was missing a deadline") + } + stateConf := &pluginsdk.StateChangeConf{ Target: []string{ "NotFound", @@ -175,7 +185,7 @@ func resourceManagementLockDelete(d *pluginsdk.ResourceData, meta interface{}) e Refresh: managementLockStateRefreshFunc(ctx, client, *id), MinTimeout: 10 * time.Second, ContinuousTargetOccurence: 12, - Timeout: d.Timeout(pluginsdk.TimeoutUpdate), + Timeout: time.Until(deadline), } if _, err := stateConf.WaitForStateContext(ctx); err != nil { return fmt.Errorf("waiting for %s to finish delete replication", id)