Skip to content

Commit e764422

Browse files
author
cortlyons
committed
Update migration logic
1 parent 5462e19 commit e764422

File tree

4 files changed

+98
-42
lines changed

4 files changed

+98
-42
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
{
2424
"attributes": {
2525
"id": "test-zone-id-generic",
26-
"value": "on",
26+
"value": "off",
2727
"zone_id": "test-zone-id"
2828
},
2929
"schema_version": 0
@@ -32,7 +32,7 @@
3232
"mode": "managed",
3333
"name": "generic",
3434
"provider": "provider[\"registry.terraform.io/cloudflare/cloudflare\"]",
35-
"type": "cloudflare_argo_tiered_caching"
35+
"type": "cloudflare_tiered_cache"
3636
},
3737
{
3838
"instances": [
@@ -71,7 +71,7 @@
7171
{
7272
"attributes": {
7373
"id": "test-zone-id-lifecycle",
74-
"value": "on",
74+
"value": "off",
7575
"zone_id": "test-zone-id"
7676
},
7777
"schema_version": 0
@@ -80,7 +80,7 @@
8080
"mode": "managed",
8181
"name": "lifecycle",
8282
"provider": "provider[\"registry.terraform.io/cloudflare/cloudflare\"]",
83-
"type": "cloudflare_argo_tiered_caching"
83+
"type": "cloudflare_tiered_cache"
8484
}
8585
],
8686
"serial": 1,

integration/v4_to_v5/testdata/tiered_cache/expected/tiered_cache.tf

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ variable "cache_type" {
1818
default = "off"
1919
}
2020

21+
22+
23+
24+
2125
resource "cloudflare_tiered_cache" "smart" {
2226
zone_id = var.cloudflare_zone_id
2327
value = "on"
@@ -26,7 +30,6 @@ resource "cloudflare_argo_tiered_caching" "smart" {
2630
zone_id = var.cloudflare_zone_id
2731
value = "on"
2832
}
29-
3033
resource "cloudflare_tiered_cache" "off" {
3134
zone_id = var.cloudflare_zone_id
3235
value = "off"
@@ -35,7 +38,6 @@ resource "cloudflare_argo_tiered_caching" "off" {
3538
zone_id = var.cloudflare_zone_id
3639
value = "off"
3740
}
38-
3941
resource "cloudflare_tiered_cache" "generic" {
4042
zone_id = var.cloudflare_zone_id
4143
value = "off"
@@ -44,13 +46,10 @@ resource "cloudflare_argo_tiered_caching" "generic" {
4446
zone_id = var.cloudflare_zone_id
4547
value = "on"
4648
}
47-
48-
// TODO
4949
resource "cloudflare_tiered_cache" "variable" {
5050
zone_id = var.cloudflare_zone_id
5151
value = var.cache_type
5252
}
53-
5453
resource "cloudflare_tiered_cache" "lifecycle" {
5554
zone_id = var.cloudflare_zone_id
5655
value = "off"

internal/resources/tiered_cache/v4_to_v5.go

Lines changed: 87 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -50,23 +50,30 @@ func (m *V4ToV5Migrator) TransformConfig(ctx *transform.Context, block *hclwrite
5050
body := block.Body()
5151

5252
blocks := make([]*hclwrite.Block, 0)
53-
removeOriginal := false
5453

55-
// rename cache_type to value
54+
// rename cache_type to value in the original block
5655
tfhcl.RenameAttribute(body, "cache_type", "value")
5756

5857
resourceName := tfhcl.GetResourceName(block)
5958
value := tfhcl.ExtractStringFromAttribute(body.GetAttribute("value"))
59+
6060
if value == "smart" {
61-
// cache_type="smart" → value="on"
61+
// cache_type="smart" → value="on" for both resources
62+
// Create tiered_cache resource with value="on"
6263
tfhcl.SetAttribute(body, "value", "on")
63-
blocks = append(blocks, block)
64-
} else if value == "off" {
65-
// cache_type="off" → value="off"
66-
tfhcl.SetAttribute(body, "value", "off")
67-
blocks = append(blocks, block)
68-
} else if value == "generic" {
69-
newBlock := tfhcl.CreateDerivedBlock(
64+
tieredCacheBlock := tfhcl.CreateDerivedBlock(
65+
block,
66+
"cloudflare_tiered_cache",
67+
resourceName,
68+
tfhcl.AttributeTransform{
69+
Copy: []string{"zone_id", "value"},
70+
CopyMetaArguments: true,
71+
},
72+
)
73+
blocks = append(blocks, tieredCacheBlock)
74+
75+
// Create argo_tiered_caching resource with value="on"
76+
argoBlock := tfhcl.CreateDerivedBlock(
7077
block,
7178
"cloudflare_argo_tiered_caching",
7279
resourceName,
@@ -76,21 +83,78 @@ func (m *V4ToV5Migrator) TransformConfig(ctx *transform.Context, block *hclwrite
7683
CopyMetaArguments: true,
7784
},
7885
)
86+
blocks = append(blocks, argoBlock)
87+
} else if value == "off" {
88+
// cache_type="off" → value="off" for both resources
89+
// Create tiered_cache resource with value="off"
90+
tfhcl.SetAttribute(body, "value", "off")
91+
tieredCacheBlock := tfhcl.CreateDerivedBlock(
92+
block,
93+
"cloudflare_tiered_cache",
94+
resourceName,
95+
tfhcl.AttributeTransform{
96+
Copy: []string{"zone_id", "value"},
97+
CopyMetaArguments: true,
98+
},
99+
)
100+
blocks = append(blocks, tieredCacheBlock)
79101

80-
movedBlock := tfhcl.CreateMovedBlock(
81-
"cloudflare_tiered_cache."+resourceName,
82-
"cloudflare_argo_tiered_caching."+resourceName,
102+
// Create argo_tiered_caching resource with value="off"
103+
argoBlock := tfhcl.CreateDerivedBlock(
104+
block,
105+
"cloudflare_argo_tiered_caching",
106+
resourceName,
107+
tfhcl.AttributeTransform{
108+
Copy: []string{"zone_id"},
109+
Set: map[string]interface{}{"value": "off"},
110+
CopyMetaArguments: true,
111+
},
83112
)
113+
blocks = append(blocks, argoBlock)
114+
} else if value == "generic" {
115+
// cache_type="generic" → tiered_cache value="off", argo_tiered_caching value="on"
116+
// Create tiered_cache resource with value="off"
117+
tfhcl.SetAttribute(body, "value", "off")
118+
tieredCacheBlock := tfhcl.CreateDerivedBlock(
119+
block,
120+
"cloudflare_tiered_cache",
121+
resourceName,
122+
tfhcl.AttributeTransform{
123+
Copy: []string{"zone_id", "value"},
124+
CopyMetaArguments: true,
125+
},
126+
)
127+
blocks = append(blocks, tieredCacheBlock)
84128

85-
blocks = append(blocks, newBlock, movedBlock)
86-
removeOriginal = true
129+
// Create argo_tiered_caching resource with value="on"
130+
argoBlock := tfhcl.CreateDerivedBlock(
131+
block,
132+
"cloudflare_argo_tiered_caching",
133+
resourceName,
134+
tfhcl.AttributeTransform{
135+
Copy: []string{"zone_id"},
136+
Set: map[string]interface{}{"value": "on"},
137+
CopyMetaArguments: true,
138+
},
139+
)
140+
blocks = append(blocks, argoBlock)
87141
} else {
88-
blocks = append(blocks, block)
142+
// For variables or other expressions, just rename the attribute and copy as-is
143+
tieredCacheBlock := tfhcl.CreateDerivedBlock(
144+
block,
145+
"cloudflare_tiered_cache",
146+
resourceName,
147+
tfhcl.AttributeTransform{
148+
Copy: []string{"zone_id", "value"},
149+
CopyMetaArguments: true,
150+
},
151+
)
152+
blocks = append(blocks, tieredCacheBlock)
89153
}
90154

91155
return &transform.TransformResult{
92156
Blocks: blocks,
93-
RemoveOriginal: removeOriginal,
157+
RemoveOriginal: true,
94158
}, nil
95159
}
96160

@@ -119,35 +183,28 @@ func (m *V4ToV5Migrator) TransformState(ctx *transform.Context, stateJSON gjson.
119183

120184
cacheTypeValue := cacheTypeField.String()
121185

122-
// Determine target resource type based on cache_type value
123-
var targetType string
124-
125186
// Transform based on cache_type value
187+
// All resources stay as cloudflare_tiered_cache in the state
188+
// The argo_tiered_caching resources in the config are new and will be created on next apply
126189
if cacheTypeValue == "generic" {
127-
// Transform to argo_tiered_caching
128-
targetType = "cloudflare_argo_tiered_caching"
190+
// For generic, the tiered_cache resource gets value="off"
129191
result, _ = sjson.Delete(result, "attributes.cache_type")
130-
result, _ = sjson.Set(result, "attributes.value", "on")
192+
result, _ = sjson.Set(result, "attributes.value", "off")
131193
} else if cacheTypeValue == "smart" {
132-
// Keep as tiered_cache, transform smart → on
133-
targetType = "cloudflare_tiered_cache"
194+
// For smart, the tiered_cache resource gets value="on"
134195
result, _ = sjson.Delete(result, "attributes.cache_type")
135196
result, _ = sjson.Set(result, "attributes.value", "on")
136197
} else if cacheTypeValue == "off" {
137-
// Keep as tiered_cache, cache_type → value (no value change)
138-
targetType = "cloudflare_tiered_cache"
198+
// For off, the tiered_cache resource gets value="off"
139199
result, _ = sjson.Delete(result, "attributes.cache_type")
140200
result, _ = sjson.Set(result, "attributes.value", "off")
141201
} else {
142202
// Unknown value (variables, expressions), just rename the field
143-
targetType = "cloudflare_tiered_cache"
144203
result = state.RenameField(result, "attributes", attrs, "cache_type", "value")
145204
}
146205

147206
// Set schema version to 0
148207
result, _ = sjson.Set(result, "schema_version", 0)
149208

150-
transform.SetStateTypeRename(ctx, resourceName, "cloudflare_tiered_cache", targetType)
151-
152209
return result, nil
153210
}

internal/resources/tiered_cache/v4_to_v5_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,11 @@ resource "cloudflare_tiered_cache" "smart" {
104104
value = "on"
105105
}
106106
resource "cloudflare_argo_tiered_caching" "smart" {
107-
zone_id = "zone1"
107+
zone_id = "zone2"
108108
value = "on"
109109
}
110110
resource "cloudflare_tiered_cache" "generic" {
111-
zone_id = "zone2"
111+
zone_id = "zone3"
112112
value = "off"
113113
}
114114
resource "cloudflare_argo_tiered_caching" "generic" {
@@ -168,7 +168,7 @@ func TestStateTransformation(t *testing.T) {
168168
"schema_version": 0,
169169
"attributes": {
170170
"zone_id": "test-zone-id",
171-
"value": "on",
171+
"value": "off",
172172
"id": "test-id"
173173
}
174174
}`,

0 commit comments

Comments
 (0)