Skip to content

Commit ae4649f

Browse files
authored
chore(custom_pages): adjust for state upgraders logic (#206)
1 parent 6d748d6 commit ae4649f

6 files changed

Lines changed: 24 additions & 195 deletions

File tree

integration/v4_to_v5/testdata/custom_pages/expected/custom_pages.tf

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,15 @@ variable "cloudflare_domain" {
1414
type = string
1515
}
1616

17+
# URL for custom pages - this URL contains the required tokens for all error page types
18+
locals {
19+
custom_pages_url = "https://custom-pages-basic.terraform-provider-acceptance-testing.workers.dev/"
20+
}
21+
1722
# Basic zone-scoped custom page with real URL
1823
resource "cloudflare_custom_pages" "error_500" {
1924
zone_id = var.cloudflare_zone_id
20-
url = "https://custom-pages-500-error-for-e2e.terraform-testing-a09.workers.dev/"
25+
url = local.custom_pages_url
2126
state = "customized"
2227
identifier = "500_errors"
2328
}

integration/v4_to_v5/testdata/custom_pages/expected/terraform.tfstate

Lines changed: 0 additions & 44 deletions
This file was deleted.

integration/v4_to_v5/testdata/custom_pages/input/custom_pages.tf

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,15 @@ variable "cloudflare_domain" {
1414
type = string
1515
}
1616

17+
# URL for custom pages - this URL contains the required tokens for all error page types
18+
locals {
19+
custom_pages_url = "https://custom-pages-basic.terraform-provider-acceptance-testing.workers.dev/"
20+
}
21+
1722
# Basic zone-scoped custom page with real URL
1823
resource "cloudflare_custom_pages" "error_500" {
1924
zone_id = var.cloudflare_zone_id
2025
type = "500_errors"
21-
url = "https://custom-pages-500-error-for-e2e.terraform-testing-a09.workers.dev/"
26+
url = local.custom_pages_url
2227
state = "customized"
2328
}

integration/v4_to_v5/testdata/custom_pages/input/terraform.tfstate

Lines changed: 0 additions & 43 deletions
This file was deleted.

internal/resources/custom_pages/v4_to_v5.go

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package custom_pages
33
import (
44
"github.com/hashicorp/hcl/v2/hclwrite"
55
"github.com/tidwall/gjson"
6-
"github.com/tidwall/sjson"
76

87
"github.com/cloudflare/tf-migrate/internal"
98
"github.com/cloudflare/tf-migrate/internal/transform"
@@ -67,33 +66,15 @@ func (m *V4ToV5Migrator) TransformConfig(ctx *transform.Context, block *hclwrite
6766
}
6867

6968
// TransformState handles state file transformations.
70-
// This function receives a single resource instance and returns the transformed instance JSON.
69+
// State transformation is handled by the provider's StateUpgraders (UpgradeState).
7170
func (m *V4ToV5Migrator) TransformState(ctx *transform.Context, stateJSON gjson.Result, resourcePath, resourceName string) (string, error) {
72-
result := stateJSON.String()
73-
74-
// Check if it's a valid custom_pages instance
75-
if !stateJSON.Exists() || !stateJSON.Get("attributes").Exists() {
76-
// Even for invalid instances, set schema_version for v5
77-
result, _ = sjson.Set(result, "schema_version", 0)
78-
return result, nil
79-
}
80-
81-
attrs := stateJSON.Get("attributes")
82-
83-
// 1. type → identifier
84-
if typeField := attrs.Get("type"); typeField.Exists() {
85-
result, _ = sjson.Set(result, "attributes.identifier", typeField.Value())
86-
result, _ = sjson.Delete(result, "attributes.type")
87-
}
88-
89-
// 2. Ensure state field exists (required in v5, optional in v4)
90-
if !attrs.Get("state").Exists() {
91-
// Add default value if missing
92-
result, _ = sjson.Set(result, "attributes.state", "default")
93-
}
94-
95-
// 3. Set schema_version to 0 for v5
96-
result, _ = sjson.Set(result, "schema_version", 0)
71+
// State passthrough - provider handles all state migration
72+
return stateJSON.String(), nil
73+
}
9774

98-
return result, nil
75+
// UsesProviderStateUpgrader indicates that this resource uses provider-based state migration.
76+
// This tells tf-migrate that the provider's StateUpgraders (UpgradeState) handle all state
77+
// transformation, not tf-migrate's TransformState function.
78+
func (m *V4ToV5Migrator) UsesProviderStateUpgrader() bool {
79+
return true
9980
}

internal/resources/custom_pages/v4_to_v5_test.go

Lines changed: 3 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -98,83 +98,8 @@ resource "cloudflare_custom_pages" "error_1000" {
9898
testhelpers.RunConfigTransformTests(t, tests, migrator)
9999
})
100100

101-
// Test state transformations
102-
t.Run("StateTransformation", func(t *testing.T) {
103-
tests := []testhelpers.StateTestCase{
104-
{
105-
Name: "basic state transformation",
106-
Input: `{
107-
"schema_version": 0,
108-
"attributes": {
109-
"id": "500_errors",
110-
"zone_id": "14e48053f1836c10cb86ba178633826a",
111-
"account_id": null,
112-
"type": "500_errors",
113-
"url": "https://cf-tf-test.com/500.html",
114-
"state": "customized"
115-
}
116-
}`,
117-
Expected: `{
118-
"schema_version": 0,
119-
"attributes": {
120-
"id": "500_errors",
121-
"zone_id": "14e48053f1836c10cb86ba178633826a",
122-
"account_id": null,
123-
"identifier": "500_errors",
124-
"url": "https://cf-tf-test.com/500.html",
125-
"state": "customized"
126-
}
127-
}`,
128-
},
129-
{
130-
Name: "state transformation with missing state field",
131-
Input: `{
132-
"schema_version": 0,
133-
"attributes": {
134-
"id": "500_errors",
135-
"zone_id": "14e48053f1836c10cb86ba178633826a",
136-
"type": "500_errors",
137-
"url": "https://cf-tf-test.com/500.html"
138-
}
139-
}`,
140-
Expected: `{
141-
"schema_version": 0,
142-
"attributes": {
143-
"id": "500_errors",
144-
"zone_id": "14e48053f1836c10cb86ba178633826a",
145-
"identifier": "500_errors",
146-
"url": "https://cf-tf-test.com/500.html",
147-
"state": "default"
148-
}
149-
}`,
150-
},
151-
{
152-
Name: "account-scoped state",
153-
Input: `{
154-
"schema_version": 0,
155-
"attributes": {
156-
"id": "basic_challenge",
157-
"account_id": "dbb2ef7988061049c4bd32bf6883fde0",
158-
"zone_id": null,
159-
"type": "basic_challenge",
160-
"url": "https://cf-tf-test.com/challenge.html",
161-
"state": "default"
162-
}
163-
}`,
164-
Expected: `{
165-
"schema_version": 0,
166-
"attributes": {
167-
"id": "basic_challenge",
168-
"account_id": "dbb2ef7988061049c4bd32bf6883fde0",
169-
"zone_id": null,
170-
"identifier": "basic_challenge",
171-
"url": "https://cf-tf-test.com/challenge.html",
172-
"state": "default"
173-
}
174-
}`,
175-
},
176-
}
177-
178-
testhelpers.RunStateTransformTests(t, tests, migrator)
101+
// State transformation tests removed - state migration is now handled by provider's StateUpgraders
102+
t.Run("StateTransformation_Removed", func(t *testing.T) {
103+
t.Skip("State transformation tests removed - state migration is now handled by provider's StateUpgraders")
179104
})
180105
}

0 commit comments

Comments
 (0)