Skip to content

Commit 4da2b3e

Browse files
committed
Adjusting documentation and releasing the version v0.1.0
1 parent bf08092 commit 4da2b3e

File tree

13 files changed

+459
-337
lines changed

13 files changed

+459
-337
lines changed

.github/workflows/release.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,25 @@ jobs:
5353
TWINE_USERNAME: __token__
5454
TWINE_PASSWORD: ${{ secrets.PYPI_API_TOKEN }}
5555
run: twine upload dist/*
56+
57+
docs:
58+
needs: release
59+
runs-on: ubuntu-latest
60+
if: startsWith(github.ref, 'refs/tags/v')
61+
steps:
62+
- uses: actions/checkout@v4
63+
64+
- name: Set up Python
65+
uses: actions/setup-python@v4
66+
with:
67+
python-version: '3.10'
68+
69+
- name: Install MkDocs and Material theme
70+
run: |
71+
python -m pip install --upgrade pip
72+
pip install mkdocs mkdocs-material
73+
74+
- name: Deploy documentation to GitHub Pages
75+
env:
76+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
77+
run: mkdocs gh-deploy --force --clean --remote-branch gh-pages

README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# tfsumpy - Terraform Plan Summary Tool
22

33
[![CI](https://github.com/rafaelherik/tfsumpy/actions/workflows/ci.yaml/badge.svg)](https://github.com/rafaelherik/tfsumpy/actions/workflows/ci.yaml)
4+
[![PyPI](https://img.shields.io/pypi/v/tfsumpy.svg)](https://pypi.org/project/tfsumpy/)
45

56
tfsumpy is a Python-based tool that summarizes Terraform plan files to provide a clear overview of infrastructure changes. It helps DevOps teams review infrastructure changes more effectively by providing detailed plan summaries in different formats.
67

@@ -195,3 +196,29 @@ See all available tasks:
195196
```bash
196197
task --list
197198
```
199+
200+
## 🧩 Extending tfsumpy (Plugins)
201+
202+
tfsumpy supports plug-and-play extensions! You can add your own analyzers or reporters by dropping Python files in a `plugins/` directory (or specify a custom directory with `--plugin-dir`).
203+
204+
- Each plugin should define a `register(context)` function that registers analyzers/reporters.
205+
- tfsumpy will automatically load and register all plugins in the directory at startup.
206+
207+
**Example plugin:**
208+
```python
209+
from tfsumpy.analyzer import AnalyzerInterface, AnalyzerResult
210+
class MyCostAnalyzer(AnalyzerInterface):
211+
@property
212+
def category(self): return "cost"
213+
def analyze(self, context, **kwargs):
214+
return AnalyzerResult(category="cost", data={"total_cost": 42})
215+
def register(context):
216+
context.register_analyzer(MyCostAnalyzer())
217+
```
218+
219+
**Usage:**
220+
```bash
221+
tfsumpy plan.json --plugin-dir my_plugins/
222+
```
223+
224+
See [Extending tfsumpy](docs/extending.md) for more details and advanced examples.

docs/api/analyzers.md

Lines changed: 60 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,50 +2,93 @@
22

33
## Overview
44

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+
---
68

79
## Base Analyzer Interface
810

11+
All analyzers must implement the `AnalyzerInterface`:
12+
913
```python
1014
from abc import ABC, abstractmethod
11-
from typing import Any
15+
from tfsumpy.analyzer import AnalyzerResult
1216

1317
class AnalyzerInterface(ABC):
1418
@property
1519
@abstractmethod
1620
def category(self) -> str:
17-
"""Return the analyzer category"""
21+
"""Return the analyzer category (e.g., 'plan')."""
1822
pass
19-
23+
2024
@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+
"""
2335
pass
2436
```
2537

26-
## Plan Analyzer
38+
---
2739

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:**
2945

3046
```python
3147
from tfsumpy.plan.analyzer import PlanAnalyzer
48+
from tfsumpy.context import Context
3249

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
3556
```
3657

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
3867

39-
To create a custom analyzer:
68+
---
69+
70+
## Extending: Custom Analyzers
71+
72+
You can create your own analyzer by subclassing `AnalyzerInterface`:
4073

4174
```python
4275
from tfsumpy.analyzer import AnalyzerInterface, AnalyzerResult
4376

44-
class CustomAnalyzer(AnalyzerInterface):
77+
class MyCustomAnalyzer(AnalyzerInterface):
4578
@property
4679
def category(self) -> str:
4780
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.

docs/api/models.md

Lines changed: 52 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,63 @@
22

33
## Overview
44

5-
tfsumpy uses several data models to represent different aspects of infrastructure analysis.
5+
**tfsumpy** uses several data models to represent the results of plan analysis and reporting. Understanding these models is essential for extending tfsumpy or integrating it into other tools.
66

7-
## Resource Change Model
7+
---
8+
9+
## ResourceChange
10+
11+
Represents a single resource change in a Terraform plan.
812

913
```python
1014
from dataclasses import dataclass
1115
from typing import Dict, List
1216

1317
@dataclass
1418
class ResourceChange:
15-
action: str
16-
resource_type: str
17-
identifier: str
18-
changes: List[str]
19-
module: str = 'root'
20-
before: Dict = None
21-
after: Dict = None
19+
action: str # 'create', 'update', or 'delete'
20+
resource_type: str # e.g., 'aws_s3_bucket'
21+
identifier: str # Resource address (e.g., 'aws_s3_bucket.my_bucket')
22+
changes: List[str] # List of changed attributes (optional/legacy)
23+
module: str = 'root' # Module path (e.g., 'root', 'network.vpc')
24+
before: Dict = None # State before the change
25+
after: Dict = None # State after the change
2226
```
2327

24-
## Analysis Result Model
28+
**Fields:**
29+
- `action`: The type of change ('create', 'update', 'delete')
30+
- `resource_type`: The Terraform resource type
31+
- `identifier`: The resource address in the plan
32+
- `changes`: List of changed attributes (may be empty)
33+
- `module`: Module path (default 'root')
34+
- `before`: Dictionary of the resource's state before the change
35+
- `after`: Dictionary of the resource's state after the change
36+
37+
---
38+
39+
## AnalyzerResult
40+
41+
Represents the result of an analyzer (e.g., PlanAnalyzer).
2542

2643
```python
44+
from dataclasses import dataclass
45+
from typing import Any
46+
2747
@dataclass
2848
class AnalyzerResult:
29-
category: str
30-
data: Any
49+
category: str # e.g., 'plan'
50+
data: Any # Typically a dict with summary and resource changes
3151
```
3252

33-
## Using Models
53+
**Fields:**
54+
- `category`: The analyzer category (e.g., 'plan')
55+
- `data`: The analysis result (usually a dict with summary and resource changes)
56+
57+
---
58+
59+
## Example: Using Models
3460

35-
### Resource Change Example
61+
### ResourceChange Example
3662

3763
```python
3864
change = ResourceChange(
@@ -45,22 +71,21 @@ change = ResourceChange(
4571
)
4672
```
4773

48-
### Analysis Result Example
74+
### AnalyzerResult Example
4975

5076
```python
5177
result = AnalyzerResult(
52-
category="terraform_plan_summary",
78+
category="plan",
5379
data={
54-
"resource_changes": [
55-
ResourceChange(
56-
action="create",
57-
resource_type="aws_s3_bucket",
58-
identifier="my_bucket",
59-
changes=["bucket_name", "versioning"],
60-
before={},
61-
after={"bucket_name": "new-bucket", "versioning": True}
62-
)
63-
]
80+
"total_changes": 1,
81+
"change_breakdown": {"create": 1, "update": 0, "delete": 0},
82+
"resources": [change]
6483
}
6584
)
66-
```
85+
```
86+
87+
---
88+
89+
## Notes
90+
- The `data` field in `AnalyzerResult` is typically a dictionary with keys like `total_changes`, `change_breakdown`, and `resources` (a list of `ResourceChange` objects or dicts).
91+
- See the [Analyzers API](analyzers.md) and [Reporters API](reporters.md) for how these models are used in practice.

docs/api/reporters.md

Lines changed: 51 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,53 +2,88 @@
22

33
## Overview
44

5-
Reporters in tfsumpy are responsible for formatting and displaying analysis results. Each reporter corresponds to a specific type of analysis output (plan).
5+
Reporters in **tfsumpy** are responsible for formatting and displaying the results of plan analysis. Each reporter implements a specific output format (e.g., CLI, Markdown, JSON). The primary built-in reporter is the `PlanReporter`, which provides human-friendly and Markdown output for Terraform plan summaries.
6+
7+
---
68

79
## Base Reporter Interface
810

11+
All reporters must implement the `ReporterInterface`:
12+
913
```python
1014
from abc import ABC, abstractmethod
11-
from typing import Any
1215

1316
class ReporterInterface(ABC):
1417
@property
1518
@abstractmethod
1619
def category(self) -> str:
17-
"""Return the reporter category"""
20+
"""Return the reporter category (e.g., 'plan')."""
1821
pass
19-
22+
2023
@abstractmethod
2124
def print_report(self, data: Any, **kwargs) -> None:
22-
"""Format and print the analysis results"""
25+
"""
26+
Format and print the analysis results.
27+
28+
Args:
29+
data: The analysis results (typically a dict)
30+
**kwargs: Additional display options (e.g., show_changes, show_details)
31+
"""
2332
pass
2433
```
2534

26-
## Plan Reporter
35+
---
2736

28-
The `PlanReporter` formats and displays Terraform plan analysis results.
37+
## PlanReporter
38+
39+
The `PlanReporter` is the default reporter for plan summaries. It supports both colorized CLI output and Markdown output for sharing or documentation.
40+
41+
**Example usage:**
2942

3043
```python
3144
from tfsumpy.plan.reporter import PlanReporter
3245

3346
reporter = PlanReporter()
34-
reporter.print_report(plan_results)
47+
reporter.print_report(plan_results, show_changes=True)
48+
reporter.print_report_markdown(plan_results, show_changes=True)
3549
```
3650

37-
## Creating Custom Reporters
51+
**Parameters:**
52+
- `data`: The analysis results (from `PlanAnalyzer`)
53+
- `show_changes`: Show detailed attribute changes (bool)
54+
- `show_details`: Show full resource details (bool)
55+
56+
**Output:**
57+
- Prints formatted summary to stdout (or to a file/stream if specified)
58+
- Markdown output is suitable for PRs, documentation, or compliance
3859

39-
To create a custom reporter:
60+
---
61+
62+
## Extending: Custom Reporters
63+
64+
You can create your own reporter by subclassing `ReporterInterface` (optionally inheriting from `BaseReporter` for convenience):
4065

4166
```python
4267
from tfsumpy.reporter import ReporterInterface
4368
from tfsumpy.reporters.base_reporter import BaseReporter
4469

45-
class CustomReporter(BaseReporter, ReporterInterface):
70+
class MyCustomReporter(BaseReporter, ReporterInterface):
4671
@property
4772
def category(self) -> str:
4873
return "custom"
49-
74+
5075
def print_report(self, data: Any, **kwargs) -> None:
51-
# Implement report formatting and display logic
52-
self._write("Custom Report\n")
53-
self._write("=============\n")
54-
# Add custom formatting logic
76+
# Custom formatting logic here
77+
self._write("My Custom Report\n")
78+
self._write("=================\n")
79+
# ...
80+
```
81+
82+
Register your custom reporter with the tfsumpy `Context` to use it in your workflow.
83+
84+
---
85+
86+
## Notes
87+
- The `PlanReporter` supports both CLI and Markdown output.
88+
- You can direct output to a file or stream by passing a custom `output` to `BaseReporter`.
89+
- See the [Models API](models.md) for details on the data structures passed to reporters.

0 commit comments

Comments
 (0)