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

fix: add terramate create --all-terragrunt --tags a,b,c #1985

Merged
merged 1 commit into from
Dec 2, 2024
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Given a version number `MAJOR.MINOR.PATCH`, we increment the:
For further details, see [documentation](https://terramate.io/docs/cli/telemetry).
- Can be turned off by setting `terramate.config.telemetry.enabled = false` in the project configuration,
or by setting `disable_telemetry = true` in the user configuration.
- Add `create --all-terragrunt --tags a,b,c` for creating stacks with the given tags.

### Fixed

Expand Down
8 changes: 8 additions & 0 deletions cmd/terramate/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -1605,6 +1605,7 @@ func (c *cli) initTerragrunt() {
if err != nil {
fatalWithDetailf(err, "creating stack UUID")
}

after := []string{}
for _, otherMod := range mod.After.Strings() {
// Parent stack modules must be excluded because of implicit filesystem ordering.
Expand All @@ -1622,11 +1623,18 @@ func (c *cli) initTerragrunt() {
}
after = append(after, otherMod)
}

var tags []string
for _, tag := range c.parsedArgs.Tags {
tags = append(tags, strings.Split(tag, ",")...)
}

stackSpec := config.Stack{
Dir: mod.Path,
ID: stackID.String(),
Name: dirBasename,
Description: dirBasename,
Tags: tags,
After: after,
}

Expand Down
59 changes: 58 additions & 1 deletion e2etests/core/create_all_terragrunt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@ package core_test
import (
"path/filepath"
"regexp"
"slices"
"strings"
"testing"

"github.com/madlambda/spells/assert"
. "github.com/terramate-io/terramate/e2etests/internal/runner"
"github.com/terramate-io/terramate/project"
"github.com/terramate-io/terramate/test/hclwrite"
. "github.com/terramate-io/terramate/test/hclwrite/hclutils"
"github.com/terramate-io/terramate/test/sandbox"
Expand All @@ -19,6 +23,7 @@ func TestCreateAllTerragrunt(t *testing.T) {
type testcase struct {
name string
layout []string
tags []string
wd string
want RunExpected
wantOrder []string
Expand Down Expand Up @@ -55,6 +60,25 @@ func TestCreateAllTerragrunt(t *testing.T) {
},
wantOrder: []string{"."},
},
{
name: "terragrunt modules with tags",
layout: []string{
hclfile("stacks/a/terragrunt.hcl", Block("terraform",
Str("source", "github.com/some/repo"),
)),
hclfile("stacks/b/terragrunt.hcl", Block("terraform",
Str("source", "github.com/some/repo"),
)),
hclfile("stacks/c/terragrunt.hcl", Block("terraform",
Str("source", "github.com/some/repo"),
)),
},
tags: []string{"terragrunt", "terraform"},
want: RunExpected{
Stdout: nljoin("Created stack /stacks/a", "Created stack /stacks/b", "Created stack /stacks/c"),
},
wantOrder: []string{"stacks/a", "stacks/b", "stacks/c"},
},
{
name: "terragrunt module at /dir but terraform.source imported from /_common",
layout: []string{
Expand Down Expand Up @@ -506,7 +530,11 @@ func TestCreateAllTerragrunt(t *testing.T) {
s := sandbox.NoGit(t, true)
s.BuildTree(tc.layout)
tm := NewCLI(t, filepath.Join(s.RootDir(), tc.wd))
res := tm.Run("create", "--all-terragrunt")
args := []string{"create", "--all-terragrunt"}
if len(tc.tags) > 0 {
args = append(args, "--tags", strings.Join(tc.tags, ","))
}
res := tm.Run(args...)
AssertRunResult(t,
res,
tc.want,
Expand All @@ -517,6 +545,35 @@ func TestCreateAllTerragrunt(t *testing.T) {
AssertRunResult(t, res, RunExpected{
Stdout: nljoin(tc.wantOrder...),
})

if len(tc.tags) == 0 {
return
}

// check if tags are present
root := s.ReloadConfig()
rootTree := root.Tree()
if tc.wd != "" {
if !filepath.IsAbs(tc.wd) {
tc.wd = filepath.Join(s.RootDir(), tc.wd)
}
rootTree, _ = root.Lookup(project.PrjAbsPath(s.RootDir(), tc.wd))
}

assert.EqualInts(t, len(tc.wantOrder), len(rootTree.Stacks()))

for _, tree := range rootTree.Stacks() {
st, err := tree.Stack()
assert.NoError(t, err)

assert.EqualInts(t, len(tc.tags), len(st.Tags))

for _, tag := range tc.tags {
if !slices.Contains(st.Tags, tag) {
t.Errorf("stack %s does not have tag %s", st.Dir, tag)
}
}
}
}
})
}
Expand Down
Loading