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

Parse tf plan #4

Merged
merged 25 commits into from
Jan 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
2a7a368
feat: parse terraform plan
bcochofel Jan 27, 2021
17766d5
Apply automatic changes
bcochofel Jan 27, 2021
4cbc344
feat: module structure
bcochofel Jan 27, 2021
6204f1f
Merge branch 'parse_tf_plan' of github.com:bcochofel/terraplanfeed in…
bcochofel Jan 27, 2021
4831bd3
Apply automatic changes
bcochofel Jan 27, 2021
ee16fcc
fix: validate json file
bcochofel Jan 27, 2021
81eecb2
Merge branch 'parse_tf_plan' of github.com:bcochofel/terraplanfeed in…
bcochofel Jan 27, 2021
218579d
Apply automatic changes
bcochofel Jan 27, 2021
a6f0fd8
chore: vscode settings for project
bcochofel Jan 27, 2021
3b86f72
Merge branch 'parse_tf_plan' of github.com:bcochofel/terraplanfeed in…
bcochofel Jan 27, 2021
18d64f3
Apply automatic changes
bcochofel Jan 27, 2021
4cdab67
docs: terraform link for json docs
bcochofel Jan 27, 2021
ac567e7
Merge branch 'parse_tf_plan' of github.com:bcochofel/terraplanfeed in…
bcochofel Jan 27, 2021
27257c1
Apply automatic changes
bcochofel Jan 27, 2021
f83af9f
feat: terraform functions for filtering data
bcochofel Jan 28, 2021
85913a2
Apply automatic changes
bcochofel Jan 28, 2021
ec894cf
feat: create resource attributes
bcochofel Jan 28, 2021
549bd5c
Merge branch 'parse_tf_plan' of github.com:bcochofel/terraplanfeed in…
bcochofel Jan 28, 2021
2a719ee
Apply automatic changes
bcochofel Jan 28, 2021
bf6427a
docs: docstrings update
bcochofel Jan 29, 2021
5b468d4
Merge branch 'parse_tf_plan' of github.com:bcochofel/terraplanfeed in…
bcochofel Jan 29, 2021
c4d06ea
Apply automatic changes
bcochofel Jan 29, 2021
2c38035
feat(terraform): calculate resource name
bcochofel Jan 30, 2021
d2c6c9e
Merge branch 'parse_tf_plan' of github.com:bcochofel/terraplanfeed in…
bcochofel Jan 30, 2021
0e094d0
Apply automatic changes
bcochofel Jan 30, 2021
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
8 changes: 4 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -247,10 +247,10 @@ dmypy.json

### vscode ###
.vscode/*
#!.vscode/settings.json
#!.vscode/tasks.json
#!.vscode/launch.json
#!.vscode/extensions.json
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
*.code-workspace

# End of https://www.toptal.com/developers/gitignore/api/python,intellij,vscode
24 changes: 24 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python: Module",
"type": "python",
"request": "launch",
"module": "terraplanfeed",
"args": [
"'${input:filename}'"
]
}
],
"inputs": [
{
"id": "filename",
"type": "promptString",
"description": "Terraform plan in JSON format"
}
]
}
5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"python.pythonPath": ".venv/bin/python3",
"editor.formatOnSave": true,
"python.formatting.provider": "black"
}
33 changes: 32 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,32 @@
## [Unreleased]


<a name="0.2.0"></a>
## [0.2.0] - 2021-01-30
### Chore
- vscode settings for project

### Docs
- docstrings update
- terraform link for json docs

### Feat
- create resource attributes
- terraform functions for filtering data
- module structure
- parse terraform plan
- **terraform:** calculate resource name

### Fix
- validate json file


<a name="0.1.3"></a>
## [0.1.3] - 2021-01-25
### Fix
- gitignore


<a name="0.1.2"></a>
## [0.1.2] - 2021-01-25
### Chore
Expand All @@ -12,6 +38,9 @@
- requirements.txt
- setup.py classifiers

### Pull Requests
- Merge pull request [#3](https://github.com/bcochofel/terraplanfeed/issues/3) from bcochofel/env_configuration


<a name="0.1.1"></a>
## [0.1.1] - 2021-01-24
Expand Down Expand Up @@ -43,6 +72,8 @@
- Merge pull request [#1](https://github.com/bcochofel/terraplanfeed/issues/1) from bcochofel/package_skel


[Unreleased]: https://github.com/bcochofel/terraplanfeed/compare/0.1.2...HEAD
[Unreleased]: https://github.com/bcochofel/terraplanfeed/compare/0.2.0...HEAD
[0.2.0]: https://github.com/bcochofel/terraplanfeed/compare/0.1.3...0.2.0
[0.1.3]: https://github.com/bcochofel/terraplanfeed/compare/0.1.2...0.1.3
[0.1.2]: https://github.com/bcochofel/terraplanfeed/compare/0.1.1...0.1.2
[0.1.1]: https://github.com/bcochofel/terraplanfeed/compare/0.1.0...0.1.1
5 changes: 5 additions & 0 deletions GitVersion.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
next-version: 0.1.0
major-version-bump-message: '^?(BREAKING CHANGE|\+semver:\s?(breaking|major))'
minor-version-bump-message: '^?(feat.*:\s|\+semver:\s?(feature|minor))'
patch-version-bump-message: '^?(fix.*:\s|\+semver:\s?(fix|patch))'
no-bump-message: '\+semver:\s?(none|skip)'
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ This tool aims to parse Terraform plan files (in JSON format) and gives feedback

Outputs can be:
* stdout
* stdout in JSON
* Github pull request comment
* Azure DevOps pull request comment

Expand All @@ -19,7 +18,7 @@ To create the Terraform plan file:
```bash
terraform init
terraform plan -out=plan.out
terraform show -no-color plan.out > plan.json
terraform show -no-color -json plan.out > plan.json
```

# Run and test locally
Expand All @@ -36,6 +35,14 @@ Read the [pre-commit hooks](docs/pre-commit-hooks.md) document for more info.

Read the [git-chglog](docs/git-chlog.md) document for more info.

# References

* [Making a Python package](https://python-packaging-tutorial.readthedocs.io/en/latest/setup_py.html)
* [Documenting Python code](https://realpython.com/documenting-python-code/)
* [Python Docstrings Google](https://google.github.io/styleguide/pyguide.html)
* [Python Click](https://click.palletsprojects.com)
* [Terraform JSON output format](https://www.terraform.io/docs/internals/json-format.html)

[pre-commit]: https://github.com/pre-commit/pre-commit
[pre-commit-badge]: https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit&logoColor=white
[conventional-commits-badge]: https://img.shields.io/badge/Conventional%20Commits-1.0.0-green.svg
Expand Down
12 changes: 10 additions & 2 deletions terraplanfeed/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,10 @@
"""Main package for terraplanfeed."""
__version__ = "0.1.2"
"""
Main package for terraplanfeed.

This package receives a file in Terraform JSON output, parses to get the
changes, and creates output that can be sent to different output drivers.

Modules:
terraform
"""
__version__ = "0.2.0"
41 changes: 38 additions & 3 deletions terraplanfeed/cli.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
"""Entry point for package."""
"""
Entry point for package.

Arguments:
-h, --help: prints help
-V, --version: prints version, location and python powering it
<filename> (required): terraform plan in json format
-v, --verbose: prints debug information
-d, --debug-file: filename to print debug information
"""

import os
import sys
import click

from terraplanfeed import __version__
from terraplanfeed.log import configure_logger
from terraplanfeed.main import terraplanfeed


def version_msg():
Expand All @@ -17,6 +28,30 @@ def version_msg():

@click.command(context_settings=dict(help_option_names=["-h", "--help"]))
@click.version_option(__version__, "-V", "--version", message=version_msg())
def main():
@click.argument("filename", type=click.Path(exists=True))
@click.option(
"-v",
"--verbose",
is_flag=True,
help="Print debug information",
default=False,
)
@click.option(
"-d",
"--debug-file",
type=click.Path(),
default=None,
help="Print debug information to this file",
)
def main(filename, verbose, debug_file):
"""Parse Terraform plan in JSON format."""
click.echo("Hello World")

configure_logger(
stream_level="DEBUG" if verbose else "INFO", debug_file=debug_file
)

terraplanfeed(filename)


if __name__ == "__main__":
main()
55 changes: 55 additions & 0 deletions terraplanfeed/log.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
"""
Module for setting up logging.

Functions:
configure_logger: configures logger
"""
import logging
import sys

LOG_LEVELS = {
"DEBUG": logging.DEBUG,
"INFO": logging.INFO,
"WARNING": logging.WARNING,
"ERROR": logging.ERROR,
"CRITICAL": logging.CRITICAL,
}

LOG_FORMATS = {
"DEBUG": "%(levelname)s %(name)s: %(message)s",
"INFO": "%(levelname)s: %(message)s",
}


def configure_logger(stream_level="DEBUG", debug_file=None):
"""Configure logging.
Set up logging to stdout with given level. If ``debug_file`` is given set
up logging to file with DEBUG level.
"""
# Set up 'terraplanfeed' logger
logger = logging.getLogger("terraplanfeed")
logger.setLevel(logging.DEBUG)

# Remove all attached handlers, in case there was
# a logger with using the name 'terraplanfeed'
del logger.handlers[:]

# Create a file handler if a log file is provided
if debug_file is not None:
debug_formatter = logging.Formatter(LOG_FORMATS["DEBUG"])
file_handler = logging.FileHandler(debug_file)
file_handler.setLevel(LOG_LEVELS["DEBUG"])
file_handler.setFormatter(debug_formatter)
logger.addHandler(file_handler)

# Get settings based on the given stream_level
log_formatter = logging.Formatter(LOG_FORMATS[stream_level])
log_level = LOG_LEVELS[stream_level]

# Create a stream handler
stream_handler = logging.StreamHandler(stream=sys.stdout)
stream_handler.setLevel(log_level)
stream_handler.setFormatter(log_formatter)
logger.addHandler(stream_handler)

return logger
32 changes: 32 additions & 0 deletions terraplanfeed/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"""
Main entrypoint for terraplanfeed command.

Main code execution for terraplanfeed command.

Functions:
terraplanfeed: main code execution
"""
import logging
import sys
import os
import json

from terraplanfeed.terraform import parsePlan

logger = logging.getLogger(__name__)


def terraplanfeed(filename):
"""Execute terraplanfeed."""

"""Open JSON file"""
logger.debug("reading file %s", filename)
with open(filename) as f:
try:
tf = json.load(f)
except ValueError as e:
logger.error("%s", e)
sys.exit(1)

resources = parsePlan(tf)
print(resources)
Loading