Skip to content

Commit

Permalink
Merge pull request #16 from holgerson97/dev
Browse files Browse the repository at this point in the history
Added: Now collects resources
  • Loading branch information
holgerson97 authored Jul 2, 2021
2 parents 047d1b8 + 9a5a4c3 commit a01c755
Show file tree
Hide file tree
Showing 9 changed files with 134 additions and 11 deletions.
7 changes: 6 additions & 1 deletion .github/workflows/python.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,9 @@ jobs:
- name: Test with pytest
run: |
pip install pytest
cd src/ && pytest
cd src/ && pytest
- name: Print Sample and test file
run: |
cat tests/README.md
cat tests/samples/README.md
25 changes: 22 additions & 3 deletions src/iterateTerraformFiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ def createOutput(tfObject):
outObject.update({'name': tfName})
except (ValueError, IndexError):
print('Output name not found for:\n' + tfObject +
'\n Please make sure, that your configuration is valid.')
'\n Please make sure, that your configuration is valid.')

# Get description from tfObject
try:
Expand All @@ -145,7 +145,7 @@ def createOutput(tfObject):
outObject.update({'Value': tfValue})
except (ValueError, IndexError):
print('Value not specified for output object:\n' + tfObject +
'\n Please make sure, that your configuration is valid.')
'\n Please make sure, that your configuration is valid.')

# Get sensitive from tfObject
try:
Expand Down Expand Up @@ -207,6 +207,20 @@ def createVersion(tfObject):

return verObject

def createResource(tfObject):
'''
Create a dict for every
'''

# Get Resource name from object
try:
tfResource = findall('^resource\\s\"(\\w*)\"', tfObject)[0]
except (ValueError, IndexError):
print('Resource definition not valid:\n' + tfObject +
'\n Please make sure, that your configuration is valid.')

return tfResource

def getTerraformObjects(tfFile):
'''
Iterate through Terraform files and get all objects.
Expand All @@ -227,7 +241,12 @@ def getTerraformObjects(tfFile):
tfVersion = captureCurlyBraces(tfLines, i)
struc['versions'].append(createVersion(tfVersion))

struc = {'variables': [], 'outputs': [], 'versions': []}
elif match('^resource\\s+', tfLines[i]):
tfResource = createResource(tfLines[i])
struc['resources'].append(
tfResource) if tfResource not in struc['resources'] else struc['resources']

struc = {'resources': ([]), 'variables': [], 'outputs': [], 'versions': []}

for i in lookupFiles():
getTerraformObjects(i)
Expand Down
9 changes: 9 additions & 0 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ def main():
args.requirements,
args.gettingstarted,
struc['versions'],
struc['resources'],
args.resources,
struc['variables'],
args.variables,
struc['outputs'],
Expand Down Expand Up @@ -51,6 +53,13 @@ def main():
help='Choose if you want to display a table of contents. \
Defaults to true.')

parser.add_argument(
'--resources',
type=bool,
default=True,
help='Choose if you want to display resources. \
Defaults to true.')

parser.add_argument(
'--variables',
type=bool,
Expand Down
4 changes: 4 additions & 0 deletions src/renderReadMe.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ def render(
requirements,
gettingStarted,
versions,
res,
resStatement,
vars,
varsstatement,
outs,
Expand All @@ -22,6 +24,8 @@ def render(
gettingStarted=gettingStarted,
terraformVersion=versions[0],
versions=versions[0]['Modules'],
resources=res,
resourcesStatement=resStatement,
variables=vars,
varsstatement=varsstatement,
outputs=outs,
Expand Down
23 changes: 20 additions & 3 deletions src/templates/readme.j2
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# {{ name }}

 
{% if tableOfContents == true %}
# Table of Contents
{% if requirements == true %}
Expand All @@ -9,6 +11,9 @@
- [Basic usage](#basic-usage)
- [Custom usage](#custom-usage)
{%- endif -%}
{% if resourceStatement == true %}
- [Resources](#resources)
{%- endif -%}
{% if varsstatement == true %}
- [Variables](#variables)
{%- endif -%}
Expand All @@ -18,8 +23,8 @@
 
- [Contributing](#contributing)
{%- endif -%}

{% if requirements == true %}

 
# Requirements
| Software | Version | Source |
Expand All @@ -31,8 +36,21 @@
{{ buildRequirements(items.name, items.version, items.source) }}
{%- endfor %}
{%- endif -%}
{% if resourcesStatement == true %}

 
# Resources
| Resource |
| :------- |
{%- macro buildResources(name) -%}
| `{{name}}` |
{%- endmacro -%}
{% for items in resources %}
{{ buildResources(items) }}
{%- endfor %}
{%- endif -%}
{% if varsstatement == true %}

 
# Variables
| Variable | Type | Default | Description | Sensitive |
Expand All @@ -44,8 +62,8 @@
{{ buildVars(items.name, items.type, items.default, items.description, items.sensitive) }}
{%- endfor %}
{%- endif -%}

{% if outsstatement == true %}

 
# Outputs
| Output | Description |
Expand All @@ -57,7 +75,6 @@
{{ buildOuts(items.name, items.description) }}
{%- endfor %}
{%- endif -%}

 
## Contributing
{%- if contribute == True %}
Expand Down
4 changes: 3 additions & 1 deletion src/test_prerendering.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ class TestRender(unittest.TestCase):
def test_file(self):

system('./main.py')
self.assertTrue(cmp('../tests/samples/README.md', '../tests/README.md'), "The to files aren't identical.")
self.assertTrue(cmp('../tests/samples/README.md',
'../tests/README.md'),
"The to files aren't identical.")


if __name__ == '__main__':
Expand Down
53 changes: 53 additions & 0 deletions tests/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# default_name

 

# Table of Contents

- [Requiremets](#requirements)
- [Getting Started](#getting-started)
- [Basic usage](#basic-usage)
- [Custom usage](#custom-usage)
- [Variables](#variables)
- [Outputs](#outputs) 
- [Contributing](#contributing)

 
# Requirements
| Software | Version | Source |
| :-------- | :-------- | :----- |
| random | 3.1.0 | hashicorp/random |
| template | 2.2.0 | hashicorp/template |

 
# Resources
| Resource |
| :------- |
| `random_resource1` |
| `random_resource2` |
| `random_resource3` |

 
# Variables
| Variable | Type | Default | Description | Sensitive |
| :------- | :--: | :------ | :---------- | :-------- |
| `in_main_file` | string | Value | Just antoher number. | |
| `empty` | | | | |
| `oneline1` | | | Description | |
| `multiline1` | | | Just antoher number. | |
| `multiline2` | | Value | Just antoher number. | |
| `multiline3` | string | Value | Just antoher number. | |
| `multiline4` | string | Value | Just antoher number. | true |
| `object` | object({ key = string value = number }) | { key = "integer" value = 1 } | Just an object. | true |

 
# Outputs
| Output | Description |
| :----- | :---------- |
| `oneline1` | |
| `multiline1` | |
| `multiline2` | Description |
| `multiline3` | Description |
| `multiline4` | Description | 
## Contributing
Feel free to create pull requests.
1 change: 1 addition & 0 deletions tests/main.tf
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
resource "random_resource1" "main" {}
resource "random_resource1" "name" {}
resource "random_resource2" "main" {}
resource "random_resource3" "main" {}

Expand Down
19 changes: 16 additions & 3 deletions tests/samples/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# default_name

 

# Table of Contents

- [Requiremets](#requirements)
Expand All @@ -9,32 +11,43 @@
- [Variables](#variables)
- [Outputs](#outputs) 
- [Contributing](#contributing)

 
# Requirements
| Software | Version | Source |
| :-------- | :-------- | :----- |
| random | 3.1.0 | hashicorp/random |
| template | 2.2.0 | hashicorp/template |

 
# Resources
| Resource |
| :------- |
| `random_resource1` |
| `random_resource2` |
| `random_resource3` |

 
# Variables
| Variable | Type | Default | Description | Sensitive |
| :------- | :--: | :------ | :---------- | :-------- |
| `in_main_file` | string | Value | Just antoher number. | |
| `empty` | | | | |
| `oneline1` | | | Description | |
| `multiline1` | | | Just antoher number. | |
| `multiline2` | | Value | Just antoher number. | |
| `multiline3` | string | Value | Just antoher number. | |
| `multiline4` | string | Value | Just antoher number. | true |
| `object` | object({ key = string value = number }) | { key = "integer" value = 1 } | Just an object. | true |
| `in_main_file` | string | Value | Just antoher number. | |

 
# Outputs
| Output | Description |
| :----- | :---------- |
| `multiline4` | Description |
| `oneline1` | |
| `multiline1` | |
| `multiline2` | Description |
| `multiline3` | Description | 
| `multiline3` | Description |
| `multiline4` | Description | 
## Contributing
Feel free to create pull requests.

0 comments on commit a01c755

Please sign in to comment.