Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Yda 5942 add user friendly messages csv import #522

Merged
merged 25 commits into from
Oct 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 27 additions & 9 deletions groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -529,11 +529,11 @@ def api_group_process_csv(ctx, csv_header_and_data, allow_update, delete_users):
return api.Error('errors', validation_errors)

# Step 3: Create / update groups.
error = apply_data(ctx, data, allow_update, delete_users)
if len(error):
return api.Error('errors', [error])
status_msg = apply_data(ctx, data, allow_update, delete_users)
if status_msg['status'] == 'error':
return api.Error('errors', [status_msg['message']])

return api.Result.ok()
return api.Result.ok(info=[status_msg['message']])


def validate_data(ctx, data, allow_update):
Expand All @@ -553,7 +553,7 @@ def validate_data(ctx, data, allow_update):
for (category, subcategory, groupname, _managers, _members, _viewers, _schema_id, _expiration_date) in data:

if group.exists(ctx, groupname) and not allow_update:
errors.append('Group "{}" already exists'.format(groupname))
errors.append('Group "{}" already exists. It has not been updated.'.format(groupname))

# Is user admin or has category add privileges?
if not (is_admin or can_add_category):
Expand All @@ -575,11 +575,13 @@ def apply_data(ctx, data, allow_update, delete_users):
:param allow_update: Allow updates in groups
:param delete_users: Allow for deleting of users from groups

:returns: Errors if found any
:returns: Errors if found any, or message with actions if everything is successful
"""

for (category, subcategory, group_name, managers, members, viewers, schema_id, expiration_date) in data:
new_group = False
users_added, users_removed = 0, 0
message = ''

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

Expand All @@ -590,10 +592,12 @@ def apply_data(ctx, data, allow_update, delete_users):

if response:
new_group = True
message += "Group '{}' created.".format(group_name)
elif response.status == "error_group_exists" and allow_update:
log.write(ctx, 'CSV import - WARNING: group "{}" not created, it already exists'.format(group_name))
message += "Group '{}' already exists.".format(group_name)
else:
return "Error while attempting to create group {}. Status/message: {} / {}".format(group_name, response.status, response.status_info)
return {status: 'error', message: "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
Expand All @@ -604,6 +608,7 @@ def apply_data(ctx, data, allow_update, delete_users):
if response:
currentrole = "normal"
log.write(ctx, "CSV import - Notice: added user {} to group {}".format(username, group_name))
users_added += 1
else:
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(response.status, response.status_info))
Expand Down Expand Up @@ -669,11 +674,21 @@ def apply_data(ctx, data, allow_update, delete_users):
response = group_remove_user_from_group(ctx, username, usergroupname)
if response:
log.write(ctx, "CSV import - Removing user {} from group {}".format(username, usergroupname))
users_removed += 1
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(response.status, response.status_info))

return ''
if users_added > 0:
message += ' Users added ({}).'.format(users_added)
if users_removed > 0:
message += ' Users removed ({}).'.format(users_removed)

# If no users added, no users removed and not new group created.
if not users_added and not users_removed and not new_group:
message += ' No changes made.'

return {"status": "ok", "message": message}


def _are_roles_equivalent(a, b):
Expand Down Expand Up @@ -973,12 +988,15 @@ def group_create(ctx, group_name, category, subcategory, schema_id, expiration_d
if not sram.sram_connect_service_collaboration(ctx, short_name):
return api.Error('sram_error', 'Something went wrong connecting service to group "{}" in SRAM'.format(group_name))

if group.exists(ctx, group_name):
return api.Error('group_exists', "Group {} not created, it already exists".format(group_name))

response = ctx.uuGroupAdd(group_name, category, subcategory, schema_id, expiration_date, description, data_classification, co_identifier, '', '')['arguments']
status = response[8]
message = response[9]
if status == '0':
return api.Result.ok()
elif status == '-1089000' or status == '-809000':
elif status == '-1089000' or status == '-809000' or status == '-806000':
leonidastri marked this conversation as resolved.
Show resolved Hide resolved
return api.Error('group_exists', "Group {} not created, it already exists".format(group_name))
else:
return api.Error('policy_error', message)
Expand Down
Loading