Skip to main content

Python Terraform checker from GCP good practices

Project description

Terracheck

A Python library to check Terraform style and structure good practices defined in Google Coud Terraform sytel and practices documentation.

The code is Python 3. Compatibility with Python 2 has not been tested.

Installation

Fast install:

pip install terracheck

Install the package:

python setup.py install

Example

StyleChecker

    from terracheck.io.HclCheckerReader import HclCheckerReader
    from terracheck.checker.StyleChecker import StyleChecker

    # Read files in a HCL folder
    hcl_checker_reader = HclCheckerReader()
    hcl_checker_reader.read("/hcl/folder/path")

    # Check style good practices
    style_checker = StyleChecker()
    style_checker.check(hcl_checker_reader.extract)
    
    style_report = style_checker.report

    style_metadata = style_checker.metadata

An output example :

    [DASH_IN_NAME]: Name of variable named 'gcp-folder_parent_id' contains dash ('-'). Shoud be underscores ('_').
    [DASH_IN_NAME]: Name of data 'google_storage_bucket' 'google-storage_bucket' contains dash ('-'). Shoud be underscores ('_').
    [DASH_IN_NAME]: Name of resource 'google_storage_bucket' 'google-storage_bucket' contains dash ('-'). Shoud be underscores ('_').
    [DASH_IN_NAME]: Name of output named 'my-output' contains dash ('-'). Shoud be underscores ('_').
    [NAME_IS_RESOURCE]: Resource 'google_storage_bucket' is named 'google_storage_bucket'. Chose a name different than the resource name.
    [FILE_PURPOSE]: Required_providers 'google' should be declared in file versions.tf not in version.tf.
    [FILE_PURPOSE]: Variable 'gcp-folder_parent_id' should be declared in file variables.tf not in hcl_2.tf.
    [FILE_PURPOSE]: Output 'my-output' should be declared in file outputs.tf not in hcl_1.tf.
    [FILE_PURPOSE]: Required_providers 'google-beta' should be declared in file versions.tf not in variables.tf.
    [FILE_PURPOSE]: Required_providers 'gitlab' should be declared in file versions.tf not in variables.tf.
    [FILE_PURPOSE]: Provider 'gitlab' should be declared in file versions.tf not in variables.tf.
    [OUTPUT_EXPOSURE]: Output 'cycling_output' references a variable as value. It should not.
    [NAME_UNIQUENESS]: Resource 'google_bigquery_dataset' is unique, and thus should be named 'main', not 'dataset'.
    [NAME_UNIQUENESS]: Resource 'google_service_account' is unique, and thus should be named 'main', not 'bqowner'.

StructureChecker

    from terracheck.checker.StructureChecker import StructureChecker
    
    structure_checker = StructureChecker("/checker/config/path")

    structure_checker.check("/hcl/folder/path")

    structure_report = structure_checker.report

    structure_metadata = structure_checker.metadata

With respect to the following hcl folder structure:

demo_hcl_folder
   | google-hcl_1.tf
   | hcl_2-org.tf
   | outputs.tf
   | variables.tf
   | version.tf

and the following StructureChecker config:

mandatory_files:
  version:
    - "versions.tf"
  readme:
    - "README.MD"
    - "README.md"

filenames_nomenclature:
  startswith: "google"
  endswith: "org"
  contains: "-"

the output is :

    [MISSING_FILE]: File 'version' is missing in your terraform folder. Available denominations : ['versions.tf'].
    [MISSING_FILE]: File 'readme' is missing in your terraform folder. Available denominations : ['README.MD', 'README.md'].
    [FILENAME_CONSTRAINT]: File 'google-hcl_1.tf' does not respect constraint 'endswith org'.
    [FILENAME_CONSTRAINT]: File 'version.tf' does not respect constraint 'startswith google'.
    [FILENAME_CONSTRAINT]: File 'version.tf' does not respect constraint 'endswith org'.
    [FILENAME_CONSTRAINT]: File 'version.tf' does not respect constraint 'contains -'.
    [FILENAME_CONSTRAINT]: File 'hcl_2-org.tf' does not respect constraint 'startswith google'.
    [FILENAME_CONSTRAINT]: File 'hcl_2-org.tf' does not respect constraint 'endswith org'.

HCL Reader

As in examples bellow, HclCheckerReader reads from a folder path. But the content to check can also be extended from string HCL content :

    import json
    from terracheck.io.HclCheckerReader import HclCheckerReader
    
    hcl_checker_reader = HclCheckerReader()
    
    file_path_1 = "/file/path/1"
    file_path_2 = "/file/path/2"
    
    with open(file_path_1, "r") as f:
        hcl_checker_reader.extend(f.read(), file_path_1)
    
    with open(file_path_2, "r") as f:
        hcl_checker_reader.extend(f.read(), file_path_2)
    
    for element_type, checker_element in hcl_checker_reader.extract.items():
        print(json.dumps(checker_element.to_dict(), indent=4))

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

terracheck-0.2.0.tar.gz (8.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

terracheck-0.2.0-py3-none-any.whl (10.1 kB view details)

Uploaded Python 3

File details

Details for the file terracheck-0.2.0.tar.gz.

File metadata

  • Download URL: terracheck-0.2.0.tar.gz
  • Upload date:
  • Size: 8.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.8.3

File hashes

Hashes for terracheck-0.2.0.tar.gz
Algorithm Hash digest
SHA256 cb6289f95721476e981e4c1506e87edc0c5190283e53090479f70761d48648d4
MD5 13014dfc253fdba95bb64f9afdb7f92a
BLAKE2b-256 6450200639c8746044a82922120b1a14507549acf7157ead00406e380bba0156

See more details on using hashes here.

File details

Details for the file terracheck-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: terracheck-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 10.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.8.3

File hashes

Hashes for terracheck-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9c50d0393a404222e070edbe29bcb0e3067dbc6dcc112bcdea45f1653573fdbf
MD5 ebe1e98eb5b2c1723c379acf5ce18861
BLAKE2b-256 6db079167be2b75d85dc14004928faee259c017727d2b8f01043929a3998952b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page