Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update operation count outputs #321

Merged
merged 3 commits into from
Jan 9, 2024
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
3 changes: 3 additions & 0 deletions image/src/github_actions/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ def generate_delimiter():
return ''.join(random.choice(string.ascii_lowercase) for _ in range(20))

def output(name: str, value: Any) -> None:
if not isinstance(value, str):
value = str(value)

if 'GITHUB_OUTPUT' in os.environ and Path(os.environ['GITHUB_OUTPUT']).is_file():
with open(os.environ['GITHUB_OUTPUT'], 'a') as f:
if len(value.splitlines()) > 1:
Expand Down
36 changes: 31 additions & 5 deletions image/src/plan_summary/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,42 @@
import sys
from github_actions.commands import output

def summary(plan: str) -> dict[str, int]:
operations = {
'add': 0,
'change': 0,
'destroy': 0,
'move': None,
'import': 0
}

to_move = 0

for line in plan.splitlines():
if re.match(r' # \S+ has moved to \S+$', line):
to_move += 1

if not line.startswith('Plan:'):
continue

for op in re.finditer(r'(\d+) to (\w+)', line):
operations[op[2]] = int(op[1])

if operations['move'] is None:
operations['move'] = to_move

return operations


def main() -> None:
"""Entrypoint for terraform-backend"""
"""Entrypoint for plan_summary"""

with open(sys.argv[1]) as f:
plan = f.read()

if match := re.search(r'^Plan: (\d+) to add, (\d+) to change, (\d+) to destroy', plan, re.MULTILINE):
output('to_add', match[1])
output('to_change', match[2])
output('to_destroy', match[3])
for action, count in summary(plan).items():
output(f'to_{action}', count)


if __name__ == '__main__':
sys.exit(main())
123 changes: 123 additions & 0 deletions tests/test_action_summary.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
from plan_summary.__main__ import summary


def test_summary():
plan = '''An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
+ create

Terraform will perform the following actions:

+ random_string.my_string
id: <computed>
length: "11"
lower: "true"
min_lower: "0"
min_numeric: "0"
min_special: "0"
min_upper: "0"
number: "true"
result: <computed>
special: "true"
upper: "true"
Plan: 1 to add, 0 to change, 0 to destroy.
'''

assert summary(plan) == {
'add': 1,
'change': 0,
'destroy': 0,
'move': 0,
'import': 0
}

def test_summary_import():
plan = '''An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
+ create

Terraform will perform the following actions:

+ random_string.my_string
id: <computed>
length: "11"
lower: "true"
min_lower: "0"
min_numeric: "0"
min_special: "0"
min_upper: "0"
number: "true"
result: <computed>
special: "true"
upper: "true"
Plan: 5 to import, 1 to add, 0 to change, 0 to destroy.
'''

assert summary(plan) == {
'add': 1,
'change': 0,
'destroy': 0,
'move': 0,
'import': 5
}

def test_summary_remove():
plan = '''An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
+ create

Terraform will perform the following actions:

+ random_string.my_string
id: <computed>
length: "11"
lower: "true"
min_lower: "0"
min_numeric: "0"
min_special: "0"
min_upper: "0"
number: "true"
result: <computed>
special: "true"
upper: "true"
Plan: 5 to import, 1 to add, 3 to remove, 0 to change, 0 to destroy.
'''

assert summary(plan) == {
'add': 1,
'change': 0,
'destroy': 0,
'move': 0,
'import': 5,
'remove': 3
}

def test_summary_move():
plan = """Terraform will perform the following actions:

# random_string.your_string has moved to random_string.my_string
resource "random_string" "my_string" {
id = "Iyh3jLKc"
length = 8
# (8 unchanged attributes hidden)
}

# random_string.blah_string has moved to random_string.my_string2
resource "random_string" "my_string2" {
id = "Iyh3jLKc"
length = 8
# (8 unchanged attributes hidden)
}

Plan: 4 to add, 8 to change, 1 to destroy.
"""

actual = summary(plan)
assert actual == {
'add': 4,
'change': 8,
'destroy': 1,
'move': 2,
'import': 0
}

Loading