Skip to content

Commit c98e2c6

Browse files
authored
Merge pull request #281 from bash-my-aws/list-accounts
aws-accounts() lists AWS Accounts in an Organization
2 parents 85eb031 + e23ad34 commit c98e2c6

File tree

7 files changed

+72
-1
lines changed

7 files changed

+72
-1
lines changed

aliases

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ alias aws-account-cost-explorer='~/.bash-my-aws/bin/bma aws-account-cost-explore
3535
alias aws-account-cost-recommendations='~/.bash-my-aws/bin/bma aws-account-cost-recommendations'
3636
alias aws-account-each='~/.bash-my-aws/bin/bma aws-account-each'
3737
alias aws-account-id='~/.bash-my-aws/bin/bma aws-account-id'
38+
alias aws-accounts='~/.bash-my-aws/bin/bma aws-accounts'
3839
alias aws-panopticon='~/.bash-my-aws/bin/bma aws-panopticon'
3940
alias bucket-acls='~/.bash-my-aws/bin/bma bucket-acls'
4041
alias bucket-remove='~/.bash-my-aws/bin/bma bucket-remove'

bash_completion.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@ _bma_asgs_completion() {
66
COMPREPLY=($(compgen -W "${options}" -- ${word}))
77
return 0
88
}
9+
_bma_aws-accounts_completion() {
10+
local command="$1"
11+
local word="$2"
12+
local options=$(bma aws-accounts | awk '{ print $1 }')
13+
COMPREPLY=($(compgen -W "${options}" -- ${word}))
14+
return 0
15+
}
916
_bma_buckets_completion() {
1017
local command="$1"
1118
local word="$2"
@@ -127,6 +134,9 @@ complete -F _bma_asgs_completion asg-scaling-activities
127134
complete -F _bma_asgs_completion asg-stack
128135
complete -F _bma_asgs_completion asg-suspend
129136
complete -F _bma_asgs_completion asgs
137+
complete -F _bma_aws-accounts_completion aws-account-cost-explorer
138+
complete -F _bma_aws-accounts_completion aws-account-cost-recommendations
139+
complete -F _bma_aws-accounts_completion aws-accounts
130140
complete -F _bma_buckets_completion bucket-acls
131141
complete -F _bma_buckets_completion bucket-remove
132142
complete -F _bma_buckets_completion bucket-remove-force

docs/command-reference.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,20 @@ the most interest to readers.
2929
## aws-account-commands
3030

3131

32+
### aws-accounts
33+
34+
List AWS Accounts in an [Organization](https://aws.amazon.com/organizations/)
35+
36+
$ aws-accounts
37+
089834043791 ACTIVE INVITED 1488257653.638 [email protected]
38+
812094344564 ACTIVE CREATED 1537922950.972 [email protected]
39+
001721147249 ACTIVE INVITED 1548752330.723 [email protected]
40+
867077406134 ACTIVE CREATED 1557910982.885 [email protected]
41+
892345420873 ACTIVE CREATED 1557911243.358 [email protected]
42+
43+
*Optionally provide a filter string for a `| grep` effect with tighter columisation:*
44+
45+
3246
### aws-account-alias
3347

3448
Retrieve AWS Account Alias for current account

functions

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ aws-account-cost-explorer
3333
aws-account-cost-recommendations
3434
aws-account-each
3535
aws-account-id
36+
aws-accounts
3637
aws-panopticon
3738
bucket-acls
3839
bucket-remove

lib/aws-account-functions

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,39 @@
66
# authenticated to or the Account IDs provided to them.
77

88

9+
aws-accounts() {
10+
11+
# List AWS Accounts in an [Organization](https://aws.amazon.com/organizations/)
12+
#
13+
# $ aws-accounts
14+
# 089834043791 ACTIVE INVITED 1488257653.638 [email protected]
15+
# 812094344564 ACTIVE CREATED 1537922950.972 [email protected]
16+
# 001721147249 ACTIVE INVITED 1548752330.723 [email protected]
17+
# 867077406134 ACTIVE CREATED 1557910982.885 [email protected]
18+
# 892345420873 ACTIVE CREATED 1557911243.358 [email protected]
19+
#
20+
# *Optionally provide a filter string for a `| grep` effect with tighter columisation:*
21+
22+
local account_ids=$(skim-stdin)
23+
local filters=$(__bma_read_filters "$@")
24+
25+
aws organizations list-accounts \
26+
--output text \
27+
--query "
28+
Accounts[${account_ids:+?contains(['${account_ids// /"','"}'], Id)}].[
29+
Id,
30+
Status,
31+
JoinedMethod,
32+
JoinedTimestamp,
33+
Email
34+
]" |
35+
grep -E -- "$filters" |
36+
LC_ALL=C sort -b -k 4 |
37+
column -s$'\t' -t
38+
39+
}
40+
41+
942
aws-account-alias() {
1043

1144
# Retrieve AWS Account Alias for current account

scripts/build-completions

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,24 @@ done
2121

2222
cat "${bma_path}/scripts/completions/bma-completion"
2323

24+
25+
2426
# dont add auto_completions for these functions
2527
funcs_no_completion=(
2628
asg-desired-size-set
2729
asg-max-size-set
2830
asg-min-size-set
31+
aws-account-alias
32+
aws-account-each
33+
aws-account-id
2934
keypair-create
3035
region-each
3136
regions
32-
stack-validate
3337
stack-create
3438
stack-tag
3539
stack-tag-apply
3640
stack-tag-delete
41+
stack-validate
3742
vpc-default-delete
3843
vpc-dhcp-options-ntp
3944
vpc-endpoint-services
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
_bma_aws-accounts_completion() {
2+
local command="$1"
3+
local word="$2"
4+
local options=$(bma aws-accounts | awk '{ print $1 }')
5+
COMPREPLY=($(compgen -W "${options}" -- ${word}))
6+
return 0
7+
}

0 commit comments

Comments
 (0)