Skip to content

Commit

Permalink
Have parent dependencies be relative to the child (#71)
Browse files Browse the repository at this point in the history
Co-authored-by: dmattia <david@transcend.io>
  • Loading branch information
dmattia and dmattia authored Oct 1, 2020
1 parent 93800b3 commit baa7400
Show file tree
Hide file tree
Showing 11 changed files with 46 additions and 36 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION=0.9.4
VERSION=0.9.5
PATH_BUILD=build/
FILE_COMMAND=terragrunt-atlantis-config
FILE_ARCH=darwin_amd64
Expand Down
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,11 @@ In your `atlantis.yaml` file, you will end up seeing output like:
dir: example-setup/extra_dependency
```
If you specify `extra_atlantis_dependencies` in the parent Terragrunt module, they will be merged with the child dependencies.
If you specify `extra_atlantis_dependencies` in the parent Terragrunt module, they will be merged with the child dependencies using the following rules:

1. Any function in a parent will be evaluated from the child's directory. So you can use `get_parent_terragrunt_dir()` and other functions like you normally would in terragrunt.
2. Absolute paths will work as they would in a child module, and the path in the output will be relative from the child module to the absolute path
3. Relative paths, like the string `"foo.json"`, will be evaluated as relative to the Child module. This means that if you need something relative to the parent module, you should use something like `"${get_parent_terragrunt_dir()}/foo.json"`

## Custom workflows

Expand Down Expand Up @@ -154,7 +158,7 @@ jobs:
id: atlantis_validator
uses: transcend-io/terragrunt-atlantis-config-github-action@v0.0.3
with:
version: v0.9.4
version: v0.9.5
extra_args: '--autoplan --parallel=false
```

Expand Down
26 changes: 13 additions & 13 deletions cmd/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,17 +94,10 @@ func makePathAbsolute(path string, parentPath string) string {
}

// Parses the terragrunt config at <path> to find all modules it depends on
func getDependencies(path string) ([]string, error) {
options, err := options.NewTerragruntOptions(path)
if err != nil {
return nil, err
}
options.RunTerragrunt = cli.RunTerragrunt
options.Env = getEnvs()

func getDependencies(path string, terragruntOptions *options.TerragruntOptions) ([]string, error) {
// if theres no terraform source and we're ignoring parent terragrunt configs
// return nils to indicate we should skip this project
isParent, err := isParentModule(path, options)
isParent, err := isParentModule(path, terragruntOptions)
if err != nil {
return nil, err
}
Expand All @@ -118,12 +111,12 @@ func getDependencies(path string) ([]string, error) {
config.TerraformBlock,
}

parsedConfig, err := config.PartialParseConfigFile(path, options, nil, decodeTypes)
parsedConfig, err := config.PartialParseConfigFile(path, terragruntOptions, nil, decodeTypes)
if err != nil {
return nil, err
}

locals, err := parseLocals(path)
locals, err := parseLocals(path, terragruntOptions, nil)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -179,7 +172,14 @@ func getDependencies(path string) ([]string, error) {

// Creates an AtlantisProject for a directory
func createProject(sourcePath string) (*AtlantisProject, error) {
dependencies, err := getDependencies(sourcePath)
options, err := options.NewTerragruntOptions(sourcePath)
if err != nil {
return nil, err
}
options.RunTerragrunt = cli.RunTerragrunt
options.Env = getEnvs()

dependencies, err := getDependencies(sourcePath, options)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -213,7 +213,7 @@ func createProject(sourcePath string) (*AtlantisProject, error) {
relativeSourceDir = "."
}

locals, err := parseLocals(sourcePath)
locals, err := parseLocals(sourcePath, options, nil)
if err != nil {
return nil, err
}
Expand Down
4 changes: 3 additions & 1 deletion cmd/golden/mergeParentDependencies.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ projects:
- '*.hcl'
- '*.tf*'
- some_parent_dep
- ../file_in_parent_of_child.json
- ../../parent/folder_under_parent/common_tags.hcl
- some_child_dep
dir: child
dir: deep/child
version: 3
17 changes: 5 additions & 12 deletions cmd/parse_locals.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,8 @@ func parseHcl(parser *hclparse.Parser, hcl string, filename string) (file *hcl.F
return file, nil
}

// TODO: memoize so that this is only done once per path per run
// Parses a given file, returning a map of all it's `local` values
func parseLocals(path string) (ResolvedLocals, error) {
func parseLocals(path string, terragruntOptions *options.TerragruntOptions, includeFromChild *config.IncludeConfig) (ResolvedLocals, error) {
if cachedResult, ok := parseLocalsCache[path]; ok {
return cachedResult.resolvedLocals, cachedResult.err
}
Expand All @@ -65,12 +64,6 @@ func parseLocals(path string) (ResolvedLocals, error) {
return ResolvedLocals{}, err
}

options, err := options.NewTerragruntOptions(path)
if err != nil {
parseLocalsCache[path] = ParseLocalResult{err: err}
return ResolvedLocals{}, err
}

// Parse the HCL string into an AST body
parser := hclparse.NewParser()
file, err := parseHcl(parser, configString, path)
Expand All @@ -80,17 +73,17 @@ func parseLocals(path string) (ResolvedLocals, error) {
}

// Decode just the Base blocks. See the function docs for DecodeBaseBlocks for more info on what base blocks are.
localsAsCty, _, includeConfig, err := config.DecodeBaseBlocks(options, parser, file, path, nil)
localsAsCty, _, includeConfig, err := config.DecodeBaseBlocks(terragruntOptions, parser, file, path, includeFromChild)
if err != nil {
parseLocalsCache[path] = ParseLocalResult{err: err}
return ResolvedLocals{}, err
}

// Recurse on the parent to merge in the locals from that file
parentLocals := ResolvedLocals{}
if includeConfig != nil {
// Ignore errors if the parent cannot be parsed
parentLocals, _ = parseLocals(includeConfig.Path)
if includeConfig != nil && includeFromChild == nil {
// Ignore errors if the parent cannot be parsed. Terragrunt Errors still will be logged
parentLocals, _ = parseLocals(includeConfig.Path, terragruntOptions, includeConfig)
}

childLocals := resolveLocals(*localsAsCty)
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import "github.com/transcend-io/terragrunt-atlantis-config/cmd"
// This variable is set at build time using -ldflags parameters.
// But we still set a default here for those using plain `go get` downloads
// For more info, see: http://stackoverflow.com/a/11355611/483528
var VERSION string = "0.9.4"
var VERSION string = "0.9.5"

func main() {
cmd.Execute(VERSION)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
include {
path = find_in_parent_folders()
path = "${find_in_parent_folders("parent")}/terragrunt.hcl"
}

terraform {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"noice": "no ice"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
inputs = {
nice = "dog"
}
10 changes: 10 additions & 0 deletions test_examples/parent_with_extra_deps/parent/terragrunt.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
locals {
extra_atlantis_dependencies = [
# A relative file to the child should work
"some_parent_dep",

# Functions should run from the child dir, not the parent dir
find_in_parent_folders("file_in_parent_of_child.json"),
"${get_parent_terragrunt_dir()}/folder_under_parent/common_tags.hcl",
]
}
5 changes: 0 additions & 5 deletions test_examples/parent_with_extra_deps/terragrunt.hcl

This file was deleted.

0 comments on commit baa7400

Please sign in to comment.