Skip to content

Commit

Permalink
Change behavior of updating and getting group admins
Browse files Browse the repository at this point in the history
Signed-off-by: Timothy Lee <[email protected]>
  • Loading branch information
ctiml committed Apr 18, 2023
1 parent cc13a0e commit 6c32ec9
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 4 deletions.
70 changes: 66 additions & 4 deletions primehub/admin_groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -856,9 +856,26 @@ def create(self, config: dict):
...GroupBasicInfo
}
}
fragment GroupBasicInfo on Group {
id
displayName
name
quotaCpu
quotaGpu
quotaMemory
projectQuotaCpu
projectQuotaGpu
projectQuotaMemory
sharedVolumeCapacity
}
""" + group_basic_info

apply_auto_fill(config)

# cannot specify admins when creating
if config.get('admins'):
config['admins'] = ''

results = self.request({'data': validate(config)}, query)

if 'data' not in results:
Expand Down Expand Up @@ -1002,10 +1019,13 @@ def get(self, id: str) -> dict:

if 'data' not in results:
return results
group = results['data']['group']
if not group:
return group

results['data']['group']['volumes'] = results['data']['group'].pop('datasets', '[]')

return results['data']['group']
group['volumes'] = group.pop('datasets', '[]')
self._output_format_admins(id, group)
return group

def _everyone_group_id(self) -> dict:
query = """
Expand Down Expand Up @@ -1070,6 +1090,9 @@ def update(self, id: str, config: dict):
}
""" + group_basic_info

if config.get('admins'):
config['admins'] = self._transform_admins(id, config.get('admins', []))

variables = {'where': {'id': id}, 'data': validate(config, True)}
results = self.request(variables, query)

Expand All @@ -1088,7 +1111,9 @@ def update(self, id: str, config: dict):
updated_results = self.request({'where': {'id': id}}, updated_query)
if 'data' not in updated_results:
return updated_results
return updated_results['data']['group']
updated_group = updated_results['data']['group']
self._output_format_admins(id, updated_group)
return updated_group

@cmd(name='delete', description='Delete the group by id', return_required=True)
def delete(self, id: str) -> dict:
Expand All @@ -1115,5 +1140,42 @@ def delete(self, id: str) -> dict:
return results
return results['data']['deleteGroup']

def _transform_admins(self, id: str, user_ids: List[str]):
if len(user_ids) == 0:
return ''

member_dict = {}
users = self.primehub.admin.admin_groups.list_users(id)
for user in users:
user_id = user['id']
username = user['username']
member_dict[user_id] = username

admin_usernames = []
invalid_user_ids = []
for user_id in user_ids:
if user_id in member_dict:
admin_usernames.append(member_dict[user_id])
else:
invalid_user_ids.append(user_id)

if len(invalid_user_ids) > 0:
_invalid_ids = ', '.join(invalid_user_ids)
msg = f'admins contain invalid user ids: {_invalid_ids}'
raise PrimeHubException(msg)
return ','.join(admin_usernames)

def _output_format_admins(self, id: str, group: dict):
admin_users = []
admin_usernames = group.get('admins', '').split(',')
users = self.primehub.admin.admin_groups.list_users(id)
for user in users:
if user['username'] in admin_usernames:
admin_users.append(dict(
id=user['id'],
username=user['username']
))
group['admins'] = admin_users

def help_description(self):
return "Manage groups"
2 changes: 2 additions & 0 deletions primehub/admin_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,8 @@ def get(self, id: str) -> dict:
if 'data' not in results:
return results
user = results['data']['user']
if not user:
return user

# hide the everyone group
groups = user['groups']
Expand Down

0 comments on commit 6c32ec9

Please sign in to comment.