Skip to content

Instantly share code, notes, and snippets.

@jsanz
Last active September 14, 2016 09:02
Show Gist options
  • Save jsanz/586bb959915029caeedfa1178c933aa1 to your computer and use it in GitHub Desktop.
Save jsanz/586bb959915029caeedfa1178c933aa1 to your computer and use it in GitHub Desktop.
Enterprise API: managing organization users from the command line. bash

The manage-users.sh script helps on performing basic tasks to manage users using CartoDB Enterprise API. Actions available are:

  • List users of an organization
  • Create new users
  • Delete users
  • Update existing users (password and data quota)
  • Retrieve users info (in JSON format)

You need to adapt the script for your environment, the variables are at the beginning of the script as follow:

BASE_URL="cartodb.lan"
ORG_NAME="organization"
ORG_OWNER="org-admin"

So the script is executed at organization level. Then, with the API key, run the script to perform any operation like:

API_KEY=you_api_key ./manage-users.sh list sample-users.csv

The CSV file needs to have columns on this form:

username,email,password,quota

Where quota needs to be expresed in megabytes (MB). For example:

bob,[email protected],bobsecret!,100
alice,[email protected],alicesecret!,100

There's a sample CSV file next to the management script.

#!/bin/bash
# usage:
#
# manage-users [action] file.csv
#
# where [action] can be:
# create
# list
# info
# update
# delete
#
# and the CSV file needs to be on this form:
# username,email,password,quota(in MB)
#
# Author: [email protected]
# Configure start
BASE_URL="cartodb.lan"
ORG_NAME="demo"
ORG_OWNER="demo-admin"
# Configure end
function usage_and_quit {
echo "Usage:"
echo "manage-users.sh [create|list|info|update|delete] file.csv"
echo ""
echo "API_KEY environment variable needs to be set up."
exit 1
}
# Check arguments
if [ "$#" -ne 2 ]; then
usage_and_quit
fi
# Check API key
if [ -z "$API_KEY" ]; then
echo "API_KEY not set!"
exit 1
fi
# Variables
API_URL="https://$BASE_URL/u/$ORG_OWNER/api/v1/organization/$ORG_NAME/users"
ACTION=$1
CSV=$2
# Iterate the CSV file
while IFS=, read username email password quota rest; do
case "$ACTION" in
create)
URL_USER="$API_URL/?api_key=$API_KEY";
DATA="{\"username\":\"$username\", \"email\":\"$email\", \"password\":\"$password\", \"quota_in_bytes\":$(expr $quota \* 1048576)}"
METHOD="POST"
;;
list)
#use the SQL API until this is available https://github.com/CartoDB/cartodb/issues/7148
SQL_URL="https://$BASE_URL/user/$ORG_OWNER/api/v1/sql?api_key=$API_KEY&format=csv"
SQL_QUERY="select nspname as user from pg_catalog.pg_namespace where not nspowner = 10;"
curl -skG $SQL_URL --data-urlencode "q=$SQL_QUERY" | tail -n +2
exit 0
;;
update)
URL_USER="$API_URL/$username?api_key=$API_KEY";
DATA="{\"email\":\"$email\", \"password\":\"$password\", \"quota_in_bytes\":$(expr $quota \* 1048576)}"
METHOD="PUT"
;;
info)
URL_USER="$API_URL/$username?api_key=$API_KEY";
METHOD="GET"
DATA=""
;;
delete)
URL_USER="$API_URL/$username?api_key=$API_KEY";
METHOD="DELETE"
DATA=""
;;
*)
usage_and_quit
esac
curl -k -H 'Content-Type: application/json' -X $METHOD $URL_USER --data "$DATA"
done < "$CSV"
bob [email protected] bobsecret! 100
alice [email protected] alicesecret! 100
alex [email protected] alexsecret! 400
sandy [email protected] sandysecret! 100
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment