Skip to content

Commit

Permalink
Merge pull request #713 from MetaCell/release/2.2.0
Browse files Browse the repository at this point in the history
Release/2.2.0
  • Loading branch information
filippomc authored Dec 21, 2023
2 parents 8f52735 + d877554 commit 8f83e50
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
12 changes: 9 additions & 3 deletions libraries/cloudharness-common/cloudharness/auth/keycloak.py
Original file line number Diff line number Diff line change
Expand Up @@ -442,13 +442,19 @@ def get_user(self, user_id, with_details=False) -> User:
raise UserNotFound(user_id)
except InvalidToken as e:
raise UserNotFound(user_id)


else:
found_users = admin_client.get_users({"username": user_id})
found_users = admin_client.get_users({"username": user_id, "exact": True})
if len(found_users) == 0:
raise UserNotFound(user_id)
user = admin_client.get_user(found_users[0]['id']) # Load full data

try:
user = admin_client.get_user(found_users[0]['id']) # Load full data
except KeycloakGetError as e:
raise UserNotFound(user_id)
except InvalidToken as e:
raise UserNotFound(user_id)

user.update({
"userGroups": admin_client.get_user_groups(user_id=user['id'], brief_representation=not with_details),
'realmRoles': admin_client.get_realm_roles_of_user(user['id'])
Expand Down
15 changes: 11 additions & 4 deletions libraries/cloudharness-common/cloudharness/auth/quota.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import re
from keycloak import KeycloakError
from .keycloak import AuthClient
from cloudharness.applications import get_current_configuration
Expand Down Expand Up @@ -93,17 +94,23 @@ def _compute_quotas_from_tree(node: QuotaNode):
child_attrs = _compute_quotas_from_tree(child)
for key in child_attrs:
try:
child_val = float(child_attrs[key])
# we expect all quota values to be numbers: the unit is implicit and
# defined at usage time
child_val = attribute_to_quota(child_attrs[key])
except:
# value not a float, skip (use 0)
child_val = 0
# value not a float, skip
continue
if not key in new_attrs or new_attrs[key] < child_val:
new_attrs.update({key: child_val})
for key in new_attrs:
node.attrs.update({key: new_attrs[key]})
return node.attrs


def attribute_to_quota(attr_value: str):
return float(re.sub("[^0-9.]", "", attr_value) if type(attr_value) is str else attr_value)


def get_user_quotas(application_config: ApplicationConfig = None, user_id: str = None) -> dict:
"""Get the user quota from Keycloak and application
Expand Down Expand Up @@ -142,5 +149,5 @@ def get_user_quotas(application_config: ApplicationConfig = None, user_id: str =
user_quotas.update({key: group_quotas[key]})
for key in base_quotas:
if key not in user_quotas:
user_quotas.update({key: base_quotas[key]})
user_quotas.update({key: attribute_to_quota(base_quotas[key])})
return user_quotas

0 comments on commit 8f83e50

Please sign in to comment.