Skip to content

Commit

Permalink
YDA-5553: add SRAM support to CSV group import
Browse files Browse the repository at this point in the history
  • Loading branch information
lwesterhof authored Dec 6, 2023
1 parent 1a2d2de commit cb963f1
Showing 1 changed file with 55 additions and 52 deletions.
107 changes: 55 additions & 52 deletions groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -585,39 +585,35 @@ def apply_data(ctx, data, allow_update, delete_users):
:returns: Errors if found any
"""

for (category, subcategory, groupname, managers, members, viewers) in data:
for (category, subcategory, group_name, managers, members, viewers) in data:
new_group = False

log.write(ctx, 'CSV import - Adding and updating group: {}'.format(groupname))
log.write(ctx, 'CSV import - Adding and updating group: {}'.format(group_name))

# First create the group. Note that the actor will become a groupmanager
response = ctx.uuGroupAdd(groupname, category, subcategory, config.default_yoda_schema, '', '', 'unspecified', '', '', '')['arguments']
status = response[8]
message = response[9]
response = group_create(ctx, group_name, category, subcategory, config.default_yoda_schema, '', '', 'unspecified')

if ((status == '-1089000') | (status == '-809000')) and allow_update:
log.write(ctx, 'CSV import - WARNING: group "{}" not created, it already exists'.format(groupname))
elif status != '0':
return "Error while attempting to create group {}. Status/message: {} / {}".format(groupname, status, message)
else:
if response:
new_group = True
elif response.status == "error_group_exists" and allow_update:
log.write(ctx, 'CSV import - WARNING: group "{}" not created, it already exists'.format(group_name))
else:
return "Error while attempting to create group {}. Status/message: {} / {}".format(group_name, response.status, response.status_info)

# Now add the users and set their role if other than member
allusers = managers + members + viewers
for username in list(set(allusers)): # duplicates removed
currentrole = user_role(ctx, groupname, username)
currentrole = user_role(ctx, group_name, username)
if currentrole == "none":
response = ctx.uuGroupUserAdd(groupname, username, '', '')['arguments']
status = response[2]
message = response[3]
if status == '0':
response = group_user_add(ctx, username, group_name)
if response:
currentrole = "member"
log.write(ctx, "CSV import - Notice: added user {} to group {}".format(username, groupname))
log.write(ctx, "CSV import - Notice: added user {} to group {}".format(username, group_name))
else:
log.write(ctx, "CSV import - Warning: error occurred while attempting to add user {} to group {}".format(username, groupname))
log.write(ctx, "CSV import - Warning: error occurred while attempting to add user {} to group {}".format(username, group_name))
log.write(ctx, "CSV import - Status: {} , Message: {}".format(status, message))
else:
log.write(ctx, "CSV import - Notice: user {} is already present in group {}.".format(username, groupname))
log.write(ctx, "CSV import - Notice: user {} is already present in group {}.".format(username, group_name))

# Set requested role. Note that user could be listed in multiple roles.
# In case of multiple roles, manager takes precedence over normal,
Expand All @@ -629,30 +625,25 @@ def apply_data(ctx, data, allow_update, delete_users):
role = 'manager'

if _are_roles_equivalent(role, currentrole):
log.write(ctx, "CSV import - Notice: user {} already has role {} in group {}.".format(username, role, groupname))
log.write(ctx, "CSV import - Notice: user {} already has role {} in group {}.".format(username, role, group_name))
else:
response = ctx.uuGroupUserChangeRole(groupname, username, role, '', '')['arguments']
status = response[3]
message = response[4]
response = group_user_update_role(ctx, username, group_name, role)

if status == '0':
log.write(ctx, "CSV import - Notice: changed role of user {} in group {} to {}".format(username, groupname, role))
if response:
log.write(ctx, "CSV import - Notice: changed role of user {} in group {} to {}".format(username, group_name, role))
else:
log.write(ctx, "CSV import - Warning: error while attempting to change role of user {} in group {} to {}".format(username, groupname, role))
log.write(ctx, "CSV import - Status: {} , Message: {}".format(status, message))
log.write(ctx, "CSV import - Warning: error while attempting to change role of user {} in group {} to {}".format(username, group_name, role))
log.write(ctx, "CSV import - Status: {} , Message: {}".format(response.status, response.status_info))

# Always remove the rods user for new groups, unless it is in the
# CSV file.
if (new_group and "rods" not in allusers and user_role(ctx, groupname, "rods") != "none"):
response = ctx.uuGroupUserRemove(groupname, "rods", '', '')['arguments']
status = response[2]
message = response[3]
if status == "0":
log.write(ctx, "CSV import - Notice: removed rods user from group " + groupname)
if (new_group and "rods" not in allusers and user_role(ctx, group_name, "rods") != "none"):
response = group_remove_user_from_group(ctx, 'rods', group_name)
if response:
log.write(ctx, "CSV import - Notice: removed rods user from group " + group_name)
else:
if status != 0:
log.write(ctx, "CSV import - Warning: error while attempting to remove user rods from group {}".format(groupname))
log.write(ctx, "CSV import - Status: {} , Message: {}".format(status, message))
log.write(ctx, "CSV import - Warning: error while attempting to remove user rods from group {}".format(group_name))
log.write(ctx, "CSV import - Status: {} , Message: {}".format(response.status, response.status_info))

# Remove users not in sheet
if delete_users:
Expand All @@ -661,12 +652,12 @@ def apply_data(ctx, data, allow_update, delete_users):
for prefix in ['read-', 'initial-', 'research-']:
iter = genquery.row_iterator(
"USER_GROUP_NAME, USER_NAME, USER_ZONE",
"USER_TYPE != 'rodsgroup' AND USER_GROUP_NAME = '{}'".format(prefix + '-'.join(groupname.split('-')[1:])),
"USER_TYPE != 'rodsgroup' AND USER_GROUP_NAME = '{}'".format(prefix + '-'.join(group_name.split('-')[1:])),
genquery.AS_LIST, ctx
)

for row in iter:
# append [user,groupname]
# append [user,group_name]
currentusers.append([row[1], row[0]])

for userdata in currentusers:
Expand All @@ -679,14 +670,13 @@ def apply_data(ctx, data, allow_update, delete_users):
continue
else:
managers.remove(username)
log.write(ctx, "CSV import - Removing user {} from group {}".format(username, usergroupname))

response = ctx.uuGroupUserRemove(usergroupname, username, '', '')['arguments']
status = response[2]
message = response[3]
if status != "0":
response = group_remove_user_from_group(ctx, username, usergroupname)
if response:
log.write(ctx, "CSV import - Removing user {} from group {}".format(username, usergroupname))
else:
log.write(ctx, "CSV import - Warning: error while attempting to remove user {} from group {}".format(username, usergroupname))
log.write(ctx, "CSV import - Status: {} , Message: {}".format(status, message))
log.write(ctx, "CSV import - Status: {} , Message: {}".format(response.status, response.status_info))

return ''

Expand Down Expand Up @@ -1042,8 +1032,7 @@ def api_group_exists(ctx, group_name):
return group.exists(ctx, group_name)


@api.make()
def api_group_create(ctx, group_name, category, subcategory, schema_id, expiration_date, description, data_classification):
def group_create(ctx, group_name, category, subcategory, schema_id, expiration_date, description, data_classification):
"""Create a new group.
:param ctx: Combined type of a ctx and rei struct
Expand Down Expand Up @@ -1078,12 +1067,18 @@ def api_group_create(ctx, group_name, category, subcategory, schema_id, expirati
message = response[9]
if status == '0':
return api.Result.ok()
elif status == '-1089000' or status == '-809000':
return api.Error('group_exists', "Group {} not created, it already exists".format(group_name))
else:
return api.Error('policy_error', message)
except Exception:
return api.Error('error_internal', 'Something went wrong creating group "{}". Please contact a system administrator'.format(group_name))


"""API to create a new group."""
api_group_create = api.make()(group_create)


@api.make()
def api_group_update(ctx, group_name, property_name, property_value):
"""Update group property.
Expand Down Expand Up @@ -1164,8 +1159,7 @@ def api_group_user_is_member(ctx, username, group_name):
return group_user_exists(ctx, group_name, username, True)


@api.make()
def api_group_user_add(ctx, username, group_name):
def group_user_add(ctx, username, group_name):
"""Add a user to a group.
:param ctx: Combined type of a ctx and rei struct
Expand Down Expand Up @@ -1203,8 +1197,11 @@ def api_group_user_add(ctx, username, group_name):
return api.Error('error_internal', 'Something went wrong adding {} to group "{}". Please contact a system administrator'.format(username, group_name))


@api.make()
def api_group_user_update_role(ctx, username, group_name, new_role):
"""API to add a user to a group."""
api_group_user_add = api.make()(group_user_add)


def group_user_update_role(ctx, username, group_name, new_role):
"""Update role of a user in a group.
:param ctx: Combined type of a ctx and rei struct
Expand Down Expand Up @@ -1236,6 +1233,10 @@ def api_group_user_update_role(ctx, username, group_name, new_role):
return api.Error('error_internal', 'Something went wrong updating role for {} in group "{}". Please contact a system administrator'.format(username, group_name))


"""API to update role of a user in a group."""
api_group_user_update_role = api.make()(group_user_update_role)


@api.make()
def api_group_get_user_role(ctx, username, group_name):
"""Get role of a user in a group.
Expand All @@ -1249,8 +1250,7 @@ def api_group_get_user_role(ctx, username, group_name):
return user_role(ctx, group_name, username)


@api.make()
def api_group_remove_user_from_group(ctx, username, group_name):
def group_remove_user_from_group(ctx, username, group_name):
"""Remove a user from a group.
:param ctx: Combined type of a ctx and rei struct
Expand All @@ -1259,7 +1259,6 @@ def api_group_remove_user_from_group(ctx, username, group_name):
:returns: Dict with API status result
"""
# ctx.uuGroupUserRemove(group_name, username, '', '')
try:
if config.enable_sram:
sram_group, co_identifier = sram_enabled(ctx, group_name)
Expand All @@ -1283,6 +1282,10 @@ def api_group_remove_user_from_group(ctx, username, group_name):
return api.Error('error_internal', 'Something went wrong removing {} from group "{}". Please contact a system administrator'.format(username, group_name))


"""API to remove a user from a group."""
api_group_remove_user_from_group = api.make()(group_remove_user_from_group)


def sram_enabled(ctx, group_name):
"""Checks if the group is SRAM enabled
Expand Down

0 comments on commit cb963f1

Please sign in to comment.