#!/usr/bin/python3
#
# This script will create an initial skeleton of an ssofresh.ini for a whole org
#
# Usage : Set up credentials with access to the root organization
# 
#         Run with prefix e.g. `ssoinit myorg`
#
#         Prints a sample ssofresh.ini to console with all account IDs
#         Add roles etc and use

import sys
import boto3

def smushed_name(prefix, name):
  return f"{prefix}-{name.lower()}" 

prefix = sys.argv[1]

client = boto3.client('organizations')

next_token = None

accounts = []

response = client.list_accounts(
  MaxResults=10
)
accounts.extend(response["Accounts"])

if "NextToken" in response:
  next_token = response["NextToken"]

while next_token is not None:
  response = client.list_accounts(
    NextToken=next_token,
    MaxResults=10
  )
  accounts.extend(response["Accounts"])
  if "NextToken" in response:
    next_token = response["NextToken"]
  else:
    next_token = None

for account in accounts:
  name = smushed_name(prefix, account["Name"])
  id = account["Id"]
  print(f"[{name}]")
  print(f"account_id = {id}")
  print()

print(f"[{prefix}]")
profiles = ",".join([smushed_name(prefix, account["Name"]) for account in accounts])
print(f"profiles = {profiles}")

