Skip to content

Commit 96fb6fe

Browse files
committed
Fix the markdown output and adjust the dev installation
1 parent 57a8391 commit 96fb6fe

File tree

4 files changed

+87
-57
lines changed

4 files changed

+87
-57
lines changed

.task/checksum/install

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
9192b1e72423ee1c4741793e0965836d
1+
1603f214e5fa9a54e3841e5e0f5f7bf5

pyproject.toml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "tfsumpy"
3-
version = "0.1.0"
3+
version = "0.2.0"
44
description = "A Python tool for Terraform state summary"
55
authors = ["Rafael H de Carvalho <[email protected]>"]
66
readme = "README.md"
@@ -37,6 +37,13 @@ pytest-cov = ">=4.1.0"
3737
pytest-mock = ">=3.6.1"
3838
coverage = ">=7.2.0"
3939

40+
[tool.poetry.extras]
41+
dev = [
42+
"pytest-cov",
43+
"pytest-mock",
44+
"coverage"
45+
]
46+
4047
[tool.poetry.scripts]
4148
tfsumpy = "tfsumpy.__main__:main"
4249

tfsumpy/plan/reporter.py

Lines changed: 77 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import logging
2-
from typing import Dict, Any
2+
from typing import Dict, Any, List
33
from ..reporters.base_reporter import BaseReporter
44
from ..reporter import ReporterInterface
55
import json as _json
@@ -48,6 +48,66 @@ def get_report(self, data: Any, **kwargs) -> Dict:
4848
self.logger.error(f"Error processing report: {str(e)}")
4949
raise
5050

51+
def _process_resources(self, resources: List[Dict], show_changes: bool = False, show_details: bool = False) -> List[Dict]:
52+
"""Process resources and their changes.
53+
54+
Args:
55+
resources: List of resources to process
56+
show_changes: Whether to include attribute changes
57+
show_details: Whether to include additional details
58+
59+
Returns:
60+
List[Dict]: Processed resources with changes and details
61+
"""
62+
processed_resources = []
63+
for resource in resources:
64+
resource_data = {
65+
'resource_type': resource['resource_type'],
66+
'identifier': resource['identifier'],
67+
'action': resource['action'],
68+
'provider': resource.get('provider', 'unknown'),
69+
'module': resource.get('module', 'root')
70+
}
71+
72+
# Process changes if requested
73+
if show_changes:
74+
before = resource.get('before', {}) or {}
75+
after = resource.get('after', {}) or {}
76+
changes = []
77+
78+
# Get all changed attributes
79+
all_attrs = set(before.keys()) | set(after.keys())
80+
skip_attrs = {'id', 'tags_all'} # Skip internal attributes
81+
82+
for attr in sorted(all_attrs - skip_attrs):
83+
before_val = before.get(attr)
84+
after_val = after.get(attr)
85+
86+
if before_val != after_val:
87+
changes.append({
88+
'attribute': attr,
89+
'before': before_val,
90+
'after': after_val
91+
})
92+
93+
if changes:
94+
resource_data['changes'] = changes
95+
96+
# Add additional details if requested
97+
if show_details:
98+
resource_data['details'] = {
99+
'dependencies': resource.get('dependencies', []),
100+
'tags': resource.get('tags', {}),
101+
'raw': {
102+
'before': resource.get('before', {}),
103+
'after': resource.get('after', {})
104+
}
105+
}
106+
107+
processed_resources.append(resource_data)
108+
109+
return processed_resources
110+
51111
def print_report(self, data: Any, **kwargs) -> None:
52112
"""Print the plan analysis report.
53113
@@ -152,13 +212,20 @@ def print_report_markdown(self, data: Any, **kwargs) -> None:
152212
show_details = kwargs.get('show_details', False)
153213
show_changes = kwargs.get('show_changes', False)
154214

215+
# Process resources
216+
processed_resources = self._process_resources(
217+
report.get('resources', []),
218+
show_changes=show_changes,
219+
show_details=show_details
220+
)
221+
155222
# Prepare template data
156223
template_data = {
157224
'total_resources': report['total_changes'],
158225
'resources_to_add': report['change_breakdown']['create'],
159226
'resources_to_change': report['change_breakdown']['update'],
160227
'resources_to_destroy': report['change_breakdown']['delete'],
161-
'resources': report.get('resources', []),
228+
'resources': processed_resources,
162229
'show_changes': show_changes,
163230
'show_details': show_details,
164231
'timestamp': datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
@@ -183,6 +250,13 @@ def print_report_json(self, data: Any, **kwargs) -> None:
183250
show_details = kwargs.get('show_details', False)
184251
show_changes = kwargs.get('show_changes', False)
185252

253+
# Process resources
254+
processed_resources = self._process_resources(
255+
report.get('resources', []),
256+
show_changes=show_changes,
257+
show_details=show_details
258+
)
259+
186260
# Prepare JSON output structure
187261
json_output = {
188262
'metadata': {
@@ -196,56 +270,9 @@ def print_report_json(self, data: Any, **kwargs) -> None:
196270
'resources_to_change': report['change_breakdown']['update'],
197271
'resources_to_destroy': report['change_breakdown']['delete']
198272
},
199-
'resources': []
273+
'resources': processed_resources
200274
}
201275

202-
# Process resources
203-
for resource in report.get('resources', []):
204-
resource_data = {
205-
'type': resource['resource_type'],
206-
'name': resource['identifier'],
207-
'action': resource['action'],
208-
'provider': resource.get('provider', 'unknown'),
209-
'module': resource.get('module', 'root')
210-
}
211-
212-
# Add changes if requested
213-
if show_changes:
214-
before = resource.get('before', {}) or {}
215-
after = resource.get('after', {}) or {}
216-
changes = []
217-
218-
# Get all changed attributes
219-
all_attrs = set(before.keys()) | set(after.keys())
220-
skip_attrs = {'id', 'tags_all'} # Skip internal attributes
221-
222-
for attr in sorted(all_attrs - skip_attrs):
223-
before_val = before.get(attr)
224-
after_val = after.get(attr)
225-
226-
if before_val != after_val:
227-
changes.append({
228-
'attribute': attr,
229-
'before': before_val,
230-
'after': after_val
231-
})
232-
233-
if changes:
234-
resource_data['changes'] = changes
235-
236-
# Add additional details if requested
237-
if show_details:
238-
resource_data['details'] = {
239-
'dependencies': resource.get('dependencies', []),
240-
'tags': resource.get('tags', {}),
241-
'raw': {
242-
'before': resource.get('before', {}),
243-
'after': resource.get('after', {})
244-
}
245-
}
246-
247-
json_output['resources'].append(resource_data)
248-
249276
# Add analysis section if available
250277
if 'analysis' in report:
251278
json_output['analysis'] = report['analysis']

tfsumpy/templates/plan_report.md

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@
77
- **Resources to Destroy**: {{ resources_to_destroy }}
88

99
## Resource Changes
10-
{% if show_changes %}
1110
{% for resource in resources %}
12-
### {{ resource.type }}.{{ resource.name }}
11+
### {{ resource.resource_type }}.{{ resource.identifier }}
1312
{% if resource.changes %}
1413
#### Changes:
1514
{% for change in resource.changes %}
@@ -23,9 +22,6 @@
2322
- **Dependencies**: {{ resource.dependencies|join(', ') }}
2423
{% endif %}
2524
{% endfor %}
26-
{% else %}
27-
*Detailed changes are hidden. Use --hide-changes=false to show them.*
28-
{% endif %}
2925

3026
## Analysis
3127
{% if analysis %}

0 commit comments

Comments
 (0)