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

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

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.0.tar.gz (8.0 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.0-py3-none-any.whl (9.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: terracheck-0.1.0.tar.gz
  • Upload date:
  • Size: 8.0 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.0.tar.gz
Algorithm Hash digest
SHA256 946017ec20ce30a174af2ba8acb35001ec8b78a2eb07c10a812dfcaae0440029
MD5 10128dd421ee64cebd4d653b133b69af
BLAKE2b-256 84c19309d53de4e9cf8da9f7c9719432fba73feb40419faa233df7d1e41a15a8

See more details on using hashes here.

File details

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

File metadata

  • Download URL: terracheck-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 9.6 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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 889e02c50b0988c84ffa220052de6cc2f38d7db599a245a578e4e7381dbe6f40
MD5 339eea1a69a3dba6484606980757e745
BLAKE2b-256 d22ff5fa47749c1cde43ed03731d2e9fd85ec332482118ed5411c50e9b1308e1

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