Skip to content

Commit

Permalink
Merge pull request #103 from lightstep/LS-58504-cleanup-generaterootmod
Browse files Browse the repository at this point in the history
LS-58504-cleanup-generaterootmod and CI checks
  • Loading branch information
adil-sultan authored Jan 29, 2024
2 parents dae5210 + adb5407 commit 171b67d
Show file tree
Hide file tree
Showing 19 changed files with 70 additions and 22 deletions.
5 changes: 2 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -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"
- --minimum-failure-severity=error
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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*
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
Expand Down
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module terraform-lightstep-aws-dashboards

go 1.21.5
2 changes: 1 addition & 1 deletion modules/apigateway-dashboard/variables.tf
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
variable "lightstep_project" {
description = "Name of Cloud Observability project"
type = string
}
}
2 changes: 1 addition & 1 deletion modules/aurora-dashboard/variables.tf
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
variable "lightstep_project" {
description = "Name of Cloud Observability project"
type = string
}
}
2 changes: 1 addition & 1 deletion modules/backup-dashboard/variables.tf
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
variable "lightstep_project" {
description = "Name of Cloud Observability project"
type = string
}
}
2 changes: 1 addition & 1 deletion modules/chatbot-dashboard/variables.tf
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
variable "lightstep_project" {
description = "Name of Cloud Observability project"
type = string
}
}
2 changes: 1 addition & 1 deletion modules/dynamodb-dashboard/variables.tf
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
variable "lightstep_project" {
description = "Name of Cloud Observability project"
type = string
}
}
2 changes: 1 addition & 1 deletion modules/ecs-dashboard/variables.tf
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
variable "lightstep_project" {
description = "Name of Cloud Observability project"
type = string
}
}
2 changes: 1 addition & 1 deletion modules/elasticache-redis-dashboard/variables.tf
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
variable "lightstep_project" {
description = "Name of Cloud Observability project"
type = string
}
}
2 changes: 1 addition & 1 deletion modules/kinesis-dashboard/variables.tf
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
variable "lightstep_project" {
description = "Name of Cloud Observability project"
type = string
}
}
2 changes: 1 addition & 1 deletion modules/lambda-dashboard/variables.tf
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
variable "lightstep_project" {
description = "Name of Cloud Observability project"
type = string
}
}
2 changes: 1 addition & 1 deletion modules/rds-dashboard/variables.tf
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
variable "lightstep_project" {
description = "Name of Cloud Observability project"
type = string
}
}
2 changes: 1 addition & 1 deletion modules/redshift-dashboard/variables.tf
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
variable "lightstep_project" {
description = "Name of Cloud Observability project"
type = string
}
}
2 changes: 1 addition & 1 deletion modules/route53-dashboard/variables.tf
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
variable "lightstep_project" {
description = "Name of Cloud Observability project"
type = string
}
}
2 changes: 1 addition & 1 deletion modules/s3-dashboard/variables.tf
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
variable "lightstep_project" {
description = "Name of Cloud Observability project"
type = string
}
}
2 changes: 1 addition & 1 deletion modules/sns-dashboard/variables.tf
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
variable "lightstep_project" {
description = "Name of Cloud Observability project"
type = string
}
}
50 changes: 47 additions & 3 deletions tools/generaterootmod.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 {
Expand Down

0 comments on commit 171b67d

Please sign in to comment.