Skip to content

Commit

Permalink
feat: add terramate create --all-terraform --tags a,b,c (#1990)
Browse files Browse the repository at this point in the history
## What this PR does / why we need it:

Add `terramate create --all-terraform --tags a,b,c`.

## Which issue(s) this PR fixes:
Related #1966 

## Special notes for your reviewer:

## Does this PR introduce a user-facing change?
```
yes, add a feature.
```
  • Loading branch information
i4ki authored Dec 2, 2024
2 parents 385a749 + 0166e9c commit e432789
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
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

0 comments on commit e432789

Please sign in to comment.