Skip to content

Commit

Permalink
Add suspended accounts functionality (#36)
Browse files Browse the repository at this point in the history
* Add new remove suspended accounts function.

* Add an optional flag
  • Loading branch information
porrige51122 committed Oct 30, 2023
1 parent 14a5822 commit d30fee8
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 7 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ Usage:

Flags:

-remove-suspended-accounts
Remove suspended accounts from the output (default false)
-include-json
Include the JSON representation of the AWS Organizations structure in the output (default true)
-include-visual
Expand Down
17 changes: 17 additions & 0 deletions generation/ou_struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,20 @@ func (parent *OU) fillAccountsRecursive(ctx context.Context, api *organizations.

return parent, nil
}

// removeSuspendedAccounts removes all suspended accounts from the OU tree.
func (parent *OU) RemoveSuspendedAccounts() *OU {
accounts := make([]types.Account, 0)
for _, account := range parent.Accounts {
if account.Status != types.AccountStatusSuspended {
accounts = append(accounts, account)
}
}
parent.Accounts = accounts

for i := range parent.Children {
parent.Children[i] = parent.Children[i].RemoveSuspendedAccounts()
}

return parent
}
31 changes: 31 additions & 0 deletions generation/ou_struct_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,34 @@ func TestOuFillOuTree(t *testing.T) {
require.Equal(t, "ou-5678", ou.Children[0].Id, "OU children was not set correctly")
require.Equal(t, "TestChildOU", ou.Children[0].Name, "OU children was not set correctly")
}

// TestRemoveSuspendedAccounts tests the removeSuspendedAccounts method of the OU struct.
func TestRemoveSuspendedAccounts(t *testing.T) {
// Create an OU struct.
ou := &OU{
Id: "ou-1234",
Name: "TestOU",
}

// Create a list of accounts.
accounts := []types.Account{
{
Id: aws.String("123456789"),
Status: types.AccountStatusActive,
},
{
Id: aws.String("987654321"),
Status: types.AccountStatusSuspended,
},
}

// Add the accounts to the OU.
ou.Accounts = accounts

// Remove the suspended accounts.
ou = ou.RemoveSuspendedAccounts()

// Check that the correct accounts were removed.
require.Len(t, ou.Accounts, 1, "removeSuspendedAccounts did not remove the correct accounts")
require.Equal(t, "123456789", *ou.Accounts[0].Id, "removeSuspendedAccounts did not remove the correct accounts")
}
19 changes: 12 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@
//
// Flags:
//
// -include-json
// Include the JSON representation of the AWS Organizations structure in the output (default true)
// -include-visual
// Include the visual representation of the AWS Organizations structure in the output (default true)
// -o string
// The output file for the JSON representation of the AWS Organizations structure (default "output.json")
// -remove-suspended-accounts
// Remove suspended accounts from the output (default false)
// -include-json
// Include the JSON representation of the AWS Organizations structure in the output (default true)
// -include-visual
// Include the visual representation of the AWS Organizations structure in the output (default true)
// -o string
// The output file for the JSON representation of the AWS Organizations structure (default "output.json")
package main

import (
Expand Down Expand Up @@ -96,6 +98,7 @@ func checkPermissions() (context.Context, *organizations.Client, error) {
// is executed and is used to call the main logic of the application.
func main() {
// STAGE 1: Sort out the input flags
removeSuspendedAccountsPtr := flag.Bool("remove-suspended-accounts", false, "Remove suspended accounts from the output")
jsonPtr := flag.Bool("include-json", true, "Include the JSON representation of the AWS Organizations structure in the output")
visualPtr := flag.Bool("include-visual", true, "Include the visual representation of the AWS Organizations structure in the output")
outputPtr := flag.String("o", "output.json", "The output file for the JSON representation of the AWS Organizations structure")
Expand All @@ -114,13 +117,15 @@ func main() {

// STAGE 3: Run the main logic of the application to generate the data
// structure
context.Background()
tree, err := generation.GenerateStructure(ctx, cfg)
if err != nil {
fmt.Println("Error generating structure")
logs.Println(err)
return
}
if *removeSuspendedAccountsPtr {
tree = tree.RemoveSuspendedAccounts()
}

// STAGE 4: Determine the output format and output the data structure
// If no output format is specified, exit
Expand Down

0 comments on commit d30fee8

Please sign in to comment.