2022-05-14 11:33:56 +08:00
|
|
|
#!/usr/bin/python3
|
|
|
|
|
2022-05-13 23:24:11 +08:00
|
|
|
import boto3
|
2022-05-14 11:33:56 +08:00
|
|
|
# import json
|
2022-05-13 23:24:11 +08:00
|
|
|
|
|
|
|
|
|
|
|
def recurseChildren(ouid, level):
|
|
|
|
children = client.list_organizational_units_for_parent(ParentId=ouid).get('OrganizationalUnits')
|
|
|
|
if len(children) == 0:
|
2022-05-15 10:10:26 +08:00
|
|
|
printChildAccounts(ouid, level)
|
2022-05-13 23:24:11 +08:00
|
|
|
return
|
|
|
|
else:
|
2022-05-15 10:10:26 +08:00
|
|
|
if ouid.startswith('r'):
|
|
|
|
printChildAccounts(ouid, level)
|
|
|
|
for ou in children:
|
|
|
|
print('.' * level, ou.get('Name'), ou.get('Id'))
|
|
|
|
if ouid.startswith('ou'):
|
|
|
|
printChildAccounts(ouid, level)
|
|
|
|
recurseChildren(ou.get('Id'), level + 1)
|
|
|
|
|
|
|
|
|
|
|
|
def printChildAccounts(ouid, level):
|
|
|
|
childAccounts = client.list_accounts_for_parent(ParentId=ouid).get('Accounts')
|
|
|
|
for account in childAccounts:
|
|
|
|
print('.' * level, account.get('Name'), account.get('Id'))
|
|
|
|
return
|
2022-05-13 23:24:11 +08:00
|
|
|
|
|
|
|
|
|
|
|
client = boto3.client('organizations')
|
|
|
|
response = client.list_roots()
|
|
|
|
|
2022-05-15 02:03:29 +08:00
|
|
|
# print(json.dumps(response))
|
2022-05-14 11:33:56 +08:00
|
|
|
rootId = response['Roots'][0]['Id']
|
|
|
|
print('Root ', rootId)
|
2022-05-13 23:24:11 +08:00
|
|
|
|
|
|
|
recurseChildren(rootId, 1)
|
|
|
|
|