Skip to content

Commit 5c5f558

Browse files
committed
First import
0 parents  commit 5c5f558

File tree

14 files changed

+1195
-0
lines changed

14 files changed

+1195
-0
lines changed

.pre-commit-config.yaml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
---
2+
repos:
3+
- repo: https://github.com/ambv/black
4+
rev: 18.9b0
5+
hooks:
6+
- id: black
7+
args:
8+
- --line-length=79
9+
language_version: python3
10+
- repo: https://github.com/pre-commit/pre-commit-hooks
11+
rev: v2.0.0
12+
hooks:
13+
- id: check-docstring-first
14+
- id: check-merge-conflict
15+
- id: check-yaml
16+
- id: end-of-file-fixer
17+
- id: fix-encoding-pragma
18+
- id: flake8
19+
- id: mixed-line-ending
20+
- id: trailing-whitespace
21+
- repo: https://github.com/pre-commit/pre-commit
22+
rev: v1.12.0
23+
hooks:
24+
- id: validate_manifest

.pre-commit-hooks.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
- id: terraform-fmt
3+
name: Rewrite Terraform configuration files to a canonical format and style
4+
entry: terraform-fmt
5+
language: python
6+
files: (\.tf|\.tfvars)$
7+
exclude: \.terraform\/.*$

LICENSE

Lines changed: 674 additions & 0 deletions
Large diffs are not rendered by default.

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# pre-commit-terraform-fmt
2+
3+
A [pre-commit](https://pre-commit.com/) hook to rewrite Terraform configuration files to a canonical format.
4+
5+
## Usage
6+
7+
`.pre-commit-config.yaml`:
8+
9+
```yaml
10+
- repo: https://github.com/melmorabity/pre-commit-terraform-fmt
11+
rev: 0.0.1
12+
hooks:
13+
- id: terraform-fmt
14+
# Optional argument: path to the Terraform executable
15+
# args: [--terraform=/usr/local/bin/terraform]
16+
```

setup.cfg

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[aliases]
2+
test = pytest
3+
4+
[tool:pytest]
5+
addopts = --pylint
6+
max-line-length = 79

setup.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
"""setup.py - Generic setup script."""
5+
6+
7+
import setuptools
8+
9+
10+
setuptools.setup(
11+
name="pre-commit-terraform-fmt",
12+
description="Rewrite Terraform configuration files to a canonical format"
13+
"and style",
14+
url="https://github.com/melmorabity/pre-commit-terraform-fmt",
15+
version="0.0.1",
16+
author="Mohamed El Morabity",
17+
author_email="melmorabity@fedoraproject.org",
18+
classifiers=[
19+
"License :: OSI Approved :: GNU General Public License v3 or later "
20+
"(GPLv3+)",
21+
"Programming Language :: Python :: 2",
22+
"Programming Language :: Python :: 2.7",
23+
"Programming Language :: Python :: 3",
24+
"Programming Language :: Python :: 3.6",
25+
"Programming Language :: Python :: 3.7",
26+
],
27+
packages=setuptools.find_packages(exclude="tests"),
28+
setup_requires=["pytest-runner"],
29+
install_requires=['future;python_version<"3.0"'],
30+
tests_require=["pytest", "pytest-datafiles", "pytest-pylint"],
31+
entry_points={"console_scripts": ["terraform-fmt = terraform.fmt:main"]},
32+
)

terraform/__init__.py

Whitespace-only changes.

terraform/fmt.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# -*- coding: utf-8 -*-
2+
3+
"""Rewrite Terraform configuration files to a canonical format and style."""
4+
5+
6+
from __future__ import print_function
7+
import argparse
8+
import subprocess
9+
import sys
10+
11+
12+
def run(filenames, terraform=None):
13+
"""Run 'terraform fmt' command on a set of files."""
14+
15+
if not terraform:
16+
terraform = "terraform"
17+
18+
invalid = False
19+
command = [
20+
terraform,
21+
"fmt",
22+
"-check=false",
23+
"-list=true",
24+
"-diff=false",
25+
"-write=true",
26+
]
27+
28+
# terraform 'fmt' command only takes one file at a time
29+
for path in filenames:
30+
try:
31+
stdout = subprocess.check_output(command + [path])
32+
if stdout:
33+
invalid = True
34+
print("reformatted {}".format(path), file=sys.stderr)
35+
except subprocess.CalledProcessError:
36+
invalid = True
37+
except IOError as ex:
38+
print(ex, file=sys.stderr)
39+
invalid = True
40+
break
41+
42+
return int(invalid)
43+
44+
45+
def main(argv=None):
46+
"""Main execution path."""
47+
48+
parser = argparse.ArgumentParser()
49+
parser.add_argument(
50+
"filenames",
51+
nargs="*",
52+
help="Filenames pre-commit believes are changed.",
53+
)
54+
parser.add_argument(
55+
"--terraform", help="Path to the Terraform executable."
56+
)
57+
58+
args = parser.parse_args(argv)
59+
return run(args.filenames, terraform=args.terraform)
60+
61+
62+
if __name__ == "__main__":
63+
exit(main())

tests/data/ko/ko_terraform.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This is not a Terraform file
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
# Specify the provider and access details
2+
provider "aws" {
3+
region = "${var.aws_region}"
4+
}
5+
6+
# Create a VPC to launch our instances into
7+
resource "aws_vpc" "default" {
8+
cidr_block = "10.0.0.0/16"
9+
}
10+
11+
# Create an internet gateway to give our subnet access to the outside world
12+
resource "aws_internet_gateway" "default" {
13+
vpc_id = "${aws_vpc.default.id}"
14+
}
15+
16+
# Grant the VPC internet access on its main route table
17+
resource "aws_route" "internet_access" {
18+
route_table_id = "${aws_vpc.default.main_route_table_id}"
19+
destination_cidr_block = "0.0.0.0/0"
20+
gateway_id = "${aws_internet_gateway.default.id}"
21+
}
22+
23+
# Create a subnet to launch our instances into
24+
resource "aws_subnet" "default" {
25+
vpc_id = "${aws_vpc.default.id}"
26+
cidr_block = "10.0.1.0/24"
27+
map_public_ip_on_launch = true
28+
}
29+
30+
# A security group for the ELB so it is accessible via the web
31+
resource "aws_security_group" "elb" {
32+
name = "terraform_example_elb"
33+
description = "Used in the terraform"
34+
vpc_id = "${aws_vpc.default.id}"
35+
36+
# HTTP access from anywhere
37+
ingress {
38+
from_port = 80
39+
to_port = 80
40+
protocol = "tcp"
41+
cidr_blocks = ["0.0.0.0/0"]
42+
}
43+
44+
# outbound internet access
45+
egress {
46+
from_port = 0
47+
to_port = 0
48+
protocol = "-1"
49+
cidr_blocks = ["0.0.0.0/0"]
50+
}
51+
}
52+
53+
# Our default security group to access
54+
# the instances over SSH and HTTP
55+
resource "aws_security_group" "default" {
56+
name = "terraform_example"
57+
description = "Used in the terraform"
58+
vpc_id = "${aws_vpc.default.id}"
59+
60+
# SSH access from anywhere
61+
ingress {
62+
from_port = 22
63+
to_port = 22
64+
protocol = "tcp"
65+
cidr_blocks = ["0.0.0.0/0"]
66+
}
67+
68+
# HTTP access from the VPC
69+
ingress {
70+
from_port = 80
71+
to_port = 80
72+
protocol = "tcp"
73+
cidr_blocks = ["10.0.0.0/16"]
74+
}
75+
76+
# outbound internet access
77+
egress {
78+
from_port = 0
79+
to_port = 0
80+
protocol = "-1"
81+
cidr_blocks = ["0.0.0.0/0"]
82+
}
83+
}
84+
85+
resource "aws_elb" "web" {
86+
name = "terraform-example-elb"
87+
88+
subnets = ["${aws_subnet.default.id}"]
89+
security_groups = ["${aws_security_group.elb.id}"]
90+
instances = ["${aws_instance.web.id}"]
91+
92+
listener {
93+
instance_port = 80
94+
instance_protocol = "http"
95+
lb_port = 80
96+
lb_protocol = "http"
97+
}
98+
}
99+
100+
resource "aws_key_pair" "auth" {
101+
key_name = "${var.key_name}"
102+
public_key = "${file(var.public_key_path)}"
103+
}
104+
105+
resource "aws_instance" "web" {
106+
# The connection block tells our provisioner how to
107+
# communicate with the resource (instance)
108+
connection {
109+
# The default username for our AMI
110+
user = "ubuntu"
111+
112+
# The connection will use the local SSH agent for authentication.
113+
}
114+
115+
instance_type = "t2.micro"
116+
117+
# Lookup the correct AMI based on the region
118+
# we specified
119+
ami = "${lookup(var.aws_amis, var.aws_region)}"
120+
121+
# The name of our SSH keypair we created above.
122+
key_name = "${aws_key_pair.auth.id}"
123+
124+
# Our Security group to allow HTTP and SSH access
125+
vpc_security_group_ids = ["${aws_security_group.default.id}"]
126+
127+
# We're going to launch into the same subnet as our ELB. In a production
128+
# environment it's more common to have a separate private subnet for
129+
# backend instances.
130+
subnet_id = "${aws_subnet.default.id}"
131+
132+
# We run a remote provisioner on the instance after creating it.
133+
# In this case, we just install nginx and start it. By default,
134+
# this should be on port 80
135+
provisioner "remote-exec" {
136+
inline = [
137+
"sudo apt-get -y update",
138+
"sudo apt-get -y install nginx",
139+
"sudo service nginx start",
140+
]
141+
}
142+
}

0 commit comments

Comments
 (0)