From 83327929f2c74820ac2389affde22bc410929ee0 Mon Sep 17 00:00:00 2001 From: Nils Carstensen Date: Fri, 2 Jul 2021 22:13:24 +0200 Subject: [PATCH 1/6] Added: Now collects resources --- src/iterateTerraformFiles.py | 25 +++++++++++++++-- src/main.py | 9 ++++++ src/renderReadMe.py | 4 +++ src/templates/readme.j2 | 23 ++++++++++++++-- src/test_prerendering.py | 4 ++- tests/README.md | 53 ++++++++++++++++++++++++++++++++++++ tests/main.tf | 1 + tests/samples/README.md | 17 ++++++++++-- 8 files changed, 127 insertions(+), 9 deletions(-) diff --git a/src/iterateTerraformFiles.py b/src/iterateTerraformFiles.py index 6e57c06..02aae36 100644 --- a/src/iterateTerraformFiles.py +++ b/src/iterateTerraformFiles.py @@ -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: @@ -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: @@ -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. @@ -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) diff --git a/src/main.py b/src/main.py index 5a53ee9..e52a3e6 100755 --- a/src/main.py +++ b/src/main.py @@ -19,6 +19,8 @@ def main(): args.requirements, args.gettingstarted, struc['versions'], + struc['resources'], + args.resources, struc['variables'], args.variables, struc['outputs'], @@ -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, diff --git a/src/renderReadMe.py b/src/renderReadMe.py index 514b60d..68db0da 100644 --- a/src/renderReadMe.py +++ b/src/renderReadMe.py @@ -7,6 +7,8 @@ def render( requirements, gettingStarted, versions, + res, + resStatement, vars, varsstatement, outs, @@ -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, diff --git a/src/templates/readme.j2 b/src/templates/readme.j2 index e8d85c3..d49980a 100644 --- a/src/templates/readme.j2 +++ b/src/templates/readme.j2 @@ -1,4 +1,6 @@ # {{ name }} + +  {% if tableOfContents == true %} # Table of Contents {% if requirements == true %} @@ -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 -%} @@ -18,8 +23,8 @@   - [Contributing](#contributing) {%- endif -%} - {% if requirements == true %} +   # Requirements | Software | Version | Source | @@ -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 | @@ -44,8 +62,8 @@ {{ buildVars(items.name, items.type, items.default, items.description, items.sensitive) }} {%- endfor %} {%- endif -%} - {% if outsstatement == true %} +   # Outputs | Output | Description | @@ -57,7 +75,6 @@ {{ buildOuts(items.name, items.description) }} {%- endfor %} {%- endif -%} -   ## Contributing {%- if contribute == True %} diff --git a/src/test_prerendering.py b/src/test_prerendering.py index 5c3efdf..16bdde2 100644 --- a/src/test_prerendering.py +++ b/src/test_prerendering.py @@ -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__': diff --git a/tests/README.md b/tests/README.md index e69de29..a5b5d86 100644 --- a/tests/README.md +++ b/tests/README.md @@ -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. \ No newline at end of file diff --git a/tests/main.tf b/tests/main.tf index cfceeb5..7592a84 100644 --- a/tests/main.tf +++ b/tests/main.tf @@ -1,4 +1,5 @@ resource "random_resource1" "main" {} +resource "random_resource1" "name" {} resource "random_resource2" "main" {} resource "random_resource3" "main" {} diff --git a/tests/samples/README.md b/tests/samples/README.md index 4f134ab..a5b5d86 100644 --- a/tests/samples/README.md +++ b/tests/samples/README.md @@ -1,5 +1,7 @@ # default_name +  + # Table of Contents - [Requiremets](#requirements) @@ -9,12 +11,22 @@ - [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 | @@ -27,14 +39,15 @@ | `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 | | :----- | :---------- | -| `multiline4` | Description | | `oneline1` | | | `multiline1` | | | `multiline2` | Description | -| `multiline3` | Description |  +| `multiline3` | Description | +| `multiline4` | Description |  ## Contributing Feel free to create pull requests. \ No newline at end of file From 44c0f3e352f75015b214687a8fe071afebdd359c Mon Sep 17 00:00:00 2001 From: Nils Carstensen Date: Fri, 2 Jul 2021 22:19:22 +0200 Subject: [PATCH 2/6] Print Test files --- .github/workflows/python.yml | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/.github/workflows/python.yml b/.github/workflows/python.yml index b0c1c98..bc15d04 100644 --- a/.github/workflows/python.yml +++ b/.github/workflows/python.yml @@ -58,4 +58,9 @@ jobs: - name: Test with pytest run: | pip install pytest - cd src/ && pytest \ No newline at end of file + cd src/ && pytest + + - name: Print Sample and test file + run: | + cat tests/README.md + cat tests/samples/README.md \ No newline at end of file From 695c1d937e2cb735fe65a7cde056865010da7d91 Mon Sep 17 00:00:00 2001 From: Nils Carstensen Date: Fri, 2 Jul 2021 22:20:49 +0200 Subject: [PATCH 3/6] Print Test files --- .github/workflows/python.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/python.yml b/.github/workflows/python.yml index bc15d04..a774f7e 100644 --- a/.github/workflows/python.yml +++ b/.github/workflows/python.yml @@ -56,6 +56,7 @@ jobs: pip install -r src/requirements.txt - name: Test with pytest + continueOnErrror: true run: | pip install pytest cd src/ && pytest From b117a81f2df3d005a2ae11a924153cabd42e304d Mon Sep 17 00:00:00 2001 From: Nils Carstensen Date: Fri, 2 Jul 2021 22:22:24 +0200 Subject: [PATCH 4/6] Print Test files --- .github/workflows/python.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/python.yml b/.github/workflows/python.yml index a774f7e..09bdd13 100644 --- a/.github/workflows/python.yml +++ b/.github/workflows/python.yml @@ -56,7 +56,7 @@ jobs: pip install -r src/requirements.txt - name: Test with pytest - continueOnErrror: true + continue-on-error: true run: | pip install pytest cd src/ && pytest From 69404817760039fb698f4afb09514bc13f133bce Mon Sep 17 00:00:00 2001 From: Nils Carstensen Date: Fri, 2 Jul 2021 22:26:31 +0200 Subject: [PATCH 5/6] Updated sample file --- tests/samples/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/samples/README.md b/tests/samples/README.md index a5b5d86..82f243e 100644 --- a/tests/samples/README.md +++ b/tests/samples/README.md @@ -31,7 +31,6 @@ # Variables | Variable | Type | Default | Description | Sensitive | | :------- | :--: | :------ | :---------- | :-------- | -| `in_main_file` | string | Value | Just antoher number. | | | `empty` | | | | | | `oneline1` | | | Description | | | `multiline1` | | | Just antoher number. | | @@ -39,6 +38,7 @@ | `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 From 9a5a4c3f971c9a140b1ac9006d2ffd9449b01957 Mon Sep 17 00:00:00 2001 From: Nils Carstensen Date: Fri, 2 Jul 2021 22:28:30 +0200 Subject: [PATCH 6/6] Revert change --- .github/workflows/python.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/python.yml b/.github/workflows/python.yml index 09bdd13..bc15d04 100644 --- a/.github/workflows/python.yml +++ b/.github/workflows/python.yml @@ -56,7 +56,6 @@ jobs: pip install -r src/requirements.txt - name: Test with pytest - continue-on-error: true run: | pip install pytest cd src/ && pytest