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

resource account: include AccountName as tag by default #86

Merged
merged 2 commits into from
Jul 3, 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
2 changes: 1 addition & 1 deletion cmd/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ var (
func init() {
rootCmd.AddCommand(deployCmd)
deployCmd.Flags().StringVar(&stacks, "stacks", "", "Filter stacks to deploy")
deployCmd.Flags().StringVar(&tag, "tag", "", "Filter accounts and organization units to deploy")
deployCmd.Flags().StringVar(&tag, "tag", "", "Filter accounts and organization units to deploy with a comma separated list")
deployCmd.Flags().StringVar(&targets, "targets", "", "Filter resource types to deploy. Options: organization, scp, stacks")
deployCmd.Flags().StringVar(&orgFile, "org", "organization.yml", "Path to the organization.yml file")
deployCmd.Flags().BoolVar(&useTUI, "tui", false, "use the TUI for deploy")
Expand Down
2 changes: 1 addition & 1 deletion cmd/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
func init() {
rootCmd.AddCommand(diffCmd)
diffCmd.Flags().StringVar(&stacks, "stacks", "", "Filter stacks to deploy")
diffCmd.Flags().StringVar(&tag, "tag", "", "Filter accounts and organization units to deploy.")
diffCmd.Flags().StringVar(&tag, "tag", "", "Filter accounts and organization units to deploy with a comma separated list")
diffCmd.Flags().StringVar(&targets, "targets", "", "Filter resource types to deploy. Options: organization, scp, stacks")
diffCmd.Flags().StringVar(&orgFile, "org", "organization.yml", "Path to the organization.yml file")
diffCmd.Flags().BoolVar(&useTUI, "tui", false, "use the TUI for diff")
Expand Down
7 changes: 5 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,13 @@ func ProcessOrgEndToEnd(consoleUI runner.ConsoleUI, cmd int, targets []string) e
}

if len(targets) == 0 || deployStacks {
totalTags := strings.Split(tag, ",")
var accountsToApply []resource.Account
for _, acct := range rootAWSOU.AllDescendentAccounts() {
if contains(tag, acct.AllTags()) || tag == "" {
accountsToApply = append(accountsToApply, *acct)
for _, tag := range totalTags {
if contains(tag, acct.AllTags()) || tag == "" {
accountsToApply = append(accountsToApply, *acct)
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion mintlifydocs/commands/deploy.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Flags:
-h, --help help for deploy
--org string Path to the organization.yml file (default "organization.yml")
--stacks string Filter stacks to deploy
--tag string Filter accounts and account groups to deploy
--tag string Filter accounts and account groups to deploy via a comma separated list
--tui use the TUI for deploy
```

Expand Down
2 changes: 1 addition & 1 deletion mintlifydocs/commands/diff.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Flags:
-h, --help help for diff
--org string Path to the organization.yml file (default "organization.yml")
--stacks string Filter stacks to diff
--tag string Filter accounts and account groups to diff.
--tag string Filter accounts and account groups to diff via a comma separated list.
--tui use the TUI for diff
```

Expand Down
2 changes: 1 addition & 1 deletion resource/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (a Account) IsProvisioned() bool {
}

func (a Account) AllTags() []string {
var tags []string
tags := []string{"AccountName=" + a.AccountName}
tags = append(tags, a.Tags...)
if a.Parent != nil {
tags = append(tags, a.Parent.AllTags()...)
Expand Down
16 changes: 15 additions & 1 deletion resourceoperation/organization_unit.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"fmt"
"log"
"strings"
"text/template"

"github.com/fatih/color"
Expand Down Expand Up @@ -473,6 +474,10 @@ func diffTags(taggable Taggable) (added, removed []string) {

for _, tag := range taggable.AllTags() {
if _, ok := oldMap[tag]; !ok {
if ignorableTag(tag) {
continue
}

if contains(added, tag) {
// There can be duplicates when tags are inherited from an OU
continue
Expand Down Expand Up @@ -514,9 +519,18 @@ func ignorableTag(tag string) bool {
ignorableTags := map[string]struct{}{
"TelophaseManaged=true": {},
}
ignorableKeys := map[string]struct{}{
"AccountName": {},
}

_, ok := ignorableTags[tag]
return ok
if ok {
return true
}
if _, ok := ignorableKeys[strings.Split(tag, "=")[0]]; ok {
return true
}
return false
}

func oneOf(check string, slc []string) bool {
Expand Down
72 changes: 72 additions & 0 deletions resourceoperation/organization_unit_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package resourceoperation

import (
"fmt"
"testing"

"github.com/santiago-labs/telophasecli/resource"
"github.com/stretchr/testify/assert"
)

func TestDiffTags(t *testing.T) {
tests := []struct {
description string
input resource.OrganizationUnit

wantAdded []string
wantRemoved []string
}{
{
description: "adding basic tag",
input: resource.OrganizationUnit{
Accounts: []*resource.Account{
{
AccountName: "mgmt",
Tags: []string{
"ou=mgmt",
},
},
},
},
wantAdded: []string{"ou=mgmt"},
},
{
description: "removing basic tag",
input: resource.OrganizationUnit{
Accounts: []*resource.Account{
{
AccountName: "mgmt",
AWSTags: []string{
"ou=mgmt",
},
},
},
},
wantRemoved: []string{"ou=mgmt"},
},
{
description: "no tag diff",
input: resource.OrganizationUnit{
Accounts: []*resource.Account{
{
AccountName: "mgmt",
Tags: []string{
"ou=mgmt",
},
AWSTags: []string{
"ou=mgmt",
},
},
},
},
},
}

for _, tc := range tests {
added, removed := diffTags(tc.input.Accounts[0])
fmt.Println("added", added)
fmt.Println("removed", removed)
assert.Equal(t, tc.wantAdded, added, "added: "+tc.description)
assert.Equal(t, tc.wantRemoved, removed, "removed: "+tc.description)
}
}
Loading