From e23ad34416dc9b2ff7fdac226bda0a317a4ea1dd Mon Sep 17 00:00:00 2001 From: Mike Bailey Date: Sun, 19 Jan 2020 21:00:45 +1100 Subject: [PATCH] aws-accounts() lists AWS Accounts in an Organization The account details produced can be piped into some of the other aws-account functions or enjoyed on their own. . --- aliases | 1 + bash_completion.sh | 10 +++++++ docs/command-reference.md | 14 +++++++++ functions | 1 + lib/aws-account-functions | 33 ++++++++++++++++++++++ scripts/build-completions | 7 ++++- scripts/completions/aws-account-completion | 7 +++++ 7 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 scripts/completions/aws-account-completion diff --git a/aliases b/aliases index d364e37e..c3407bf4 100644 --- a/aliases +++ b/aliases @@ -35,6 +35,7 @@ alias aws-account-cost-explorer='~/.bash-my-aws/bin/bma aws-account-cost-explore alias aws-account-cost-recommendations='~/.bash-my-aws/bin/bma aws-account-cost-recommendations' alias aws-account-each='~/.bash-my-aws/bin/bma aws-account-each' alias aws-account-id='~/.bash-my-aws/bin/bma aws-account-id' +alias aws-accounts='~/.bash-my-aws/bin/bma aws-accounts' alias aws-panopticon='~/.bash-my-aws/bin/bma aws-panopticon' alias bucket-acls='~/.bash-my-aws/bin/bma bucket-acls' alias bucket-remove='~/.bash-my-aws/bin/bma bucket-remove' diff --git a/bash_completion.sh b/bash_completion.sh index 2b558f3a..8f256004 100644 --- a/bash_completion.sh +++ b/bash_completion.sh @@ -6,6 +6,13 @@ _bma_asgs_completion() { COMPREPLY=($(compgen -W "${options}" -- ${word})) return 0 } +_bma_aws-accounts_completion() { + local command="$1" + local word="$2" + local options=$(bma aws-accounts | awk '{ print $1 }') + COMPREPLY=($(compgen -W "${options}" -- ${word})) + return 0 +} _bma_buckets_completion() { local command="$1" local word="$2" @@ -127,6 +134,9 @@ complete -F _bma_asgs_completion asg-scaling-activities complete -F _bma_asgs_completion asg-stack complete -F _bma_asgs_completion asg-suspend complete -F _bma_asgs_completion asgs +complete -F _bma_aws-accounts_completion aws-account-cost-explorer +complete -F _bma_aws-accounts_completion aws-account-cost-recommendations +complete -F _bma_aws-accounts_completion aws-accounts complete -F _bma_buckets_completion bucket-acls complete -F _bma_buckets_completion bucket-remove complete -F _bma_buckets_completion bucket-remove-force diff --git a/docs/command-reference.md b/docs/command-reference.md index f0751972..d614217b 100644 --- a/docs/command-reference.md +++ b/docs/command-reference.md @@ -29,6 +29,20 @@ the most interest to readers. ## aws-account-commands +### aws-accounts + +List AWS Accounts in an [Organization](https://aws.amazon.com/organizations/) + + $ aws-accounts + 089834043791 ACTIVE INVITED 1488257653.638 mike-aws@bailey.net.au + 812094344564 ACTIVE CREATED 1537922950.972 mike-bash-my-aws@bailey.net.au + 001721147249 ACTIVE INVITED 1548752330.723 mike@bailey.net.au + 867077406134 ACTIVE CREATED 1557910982.885 mike-deleteme@bailey.net.au + 892345420873 ACTIVE CREATED 1557911243.358 mike-delete@bailey.net.au + +*Optionally provide a filter string for a `| grep` effect with tighter columisation:* + + ### aws-account-alias Retrieve AWS Account Alias for current account diff --git a/functions b/functions index bf8fd9bd..e7b1f57e 100644 --- a/functions +++ b/functions @@ -33,6 +33,7 @@ aws-account-cost-explorer aws-account-cost-recommendations aws-account-each aws-account-id +aws-accounts aws-panopticon bucket-acls bucket-remove diff --git a/lib/aws-account-functions b/lib/aws-account-functions index da1e0f52..ec432d38 100644 --- a/lib/aws-account-functions +++ b/lib/aws-account-functions @@ -6,6 +6,39 @@ # authenticated to or the Account IDs provided to them. +aws-accounts() { + + # List AWS Accounts in an [Organization](https://aws.amazon.com/organizations/) + # + # $ aws-accounts + # 089834043791 ACTIVE INVITED 1488257653.638 mike-aws@bailey.net.au + # 812094344564 ACTIVE CREATED 1537922950.972 mike-bash-my-aws@bailey.net.au + # 001721147249 ACTIVE INVITED 1548752330.723 mike@bailey.net.au + # 867077406134 ACTIVE CREATED 1557910982.885 mike-deleteme@bailey.net.au + # 892345420873 ACTIVE CREATED 1557911243.358 mike-delete@bailey.net.au + # + # *Optionally provide a filter string for a `| grep` effect with tighter columisation:* + + local account_ids=$(skim-stdin) + local filters=$(__bma_read_filters "$@") + + aws organizations list-accounts \ + --output text \ + --query " + Accounts[${account_ids:+?contains(['${account_ids// /"','"}'], Id)}].[ + Id, + Status, + JoinedMethod, + JoinedTimestamp, + Email + ]" | + grep -E -- "$filters" | + LC_ALL=C sort -b -k 4 | + column -s$'\t' -t + +} + + aws-account-alias() { # Retrieve AWS Account Alias for current account diff --git a/scripts/build-completions b/scripts/build-completions index 77341727..0936b8a4 100755 --- a/scripts/build-completions +++ b/scripts/build-completions @@ -21,19 +21,24 @@ done cat "${bma_path}/scripts/completions/bma-completion" + + # dont add auto_completions for these functions funcs_no_completion=( asg-desired-size-set asg-max-size-set asg-min-size-set + aws-account-alias + aws-account-each + aws-account-id keypair-create region-each regions - stack-validate stack-create stack-tag stack-tag-apply stack-tag-delete + stack-validate vpc-default-delete vpc-dhcp-options-ntp vpc-endpoint-services diff --git a/scripts/completions/aws-account-completion b/scripts/completions/aws-account-completion new file mode 100644 index 00000000..2e6b7c31 --- /dev/null +++ b/scripts/completions/aws-account-completion @@ -0,0 +1,7 @@ +_bma_aws-accounts_completion() { + local command="$1" + local word="$2" + local options=$(bma aws-accounts | awk '{ print $1 }') + COMPREPLY=($(compgen -W "${options}" -- ${word})) + return 0 +}