A script that outputs a list of SoftLayer portal users

SoftLayer Portal User Management

SoftLayer manages systems in the cloud from Portal. SoftLayer Customer Portal

You need a user ID to access the Portal. The first user ID created when you create a SoftLayer account is called the master user and has all permissions for that account's system.

Since it is not practical for a master user to manage all systems, the master user typically creates child users and delegates administrative privileges over some systems. The child user further creates a child user and delegates the authority, and delegates the development and operation management on the system to the developer and the person in charge of operation.

The figure is as follows. sl_users.png

The master user assigns permissions to child users and specifies which devices they can access. The child user assigns permissions to the grandchild user to delegate management and specifies which devices they can access.

script

In this way, users have a hierarchical structure. You can see the list of users in Portal, but you cannot see the parent-child relationship of users in the list in Portal or CLI. This is inconvenient for user management, so I created a script that uses the SoftLayer API to get a list of users and a parent-child relationship.

sluser.py


#!/usr/bin/env python
# -*- coding: utf-8 -*-

SL_USERNAME = <SL User Name>
SL_API_KEY = <SL API Key>

sl_getUser.py


#!/usr/bin/env python
# -*- coding: utf-8 -*-

__author__ = 'takechika'

import sys
from prettytable import PrettyTable
import SoftLayer
import sluser

SL_USERNAME = sluser.SL_USERNAME
SL_API_KEY = sluser.SL_API_KEY

_userMask = '''
    id,
    accountId,
    parentId,
    firstName,
    lastName,
    email
    '''
_userTableHeader = [
    'ParentID',
    'ParentName',
    'UserID',
    'UserName',
    'EMail'
    ]

def getChildren(parent, users):
    """Get children of the parent from the user list /Get the child user that this user is a parent from the list.

    parent:Parent user object(User_Customer)
    users:List of child users to search(an array of User_Customer)
    """

    a = {}
    b = []

    for u in users:
        if u.get('parentId') == parent.get('id'):
            a = {
                "ParentID": u.get('parentId'),
                "ParentName": parent.get('lastName') + " " + parent.get('firstName'),
                "ChildID": u.get('id'),
                "ChildName": u.get('lastName') + " " + u.get('firstName'),
                "Email": u.get('email')
            }
            b.append(a)
    return(b)

client = SoftLayer.Client(username=SL_USERNAME, api_key=SL_API_KEY)
users = client['Account'].getUsers(mask=_userMask)

# Find the master user id
for u in users:
    if u.get('parentId') == '':
        masterUser = u
        print("Master user is \"%s %s\" (id: %s)" % (masterUser.get('lastName'), masterUser.get('firstName'), masterUser.get('id')))

# Table definition
table = PrettyTable(_userTableHeader)
table.padding_width = 1

# Get user list
count = 0
for parent in users:
    userList = getChildren(parent, users)
    for x in userList[0:]:
        table.add_row(
            [
                x['ParentID'],
                x['ParentName'],
                x['ChildID'],
                x['ChildName'],
                x['Email']
            ]
        )
        count = count + 1

print(table)
print(count, "users")

exit()

The output looks like this (username and email address are fictitious): The child user and the corresponding parent user are displayed in a list.

Master user is "Jedi Master" (id: 182178)
+----------+--------------------+--------+-----------------------+--------------------------+
| ParentID |     ParentName     | UserID |        UserName       |          EMail           |
+----------+--------------------+--------+-----------------------+--------------------------+
|  182178  |    Jedi Master     | 183358 |    Obi-Wan Kenobi     |    [email protected]    |
|  182178  |    Jedi Master     | 183362 |        R2 D2          |     [email protected]     |
|  182178  |    Jedi Master     | 183372 |        C 3PO          |     [email protected]     |
|  182178  |    Jedi Master     | 183376 |    Luke Skywalker     |     [email protected]      |
|  183362  |    Jedi Master     | 183378 |       Chew Bacca      |   [email protected]   |
|  183358  |   Obi-Wan Kenobi   | 183360 |   Anakin Skywalker    |     [email protected]    |
+----------+--------------------+--------+-----------------------+--------------------------+
6 users

Recommended Posts

A script that outputs a list of SoftLayer portal users
CLI tool that just outputs a list of prefectures quickly
Get a list of IAM users with Boto3
[Linux] A list of Linux commands that beginners should know
A script that takes a snapshot of an EBS volume
A set of script files that do wordcloud in Python3
A Python script that compares the contents of two directories
Shell script (Linux, macOS) that outputs the date of the last week
Generate a list of consecutive characters
Introduction of ls command lsix that can display a list of images
[Python] A program that rotates the contents of the list to the left
Python script that outputs all records of Oracle table to CSV file
Display a list of alphabets in Python 3
[python] Get a list of instance variables
String conversion of a list containing numbers
A script that morphologically parses a specified URL
[Python] Get a list of folders only
Get a list of articles posted by users with Python 3 Qiita API v2
[Linux] A list of unique command selections that are convenient but unexpectedly unknown
Python script to get a list of input examples for the AtCoder contest
A script that just gets an RSS feed
Display output of a list of floating point numbers
List of links that machine learning beginners are learning
"Python Kit" that calls a Python script from Swift
Get a list of Qiita likes by scraping
One-liner that outputs 10000 digits of pi with Python
A python script that generates a sample dataset for checking the operation of a classification tree
A python script that gets the number of jobs for a specified condition from indeed.com
A script that combines margins when pasting a number of graphs on tiles with gnuplot
A script that combines multiple pages of a PDF file into one page without margins
A script that can perform stress tests according to the number of CPU cores