Skip to content

Instantly share code, notes, and snippets.

Created Jan 10, 2020
What would you like to do?
#!/usr/bin/env python3
from fedora.client.fas2 import AccountSystem
from getpass import getpass
fas_user = input('Admin username: ')
fas_password = getpass('Admin password (not echoed): ')
src = input('Source group name: ')
dest = input('Destination group name: ')
preserve_s = input('Preserve roles? (n=all users are just members) [Y/n]: ')
unapproved_s = input('Import unapproved members? (they will stay unapproved) [Y/n]: ')
preserve = False
if preserve_s in ['Y', 'y', '']:
preserve = True
unapproved = False
if unapproved_s in ['Y', 'y', '']:
unapproved = True
fas = AccountSystem(
print('Obtaining list of all users, hope you have some free RAM!')
users_req = fas.send_request(
req_params={'search': '*'},
# Be able to key on user id
users = {}
for user in users_req['people']:
users[user['id']] = user
print('Got list of users. Obtaining ids of members in source group.')
src_res = fas.send_request('/group/view/%s' % src, auth=True)
print('And dest group, to avoid duplicates.')
dest_res = fas.send_request('/group/view/%s' % dest, auth=True)
print('Requesting membership for members of the source group -> new group.')
for member in src_res['members']:
uid = member['person_id']
username = users[uid]['username']
if uid in [a['person_id'] for a in dest_res['members']]:
print('%s already in destination group, skipping.' % username)
if member['role_status'] == 'unapproved' and not unapproved:
print('Skipping unapproved member %s' % username)
print('Applying for %s' % username)
'/group/apply/%s/%s' % (dest, username))
if member['role_status'] != 'unapproved':
print('Sponsoring %s' % username)
'/group/sponsor/%s/%s' % (dest, username))
if preserve:
# It should be safe to call upgrade here as needed because we
# already bailed out above if the user was already a member.
# At this point the user can only be a member because they were
# just now added. So we don't risk upgrading people who shouldn't
# be upgraded.
if member['role_type'] in ['sponsor', 'administrator']:
print('Upgrading %s.' % username)
'/group/upgrade/%s/%s' % (dest, username))
if member['role_type'] == 'administrator':
print('Upgrading %s again.' % username)
'/group/upgrade/%s/%s' % (dest, username))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment