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.io.HclCheckerReader import HclCheckerReader

    structure_checker = StructureChecker()

    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'.

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.1.1.tar.gz (8.1 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.1.1-py3-none-any.whl (9.8 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for terracheck-0.1.1.tar.gz
Algorithm Hash digest
SHA256 7c32c1b0cc613244ef81c44052a22baac18cc03ba803f2f5eff3cc62d43fd3a0
MD5 f1fdc5a5ba3c261b9bdaa69e18d74cc4
BLAKE2b-256 ccb443b2f80630379c7b47b67f22a5bde7225c2af81a3c10e3f2f254deef3447

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for terracheck-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 2a44135a73b651dc6ed45c74a4034a31a633344fadf38f1893e11c9d3bd43f3b
MD5 8402c73c2c18d7fcb8470ef4da646590
BLAKE2b-256 a6c6aafa0b9bff051e0a697fef89886124095fde8451f1a56daa32abcacbcead

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