Created
August 1, 2019 05:20
-
-
Save rdkls/5b794ea2883a42ed8f8bb086f1e06860 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# Generate config file for chrome extension "aws-extend-switch-roles" | |
# https://github.com/tilfin/aws-extend-switch-roles | |
import boto3 | |
import argparse | |
import hashlib | |
from pprint import pprint | |
parser = argparse.ArgumentParser() | |
parser.add_argument('--verbose', '-v', action='store_true') | |
args = parser.parse_args() | |
client = boto3.client('organizations') | |
res = client.list_roots() | |
root_ou_id = res['Roots'][0]['Id'] | |
ous = client.list_organizational_units_for_parent(ParentId=root_ou_id) | |
def get_accounts_with_ou_data(ous): | |
# Generate a flat list of Accounts, each of which includes the OU Name & ID | |
accounts_with_ou_data = [] | |
i=0 | |
for ou in ous['OrganizationalUnits']: | |
i += 1 | |
res = client.list_accounts_for_parent(ParentId=ou['Id']) | |
if args.verbose: | |
print('id %s' % ou['Id']) | |
pprint(res) | |
for account in res['Accounts']: | |
account_data_extended = { | |
'ou_id' : ou['Id'], | |
'ou_name' : ou['Name'], | |
'hexcolor' : s_to_hexcolor(ou['Name']), | |
} | |
for k in account.keys(): | |
# This field causes quicksight to choke on import, and we don't care about it anyway | |
if 'JoinedTimestamp' != k: | |
account_data_extended['account_'+k.lower()] = account[k] | |
accounts_with_ou_data.append(account_data_extended) | |
return accounts_with_ou_data | |
def s_to_hexcolor(s): | |
hc = hashlib.md5(s).hexdigest()[:6] | |
return hc | |
if __name__ == '__main__': | |
accounts_with_ou_data = get_accounts_with_ou_data(ous) | |
for a in accounts_with_ou_data: | |
print """\n[profile {account_name}] | |
role_arn = arn:aws:iam::{account_id}/ADFS-Administrator | |
color = {hexcolor}""".format(**a) | |
if args.verbose or args.verbose: | |
pprint(accounts_with_ou_data) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment