Skip to content

Commit 2b3e24c

Browse files
Add lock checks in delete functions of disk and config resources (#2237)
* Add checks for disks and config to prevent a locked Linode from being in an undesired state (shutdown) during deletion attempts * Update linode/instancedisk/framework_resource.go Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update linode/instancedisk/framework_resource.go Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent dfaffe7 commit 2b3e24c

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

linode/helper/instance.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"encoding/base64"
77
"encoding/json"
88
"fmt"
9+
"slices"
910
"time"
1011

1112
fwdiag "github.com/hashicorp/terraform-plugin-framework/diag"
@@ -506,3 +507,18 @@ func WaitForInstanceNonTransientStatus(
506507

507508
return instance.Status, nil
508509
}
510+
511+
// LinodeIsLockedWithCannotDeleteSubresources checks if a Linode instance
512+
// has the CannotDeleteWithSubresources lock type.
513+
func LinodeIsLockedWithCannotDeleteSubresources(
514+
ctx context.Context,
515+
client *linodego.Client,
516+
linodeID int,
517+
) (locked bool, err error) {
518+
linode, err := client.GetInstance(ctx, linodeID)
519+
if err != nil {
520+
return
521+
}
522+
523+
return slices.Contains(linode.Locks, linodego.LockTypeCannotDeleteWithSubresources), nil
524+
}

linode/instanceconfig/resource.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,19 @@ func deleteResource(ctx context.Context, d *schema.ResourceData, meta any) diag.
345345
return diag.Errorf("Error finding the specified Linode Instance: %s", err)
346346
}
347347

348+
locked, err := helper.LinodeIsLockedWithCannotDeleteSubresources(ctx, &client, linodeID)
349+
if err != nil {
350+
return diag.Errorf("failed to get locks of Linode: %s", err)
351+
}
352+
353+
if locked {
354+
return diag.Errorf(
355+
"can't delete config %d in Linode %d: the resource lock on the Linode prohibits deletion "+
356+
"of its subresources, which includes this config",
357+
id, linodeID,
358+
)
359+
}
360+
348361
// Shutdown the instance if the config is in use
349362
if booted, err := isConfigBooted(ctx, &client, inst, id, d.Get("booted").(bool)); err != nil {
350363
return diag.Errorf("failed to check if config is booted: %s", err)

linode/instancedisk/framework_resource.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,21 @@ func (r *Resource) Delete(
334334
return
335335
}
336336

337+
locked, err := helper.LinodeIsLockedWithCannotDeleteSubresources(ctx, client, linodeID)
338+
if err != nil {
339+
resp.Diagnostics.AddError("Failed to Get Locks of Linode", err.Error())
340+
return
341+
}
342+
343+
if locked {
344+
resp.Diagnostics.AddError(
345+
fmt.Sprintf("Can't Delete Disk %d in Linode %d", id, linodeID),
346+
"the resource lock on the Linode prohibits deletion "+
347+
"of its subresources, which includes this disk",
348+
)
349+
return
350+
}
351+
337352
deleteTimeout, diags := state.Timeouts.Delete(ctx, DefaultVolumeDeleteTimeout)
338353
resp.Diagnostics.Append(diags...)
339354
if resp.Diagnostics.HasError() {

0 commit comments

Comments
 (0)