-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement --terragrunt-source-map (#1674)
* [skip ci] * Implement --terragrunt-source-map * Update docs to clarify dependency block and literalness of matches
- Loading branch information
1 parent
9362e01
commit 9e59ad5
Showing
20 changed files
with
339 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,14 +2,16 @@ package config | |
|
||
import ( | ||
"fmt" | ||
"github.com/mitchellh/mapstructure" | ||
"net/url" | ||
"os" | ||
"path/filepath" | ||
"reflect" | ||
"strings" | ||
|
||
"github.com/hashicorp/go-getter" | ||
"github.com/hashicorp/hcl/v2" | ||
"github.com/hashicorp/hcl/v2/hclparse" | ||
"github.com/mitchellh/mapstructure" | ||
"github.com/sirupsen/logrus" | ||
"github.com/zclconf/go-cty/cty" | ||
|
||
|
@@ -317,14 +319,79 @@ func (conf *TerraformExtraArguments) GetVarFiles(logger *logrus.Entry) []string | |
// There are two ways a user can tell Terragrunt that it needs to download Terraform configurations from a specific | ||
// URL: via a command-line option or via an entry in the Terragrunt configuration. If the user used one of these, this | ||
// method returns the source URL or an empty string if there is no source url | ||
func GetTerraformSourceUrl(terragruntOptions *options.TerragruntOptions, terragruntConfig *TerragruntConfig) string { | ||
func GetTerraformSourceUrl(terragruntOptions *options.TerragruntOptions, terragruntConfig *TerragruntConfig) (string, error) { | ||
if terragruntOptions.Source != "" { | ||
return terragruntOptions.Source | ||
return terragruntOptions.Source, nil | ||
} else if terragruntConfig.Terraform != nil && terragruntConfig.Terraform.Source != nil { | ||
return *terragruntConfig.Terraform.Source | ||
return adjustSourceWithMap(terragruntOptions.SourceMap, *terragruntConfig.Terraform.Source, terragruntOptions.OriginalTerragruntConfigPath) | ||
} else { | ||
return "" | ||
return "", nil | ||
} | ||
} | ||
|
||
// adjustSourceWithMap implements the --terragrunt-source-map feature. This function will check if the URL portion of a | ||
// terraform source matches any entry in the provided source map and if it does, replace it with the configured source | ||
// in the map. Note that this only performs literal matches with the URL portion. | ||
// | ||
// Example: | ||
// Suppose terragrunt is called with: | ||
// | ||
// --terragrunt-source-map git::ssh://[email protected]/gruntwork-io/i-dont-exist.git=/path/to/local-modules | ||
// | ||
// and the terraform source is: | ||
// | ||
// git::ssh://[email protected]/gruntwork-io/i-dont-exist.git//fixture-source-map/modules/app?ref=master | ||
// | ||
// This function will take that source and transform it to: | ||
// | ||
// /path/to/local-modules/fixture-source-map/modules/app | ||
// | ||
func adjustSourceWithMap(sourceMap map[string]string, source string, modulePath string) (string, error) { | ||
// Skip logic if source map is not configured | ||
if len(sourceMap) == 0 { | ||
return source, nil | ||
} | ||
|
||
// use go-getter to split the module source string into a valid URL and subdirectory (if // is present) | ||
moduleUrl, moduleSubdir := getter.SourceDirSubdir(source) | ||
|
||
// if both URL and subdir are missing, something went terribly wrong | ||
if moduleUrl == "" && moduleSubdir == "" { | ||
return "", errors.WithStackTrace(InvalidSourceUrlWithMap{ModulePath: modulePath, ModuleSourceUrl: source}) | ||
} | ||
|
||
// If module URL is missing, return the source as is as it will not match anything in the map. | ||
if moduleUrl == "" { | ||
return source, nil | ||
} | ||
|
||
// Before looking up in sourceMap, make sure to drop any query parameters. | ||
moduleUrlParsed, err := url.Parse(moduleUrl) | ||
if err != nil { | ||
return source, err | ||
} | ||
moduleUrlParsed.RawQuery = "" | ||
moduleUrlQuery := moduleUrlParsed.String() | ||
|
||
// Check if there is an entry to replace the URL portion in the map. Return the source as is if there is no entry in | ||
// the map. | ||
sourcePath, hasKey := sourceMap[moduleUrlQuery] | ||
if hasKey == false { | ||
return source, nil | ||
} | ||
|
||
// Since there is a source mapping, replace the module URL portion with the entry in the map, and join with the | ||
// subdir. | ||
// If subdir is missing, check if we can obtain a valid module name from the URL portion. | ||
if moduleSubdir == "" { | ||
moduleSubdirFromUrl, err := getModulePathFromSourceUrl(moduleUrl) | ||
if err != nil { | ||
return moduleSubdirFromUrl, err | ||
} | ||
moduleSubdir = moduleSubdirFromUrl | ||
} | ||
return util.JoinTerraformModulePath(sourcePath, moduleSubdir), nil | ||
|
||
} | ||
|
||
// Return the default hcl path to use for the Terragrunt configuration file in the given directory | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -389,6 +389,7 @@ prefix `--terragrunt-` (e.g., `--terragrunt-config`). The currently available op | |
- [terragrunt-working-dir](#terragrunt-working-dir) | ||
- [terragrunt-download-dir](#terragrunt-download-dir) | ||
- [terragrunt-source](#terragrunt-source) | ||
- [terragrunt-source-map](#terragrunt-source-map) | ||
- [terragrunt-source-update](#terragrunt-source-update) | ||
- [terragrunt-ignore-dependency-errors](#terragrunt-ignore-dependency-errors) | ||
- [terragrunt-iam-role](#terragrunt-iam-role) | ||
|
@@ -501,6 +502,35 @@ for all of your Terraform modules, and for each module processed by the `xxx-all | |
append the path of `source` parameter in each module to the `--terragrunt-source` parameter you passed in. | ||
|
||
|
||
### terragrunt-source-map | ||
|
||
**CLI Arg**: `--terragrunt-source-map`<br/> | ||
**Requires an argument**: `--terragrunt-source-map git::ssh://github.com=/path/to/local-terraform-code` | ||
|
||
Can be supplied multiple times: `--terragrunt-source-map source1=dest1 --terragrunt-source-map source2=dest2` | ||
|
||
The `--terragrunt-source-map source=dest` param replaces any `source` URL (including the source URL of a config pulled | ||
in with `dependency` blocks) that has root `source` with `dest`. | ||
|
||
For example: | ||
|
||
``` | ||
terragrunt apply --terragrunt-source-map github.com/org/modules.git:/local/path/to/modules | ||
``` | ||
|
||
The above would replace `terraform { source = "github.com/org/modules.git//xxx" }` with `terraform { source = /local/path/to/modules//xxx }` regardless of | ||
whether you were running `apply`, or `run-all`, or using a `dependency`. | ||
|
||
**NOTE**: This setting is ignored if you pass in `--terragrunt-source`. | ||
|
||
Note that this only performs literal matches on the URL portion. For example, a map key of | ||
`ssh://[email protected]/gruntwork-io/terragrunt.git` will only match terragrunt configurations with source `source = | ||
"ssh://[email protected]/gruntwork-io/terragrunt.git//xxx"` and not sources of the form `source = | ||
"git::ssh://[email protected]/gruntwork-io/terragrunt.git//xxx"`. The latter requires a map key of | ||
`git::ssh://[email protected]/gruntwork-io/terragrunt.git`. | ||
|
||
|
||
|
||
### terragrunt-source-update | ||
|
||
**CLI Arg**: `--terragrunt-source-update`<br/> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
variable "name" {} | ||
|
||
variable "vpc_id" {} | ||
|
||
output "app_url" { | ||
value = "https://${var.name}.${var.vpc_id}.foo.io" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
variable "name" {} | ||
|
||
output "vpc_id" { | ||
value = "vpc-${var.name}-asdf1234" | ||
} |
7 changes: 7 additions & 0 deletions
7
test/fixture-source-map/multiple-match/terragrunt-vpc/terragrunt.hcl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
terraform { | ||
source = "git::ssh://[email protected]/gruntwork-io/another-dont-exist.git//fixture-source-map/modules/vpc?ref=master" | ||
} | ||
|
||
inputs = { | ||
name = "terragrunt" | ||
} |
7 changes: 7 additions & 0 deletions
7
test/fixture-source-map/multiple-match/terratest-vpc/terragrunt.hcl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
terraform { | ||
source = "git::ssh://[email protected]/gruntwork-io/i-dont-exist.git//fixture-source-map/modules/vpc?ref=master" | ||
} | ||
|
||
inputs = { | ||
name = "terratest" | ||
} |
7 changes: 7 additions & 0 deletions
7
test/fixture-source-map/multiple-only-one-match/terragrunt-vpc/terragrunt.hcl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
terraform { | ||
source = "../../modules/vpc" | ||
} | ||
|
||
inputs = { | ||
name = "terragrunt" | ||
} |
7 changes: 7 additions & 0 deletions
7
test/fixture-source-map/multiple-only-one-match/terratest-vpc/terragrunt.hcl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
terraform { | ||
source = "git::ssh://[email protected]/gruntwork-io/i-dont-exist.git//fixture-source-map/modules/vpc?ref=master" | ||
} | ||
|
||
inputs = { | ||
name = "terratest" | ||
} |
12 changes: 12 additions & 0 deletions
12
test/fixture-source-map/multiple-with-dependency-same-url/app/terragrunt.hcl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
terraform { | ||
source = "git::ssh://[email protected]/gruntwork-io/i-dont-exist.git//fixture-source-map/modules/app?ref=master" | ||
} | ||
|
||
dependency "vpc" { | ||
config_path = "../vpc" | ||
} | ||
|
||
inputs = { | ||
name = "terragrunt" | ||
vpc_id = dependency.vpc.outputs.vpc_id | ||
} |
7 changes: 7 additions & 0 deletions
7
test/fixture-source-map/multiple-with-dependency-same-url/vpc/terragrunt.hcl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
terraform { | ||
source = "git::ssh://[email protected]/gruntwork-io/i-dont-exist.git//fixture-source-map/modules/vpc?ref=master" | ||
} | ||
|
||
inputs = { | ||
name = "terragrunt" | ||
} |
12 changes: 12 additions & 0 deletions
12
test/fixture-source-map/multiple-with-dependency/app/terragrunt.hcl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
terraform { | ||
source = "git::ssh://[email protected]/gruntwork-io/another-dont-exist.git//fixture-source-map/modules/app?ref=master" | ||
} | ||
|
||
dependency "vpc" { | ||
config_path = "../vpc" | ||
} | ||
|
||
inputs = { | ||
name = "terragrunt" | ||
vpc_id = dependency.vpc.outputs.vpc_id | ||
} |
7 changes: 7 additions & 0 deletions
7
test/fixture-source-map/multiple-with-dependency/vpc/terragrunt.hcl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
terraform { | ||
source = "git::ssh://[email protected]/gruntwork-io/i-dont-exist.git//fixture-source-map/modules/vpc?ref=master" | ||
} | ||
|
||
inputs = { | ||
name = "terragrunt" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
terraform { | ||
source = "git::ssh://[email protected]/gruntwork-io/i-dont-exist.git//fixture-source-map/modules/vpc?ref=master" | ||
} | ||
|
||
inputs = { | ||
name = "terragrunt" | ||
} |
Oops, something went wrong.