Skip to content

Commit 1e175ea

Browse files
authored
Merge pull request #882 from SimoneDutto/upgrade-deprecated-fieldss
#882 ## Description Handle deprecated fields. I've decided not to handle `machines` automatically because it can require creating new machine resources, and in general is not an obvious upgrade path.
2 parents a2b8b34 + 53eff87 commit 1e175ea

11 files changed

+163
-5
lines changed

tf-upgrader/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ It also upgrades output blocks that reference `juju_model.*.name` to use `juju_m
2626

2727
It also upgrades the `required_providers` block from specifying version `0.x` to `>= 1.0.0`.
2828

29+
It also handles deprecated fields in resource configurations:
30+
31+
- **`placement`**: Shows a warning for `juju_application` resources using the deprecated `placement` field, recommending migration to the `machines` field
32+
- **`principal`**: Automatically removes the unused `principal` field from `juju_application` resources
33+
- **`series`**: Automatically upgrades the deprecated `series` field to `base` for both `juju_application` and `juju_machine` resources
34+
2935
## Usage
3036

3137
Upgrade a single file:
@@ -78,6 +84,8 @@ output "model_name" {
7884

7985
The tool will show warnings for variables that contain "model" in their name, as these may need manual review.
8086

87+
The tool will also show warnings for deprecated fields that require manual intervention, such as the `placement` field which should be migrated to use the `machines` field according to the documentation.
88+
8189
## Testing
8290

8391
```bash
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
resource "juju_application" "wordpress" {
2+
model = juju_model.my_model.name
3+
name = "wordpress"
4+
charm {
5+
name = "wordpress"
6+
}
7+
placement = "0"
8+
principal = true
9+
series = "jammy"
10+
units = 1
11+
}
12+
13+
resource "juju_model" "my_model" {
14+
name = "wordpress-model"
15+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
resource "juju_application" "wordpress" {
2+
model = juju_model.my_model.name
3+
name = "wordpress"
4+
charm {
5+
name = "wordpress"
6+
}
7+
principal = true
8+
units = 1
9+
}
10+
11+
resource "juju_model" "my_model" {
12+
name = "wordpress-model"
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
resource "juju_application" "wordpress" {
2+
model = juju_model.my_model.name
3+
name = "wordpress"
4+
charm {
5+
name = "wordpress"
6+
}
7+
series = "jammy"
8+
units = 1
9+
}
10+
11+
resource "juju_model" "my_model" {
12+
name = "wordpress-model"
13+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
resource "juju_machine" "machine1" {
2+
model = juju_model.my_model.name
3+
series = "jammy"
4+
}
5+
6+
resource "juju_model" "my_model" {
7+
name = "machine-model"
8+
}

tf-upgrader/main.go

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,8 @@ func transformTerraformFile(src []byte, filename string) (*transformationResult,
102102

103103
switch block.Type() {
104104
case "resource":
105-
processResourceBlock(block, filename, &upgraded)
105+
processResourceBlockModelUUID(block, filename, &upgraded)
106+
processResourceBlockDeprecatedFields(block, filename, srcBlockMap, blockKey, &upgraded, &warnings)
106107
case "output":
107108
processOutputBlock(block, filename, &upgraded)
108109
case "variable":
@@ -164,8 +165,8 @@ func processFile(filename string) (bool, int) {
164165
return result.WasUpgraded, result.Warnings
165166
}
166167

167-
// processResourceBlock handles resource blocks that need model -> model_uuid transformation
168-
func processResourceBlock(block *hclwrite.Block, _ string, upgraded *bool) {
168+
// processResourceBlockModelUUID handles resource blocks that need model -> model_uuid transformation
169+
func processResourceBlockModelUUID(block *hclwrite.Block, _ string, upgraded *bool) {
169170
if len(block.Labels()) < 2 {
170171
return
171172
}
@@ -473,3 +474,56 @@ func discoverTerraformFiles(target string) ([]string, error) {
473474

474475
return filesToProcess, nil
475476
}
477+
478+
// processResourceBlockDeprecatedFields handles deprecated fields in resource blocks
479+
func processResourceBlockDeprecatedFields(block *hclwrite.Block, filename string, srcBlockMap map[string]*hclsyntax.Block, blockKey string, upgraded *bool, warnings *int) {
480+
if len(block.Labels()) < 2 {
481+
return
482+
}
483+
484+
resourceType := block.Labels()[0]
485+
resourceName := block.Labels()[1]
486+
487+
// Get line number from source block
488+
lineNum := 0
489+
if srcBlock, exists := srcBlockMap[blockKey]; exists {
490+
lineNum = srcBlock.DefRange().Start.Line
491+
}
492+
493+
switch resourceType {
494+
case "juju_application":
495+
// Handle placement field - warning only
496+
if placementAttr := block.Body().GetAttribute("placement"); placementAttr != nil {
497+
*warnings++
498+
fmt.Printf(" ⚠️ WARNING: %s:%d:1 - %s.%s uses deprecated 'placement' field - use 'machines' instead. See documentation for migration guidance.\n", filename, lineNum, resourceType, resourceName)
499+
}
500+
501+
// Handle principal field - remove it
502+
if principalAttr := block.Body().GetAttribute("principal"); principalAttr != nil {
503+
block.Body().RemoveAttribute("principal")
504+
*upgraded = true
505+
fmt.Printf(" ✓ Removed deprecated 'principal' field from %s.%s (field was unused)\n", resourceType, resourceName)
506+
}
507+
508+
// Handle series field - replace with base
509+
if seriesAttr := block.Body().GetAttribute("series"); seriesAttr != nil {
510+
// Get the series value and set it as base
511+
expr := seriesAttr.Expr()
512+
block.Body().SetAttributeRaw("base", expr.BuildTokens(nil))
513+
block.Body().RemoveAttribute("series")
514+
*upgraded = true
515+
fmt.Printf(" ✓ Upgraded %s.%s: 'series' -> 'base'\n", resourceType, resourceName)
516+
}
517+
518+
case "juju_machine":
519+
// Handle series field - replace with base
520+
if seriesAttr := block.Body().GetAttribute("series"); seriesAttr != nil {
521+
// Get the series value and set it as base
522+
expr := seriesAttr.Expr()
523+
block.Body().SetAttributeRaw("base", expr.BuildTokens(nil))
524+
block.Body().RemoveAttribute("series")
525+
*upgraded = true
526+
fmt.Printf(" ✓ Upgraded %s.%s: 'series' -> 'base'\n", resourceType, resourceName)
527+
}
528+
}
529+
}

tf-upgrader/main_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,13 @@ func TestDiscoverTerraformFiles(t *testing.T) {
6666
{
6767
name: "discover files from in folder",
6868
target: "in",
69-
expectedCount: 14,
69+
expectedCount: 18,
7070
expectError: false,
7171
},
7272
{
7373
name: "discover files from in folder with relative path",
7474
target: filepath.Join(".", "in"),
75-
expectedCount: 14,
75+
expectedCount: 18,
7676
expectError: false,
7777
},
7878
{
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
resource "juju_application" "wordpress" {
2+
name = "wordpress"
3+
charm {
4+
name = "wordpress"
5+
}
6+
placement = "0"
7+
units = 1
8+
model_uuid = juju_model.my_model.uuid
9+
base = "jammy"
10+
}
11+
12+
resource "juju_model" "my_model" {
13+
name = "wordpress-model"
14+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
resource "juju_application" "wordpress" {
2+
name = "wordpress"
3+
charm {
4+
name = "wordpress"
5+
}
6+
units = 1
7+
model_uuid = juju_model.my_model.uuid
8+
}
9+
10+
resource "juju_model" "my_model" {
11+
name = "wordpress-model"
12+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
resource "juju_application" "wordpress" {
2+
name = "wordpress"
3+
charm {
4+
name = "wordpress"
5+
}
6+
units = 1
7+
model_uuid = juju_model.my_model.uuid
8+
base = "jammy"
9+
}
10+
11+
resource "juju_model" "my_model" {
12+
name = "wordpress-model"
13+
}

0 commit comments

Comments
 (0)