UPD: added comments to aws-org-dump.py

This commit is contained in:
xpk 2024-03-08 14:10:09 +08:00
parent 00260939fa
commit 88cadbd0b2
Signed by: xpk
GPG Key ID: CD4FF6793F09AB86
1 changed files with 27 additions and 13 deletions

View File

@ -1,10 +1,18 @@
#!/usr/bin/python3
import boto3
# import json
def recurseChildren(ouid, level):
def recurseChildren(ouid: str, level: int) -> None:
"""
Recurse down the AWS organization tree and invoke printChildAccounts once
the bottom has been reached
:param ouid: Parent OUID
:param level: Used internally for printing dots
:return: None
"""
global client
children = client.list_organizational_units_for_parent(ParentId=ouid).get('OrganizationalUnits')
if len(children) == 0:
printChildAccounts(ouid, level)
@ -13,25 +21,31 @@ def recurseChildren(ouid, level):
if ouid.startswith('r'):
printChildAccounts(ouid, level)
for ou in children:
print('.' * level, ou.get('Name'), ou.get('Id'))
print('.' * 2 * level, ou.get('Name'), ou.get('Id'))
if ouid.startswith('ou'):
printChildAccounts(ouid, level)
recurseChildren(ou.get('Id'), level + 1)
def printChildAccounts(ouid, level):
def printChildAccounts(ouid: str, level: int) -> None:
"""
Print child account details
:param ouid: Parent OUID
:param level: Used internally for printing dots
:return: None
"""
global client
childAccounts = client.list_accounts_for_parent(ParentId=ouid).get('Accounts')
for account in childAccounts:
print('.' * level, account.get('Name'), account.get('Id'))
print('.' * 2 * level, account.get('Name'), account.get('Id'))
return
client = boto3.client('organizations')
response = client.list_roots()
# print(json.dumps(response))
rootId = response['Roots'][0]['Id']
print('Root ', rootId)
recurseChildren(rootId, 1)
if __name__ == '__main__':
client = boto3.client('organizations')
response = client.list_roots()
rootId = response['Roots'][0]['Id']
print('Root', rootId, sep=": ")
recurseChildren(rootId, 1)