11import logging
2- from typing import Dict , Any
2+ from typing import Dict , Any , List
33from ..reporters .base_reporter import BaseReporter
44from ..reporter import ReporterInterface
55import 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' ]
0 commit comments