Skip to content

Commit a178856

Browse files
authored
fix(dns_record): migration (#18)
1 parent 895dffd commit a178856

File tree

1 file changed

+7
-73
lines changed

1 file changed

+7
-73
lines changed

internal/resources/dns_record/v4_to_v5.go

Lines changed: 7 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -144,97 +144,31 @@ func (m *V4ToV5Migrator) processDataAttribute(block *hclwrite.Block, recordType
144144
}
145145
}
146146

147-
func (m *V4ToV5Migrator) TransformState(ctx *transform.Context, stateJSON gjson.Result, resourcePath string) (string, error) {
148-
// This function can receive either:
149-
// 1. A full state document (in unit tests)
150-
// 2. A single resource instance (in actual migration framework)
151-
// We need to handle both cases
152-
153-
result := stateJSON.String()
154-
155-
// Check if this is a full state document (has "resources" key) or a single instance
156-
if stateJSON.Get("resources").Exists() {
157-
// Full state document - transform all resources
158-
return m.transformFullState(result, stateJSON)
159-
}
147+
func (m *V4ToV5Migrator) TransformState(ctx *transform.Context, instance gjson.Result, resourcePath string) (string, error) {
148+
// This function receives a single instance and needs to return the transformed instance JSON
149+
result := instance.String()
160150

161151
// Single instance - check if it's a valid DNS record instance
162-
if !stateJSON.Exists() || !stateJSON.Get("attributes").Exists() {
152+
if !instance.Exists() || !instance.Get("attributes").Exists() {
163153
return result, nil
164154
}
165155

166-
attrs := stateJSON.Get("attributes")
156+
attrs := instance.Get("attributes")
167157
if !attrs.Get("name").Exists() || !attrs.Get("type").Exists() || !attrs.Get("zone_id").Exists() {
168158
// Even for invalid/incomplete instances, we need to set schema_version for v5
169159
result, _ = sjson.Set(result, "schema_version", 0)
170160
return result, nil
171161
}
172162

173163
// Transform the single instance
174-
result = m.transformSingleDNSInstance(result, stateJSON)
175-
164+
result = m.transformSingleDNSInstance(result, instance)
165+
176166
// Ensure schema_version is set to 0 for v5
177167
result, _ = sjson.Set(result, "schema_version", 0)
178168

179169
return result, nil
180170
}
181171

182-
// transformFullState handles transformation of a full state document
183-
func (m *V4ToV5Migrator) transformFullState(result string, stateJSON gjson.Result) (string, error) {
184-
// Process all resources in the state
185-
resources := stateJSON.Get("resources")
186-
if !resources.Exists() {
187-
return result, nil
188-
}
189-
190-
resources.ForEach(func(key, resource gjson.Result) bool {
191-
resourceType := resource.Get("type").String()
192-
193-
// Check if this is a DNS record resource we need to migrate
194-
if !m.CanHandle(resourceType) {
195-
return true // continue
196-
}
197-
198-
// Rename cloudflare_record to cloudflare_dns_record
199-
if resourceType == "cloudflare_record" {
200-
resourcePath := "resources." + key.String() + ".type"
201-
result, _ = sjson.Set(result, resourcePath, "cloudflare_dns_record")
202-
}
203-
204-
// Process each instance
205-
instances := resource.Get("instances")
206-
instances.ForEach(func(instKey, instance gjson.Result) bool {
207-
instPath := "resources." + key.String() + ".instances." + instKey.String()
208-
209-
// Transform the instance attributes in place
210-
attrs := instance.Get("attributes")
211-
if attrs.Exists() && attrs.Get("name").Exists() &&
212-
attrs.Get("type").Exists() && attrs.Get("zone_id").Exists() {
213-
// Get the instance JSON string
214-
instJSON := instance.String()
215-
// Transform it
216-
transformedInst := m.transformSingleDNSInstance(instJSON, instance)
217-
218-
// Ensure schema_version is set to 0 for v5
219-
transformedInst, _ = sjson.Set(transformedInst, "schema_version", 0)
220-
221-
// Update the result with the transformed instance
222-
// Using the raw JSON string directly to preserve all fields including schema_version
223-
result, _ = sjson.SetRaw(result, instPath, transformedInst)
224-
} else {
225-
// Even if attributes don't exist or are incomplete, update schema_version
226-
schemaPath := instPath + ".schema_version"
227-
result, _ = sjson.Set(result, schemaPath, 0)
228-
}
229-
return true
230-
})
231-
232-
return true
233-
})
234-
235-
return result, nil
236-
}
237-
238172
// transformSingleDNSInstance transforms a single DNS record instance
239173
func (m *V4ToV5Migrator) transformSingleDNSInstance(result string, instance gjson.Result) string {
240174
attrs := instance.Get("attributes")

0 commit comments

Comments
 (0)