|
9 | 9 | block "github.com/scaleway/scaleway-sdk-go/api/block/v1alpha1" |
10 | 10 | "github.com/scaleway/scaleway-sdk-go/api/instance/v1" |
11 | 11 | "github.com/scaleway/scaleway-sdk-go/scw" |
| 12 | + "github.com/scaleway/terraform-provider-scaleway/v2/internal/dsf" |
12 | 13 | "github.com/scaleway/terraform-provider-scaleway/v2/internal/httperrors" |
| 14 | + "github.com/scaleway/terraform-provider-scaleway/v2/internal/locality" |
13 | 15 | "github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/zonal" |
14 | 16 | "github.com/scaleway/terraform-provider-scaleway/v2/internal/meta" |
15 | 17 | "github.com/scaleway/terraform-provider-scaleway/v2/internal/services/instance/instancehelpers" |
@@ -56,6 +58,32 @@ func customDiffCannotShrink(key string) schema.CustomizeDiffFunc { |
56 | 58 | }) |
57 | 59 | } |
58 | 60 |
|
| 61 | +func customDiffSnapshot(key string) schema.CustomizeDiffFunc { |
| 62 | + return func(ctx context.Context, diff *schema.ResourceDiff, i any) error { |
| 63 | + if !diff.HasChange(key) { |
| 64 | + return nil |
| 65 | + } |
| 66 | + |
| 67 | + oldValue, newValue := diff.GetChange(key) |
| 68 | + if dsf.Locality(key, oldValue.(string), newValue.(string), nil) { |
| 69 | + return nil |
| 70 | + } |
| 71 | + |
| 72 | + blockAPI := block.NewAPI(meta.ExtractScwClient(i)) |
| 73 | + zone, id, _ := locality.ParseLocalizedID(oldValue.(string)) |
| 74 | + |
| 75 | + _, err := blockAPI.GetSnapshot(&block.GetSnapshotRequest{ |
| 76 | + SnapshotID: id, |
| 77 | + Zone: scw.Zone(zone), |
| 78 | + }) |
| 79 | + if (httperrors.Is403(err) || httperrors.Is404(err)) && newValue == "" { |
| 80 | + return nil |
| 81 | + } |
| 82 | + |
| 83 | + return diff.ForceNew(key) |
| 84 | + } |
| 85 | +} |
| 86 | + |
59 | 87 | func migrateInstanceToBlockVolume(ctx context.Context, api *instancehelpers.BlockAndInstanceAPI, zone scw.Zone, volumeID string, timeout time.Duration) (*block.Volume, error) { |
60 | 88 | instanceVolumeResp, err := api.GetVolume(&instance.GetVolumeRequest{ |
61 | 89 | Zone: zone, |
|
0 commit comments