diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index c79eaa9..aff23c6 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,10 +1,9 @@ repos: - repo: https://github.com/gruntwork-io/pre-commit - rev: v0.1.10 + rev: v0.1.23 hooks: - id: terraform-fmt - id: gofmt - id: tflint args: - - "--module" - - "--config=.tflint.hcl" \ No newline at end of file + - --minimum-failure-severity=error \ No newline at end of file diff --git a/Makefile b/Makefile index 0414fc9..987df03 100644 --- a/Makefile +++ b/Makefile @@ -4,6 +4,7 @@ ready: gen fmt check # generates the root module main.tf, variables.tf, outputs.tf and README.md gen: + terraform init -upgrade go run tools/generaterootmod.go fmt: @@ -12,7 +13,7 @@ fmt: check: terraform fmt -check -recursive - tflint --config .tflint.hcl --recursive --minimum-failure-severity=error + tflint --recursive --minimum-failure-severity=error clean: rm -rf .terraform* diff --git a/README.md b/README.md index e15530f..a487bc9 100644 --- a/README.md +++ b/README.md @@ -77,11 +77,12 @@ To deploy create Cloud Observability dashboards for production using this repo: ## Development -This repository uses `pre-commit` to format and lint HCL code. +This repository uses `pre-commit` and `tflint` to format and lint HCL code. To install: ``` + $ brew install tflint $ brew install pre-commit $ pre-commit install ``` diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..31743ab --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module terraform-lightstep-aws-dashboards + +go 1.21.5 diff --git a/modules/apigateway-dashboard/variables.tf b/modules/apigateway-dashboard/variables.tf index b80463d..9a74482 100644 --- a/modules/apigateway-dashboard/variables.tf +++ b/modules/apigateway-dashboard/variables.tf @@ -1,4 +1,4 @@ variable "lightstep_project" { description = "Name of Cloud Observability project" type = string -} \ No newline at end of file +} diff --git a/modules/aurora-dashboard/variables.tf b/modules/aurora-dashboard/variables.tf index b80463d..9a74482 100644 --- a/modules/aurora-dashboard/variables.tf +++ b/modules/aurora-dashboard/variables.tf @@ -1,4 +1,4 @@ variable "lightstep_project" { description = "Name of Cloud Observability project" type = string -} \ No newline at end of file +} diff --git a/modules/backup-dashboard/variables.tf b/modules/backup-dashboard/variables.tf index b80463d..9a74482 100644 --- a/modules/backup-dashboard/variables.tf +++ b/modules/backup-dashboard/variables.tf @@ -1,4 +1,4 @@ variable "lightstep_project" { description = "Name of Cloud Observability project" type = string -} \ No newline at end of file +} diff --git a/modules/chatbot-dashboard/variables.tf b/modules/chatbot-dashboard/variables.tf index b80463d..9a74482 100644 --- a/modules/chatbot-dashboard/variables.tf +++ b/modules/chatbot-dashboard/variables.tf @@ -1,4 +1,4 @@ variable "lightstep_project" { description = "Name of Cloud Observability project" type = string -} \ No newline at end of file +} diff --git a/modules/dynamodb-dashboard/variables.tf b/modules/dynamodb-dashboard/variables.tf index b80463d..9a74482 100644 --- a/modules/dynamodb-dashboard/variables.tf +++ b/modules/dynamodb-dashboard/variables.tf @@ -1,4 +1,4 @@ variable "lightstep_project" { description = "Name of Cloud Observability project" type = string -} \ No newline at end of file +} diff --git a/modules/ecs-dashboard/variables.tf b/modules/ecs-dashboard/variables.tf index b80463d..9a74482 100644 --- a/modules/ecs-dashboard/variables.tf +++ b/modules/ecs-dashboard/variables.tf @@ -1,4 +1,4 @@ variable "lightstep_project" { description = "Name of Cloud Observability project" type = string -} \ No newline at end of file +} diff --git a/modules/elasticache-redis-dashboard/variables.tf b/modules/elasticache-redis-dashboard/variables.tf index b80463d..9a74482 100644 --- a/modules/elasticache-redis-dashboard/variables.tf +++ b/modules/elasticache-redis-dashboard/variables.tf @@ -1,4 +1,4 @@ variable "lightstep_project" { description = "Name of Cloud Observability project" type = string -} \ No newline at end of file +} diff --git a/modules/kinesis-dashboard/variables.tf b/modules/kinesis-dashboard/variables.tf index b80463d..9a74482 100644 --- a/modules/kinesis-dashboard/variables.tf +++ b/modules/kinesis-dashboard/variables.tf @@ -1,4 +1,4 @@ variable "lightstep_project" { description = "Name of Cloud Observability project" type = string -} \ No newline at end of file +} diff --git a/modules/lambda-dashboard/variables.tf b/modules/lambda-dashboard/variables.tf index b80463d..9a74482 100644 --- a/modules/lambda-dashboard/variables.tf +++ b/modules/lambda-dashboard/variables.tf @@ -1,4 +1,4 @@ variable "lightstep_project" { description = "Name of Cloud Observability project" type = string -} \ No newline at end of file +} diff --git a/modules/rds-dashboard/variables.tf b/modules/rds-dashboard/variables.tf index b80463d..9a74482 100644 --- a/modules/rds-dashboard/variables.tf +++ b/modules/rds-dashboard/variables.tf @@ -1,4 +1,4 @@ variable "lightstep_project" { description = "Name of Cloud Observability project" type = string -} \ No newline at end of file +} diff --git a/modules/redshift-dashboard/variables.tf b/modules/redshift-dashboard/variables.tf index b80463d..9a74482 100644 --- a/modules/redshift-dashboard/variables.tf +++ b/modules/redshift-dashboard/variables.tf @@ -1,4 +1,4 @@ variable "lightstep_project" { description = "Name of Cloud Observability project" type = string -} \ No newline at end of file +} diff --git a/modules/route53-dashboard/variables.tf b/modules/route53-dashboard/variables.tf index b80463d..9a74482 100644 --- a/modules/route53-dashboard/variables.tf +++ b/modules/route53-dashboard/variables.tf @@ -1,4 +1,4 @@ variable "lightstep_project" { description = "Name of Cloud Observability project" type = string -} \ No newline at end of file +} diff --git a/modules/s3-dashboard/variables.tf b/modules/s3-dashboard/variables.tf index b80463d..9a74482 100644 --- a/modules/s3-dashboard/variables.tf +++ b/modules/s3-dashboard/variables.tf @@ -1,4 +1,4 @@ variable "lightstep_project" { description = "Name of Cloud Observability project" type = string -} \ No newline at end of file +} diff --git a/modules/sns-dashboard/variables.tf b/modules/sns-dashboard/variables.tf index b80463d..9a74482 100644 --- a/modules/sns-dashboard/variables.tf +++ b/modules/sns-dashboard/variables.tf @@ -1,4 +1,4 @@ variable "lightstep_project" { description = "Name of Cloud Observability project" type = string -} \ No newline at end of file +} diff --git a/tools/generaterootmod.go b/tools/generaterootmod.go index 0e4c904..e913d5c 100644 --- a/tools/generaterootmod.go +++ b/tools/generaterootmod.go @@ -13,11 +13,16 @@ import ( ) func writeMainHead(w io.Writer) { - fmt.Fprint(w, `terraform { + const subModulesDirectoryPath = "modules" + requiredProviderVersion, err := getRequiredProvidersVersion(subModulesDirectoryPath) + if err != nil { + log.Fatal(err) + } + fmt.Fprintf(w, `terraform { required_providers { lightstep = { source = "lightstep/lightstep" - version = "~> 1.86.1" + %s } } required_version = ">= v1.0.11" @@ -28,7 +33,46 @@ provider "lightstep" { organization = var.lightstep_organization environment = var.lightstep_env } -`) +`, requiredProviderVersion) +} + +func getRequiredProvidersVersion(dirPath string) (string, error) { + files, err := os.ReadDir(dirPath) + if err != nil { + return "", err + } + + for _, file := range files { + if file.IsDir() { + dirPath = filepath.Join(dirPath, file.Name()) + break + } + } + + file, err := os.Open(filepath.Join(dirPath, "main.tf")) + if err != nil { + return "", err + } + defer file.Close() + + var buf bytes.Buffer + scanner := bufio.NewScanner(file) + for scanner.Scan() { + line := scanner.Text() + match, err := regexp.MatchString(`^\s*version\s*= `, line) + if err != nil { + return "", err + } + if match { + buf.WriteString(line) + } + } + + if err := scanner.Err(); err != nil { + log.Fatal(err) + } + + return buf.String(), nil } func writeMainModuleBlocks(w io.Writer, dirPath string) error {