code-dumps
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

37 lines
1006 B

#!/usr/bin/python3
import boto3
# import json
def recurseChildren(ouid, level):
children = client.list_organizational_units_for_parent(ParentId=ouid).get('OrganizationalUnits')
if len(children) == 0:
printChildAccounts(ouid, level)
return
else:
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
client = boto3.client('organizations')
response = client.list_roots()
# print(json.dumps(response))
rootId = response['Roots'][0]['Id']
print('Root ', rootId)
recurseChildren(rootId, 1)