Skip to content

Commit

Permalink
Add tests + fix two bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
EthanBlackburn committed Apr 7, 2024
1 parent a264877 commit 0cd2eba
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 26 deletions.
18 changes: 8 additions & 10 deletions cmd/provisionaccounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,16 +113,14 @@ func orgV2Diff(
) []resourceoperation.ResourceOperation {

var operations []resourceoperation.ResourceOperation
if rootAWSOU != nil {
operations = append(operations, resourceoperation.CollectOrganizationUnitOps(
ctx, outputUI, orgClient, mgmtAcct, rootAWSOU, operation,
)...)
for _, op := range resourceoperation.FlattenOperations(operations) {
outputUI.Print(op.ToString(), *mgmtAcct)
}
if len(operations) == 0 {
outputUI.Print("\033[32m No changes to AWS Organization. \033[0m", *mgmtAcct)
}
operations = append(operations, resourceoperation.CollectOrganizationUnitOps(
ctx, outputUI, orgClient, mgmtAcct, rootAWSOU, operation,
)...)
for _, op := range resourceoperation.FlattenOperations(operations) {
outputUI.Print(op.ToString(), *mgmtAcct)
}
if len(operations) == 0 {
outputUI.Print("\033[32m No changes to AWS Organization. \033[0m", *mgmtAcct)
}

return operations
Expand Down
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,24 @@ require (
github.com/rivo/tview v0.0.0-20231031172508-2dfe06011790
github.com/samsarahq/go/oops v0.0.0-20220211150445-4b291d6feac4
github.com/spf13/cobra v1.7.0
github.com/stretchr/testify v1.8.4
gopkg.in/yaml.v3 v3.0.1
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/gdamore/encoding v1.0.0 // indirect
github.com/gdamore/tcell/v2 v2.6.0 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/mattn/go-runewidth v0.0.14 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rivo/uniseg v0.4.3 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/testify v1.8.4 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/term v0.15.0 // indirect
golang.org/x/text v0.14.0 // indirect
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@ github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:ma
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fatih/color v1.15.0 h1:kOqh6YHBtK8aywxGerMG2Eq3H6Qgoqeo13Bk2Mv/nBs=
github.com/fatih/color v1.15.0/go.mod h1:0h5ZqXfHYED7Bhv2ZJamyIOUej9KtShiJESRwBDUSsw=
github.com/gdamore/encoding v1.0.0 h1:+7OoQ1Bc6eTm5niUzBa0Ctsh6JbMW6Ra+YNuAtDBdko=
github.com/gdamore/encoding v1.0.0/go.mod h1:alR0ol34c49FCSBLjhosxzcPHQbf2trDkoo5dl+VrEg=
github.com/gdamore/tcell/v2 v2.6.0 h1:OKbluoP9VYmJwZwq/iLb4BxwKcwGthaa1YNBJIyCySg=
github.com/gdamore/tcell/v2 v2.6.0/go.mod h1:be9omFATkdr0D9qewWW3d+MEvl5dha+Etb5y65J2H8Y=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.4.0 h1:MtMxsa51/r9yyhkyLsVeVt0B+BGQZzpQiTQ4eHZ8bc4=
github.com/google/uuid v1.4.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
Expand Down
25 changes: 20 additions & 5 deletions lib/ymlparser/organizationv2.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,33 +51,48 @@ func ParseOrganizationV2(filepath string) (*resource.OrganizationUnit, error) {
}
org.Organization.OUName = "root"
hydrateOU(orgClient, &org.Organization, rootOU)
hydrateAccountParent(&org.Organization)

// Hydrate Group, then fetch all accounts (pointers) and populate ID.
allAccounts, err := orgClient.CurrentAccounts(context.TODO())
if err != nil {
return nil, oops.Wrapf(err, "CurrentAccounts")
}
for _, acct := range allAccounts {
hydrateAccount(&org.Organization, acct)
for idx := range allAccounts {
hydrateAccount(&org.Organization, allAccounts[idx])
}

return &org.Organization, nil
}

func hydrateAccount(ou *resource.OrganizationUnit, acct *organizations.Account) {
found := true
for idx, parsedAcct := range ou.Accounts {
ou.Accounts[idx].Parent = ou
if parsedAcct.Email == *acct.Email {
if parsedAcct.Email == *acct.Email && !found {
ou.Accounts[idx].AccountID = *acct.Id
return
found = true
}
}

if found {
return
}

for _, childOU := range ou.ChildOUs {
hydrateAccount(childOU, acct)
}
}

func hydrateAccountParent(ou *resource.OrganizationUnit) {
for idx := range ou.Accounts {
ou.Accounts[idx].Parent = ou
}

for _, childOU := range ou.ChildOUs {
hydrateAccountParent(childOU)
}
}

func hydrateOU(orgClient awsorgs.Client, parsedOU *resource.OrganizationUnit, providerOU *organizations.OrganizationalUnit) error {
if providerOU != nil {
parsedOU.OUID = providerOU.Id
Expand Down
10 changes: 0 additions & 10 deletions resource/organization_unit.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package resource

import "sort"

type OrganizationUnit struct {
OUID *string `yaml:"-"`
OUName string `yaml:"Name,omitempty"`
Expand Down Expand Up @@ -55,10 +53,6 @@ func (grp OrganizationUnit) AllDescendentAccounts() []*Account {
accounts = append(accounts, ou.AllDescendentAccounts()...)
}

sort.Slice(accounts, func(i, j int) bool {
return accounts[i].Email < accounts[j].Email
})

return accounts
}

Expand All @@ -70,10 +64,6 @@ func (grp OrganizationUnit) AllDescendentOUs() []*OrganizationUnit {
OUs = append(OUs, childOU.AllDescendentOUs()...)
}

sort.Slice(OUs, func(i, j int) bool {
return OUs[i].OUName < OUs[j].OUName
})

return OUs

}

0 comments on commit 0cd2eba

Please sign in to comment.