Skip to content

Commit b46e3eb

Browse files
vaishakdineshtamas-jozsa
authored andcommitted
feat(state_upgarder) refactor state upgrader for dns_record, page_rule
and load_balancer_pool
1 parent 684ed91 commit b46e3eb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+5122
-1471
lines changed

.github/workflows/migration-tests.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ env:
2828
CLOUDFLARE_HYPERDRIVE_DATABASE_HOSTNAME: ${{ secrets.CLOUDFLARE_HYPERDRIVE_DATABASE_HOSTNAME }}
2929
TF_ACC: 1
3030
TF_MIGRATE_BINARY_PATH: ${{ github.workspace }}/tf-migrate
31+
TF_MIG_TEST: true
3132

3233
jobs:
3334
migration-tests:

internal/acctest/acctest.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1129,6 +1129,15 @@ func MigrationV2TestStepWithPlan(t *testing.T, v4Config string, tmpDir string, e
11291129
return []resource.TestStep{migrationStep, planStep, validationStep}
11301130
}
11311131

1132+
// InferMigrationVersions determines source and target versions from test provider version.
1133+
// Returns ("v4", "v5") for v4.x versions, ("v5", "v5") for v5.x versions.
1134+
func InferMigrationVersions(testVersion string) (source, target string) {
1135+
if strings.HasPrefix(testVersion, "5.") {
1136+
return "v5", "v5"
1137+
}
1138+
return "v4", "v5"
1139+
}
1140+
11321141
// MigrationTestStep creates a test step that runs the migration command and validates with v5 provider
11331142
func MigrationTestStep(t *testing.T, v4Config string, tmpDir string, exactVersion string, stateChecks []statecheck.StateCheck) resource.TestStep {
11341143
// Choose the appropriate plan check based on the version
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package v500
2+
3+
import (
4+
"context"
5+
6+
"github.com/hashicorp/terraform-plugin-framework/resource"
7+
"github.com/hashicorp/terraform-plugin-log/tflog"
8+
)
9+
10+
// UpgradeFromV0 handles state upgrades from earlier v500 versions (schema_version=0) to current v500.
11+
// This is a no-op upgrade since the schema is compatible - just copy state through.
12+
//
13+
// Why this exists: Terraform requires explicit upgraders to be defined for version tracking,
14+
// even when the schema is identical. This ensures the schema_version is updated in the statefile.
15+
func UpgradeFromV0(
16+
ctx context.Context,
17+
req resource.UpgradeStateRequest,
18+
resp *resource.UpgradeStateResponse,
19+
) {
20+
// No-op upgrade: schema is compatible, just copy raw state through
21+
// We use the raw state value directly to avoid issues with custom field type serialization
22+
resp.State.Raw = req.State.Raw
23+
}
24+
25+
// UpgradeFromLegacyV3 handles state upgrades from the legacy cloudflare_record resource to cloudflare_dns_record.
26+
// This is triggered when users manually run `terraform state mv cloudflare_record.x cloudflare_dns_record.x`
27+
// (Terraform < 1.8), which preserves the source schema_version=3 from the legacy provider.
28+
//
29+
// Note: schema_version=3 was the final schema version of cloudflare_record in the legacy (SDKv2) provider
30+
// before it was deprecated. The state structure matches SourceCloudflareRecordModel.
31+
func UpgradeFromLegacyV3(ctx context.Context, req resource.UpgradeStateRequest, resp *resource.UpgradeStateResponse) {
32+
tflog.Info(ctx, "Upgrading DNS record state from legacy cloudflare_record (schema_version=3)")
33+
34+
// Parse the state (schema_version=3, source resource type)
35+
var sourceState SourceCloudflareRecordModel
36+
resp.Diagnostics.Append(req.State.Get(ctx, &sourceState)...)
37+
if resp.Diagnostics.HasError() {
38+
return
39+
}
40+
41+
// Transform to target
42+
targetState, diags := Transform(ctx, sourceState)
43+
resp.Diagnostics.Append(diags...)
44+
if resp.Diagnostics.HasError() {
45+
return
46+
}
47+
48+
// Set the upgraded state
49+
resp.Diagnostics.Append(resp.State.Set(ctx, targetState)...)
50+
51+
tflog.Info(ctx, "State upgrade from legacy cloudflare_record completed successfully")
52+
}

0 commit comments

Comments
 (0)