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

WIP: Feature/terragrunt #35

Draft
wants to merge 1 commit into
base: master
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# terraform-module-versions

Checks for updates of external terraform modules referenced in given Terraform (0.10.x - 0.12.x) modules. Outputs Markdown tables by default, as well as JSONL (`-o jsonl`, one JSON object per line), JSON (`-o json`), and JUnit XML (`-o junit`).
Checks for updates of external terraform modules referenced in given Terraform modules. Outputs Markdown tables by default, as well as JSONL (`-o jsonl`, one JSON object per line), JSON (`-o json`), and JUnit XML (`-o junit`).

Supported module sources:

Expand Down
2 changes: 1 addition & 1 deletion README.template.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# ${APP}

Checks for updates of external terraform modules referenced in given Terraform (0.10.x - 0.12.x) modules. Outputs Markdown tables by default, as well as JSONL (`-o jsonl`, one JSON object per line), JSON (`-o json`), and JUnit XML (`-o junit`).
Checks for updates of external terraform modules referenced in given Terraform modules. Outputs Markdown tables by default, as well as JSONL (`-o jsonl`, one JSON object per line), JSON (`-o json`), and JUnit XML (`-o junit`).

Supported module sources:

Expand Down
3 changes: 3 additions & 0 deletions examples/terragrunt.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
terraform {
source = "git@github.com:terraform-aws-modules/terraform-aws-iam.git//modules/iam-assumable-roles?ref=v2.21.0"
}
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ require (
github.com/gliderlabs/ssh v0.3.1 // indirect
github.com/go-git/go-git/v5 v5.2.0
github.com/go-test/deep v1.0.7 // indirect
github.com/gruntwork-io/terragrunt v0.28.2
github.com/hashicorp/go-getter v1.5.2
github.com/hashicorp/go-version v1.2.1 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/hashicorp/terraform v0.14.5
github.com/hashicorp/terraform-config-inspect v0.0.0-20201102131242-0c45ba392e51
github.com/hashicorp/terraform/helper/pathorcontents v0.14.6 // indirect
github.com/imdario/mergo v0.3.11 // indirect
github.com/jstemmer/go-junit-report v0.9.1
github.com/kevinburke/ssh_config v0.0.0-20201106050909-4977a11b4351 // indirect
Expand All @@ -37,3 +38,5 @@ require (
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776 // indirect
)

replace github.com/hashicorp/terraform/helper/pathorcontents v0.14.6 => ./internal/terraform-helper-pathorcontents
404 changes: 404 additions & 0 deletions go.sum

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions internal/terraform-helper-pathorcontents/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module github.com/hashicorp/terraform/helper/pathorcontents

go 1.15

require github.com/mitchellh/go-homedir v1.1.0
2 changes: 2 additions & 0 deletions internal/terraform-helper-pathorcontents/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
40 changes: 40 additions & 0 deletions internal/terraform-helper-pathorcontents/read.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// https://www.terraform.io/docs/extend/guides/v2-upgrade-guide.html#removal-of-helper-pathorcontents-package
package pathorcontents

import (
"io/ioutil"
"os"

"github.com/mitchellh/go-homedir"
)

// If the argument is a path, Read loads it and returns the contents,
// otherwise the argument is assumed to be the desired contents and is simply
// returned.
//
// The boolean second return value can be called `wasPath` - it indicates if a
// path was detected and a file loaded.
func Read(poc string) (string, bool, error) {
if len(poc) == 0 {
return poc, false, nil
}

path := poc
if path[0] == '~' {
var err error
path, err = homedir.Expand(path)
if err != nil {
return path, true, err
}
}

if _, err := os.Stat(path); err == nil {
contents, err := ioutil.ReadFile(path)
if err != nil {
return string(contents), true, err
}
return string(contents), true, nil
}

return poc, false, nil
}
9 changes: 9 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ var (
Output flagvar.Enum
OutputFormat output.Format
RegistryHeaders flagvar.Assignments
Terragrunt bool
Quiet bool
UpdatesFoundNonzeroExit bool
All bool
Expand All @@ -59,6 +60,7 @@ func main() {
checkFlagSet := flag.NewFlagSet(appName+" check", flag.ExitOnError)

rootFlagSet.BoolVar(&config.Quiet, "quiet", false, "suppress log output (stderr)")
rootFlagSet.BoolVar(&config.Terragrunt, "terragrunt", false, "include Terragrunt configurations")
rootFlagSet.BoolVar(&config.Quiet, "q", false, "(alias for -quiet)")
rootFlagSet.Var(&config.Output, "output", "output format, "+config.Output.Help())
rootFlagSet.Var(&config.Output, "o", "(alias for -output)")
Expand Down Expand Up @@ -151,6 +153,13 @@ func scanForModuleCalls() []scan.Result {
if err != nil {
log.Fatal(err)
}
if config.Terragrunt {
scanResultsTerragrunt, err := scan.ScanTerragrunt(config.Paths)
if err != nil {
log.Fatal(err)
}
scanResults = append(scanResults, scanResultsTerragrunt...)
}
moduleNamesFilter := config.ModuleNames.Value
moduleNamesFilterEmpty := len(moduleNamesFilter) == 0
scanResultsFiltered := make([]scan.Result, 0, len(scanResults))
Expand Down
3 changes: 1 addition & 2 deletions pkg/modulecall/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,14 @@ type Parsed struct {
VersionString string
Constraints *semver.Constraints
ConstraintsString string
Raw tfconfig.ModuleCall
}

func Parse(raw tfconfig.ModuleCall) (*Parsed, error) {
src, err := source.Parse(raw.Source)
if err != nil {
return nil, fmt.Errorf("parse module call source: %w", err)
}
out := Parsed{Source: src, Raw: raw}
out := Parsed{Source: src}
switch {
case src.Git != nil:
if ref := src.Git.RefValue; ref != nil {
Expand Down
52 changes: 52 additions & 0 deletions pkg/scan/terragrunt.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package scan

import (
"fmt"

tgconfig "github.com/gruntwork-io/terragrunt/config"
tgoptions "github.com/gruntwork-io/terragrunt/options"
"github.com/hashicorp/terraform-config-inspect/tfconfig"
)

func ScanTerragrunt(paths []string) ([]Result, error) {
var out []Result
for _, path := range paths {
opts, err := tgoptions.NewTerragruntOptions(path)
if err != nil {
return nil, fmt.Errorf("build terragrunt options for %q: %w", path, err)
}
paths, err := tgconfig.FindConfigFilesInPath(path, opts)
if err != nil {
return nil, fmt.Errorf("find terragrunt config files in %q: %w", path, err)
}
for _, path := range paths {
cfg, err := tgconfig.PartialParseConfigFile(
path,
opts,
nil,
[]tgconfig.PartialDecodeSectionType{tgconfig.TerraformSource},
)
if err != nil {
return nil, fmt.Errorf("read terragrunt config %q: %w", path, err)
}
if cfg.Terraform == nil {
continue
}
if cfg.Terraform.Source == nil {
continue
}
out = append(out, Result{
Path: path,
ModuleCall: tfconfig.ModuleCall{
Name: path,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NB. this is somewhat questionable - we're pretending like the Terragrunt config defines a tf module with a path like examples/terragrunt.hcl. Not sure what the correct choice would be though

Source: *cfg.Terraform.Source,
Version: "",
Pos: tfconfig.SourcePos{
Filename: path,
},
},
})
}
}
return out, nil
}
202 changes: 202 additions & 0 deletions vendor/github.com/Azure/azure-sdk-for-go/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions vendor/github.com/Azure/azure-sdk-for-go/NOTICE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading