-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Add plain Terraform support via "plugable" IncludeFn()
- Loading branch information
Showing
20 changed files
with
275 additions
and
23 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,3 +45,4 @@ changelog: | |
- '^test' | ||
- '^style' | ||
- '^docs' | ||
- '^Merge pull request' |
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
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 @@ | ||
baz |
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,9 @@ | ||
resource "aws_vpc" "default" { | ||
cidr_block = var.vpc_cidr | ||
tags = merge( | ||
local.common-tags, | ||
map( | ||
"Description", "VPC for creating resources", | ||
) | ||
) | ||
} |
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,14 @@ | ||
terraform { | ||
source = "my-terraform-module" | ||
} | ||
|
||
include "root" { | ||
path = find_in_parent_folders() | ||
} | ||
|
||
inputs = { | ||
instance_type = "t2.medium" | ||
|
||
min_size = 3 | ||
max_size = 3 | ||
} |
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 @@ | ||
mode: bogus |
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,2 @@ | ||
8�HMԧ��:q | ||
{� |
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 @@ | ||
mode: terraform |
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 @@ | ||
mode: terraform_or_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 @@ | ||
mode: 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,70 @@ | ||
package config | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"gopkg.in/yaml.v3" | ||
|
||
"github.com/ivanilves/travelgrunt/pkg/config/include" | ||
) | ||
|
||
var configFile = ".travelgrunt.yml" | ||
|
||
// Config is a travelgrunt repo-level configuration | ||
type Config struct { | ||
Mode string `yaml:"mode"` | ||
|
||
IsDefault bool | ||
} | ||
|
||
// NewConfig creates new travelgrunt repo-level configuration | ||
func NewConfig(path string) (cfg Config, err error) { | ||
var data []byte | ||
|
||
data, err = os.ReadFile(path + "/" + configFile) | ||
// We don't care about config file not being read, | ||
// it's a common case and we just return default config. | ||
if err != nil { | ||
return DefaultConfig(), nil | ||
} | ||
// If we have a file, it should be a correct one though! | ||
if err := yaml.Unmarshal(data, &cfg); err != nil { | ||
return cfg, err | ||
} | ||
|
||
return cfg, validate(cfg) | ||
} | ||
|
||
func validate(cfg Config) error { | ||
allowedModes := []string{"terragrunt", "terraform", "terraform_or_terragrunt"} | ||
|
||
for _, mode := range allowedModes { | ||
if cfg.Mode == mode { | ||
return nil | ||
} | ||
} | ||
|
||
return fmt.Errorf("illegal mode: %s", cfg.Mode) | ||
} | ||
|
||
// DefaultConfig returns default travelgrunt repo-level configuration | ||
func DefaultConfig() Config { | ||
return Config{Mode: "terragrunt", IsDefault: true} | ||
} | ||
|
||
// IncludeFn returns the "include" function used to select relevant directories | ||
func (cfg Config) IncludeFn() (fn func(os.DirEntry) bool) { | ||
switch cfg.Mode { | ||
case "terragrunt": | ||
fn = include.IsTerragrunt | ||
case "terraform": | ||
fn = include.IsTerraform | ||
case "terraform_or_terragrunt": | ||
fn = include.IsTerraformOrTerragrunt | ||
default: | ||
fn = nil | ||
} | ||
|
||
return fn | ||
} |
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,58 @@ | ||
package config | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"os" | ||
"reflect" | ||
"runtime" | ||
|
||
"github.com/ivanilves/travelgrunt/pkg/config/include" | ||
) | ||
|
||
const ( | ||
fixturePath = "../../fixtures/config" | ||
) | ||
|
||
func getConfig(mode string, isDefault bool) Config { | ||
return Config{Mode: mode, IsDefault: isDefault} | ||
} | ||
|
||
func TestNewConfig(t *testing.T) { | ||
assert := assert.New(t) | ||
|
||
testCases := map[string]struct { | ||
cfg Config | ||
success bool | ||
includeFn func(os.DirEntry) bool | ||
}{ | ||
"travelgrunt.yml.terragrunt": {cfg: getConfig("terragrunt", false), success: true, includeFn: include.IsTerragrunt}, | ||
"travelgrunt.yml.terraform": {cfg: getConfig("terraform", false), success: true, includeFn: include.IsTerraform}, | ||
"travelgrunt.yml.terraform_or_terragrunt": {cfg: getConfig("terraform_or_terragrunt", false), success: true, includeFn: include.IsTerraformOrTerragrunt}, | ||
"travelgrunt.yml.invalid": {cfg: getConfig("", false), success: false, includeFn: nil}, | ||
"travelgrunt.yml.illegal": {cfg: getConfig("bogus", false), success: false, includeFn: nil}, | ||
"travelgrunt.yml.nonexistent": {cfg: getConfig("terragrunt", true), success: true, includeFn: include.IsTerragrunt}, | ||
} | ||
|
||
for cfgFile, expected := range testCases { | ||
configFile = cfgFile | ||
|
||
cfg, err := NewConfig(fixturePath) | ||
|
||
assert.Equal(expected.cfg, cfg) | ||
|
||
assert.Equalf( | ||
runtime.FuncForPC(reflect.ValueOf(expected.includeFn).Pointer()).Name(), | ||
runtime.FuncForPC(reflect.ValueOf(cfg.IncludeFn()).Pointer()).Name(), | ||
"got unexpected include function while loading config file: %s", configFile, | ||
) | ||
|
||
if expected.success { | ||
assert.Nil(err) | ||
} else { | ||
assert.NotNil(err) | ||
} | ||
} | ||
} |
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,25 @@ | ||
package include | ||
|
||
import ( | ||
"os" | ||
"strings" | ||
) | ||
|
||
func fileOrSymlink(d os.DirEntry) bool { | ||
return d.Type().IsRegular() || d.Type() == os.ModeSymlink | ||
} | ||
|
||
// IsTerragrunt tells us if we operate on Terragrunt config file | ||
func IsTerragrunt(d os.DirEntry) bool { | ||
return fileOrSymlink(d) && d.Name() == "terragrunt.hcl" | ||
} | ||
|
||
// IsTerraform tells us if we operate on Terraform file(s) | ||
func IsTerraform(d os.DirEntry) bool { | ||
return fileOrSymlink(d) && strings.HasSuffix(d.Name(), ".tf") | ||
} | ||
|
||
// IsTerraformOrTerragrunt tells us if we operate on Terraform or Terragrunt file(s) | ||
func IsTerraformOrTerragrunt(d os.DirEntry) bool { | ||
return IsTerraform(d) || IsTerragrunt(d) | ||
} |
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,47 @@ | ||
package include | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
|
||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
const ( | ||
fixturePath = "../../../fixtures/config/include" | ||
) | ||
|
||
func TestIncludeFn(t *testing.T) { | ||
assert := assert.New(t) | ||
|
||
type testCase struct { | ||
IsTerragrunt bool | ||
IsTerraform bool | ||
IsTerraformOrTerragrunt bool | ||
} | ||
|
||
testCases := map[string]testCase{ | ||
"../../../fixtures/config/include/terragrunt/terragrunt.hcl": testCase{true, false, true}, | ||
"../../../fixtures/config/include/terraform/main.tf": testCase{false, true, true}, | ||
"../../../fixtures/config/include/nothing/foo.bar": testCase{false, false, false}, | ||
} | ||
|
||
err := filepath.WalkDir(fixturePath, | ||
func(path string, d os.DirEntry, err error) error { | ||
assert.Nil(err) | ||
|
||
for p, expected := range testCases { | ||
if p == path { | ||
assert.Equal(expected.IsTerragrunt, IsTerragrunt(d)) | ||
assert.Equal(expected.IsTerraform, IsTerraform(d)) | ||
assert.Equal(expected.IsTerraformOrTerragrunt, IsTerraformOrTerragrunt(d)) | ||
} | ||
} | ||
|
||
return nil | ||
}) | ||
|
||
assert.Nil(err) | ||
} |
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
Oops, something went wrong.