Skip to content

Instantly share code, notes, and snippets.

@rdegges
Last active June 23, 2016 21:22
Show Gist options
  • Save rdegges/bd973488f0cafbf71f43306b8b35d803 to your computer and use it in GitHub Desktop.
Save rdegges/bd973488f0cafbf71f43306b8b35d803 to your computer and use it in GitHub Desktop.
Stormpath Account Import Script (from a csv file)
"""
import.py
~~~~~~~~~
A small script that reads in user accounts from a CSV file, and imports them
into Stormpath with a random password.
This script requires a few things.
First off, it requires the following environment variables to be set:
- STORMPATH_CLIENT_APIKEY_ID
- STORMPATH_CLIENT_APIKEY_SECRET
- STORMPATH_BASE_URL
- STORMPATH_DIRECTORY_NAME
Next, it requires a CSV file to exist called ``accounts.csv``.
It also requires the CSV file to have columns:
- username
- email
- given_name
- surname
- custom_data
It requires the CSV file to have comma delimited fields, and fields quoted
with a pipe character |.
It requires you to install the ``stormpath`` python library, eg::
$ pip install stormpath
"""
from csv import QUOTE_ALL, reader as csvreader, writer as csvwriter
from json import dumps, loads
from os import environ
from uuid import uuid4
from stormpath.client import Client
ACCOUNTS_FILE = 'accounts.csv'
NEW_ACCOUNTS_FILE = 'accounts-new.csv'
COLUMNS = ['username', 'email', 'given_name', 'surname', 'custom_data', 'href']
client = Client(
id = environ['STORMPATH_CLIENT_APIKEY_ID'],
secret = environ['STORMPATH_CLIENT_APIKEY_SECRET'],
base_url = environ['STORMPATH_BASE_URL'],
)
directory = client.directories.search(environ['STORMPATH_DIRECTORY_NAME'])[0]
total_accounts = len(open(ACCOUNTS_FILE, 'rb').readlines())
migrated_accounts = 0
with open(ACCOUNTS_FILE, 'rb') as rcsvfile:
with open(NEW_ACCOUNTS_FILE, 'wb') as wcsvfile:
reader = csvreader(rcsvfile, delimiter=',', quotechar='|')
writer = csvwriter(wcsvfile, delimiter=',', quotechar='|', quoting=QUOTE_ALL)
# Write the header row into the CSV file.
writer.writerow(COLUMNS)
for row in reader:
data = {
'username': row[0],
'email': row[1],
'given_name': row[2],
'surname': row[3],
# Parse all JSON data into a Python dictionary.
'custom_data': loads(row[4]),
# Randomly generate a password. Alphanumeric + symbol.
'password': uuid4().hex + uuid4().hex.upper() + '!',
}
try:
account = directory.accounts.create(data)
writer.writerow([
data['username'],
data['email'],
data['given_name'],
data['surname'],
dumps(data['custom_data'], sort_keys=True, separators=(',', ':')),
account.href
])
print('Successfully migrated account: {}. {} of {}'.format(account.email, migrated_accounts, total_accounts))
migrated_accounts += 1
except Exception as err:
print('Skipping Account: {}. {}'.format(data['email'], err))
@mdeggies
Copy link

Can you update the comment at the top to include the right column ordering? It should be username, given_name, surname, email, custom_data.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment