Package: terracheck
TerracheckA Python library to check Terraform style and structure good practices defined inGoogle Coud Terraform sytel and practices documentation.The code is Python 3. Compatibility with Python 2 has not been tested.InstallationFast install:pipinstallterracheckInstall the package:pythonsetup.pyinstallExampleStyleCheckerfromterracheck.io.HclCheckerReaderimportHclCheckerReaderfromterracheck.checker.StyleCheckerimportStyleChecker# Read files in a HCL folderhcl_checker_reader=HclCheckerReader()hcl_checker_reader.read("/hcl/folder/path")# Check style good practicesstyle_checker=StyleChecker()style_checker.check(hcl_checker_reader.extract)style_report=style_checker.reportstyle_metadata=style_checker.metadataAn output example :[DASH_IN_NAME]:Nameofvariablenamed'gcp-folder_parent_id'containsdash('-').Shoudbeunderscores('_').[DASH_IN_NAME]:Nameofdata'google_storage_bucket''google-storage_bucket'containsdash('-').Shoudbeunderscores('_').[DASH_IN_NAME]:Nameofresource'google_storage_bucket''google-storage_bucket'containsdash('-').Shoudbeunderscores('_').[DASH_IN_NAME]:Nameofoutputnamed'my-output'containsdash('-').Shoudbeunderscores('_').[NAME_IS_RESOURCE]:Resource'google_storage_bucket'isnamed'google_storage_bucket'.Choseanamedifferentthantheresourcename.[FILE_PURPOSE]:Required_providers'google'shouldbedeclaredinfileversions.tfnotinversion.tf.[FILE_PURPOSE]:Variable'gcp-folder_parent_id'shouldbedeclaredinfilevariables.tfnotinhcl_2.tf.[FILE_PURPOSE]:Output'my-output'shouldbedeclaredinfileoutputs.tfnotinhcl_1.tf.[FILE_PURPOSE]:Required_providers'google-beta'shouldbedeclaredinfileversions.tfnotinvariables.tf.[FILE_PURPOSE]:Required_providers'gitlab'shouldbedeclaredinfileversions.tfnotinvariables.tf.[FILE_PURPOSE]:Provider'gitlab'shouldbedeclaredinfileversions.tfnotinvariables.tf.[OUTPUT_EXPOSURE]:Output'cycling_output'referencesavariableasvalue.Itshouldnot.[NAME_UNIQUENESS]:Resource'google_bigquery_dataset'isunique,andthusshouldbenamed'main',not'dataset'.[NAME_UNIQUENESS]:Resource'google_service_account'isunique,andthusshouldbenamed'main',not'bqowner'.StructureCheckerfromterracheck.checker.StructureCheckerimportStructureCheckerstructure_checker=StructureChecker("/checker/config/path")structure_checker.check("/hcl/folder/path")structure_report=structure_checker.reportstructure_metadata=structure_checker.metadataWith respect to the following hcl folder structure:demo_hcl_folder|google-hcl_1.tf|hcl_2-org.tf|outputs.tf|variables.tf|version.tfand the followingStructureCheckerconfig:mandatory_files:version:-"versions.tf"readme:-"README.MD"-"README.md"filenames_nomenclature:startswith:"google"endswith:"org"contains:"-"the output is :[MISSING_FILE]:File'version'ismissinginyourterraformfolder.Availabledenominations:['versions.tf'].[MISSING_FILE]:File'readme'ismissinginyourterraformfolder.Availabledenominations:['README.MD','README.md'].[FILENAME_CONSTRAINT]:File'google-hcl_1.tf'doesnotrespectconstraint'endswith org'.[FILENAME_CONSTRAINT]:File'version.tf'doesnotrespectconstraint'startswith google'.[FILENAME_CONSTRAINT]:File'version.tf'doesnotrespectconstraint'endswith org'.[FILENAME_CONSTRAINT]:File'version.tf'doesnotrespectconstraint'contains -'.[FILENAME_CONSTRAINT]:File'hcl_2-org.tf'doesnotrespectconstraint'startswith google'.[FILENAME_CONSTRAINT]:File'hcl_2-org.tf'doesnotrespectconstraint'endswith org'.HCL ReaderAs in examples bellow, HclCheckerReader reads from a folder path. But the content to check can
also be extended from string HCL content :importjsonfromterracheck.io.HclCheckerReaderimportHclCheckerReaderhcl_checker_reader=HclCheckerReader()file_path_1="/file/path/1"file_path_2="/file/path/2"withopen(file_path_1,"r")asf:hcl_checker_reader.extend(f.read(),file_path_1)withopen(file_path_2,"r")asf:hcl_checker_reader.extend(f.read(),file_path_2)forelement_type,checker_elementinhcl_checker_reader.extract.items():print(json.dumps(checker_element.to_dict(),indent=4))