Skip to content

Commit

Permalink
added hcl2 support
Browse files Browse the repository at this point in the history
  • Loading branch information
Gruber committed Jun 9, 2021
1 parent 353045a commit 246eb35
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 54 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
3 changes: 1 addition & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ARG TF_VERSION=0.15.2
ARG TF_VERSION=1.0.0
ARG PYTHON_VERSION=3.8.6

FROM hashicorp/terraform:$TF_VERSION AS terraform
Expand All @@ -17,6 +17,5 @@ RUN pip install -e .

WORKDIR /data
RUN echo $(timeout 15 blast-radius --serve --port 5001; test $? -eq 124) > /output.txt

ENTRYPOINT ["/bin/docker-entrypoint.sh"]
CMD ["blast-radius", "--serve"]
12 changes: 2 additions & 10 deletions blastradius/handlers/dot.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# standard libraries
import json
import re
import subprocess
Expand Down Expand Up @@ -316,7 +315,6 @@ def focus(self, node):
graph [fontname = "courier new",fontsize=8];
node [fontname = "courier new",fontsize=8];
edge [fontname = "courier new",fontsize=8];
{# just the root module #}
{% for cluster in clusters %}
subgraph "{{cluster}}" {
Expand All @@ -335,11 +333,9 @@ def focus(self, node):
{% endfor %}
}
{% endfor %}
{# non-root modules #}
{% for node in nodes %}
{% if node.module != 'root' %}
{% if node.collapsed %}
"{{node.label}}" [ shape=none, margin=0, id={{node.svg_id}} label=<<TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0">
{% for module in node.modules %}<TR><TD>(M) {{module}}</TD></TR>{% endfor %}
Expand All @@ -354,9 +350,7 @@ def focus(self, node):
</TABLE>>];
{% endif %}
{% endif %}
{% endfor %}
{% for edge in edges %}
{% if edge.edge_type == EdgeType.NORMAL %}"{{edge.source}}" -> "{{edge.target}}" {% if edge.fmt %} [{{edge.fmt}}] {% endif %}{% endif %}
{% if edge.edge_type == EdgeType.LAYOUT_SHOWN %}"{{edge.source}}" -> "{{edge.target}}" {% if edge.fmt %} [{{edge.fmt}}] {% endif %}{% endif %}
Expand Down Expand Up @@ -451,7 +445,7 @@ def _module(label):
try:
if not re.match(r'(\[root\]\s+)*module\..*', label):
return 'root'
m = re.match(r'(\[root\]\s+)*(?P<module>\S+)\.(?P<type>\S+)\.\S+', label)
m = re.match(r'(\[root\]\s+)*(?P<module>\S+)\.(?P<type>\S+)\.?\S+', label)
return m.groupdict()['module']
except:
raise Exception("None: ", label)
Expand Down Expand Up @@ -520,6 +514,4 @@ def __init__(self, source, target, fmt=None, edge_type=EdgeType.NORMAL):

def __iter__(self):
for key in {'source', 'target', 'svg_id', 'edge_type'}:
yield (key, getattr(self, key))


yield (key, getattr(self, key))
81 changes: 41 additions & 40 deletions blastradius/handlers/terraform.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
# standard libraries
from glob import iglob
import io
import os
import re

# 3rd party libraries
import hcl # hashicorp configuration language (.tf)
import hcl2 # hashicorp configuration language (.tf)

class Terraform:
"""Finds terraform/hcl files (*.tf) in CWD or a supplied directory, parses
Expand All @@ -22,49 +21,51 @@ def __init__(self, directory=None, settings=None):
for fname in iterator:
with open(fname, 'r', encoding='utf-8') as f:
self.config_str += f.read() + ' '

config_io = io.StringIO(self.config_str)
self.config = hcl.load(config_io)
self.config = hcl2.load(config_io)

# then any submodules it may contain, skipping any remote modules for
# the time being.
self.modules = {}
if 'module' in self.config:
for name, mod in self.config['module'].items():
if 'source' not in mod:
continue
source = mod['source']
# '//' used to refer to a subdirectory in a git repo
if re.match(r'.*\/\/.*', source):
continue
# '@' should only appear in ssh urls
elif re.match(r'.*\@.*', source):
continue
# 'github.com' special behavior.
elif re.match(r'github\.com.*', source):
continue
# points to new TFE module registry
elif re.match(r'app\.terraform\.io', source):
continue
# bitbucket public and private repos
elif re.match(r'bitbucket\.org.*', source):
continue
# git::https or git::ssh sources
elif re.match(r'^git::', source):
continue
# git:// sources
elif re.match(r'^git:\/\/', source):
continue
# Generic Mercurial repos
elif re.match(r'^hg::', source):
continue
# Public Terraform Module Registry
elif re.match(r'^[a-zA-Z0-9\-_]+\/[a-zA-Z0-9\-_]+\/[a-zA-Z0-9\-_]+', source):
continue
# AWS S3 buckets
elif re.match(r's3.*\.amazonaws\.com', source):
continue
# fixme path join. eek.
self.modules[name] = Terraform(directory=self.directory+'/'+source, settings=mod)
for item in self.config['module']:
for name, mod in item.items():
if 'source' not in mod:
continue
source = mod['source'][0]
# '//' used to refer to a subdirectory in a git repo
if re.match(r'.*\/\/.*', source):
continue
# '@' should only appear in ssh urls
elif re.match(r'.*\@.*', source):
continue
# 'github.com' special behavior.
elif re.match(r'github\.com.*', source):
continue
# points to new TFE module registry
elif re.match(r'app\.terraform\.io', source):
continue
# bitbucket public and private repos
elif re.match(r'bitbucket\.org.*', source):
continue
# git::https or git::ssh sources
elif re.match(r'^git::', source):
continue
# git:// sources
elif re.match(r'^git:\/\/', source):
continue
# Generic Mercurial repos
elif re.match(r'^hg::', source):
continue
# Public Terraform Module Registry
elif re.match(r'^[a-zA-Z0-9\-_]+\/[a-zA-Z0-9\-_]+\/[a-zA-Z0-9\-_]+', source):
continue
# AWS S3 buckets
elif re.match(r's3.*\.amazonaws\.com', source):
continue
# fixme path join. eek.
self.modules[name] = Terraform(directory=self.directory+'/'+source, settings=mod)


def get_def(self, node, module_depth=0):
Expand Down Expand Up @@ -95,4 +96,4 @@ def get_def(self, node, module_depth=0):
else:
return self.config['resource'][node.type][node.resource_name]
except:
return ''
return ''
1 change: 0 additions & 1 deletion docker-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ cd /data-rw

# Reinitialize for some reason
terraform init

# it's possible that we're in a sub-directory. leave.
cd /data-rw
cat /output.txt
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ Jinja2==2.10.1
Flask==1.0.3
beautifulsoup4==4.7.1
ply>=3.11
pyhcl==0.4.4
python-hcl2==2.0.3

0 comments on commit 246eb35

Please sign in to comment.