Skip to content

Commit c76a87f

Browse files
committed
chore: refactor
1 parent 1aab1a1 commit c76a87f

File tree

46 files changed

+4220
-343
lines changed

Some content is hidden

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

46 files changed

+4220
-343
lines changed

REFACTORING_PLAN.md

Lines changed: 1882 additions & 0 deletions
Large diffs are not rendered by default.

REFACTORING_SUMMARY.md

Lines changed: 532 additions & 0 deletions
Large diffs are not rendered by default.

internal/resources/api_token/v4_to_v5.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ func (m *V4ToV5Migrator) TransformState(ctx *transform.Context, stateJSON gjson.
283283

284284
attributes = gjson.Get(result, attributesPath)
285285
result = state.EnsureField(result, attributesPath, attributes, "last_used_on", nil)
286-
result, _ = sjson.Set(result, "schema_version", 1)
286+
result = state.SetSchemaVersion(result, 1)
287287

288288
return result, nil
289289
}

internal/resources/argo/v4_to_v5.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ func (m *V4ToV5Migrator) TransformState(ctx *transform.Context, stateJSON gjson.
176176
}
177177

178178
// Set schema_version to 0 for v5
179-
result, _ = sjson.Set(result, "schema_version", 0)
179+
result = state.SetSchemaVersion(result, 0)
180180

181181
transform.SetStateTypeRename(ctx, resourceName, "cloudflare_argo", targetType)
182182

internal/resources/custom_pages/v4_to_v5.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/cloudflare/tf-migrate/internal"
99
"github.com/cloudflare/tf-migrate/internal/transform"
1010
tfhcl "github.com/cloudflare/tf-migrate/internal/transform/hcl"
11+
"github.com/cloudflare/tf-migrate/internal/transform/state"
1112
)
1213

1314
// V4ToV5Migrator handles the migration of cloudflare_custom_pages from v4 to v5.
@@ -74,7 +75,7 @@ func (m *V4ToV5Migrator) TransformState(ctx *transform.Context, stateJSON gjson.
7475
// Check if it's a valid custom_pages instance
7576
if !stateJSON.Exists() || !stateJSON.Get("attributes").Exists() {
7677
// Even for invalid instances, set schema_version for v5
77-
result, _ = sjson.Set(result, "schema_version", 0)
78+
result = state.SetSchemaVersion(result, 0)
7879
return result, nil
7980
}
8081

@@ -93,7 +94,7 @@ func (m *V4ToV5Migrator) TransformState(ctx *transform.Context, stateJSON gjson.
9394
}
9495

9596
// 3. Set schema_version to 0 for v5
96-
result, _ = sjson.Set(result, "schema_version", 0)
97+
result = state.SetSchemaVersion(result, 0)
9798

9899
return result, nil
99100
}

internal/resources/dns_record/v4_to_v5.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,15 +173,15 @@ func (m *V4ToV5Migrator) TransformState(ctx *transform.Context, stateJSON gjson.
173173
attrs := stateJSON.Get("attributes")
174174
if !attrs.Get("name").Exists() || !attrs.Get("type").Exists() || !attrs.Get("zone_id").Exists() {
175175
// Even for invalid/incomplete instances, we need to set schema_version for v5
176-
result, _ = sjson.Set(result, "schema_version", 0)
176+
result = state.SetSchemaVersion(result, 0)
177177
return result, nil
178178
}
179179

180180
// Transform the single instance
181181
result = m.transformSingleDNSInstance(result, stateJSON)
182182

183183
// Ensure schema_version is set to 0 for v5
184-
result, _ = sjson.Set(result, "schema_version", 0)
184+
result = state.SetSchemaVersion(result, 0)
185185

186186
return result, nil
187187
}

internal/resources/healthcheck/v4_to_v5.go

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ func (m *V4ToV5Migrator) TransformState(ctx *transform.Context, instance gjson.R
191191

192192
if !attrs.Exists() {
193193
// Set schema_version even for invalid instances
194-
result, _ = sjson.Set(result, "schema_version", 0)
194+
result = state.SetSchemaVersion(result, 0)
195195
return result, nil
196196
}
197197

@@ -224,7 +224,7 @@ func (m *V4ToV5Migrator) TransformState(ctx *transform.Context, instance gjson.R
224224
// We should NOT remove them, just leave them as-is for provider to handle
225225

226226
// Set schema_version
227-
result, _ = sjson.Set(result, "schema_version", 0)
227+
result = state.SetSchemaVersion(result, 0)
228228

229229
return result, nil
230230
}
@@ -263,15 +263,12 @@ func (m *V4ToV5Migrator) createHTTPConfig(stateJSON string, attrs gjson.Result)
263263
stateJSON, _ = sjson.Set(stateJSON, "attributes.http_config", httpConfig)
264264

265265
// Remove the root-level fields that moved into http_config
266+
fieldNames := make([]string, 0, len(httpFields)+1)
266267
for oldField := range httpFields {
267-
if attrs.Get(oldField).Exists() {
268-
stateJSON, _ = sjson.Delete(stateJSON, "attributes."+oldField)
269-
}
270-
}
271-
// Remove header from root if it exists
272-
if attrs.Get("header").Exists() {
273-
stateJSON, _ = sjson.Delete(stateJSON, "attributes.header")
268+
fieldNames = append(fieldNames, oldField)
274269
}
270+
fieldNames = append(fieldNames, "header")
271+
stateJSON = state.RemoveFieldsIfExist(stateJSON, "attributes", attrs, fieldNames...)
275272
}
276273

277274
return stateJSON
@@ -294,12 +291,7 @@ func (m *V4ToV5Migrator) createTCPConfig(stateJSON string, attrs gjson.Result) s
294291
stateJSON, _ = sjson.Set(stateJSON, "attributes.tcp_config", tcpConfig)
295292

296293
// Remove the root-level fields that moved into tcp_config
297-
if attrs.Get("method").Exists() {
298-
stateJSON, _ = sjson.Delete(stateJSON, "attributes.method")
299-
}
300-
if attrs.Get("port").Exists() {
301-
stateJSON, _ = sjson.Delete(stateJSON, "attributes.port")
302-
}
294+
stateJSON = state.RemoveFieldsIfExist(stateJSON, "attributes", attrs, "method", "port")
303295
}
304296

305297
return stateJSON

internal/resources/list/v4_to_v5.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ func (m *V4ToV5Migrator) TransformState(ctx *transform.Context, stateJSON gjson.
8686
attrs := stateJSON.Get("attributes")
8787

8888
if !attrs.Exists() {
89-
result, _ = sjson.Set(result, "schema_version", 0)
89+
result = state.SetSchemaVersion(result, 0)
9090
return result, nil
9191
}
9292

@@ -114,7 +114,7 @@ func (m *V4ToV5Migrator) TransformState(ctx *transform.Context, stateJSON gjson.
114114
result, _ = sjson.Set(result, "attributes.num_items", floatVal)
115115
}
116116

117-
result, _ = sjson.Set(result, "schema_version", 0)
117+
result = state.SetSchemaVersion(result, 0)
118118

119119
return result, nil
120120
}

internal/resources/load_balancer/v4_to_v5.go

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/cloudflare/tf-migrate/internal"
1111
"github.com/cloudflare/tf-migrate/internal/transform"
1212
tfhcl "github.com/cloudflare/tf-migrate/internal/transform/hcl"
13+
"github.com/cloudflare/tf-migrate/internal/transform/state"
1314
)
1415

1516
// V4ToV5Migrator handles migration of load balancer resources from v4 to v5
@@ -146,6 +147,7 @@ func (m *V4ToV5Migrator) TransformState(ctx *transform.Context, stateJSON gjson.
146147
// v4: session_affinity_attributes as array → v5: as object
147148

148149
result := stateJSON.String()
150+
attrs := stateJSON.Get("attributes")
149151

150152
// Use regex to rename attributes in JSON
151153
// Replace "default_pool_ids" with "default_pools"
@@ -154,35 +156,14 @@ func (m *V4ToV5Migrator) TransformState(ctx *transform.Context, stateJSON gjson.
154156
// Replace "fallback_pool_id" with "fallback_pool"
155157
result = strings.Replace(result, `"fallback_pool_id"`, `"fallback_pool"`, -1)
156158

157-
// Transform session_affinity_attributes from array to object
158-
// v4: "session_affinity_attributes": [{ ... }] or []
159-
// v5: "session_affinity_attributes": { ... } or null
160-
sessionAffinityAttrs := stateJSON.Get("attributes.session_affinity_attributes")
161-
if sessionAffinityAttrs.Exists() && sessionAffinityAttrs.IsArray() {
162-
if len(sessionAffinityAttrs.Array()) > 0 {
163-
firstElement := sessionAffinityAttrs.Array()[0]
164-
result, _ = sjson.Set(result, "attributes.session_affinity_attributes", firstElement.Value())
165-
} else {
166-
// Empty array -> null
167-
result, _ = sjson.Set(result, "attributes.session_affinity_attributes", nil)
168-
}
169-
}
170-
171159
// Transform single-object fields from arrays to objects or null
172160
// v4: field: [{ ... }] or []
173161
// v5: field: { ... } or null
174-
singleObjectFields := []string{"adaptive_routing", "location_strategy", "random_steering"}
162+
singleObjectFields := []string{"session_affinity_attributes", "adaptive_routing", "location_strategy", "random_steering"}
175163
for _, field := range singleObjectFields {
176-
fieldData := stateJSON.Get("attributes." + field)
177-
if fieldData.Exists() && fieldData.IsArray() {
178-
if len(fieldData.Array()) > 0 {
179-
firstElement := fieldData.Array()[0]
180-
result, _ = sjson.Set(result, "attributes."+field, firstElement.Value())
181-
} else {
182-
// Empty array -> null
183-
result, _ = sjson.Set(result, "attributes."+field, nil)
184-
}
185-
}
164+
result = state.TransformFieldArrayToObject(result, "attributes", attrs, field, state.ArrayToObjectOptions{
165+
TransformEmptyToNull: true,
166+
})
186167
}
187168

188169
// Transform empty arrays to null for map fields that v5 expects as null or maps
@@ -204,7 +185,7 @@ func (m *V4ToV5Migrator) TransformState(ctx *transform.Context, stateJSON gjson.
204185
}
205186

206187
// Reset schema_version to 0 for v5 (v4 uses schema_version 1)
207-
result, _ = sjson.Set(result, "schema_version", 0)
188+
result = state.SetSchemaVersion(result, 0)
208189

209190
return result, nil
210191
}

internal/resources/load_balancer_monitor/v4_to_v5.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ func (m *V4ToV5Migrator) TransformState(ctx *transform.Context, instance gjson.R
115115

116116
if !attrs.Exists() {
117117
// Set schema_version even for invalid instances
118-
result, _ = sjson.Set(result, "schema_version", 0)
118+
result = state.SetSchemaVersion(result, 0)
119119
return result, nil
120120
}
121121

@@ -172,7 +172,7 @@ func (m *V4ToV5Migrator) TransformState(ctx *transform.Context, instance gjson.R
172172
})
173173

174174
// Set schema_version
175-
result, _ = sjson.Set(result, "schema_version", 0)
175+
result = state.SetSchemaVersion(result, 0)
176176

177177
return result, nil
178178
}

0 commit comments

Comments
 (0)