|
2 | 2 |
|
3 | 3 | ## Overview |
4 | 4 |
|
5 | | -Analyzers in tfsumpy are responsible for analyzing different aspects of Terraform plans. Each analyzer focuses on a specific type of analysis (plan). |
| 5 | +Analyzers in **tfsumpy** are responsible for processing and interpreting Terraform plan files. Each analyzer implements a specific type of analysis. The primary built-in analyzer is the `PlanAnalyzer`, which summarizes resource changes in a Terraform plan. |
| 6 | + |
| 7 | +--- |
6 | 8 |
|
7 | 9 | ## Base Analyzer Interface |
8 | 10 |
|
| 11 | +All analyzers must implement the `AnalyzerInterface`: |
| 12 | + |
9 | 13 | ```python |
10 | 14 | from abc import ABC, abstractmethod |
11 | | -from typing import Any |
| 15 | +from tfsumpy.analyzer import AnalyzerResult |
12 | 16 |
|
13 | 17 | class AnalyzerInterface(ABC): |
14 | 18 | @property |
15 | 19 | @abstractmethod |
16 | 20 | def category(self) -> str: |
17 | | - """Return the analyzer category""" |
| 21 | + """Return the analyzer category (e.g., 'plan').""" |
18 | 22 | pass |
19 | | - |
| 23 | + |
20 | 24 | @abstractmethod |
21 | | - def analyze(self, context: 'Context', **kwargs) -> 'AnalyzerResult': |
22 | | - """Perform analysis and return results""" |
| 25 | + def analyze(self, context: 'Context', **kwargs) -> AnalyzerResult: |
| 26 | + """ |
| 27 | + Perform analysis and return results. |
| 28 | +
|
| 29 | + Args: |
| 30 | + context: The tfsumpy Context object |
| 31 | + **kwargs: Additional arguments (e.g., plan_path) |
| 32 | + Returns: |
| 33 | + AnalyzerResult: The result of the analysis |
| 34 | + """ |
23 | 35 | pass |
24 | 36 | ``` |
25 | 37 |
|
26 | | -## Plan Analyzer |
| 38 | +--- |
27 | 39 |
|
28 | | -The `PlanAnalyzer` processes Terraform plan files and extracts change information. |
| 40 | +## PlanAnalyzer |
| 41 | + |
| 42 | +The `PlanAnalyzer` is the default analyzer for Terraform plan files. It parses the plan JSON and produces a summary of all resource changes. |
| 43 | + |
| 44 | +**Example usage:** |
29 | 45 |
|
30 | 46 | ```python |
31 | 47 | from tfsumpy.plan.analyzer import PlanAnalyzer |
| 48 | +from tfsumpy.context import Context |
32 | 49 |
|
33 | | -analyzer = PlanAnalyzer(context) |
34 | | -result = analyzer.analyze(context, plan_path="plan.json") |
| 50 | +context = Context() |
| 51 | +plan_analyzer = PlanAnalyzer(context) |
| 52 | +result = plan_analyzer.analyze(context, plan_path="plan.json") |
| 53 | + |
| 54 | +print(result.data["total_changes"]) |
| 55 | +print(result.data["resources"]) # List of resource change dicts |
35 | 56 | ``` |
36 | 57 |
|
37 | | -## Creating Custom Analyzers |
| 58 | +**Parameters:** |
| 59 | +- `context`: The tfsumpy Context object (handles config, redaction, etc.) |
| 60 | +- `plan_path`: Path to the Terraform plan JSON file |
| 61 | + |
| 62 | +**Returns:** |
| 63 | +- `AnalyzerResult` with a summary of changes, including: |
| 64 | + - `total_changes`: int |
| 65 | + - `change_breakdown`: dict (create/update/delete counts) |
| 66 | + - `resources`: list of resource change dicts |
38 | 67 |
|
39 | | -To create a custom analyzer: |
| 68 | +--- |
| 69 | + |
| 70 | +## Extending: Custom Analyzers |
| 71 | + |
| 72 | +You can create your own analyzer by subclassing `AnalyzerInterface`: |
40 | 73 |
|
41 | 74 | ```python |
42 | 75 | from tfsumpy.analyzer import AnalyzerInterface, AnalyzerResult |
43 | 76 |
|
44 | | -class CustomAnalyzer(AnalyzerInterface): |
| 77 | +class MyCustomAnalyzer(AnalyzerInterface): |
45 | 78 | @property |
46 | 79 | def category(self) -> str: |
47 | 80 | return "custom" |
48 | | - |
49 | | - def analyze(self, context: 'Context', **kwargs) -> AnalyzerResult: |
50 | | - # Implement analysis logic |
51 | | - return AnalyzerResult(category="custom", data={}) |
| 81 | + |
| 82 | + def analyze(self, context, **kwargs) -> AnalyzerResult: |
| 83 | + # Custom analysis logic here |
| 84 | + return AnalyzerResult(category="custom", data={"result": "ok"}) |
| 85 | +``` |
| 86 | + |
| 87 | +Register your custom analyzer with the tfsumpy `Context` to use it in your workflow. |
| 88 | + |
| 89 | +--- |
| 90 | + |
| 91 | +## Notes |
| 92 | +- All analyzers should return an `AnalyzerResult`. |
| 93 | +- The `PlanAnalyzer` is the main entry point for plan file analysis. |
| 94 | +- See the [Models API](models.md) for details on the data structures returned by analyzers. |
0 commit comments