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

feat: add terramate create --all-terraform --tags a,b,c #1990

Merged
merged 2 commits 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
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ 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.
- Add `create --all-terragrunt --tags a,b,c` for creating all discovered Terragrunt stacks with the given tags.
- Add `create --all-terraform --tags a,b,c` for creating all discovered Terraform stacks with the given tags.

### Fixed

Expand Down
6 changes: 6 additions & 0 deletions cmd/terramate/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -1701,6 +1701,11 @@ func (c *cli) initTerraformDir(baseDir string) error {
fatalWithDetailf(err, "unable to read directory while listing directory entries")
}

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

errs := errors.L()
for _, f := range dirs {
path := filepath.Join(baseDir, f.Name())
Expand Down Expand Up @@ -1741,6 +1746,7 @@ func (c *cli) initTerraformDir(baseDir string) error {
ID: stackID.String(),
Name: dirBasename,
Description: dirBasename,
Tags: tags,
}

err = stack.Create(c.cfg(), stackSpec)
Expand Down
29 changes: 26 additions & 3 deletions e2etests/core/create_all_terraform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ package core_test
import (
"os"
"path/filepath"
"sort"
"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/stack"
errtest "github.com/terramate-io/terramate/test/errors"
. "github.com/terramate-io/terramate/test/hclwrite/hclutils"
Expand Down Expand Up @@ -40,7 +43,7 @@ func TestCreateWithAllTerraformModuleAtRoot(t *testing.T) {
}

func TestCreateWithAllTerraformModuleDeepDownInTheTree(t *testing.T) {
test := func(t *testing.T, generate bool) {
test := func(t *testing.T, generate bool, tags []string) {
s := sandbox.NoGit(t, true)
backendBlock := Block("terraform",
Block("backend",
Expand Down Expand Up @@ -76,6 +79,9 @@ func TestCreateWithAllTerraformModuleDeepDownInTheTree(t *testing.T) {
if !generate {
args = append(args, "--no-generate")
}
if len(tags) > 0 {
args = append(args, "--tags", strings.Join(tags, ","))
}
AssertRunResult(t,
tm.Run(args...),
RunExpected{
Expand All @@ -87,6 +93,9 @@ func TestCreateWithAllTerraformModuleDeepDownInTheTree(t *testing.T) {
},
)

root := s.ReloadConfig()

sort.Strings(tags)
for _, path := range []string{
"/prod/stacks/A",
"/prod/stacks/B",
Expand All @@ -102,15 +111,29 @@ func TestCreateWithAllTerraformModuleDeepDownInTheTree(t *testing.T) {
} else {
errtest.Assert(t, err, os.ErrNotExist)
}

stTree, ok := root.Lookup(project.NewPath(path))
assert.IsTrue(t, ok)
stack, err := stTree.Stack()
assert.NoError(t, err)
assert.EqualInts(t, len(tags), len(stack.Tags))
sort.Strings(stack.Tags)
for i, tag := range tags {
assert.EqualStrings(t, tag, stack.Tags[i])
}
}
}

t.Run("with generation", func(t *testing.T) {
test(t, true)
test(t, true, []string{})
})

t.Run("without generation", func(t *testing.T) {
test(t, false)
test(t, false, []string{})
})

t.Run("with tags", func(t *testing.T) {
test(t, false, []string{"tag1", "tag2"})
})
}

Expand Down
Loading