Skip to content
This repository has been archived by the owner on Feb 2, 2024. It is now read-only.

New pattern to update module #5

Merged
merged 7 commits into from
Oct 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions .grit/patterns/edit_module.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
---
title: Edit module
---

Update a module by specifying its old `source` and the new one.

It will update the `source` attribute of the modules and remove all variables that
are not found on another module `path`.

```grit
engine marzano(0.1)
language hcl

pattern fix_module($old_source, $new_source, $allow_variables) {
`module $_ {
$args
}` where {
// Make sure we're looking at a module with $old_source
$args <: contains `source = $old_source` => `source = $new_source`,
// Check all attributes
$args <: maybe contains bubble($allow_variables) `$key = $value` as $attr where {
$key <: or {
// Remap some keys
`identifier` => `db_identifier`,
// Keep source
`source`,
// Keep meta-arguments
`count`,
`depends_on`,
`for_each`,
`lifecycle`,
`provider`,
$_ where {
$allow_variables <: some bubble($key) $candidate where { $candidate <: $key }
},
// Finally, delete others we don't recognize
$_ where { $attr => .}
}

// Remove others
// or {
// `version = $_`,
// `storage_encrypted = $_`,
// `apply_immediately = $_`
// } => .
} until attribute()
}
}

pattern collect_variables($var_names) {
`variable $name {
$_
}` where {
$name <: string_lit($content),
if ($var_names <: undefined) {
$var_names = []
},
$var_names += $content,
}
}

pattern edit_module($old_source, $new_source, $module_path) {
$var_names = [],
some bubble($var_names) file($name, $body) where {
$name <: r"\./variables/.*", // Path to get variables from
$body <: contains collect_variables($var_names),
},
some bubble($old_source, $new_source, $var_names) file($name, $body) where {
$body <: contains fix_module($old_source, $new_source, allow_variables=$var_names)
}
}

files(files = edit_module(old_source=`"old_source"`, new_source=`"new_source"`, module_path=""))
```
23 changes: 23 additions & 0 deletions .grit/patterns/test_edit_module/expected/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module "test_module1" {
source = "new_source"
variable1 = "variable1"
variable2 = "variable2"


}

module "test_module2" {
source = "new_source"
variable1 = "variable1"
variable2 = "variable2"


}

module "test_module3" {
source = "another_source"
variable1 = "variable1"
variable2 = "variable2"
variable3 = "variable3"
variable4 = "variable4"
}
23 changes: 23 additions & 0 deletions .grit/patterns/test_edit_module/input/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module "test_module1" {
source = "old_source"
variable1 = "variable1"
variable2 = "variable2"
variable3 = "variable3"
variable4 = "variable4"
}

module "test_module2" {
source = "old_source"
variable1 = "variable1"
variable2 = "variable2"
variable3 = "variable3"
variable4 = "variable4"
}

module "test_module3" {
source = "another_source"
variable1 = "variable1"
variable2 = "variable2"
variable3 = "variable3"
variable4 = "variable4"
}
10 changes: 10 additions & 0 deletions .grit/patterns/test_edit_module/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash

# test.sh should be run from the current directory: hcl/.grit/patterns/test_edit_module

set -e
cp input/main.tf input/main.tf.bak # 1. Backup input.tf
grit apply ../edit_module.md --force --suppress-output # 2. Apply pattern
diff input/main.tf expected/main.tf # 3. Check that the modified input.tf equals expect.tf
mv input/main.tf.bak input/main.tf # 4. Restore input.tf from the backup
echo "Test successful"
4 changes: 4 additions & 0 deletions .grit/patterns/test_edit_module/variables/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
variable "variable1" {}
variable "variable2" {
description = "description"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
variable "variable3" {}
variable "variable4" {
description = "description"
}
Loading