Skip to content

Commit

Permalink
Extend register users from CSV to also add to rhods-notebooks (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
knikolla authored Jan 10, 2024
1 parent d4e542a commit 7f5bad4
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion tools/register_users_from_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import csv
import logging
import os
import re
import sys

import requests
Expand Down Expand Up @@ -57,7 +58,7 @@ def create_user(self, username, first_name, last_name, email):
r = self.session.get(url)
if r.status_code == 200:
print(f"User {username} exists.")
if r.status_code == 404:
if r.status_code in [404, 500]:
print(f"Creating user {username}.")
payload = {
"schemas": ["urn:ietf:params:scim:schemas:core:2.0:User"],
Expand Down Expand Up @@ -113,6 +114,21 @@ def add_user_to_group(self, username, allocation):
print(f"Added user {username} to allocation {allocation}.")


def get_sanitized_name(name):
'''
Returns a sanitized name that only contains lowercase
alphanumeric characters and dashes (not leading or trailing.)
'''
name = name.lower()

# replace special characters with dashes
name = re.sub('[^a-z0-9-]', '-', name)

# remove repeated and trailing dashes
name = re.sub('-+', '-', name).strip('-')
return name


def main():
client = ScimClient(
sys.argv[2],
Expand All @@ -133,6 +149,10 @@ def main():
client.add_user_to_group(username=row[0],
allocation=row[3])

# Add to rhods-notebook namespace
sanitized_name = get_sanitized_name(row[0])
os.system(f"oc -n rhods-notebooks create rolebinding {sanitized_name} --clusterrole=edit --user={row[0]} --as system:admin")


if __name__ == "__main__":
main()

0 comments on commit 7f5bad4

Please sign in to comment.