Load up the Keystone User Table

In the past I have created scripts for loading test data into FreeIPA. I’ve started doing the same thing for Keystone, using the Web API. Here is the first. It uses a list of usernames generated from my FreeIPA sample data, based on first initial-last name of a bunch of the most popular names in the country. The list is here. Here is the script I use to load it. Once again, I use jq to parse the JSON.

Note that HTML hates heredocs: I had to replace the here doc double-less-than-sign with a comment:

#insert two less than signs here#

To make this format correctly. Reverse this change before running this file, or fetch the clean source from here.

#!/bin/bash

OS_USERNAME=admin
OS_PASSWORD=freeipa4all
OS_PROJECT_NAME=admin

#to use devstack credential uncomment this line
#. /opt/stack/devstack/openrc

read -r -d '' TOKEN_REQUEST_JSON  #insert two less than signs here#EOF
{
    "auth": {
        "identity": {
            "methods": [
                "password"
            ],
            "password": {
                "user": {
                    "domain": {
                        "name": "Default"
                    },
                    "name": "$OS_USERNAME",
                    "password": "$OS_PASSWORD"
                }
            }
        },
        "scope": {
            "project": {
                "domain": {
                    "name": "Default"
                },
                "name": "$OS_PROJECT_NAME"
            }
        }
    }
}
EOF

#echo $TOKEN_REQUEST_JSON
TOKEN=`curl -si -d "$TOKEN_REQUEST_JSON" -H "Content-type: application/json" http://localhost:35357/v3/auth/tokens | awk '/X-Subject-Token/ {print $2}'`


create_user(){
NEW_USERNAME=$1
PROJECT_ID=$2

read -r -d '' CREATE_USER_JSON #insert two less than signs here#EOF
{ 
    "user": { 
           "default_project_id": $PROJECT_ID, 
            "description": "Description", 
            "domain_id": "default",
            "email": "unused@example.com", 
            "enabled": true, 
            "name": $NEW_USERNAME, 
            "password": "changeme" } 
}
EOF

curl -si -H"X-Auth-Token:$TOKEN" -H "Content-type: application/json" http://localhost:35357/v3/users -d "$CREATE_USER_JSON"

}



DEMO_PROJECT_ID=`curl -s -H"X-Auth-Token:$TOKEN" -H "Content-type: application/json" http://localhost:35357/v3/projects | jq '.projects[] | select( contains ({name: "demo"})) | {id}[]'`

while read USERNAME
do
create_user $USERNAME $DEMO_PROJECT_ID
done < usernames.txt

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>