Skip to content

Commit 4eca7ae

Browse files
committed
fix(block): ignore inaccessible legacy snapshot in diff
1 parent a2dbf63 commit 4eca7ae

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

internal/services/block/helpers_block.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package block
22

33
import (
44
"context"
5+
"errors"
56
"time"
67

78
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff"
@@ -56,6 +57,28 @@ func customDiffCannotShrink(key string) schema.CustomizeDiffFunc {
5657
})
5758
}
5859

60+
func customDiffSnapshot(key string) schema.CustomizeDiffFunc {
61+
return func(ctx context.Context, diff *schema.ResourceDiff, i any) error {
62+
if !diff.HasChange(key) {
63+
return nil
64+
}
65+
oldValue, NewValue := diff.GetChange(key)
66+
blockAPI, zone, _, err := NewAPIWithZoneAndID(i, diff.Id())
67+
if err != nil {
68+
return errors.New(err.Error())
69+
}
70+
_, err = blockAPI.GetSnapshot(&block.GetSnapshotRequest{
71+
Zone: zone,
72+
SnapshotID: oldValue.(string),
73+
})
74+
if (httperrors.Is403(err) || httperrors.Is404(err)) && NewValue == nil {
75+
return nil
76+
}
77+
78+
return diff.ForceNew("snapshot_id")
79+
}
80+
}
81+
5982
func migrateInstanceToBlockVolume(ctx context.Context, api *instancehelpers.BlockAndInstanceAPI, zone scw.Zone, volumeID string, timeout time.Duration) (*block.Volume, error) {
6083
instanceVolumeResp, err := api.GetVolume(&instance.GetVolumeRequest{
6184
Zone: zone,

internal/services/block/volume.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ func ResourceVolume() *schema.Resource {
5454
"snapshot_id": {
5555
Type: schema.TypeString,
5656
Optional: true,
57-
ForceNew: true,
5857
Description: "The snapshot to create the volume from",
5958
DiffSuppressFunc: dsf.Locality,
6059
},
@@ -79,6 +78,7 @@ func ResourceVolume() *schema.Resource {
7978
},
8079
CustomizeDiff: customdiff.All(
8180
customDiffCannotShrink("size_in_gb"),
81+
customDiffSnapshot("snapshot_id"),
8282
),
8383
}
8484
}

0 commit comments

Comments
 (0)