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

azurerm_management_lock - Add post-create/delete polling to tolerate RP propagation #23345

Merged
merged 2 commits into from
Sep 22, 2023
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
51 changes: 50 additions & 1 deletion internal/services/resource/management_lock_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package resource

import (
"context"
"fmt"
"time"

Expand Down Expand Up @@ -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),
},

Expand Down Expand Up @@ -100,6 +100,24 @@ 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",
},
Refresh: managementLockStateRefreshFunc(ctx, client, id),
MinTimeout: 10 * time.Second,
ContinuousTargetOccurence: 12,
Timeout: time.Until(deadline),
}
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)
}
Expand Down Expand Up @@ -155,5 +173,36 @@ 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",
},
Refresh: managementLockStateRefreshFunc(ctx, client, *id),
MinTimeout: 10 * time.Second,
ContinuousTargetOccurence: 12,
Timeout: time.Until(deadline),
}
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
}
}
Loading