|
| 1 | +package v500 |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + "github.com/hashicorp/terraform-plugin-framework/diag" |
| 7 | + "github.com/hashicorp/terraform-plugin-framework/resource" |
| 8 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 9 | + "github.com/hashicorp/terraform-plugin-log/tflog" |
| 10 | +) |
| 11 | + |
| 12 | +// UpgradeFromV0 handles state upgrades from v4 SDKv2 provider (schema_version=0) to v5 (version=500). |
| 13 | +// |
| 14 | +// This performs the transformation from v4 → v5 format: |
| 15 | +// - cache_type="smart" → value="on" |
| 16 | +// - cache_type="off" → value="off" |
| 17 | +// - cache_type="generic" → value="off" (argo_tiered_caching with value="on" is created from config) |
| 18 | +func UpgradeFromV0(ctx context.Context, req resource.UpgradeStateRequest, resp *resource.UpgradeStateResponse) { |
| 19 | + tflog.Info(ctx, "Upgrading tiered_cache state from v4 SDKv2 provider (schema_version=0)") |
| 20 | + |
| 21 | + // Parse v4 state using v4 model |
| 22 | + var v4State SourceTieredCacheModel |
| 23 | + resp.Diagnostics.Append(req.State.Get(ctx, &v4State)...) |
| 24 | + if resp.Diagnostics.HasError() { |
| 25 | + return |
| 26 | + } |
| 27 | + |
| 28 | + // Transform v4 → v5 |
| 29 | + v5State, transformDiags := Transform(ctx, v4State) |
| 30 | + resp.Diagnostics.Append(transformDiags...) |
| 31 | + if resp.Diagnostics.HasError() { |
| 32 | + return |
| 33 | + } |
| 34 | + |
| 35 | + // Write transformed state |
| 36 | + resp.Diagnostics.Append(resp.State.Set(ctx, v5State)...) |
| 37 | + tflog.Info(ctx, "State upgrade from v4 to v5 completed successfully") |
| 38 | +} |
| 39 | + |
| 40 | +// UpgradeFromV1 handles state upgrades from v5 Plugin Framework provider (version=1) to v5 (version=500). |
| 41 | +// |
| 42 | +// This is a no-op upgrade since the schema is compatible - just bumps the version. |
| 43 | +// This handler is only triggered when TF_MIG_TEST=1 (GetSchemaVersion returns 500). |
| 44 | +func UpgradeFromV1(ctx context.Context, req resource.UpgradeStateRequest, resp *resource.UpgradeStateResponse) { |
| 45 | + tflog.Info(ctx, "Upgrading tiered_cache state from version=1 to version=500 (no-op)") |
| 46 | + |
| 47 | + // CRITICAL: For no-op upgrades, copy raw state directly |
| 48 | + // This preserves all state data without any transformation |
| 49 | + resp.State.Raw = req.State.Raw |
| 50 | + |
| 51 | + tflog.Info(ctx, "State version bump from 1 to 500 completed") |
| 52 | +} |
| 53 | + |
| 54 | +// Transform converts source (legacy v4) state to target (current v5) state. |
| 55 | +// This function handles the cache_type to value transformation. |
| 56 | +func Transform(ctx context.Context, source SourceTieredCacheModel) (*TargetTieredCacheModel, diag.Diagnostics) { |
| 57 | + var diags diag.Diagnostics |
| 58 | + |
| 59 | + // Transform cache_type to value |
| 60 | + var newValue string |
| 61 | + cacheType := source.CacheType.ValueString() |
| 62 | + switch cacheType { |
| 63 | + case "smart": |
| 64 | + newValue = "on" |
| 65 | + case "off": |
| 66 | + newValue = "off" |
| 67 | + case "generic": |
| 68 | + // generic means argo tiered caching is ON and tiered caching is OFF |
| 69 | + // tf-migrate creates both cloudflare_tiered_cache (value=off) and cloudflare_argo_tiered_caching (value=on) |
| 70 | + // The argo_tiered_caching resource will be created on apply from the config (not from state upgrade) |
| 71 | + newValue = "off" |
| 72 | + default: |
| 73 | + // For unknown values (e.g., variable references), preserve as-is |
| 74 | + // The value will be validated by the schema validator |
| 75 | + newValue = cacheType |
| 76 | + } |
| 77 | + |
| 78 | + // Create the target state |
| 79 | + target := &TargetTieredCacheModel{ |
| 80 | + ID: source.ID, |
| 81 | + ZoneID: source.ZoneID, |
| 82 | + Value: types.StringValue(newValue), |
| 83 | + Editable: source.Editable, |
| 84 | + ModifiedOn: source.ModifiedOn, |
| 85 | + } |
| 86 | + |
| 87 | + return target, diags |
| 88 | +} |
0 commit comments