Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add terraform output changes along resource changes #7

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ SHELL:=/bin/sh
help: ## this help
@awk 'BEGIN {FS = ":.*?## "; printf "Usage:\n make \033[36m<target> \033[0m\n\nTargets:\n"} /^[a-zA-Z0-9_-]+:.*?## / {gsub("\\\\n",sprintf("\n%22c",""), $$2);printf " \033[36m%-20s\033[0m %s\n", $$1, $$2}' $(MAKEFILE_LIST)

local-run: ## Run tftools locally
go run main.go summarize <demo.json

doctoc: ## Create table of contents with doctoc
doctoc .

Expand All @@ -25,6 +28,7 @@ gosec: ## Run gosec
update-dependencies: ## Update dependencies
go get -u ./...


##https://github.com/moovweb/gvm
##go get golang.org/x/tools/cmd/goimports
##go install golang.org/x/tools/cmd/goimports
3 changes: 2 additions & 1 deletion cmd/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import (
"fmt"
"log"
"os"
"runtime"

Expand All @@ -21,7 +22,7 @@
showUnchanged bool
compact bool
useMarkdown bool
useJson bool

Check warning on line 25 in cmd/commands.go

View workflow job for this annotation

GitHub Actions / Lint 🚀

var-naming: var useJson should be useJSON (revive)

Check warning on line 25 in cmd/commands.go

View workflow job for this annotation

GitHub Actions / Lint 🚀

var-naming: var useJson should be useJSON (revive)
metrics bool
prettyJSON bool
)
Expand All @@ -41,7 +42,7 @@
Use: "summarize",
Short: "Get a summary of terraform/terragrunt output",
Long: "Get a summary of terraform/terragrunt output plan (created|updated|destroyed...)",
Run: func(cmd *cobra.Command, args []string) {

Check warning on line 45 in cmd/commands.go

View workflow job for this annotation

GitHub Actions / Lint 🚀

unused-parameter: parameter 'cmd' seems to be unused, consider removing or renaming it as _ (revive)

Check warning on line 45 in cmd/commands.go

View workflow job for this annotation

GitHub Actions / Lint 🚀

unused-parameter: parameter 'cmd' seems to be unused, consider removing or renaming it as _ (revive)
if useMarkdown && useJson {
fmt.Println("-m (Markdown output) and -j (JSON output) are mutually exclusive")
os.Exit(1)
Expand All @@ -54,7 +55,7 @@

output, err := reader.Reader(os.Stdin)
if err != nil {
panic(err)
log.Fatal(err)
}

parser.Parser(output, showTags, showUnchanged, compact, useMarkdown, useJson, metrics, prettyJSON)
Expand All @@ -66,7 +67,7 @@
Use: "usage",
Short: "print usage",
Long: "print usage in a pretty markdown render using terminal. This require internet connection since it fetch usage.md from github url",
Run: func(cmd *cobra.Command, args []string) {

Check warning on line 70 in cmd/commands.go

View workflow job for this annotation

GitHub Actions / Lint 🚀

unused-parameter: parameter 'cmd' seems to be unused, consider removing or renaming it as _ (revive)

Check warning on line 70 in cmd/commands.go

View workflow job for this annotation

GitHub Actions / Lint 🚀

unused-parameter: parameter 'cmd' seems to be unused, consider removing or renaming it as _ (revive)
if len(version) == 0 {
version = "main"
}
Expand All @@ -80,7 +81,7 @@
Use: "version",
Short: "tftools current version",
Long: "Get the cli tftools version installed",
Run: func(cmd *cobra.Command, args []string) {

Check warning on line 84 in cmd/commands.go

View workflow job for this annotation

GitHub Actions / Lint 🚀

unused-parameter: parameter 'cmd' seems to be unused, consider removing or renaming it as _ (revive)

Check warning on line 84 in cmd/commands.go

View workflow job for this annotation

GitHub Actions / Lint 🚀

unused-parameter: parameter 'cmd' seems to be unused, consider removing or renaming it as _ (revive)
fmt.Printf("tftools: %s with go version %s %s/%s", version, goversion, goos, goarch)
},
}
21 changes: 20 additions & 1 deletion demo.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
{
"format_version": "1.1",
"terraform_version": "1.2.9",
"terraform_version": "1.8.1",
"planned_values": {
"outputs": {
"test": {
"sensitive": false,
"type": "string",
"value": "test2"
}
},
"root_module": {
"resources": [
{
Expand Down Expand Up @@ -567,6 +574,18 @@
}
}
],
"output_changes": {
"test": {
"actions": [
"update"
],
"before": "test1",
"after": "test2",
"after_unknown": false,
"before_sensitive": false,
"after_sensitive": false
}
},
"prior_state": {
"format_version": "1.0",
"terraform_version": "1.2.9",
Expand Down
2 changes: 2 additions & 0 deletions internal/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@
resourcesList = make(map[string][]string)
)

func Parser(output []byte, showTags, showUnchanged, compact, useMarkdown bool, useJson bool, metrics bool, prettyJSON bool) {

Check warning on line 26 in internal/parser/parser.go

View workflow job for this annotation

GitHub Actions / Lint 🚀

var-naming: func parameter useJson should be useJSON (revive)

Check warning on line 26 in internal/parser/parser.go

View workflow job for this annotation

GitHub Actions / Lint 🚀

var-naming: func parameter useJson should be useJSON (revive)
var data tfjson.Plan
if err := json.Unmarshal(output, &data); err != nil {
fmt.Printf("Error unmarshalling plan: %v\n", err)
return
}

// TODO: add also output changes using data.OutputChanges

Check failure on line 34 in internal/parser/parser.go

View workflow job for this annotation

GitHub Actions / Lint 🚀

File is not `gofmt`-ed with `-s` (gofmt)

Check failure on line 34 in internal/parser/parser.go

View workflow job for this annotation

GitHub Actions / Lint 🚀

File is not `gofmt`-ed with `-s` (gofmt)
for _, resourceChange := range data.ResourceChanges {
processResourceChange(resourceChange, showTags)
}
Expand Down Expand Up @@ -66,7 +68,7 @@
addActionToResourceList(resourceChange.Change.Actions, resourceChange.Address)
}
return
} else {

Check warning on line 71 in internal/parser/parser.go

View workflow job for this annotation

GitHub Actions / Lint 🚀

indent-error-flow: if block ends with a return statement, so drop this else and outdent its block (revive)

Check warning on line 71 in internal/parser/parser.go

View workflow job for this annotation

GitHub Actions / Lint 🚀

indent-error-flow: if block ends with a return statement, so drop this else and outdent its block (revive)
// Not an update, add to other categories as necessary
addActionToResourceList(resourceChange.Change.Actions, resourceChange.Address)
}
Expand Down Expand Up @@ -270,7 +272,7 @@
}
}

func PrintPlanSummary(showTags, showUnchanged, compact, useMarkdown bool, useJson bool, metrics bool, prettyJSON bool) {

Check warning on line 275 in internal/parser/parser.go

View workflow job for this annotation

GitHub Actions / Lint 🚀

var-naming: func parameter useJson should be useJSON (revive)

Check warning on line 275 in internal/parser/parser.go

View workflow job for this annotation

GitHub Actions / Lint 🚀

var-naming: func parameter useJson should be useJSON (revive)
if useJson || prettyJSON {
PrintResourcesJson(showTags, showUnchanged, metrics, prettyJSON)
} else {
Expand All @@ -286,7 +288,7 @@
}
}

func PrintResourcesJson(showTags bool, showUnchanged bool, metrics bool, prettyJSON bool) {

Check warning on line 291 in internal/parser/parser.go

View workflow job for this annotation

GitHub Actions / Lint 🚀

var-naming: func PrintResourcesJson should be PrintResourcesJSON (revive)

Check warning on line 291 in internal/parser/parser.go

View workflow job for this annotation

GitHub Actions / Lint 🚀

var-naming: func PrintResourcesJson should be PrintResourcesJSON (revive)
if metrics {
var metricsData = make(map[string]int)

Expand Down
2 changes: 1 addition & 1 deletion internal/reader/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@ func Reader(stdin *os.File) ([]byte, error) {
}
return data, nil
}
return nil, fmt.Errorf("no data to be processed")
return nil, fmt.Errorf("no data to be processed. Please proide a valid tf plan json file")
}
Loading