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 1 commit
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
41 changes: 40 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,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)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should be getting the deadline which will give us the remaining time for the create and not using the update timeout which was removed above?

Suggested change
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)
}
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 +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)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here


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
}
}