|
| 1 | +import re |
1 | 2 | from keycloak import KeycloakError |
2 | 3 | from .keycloak import AuthClient |
3 | 4 | from cloudharness.applications import get_current_configuration |
@@ -93,17 +94,23 @@ def _compute_quotas_from_tree(node: QuotaNode): |
93 | 94 | child_attrs = _compute_quotas_from_tree(child) |
94 | 95 | for key in child_attrs: |
95 | 96 | try: |
96 | | - child_val = float(child_attrs[key]) |
| 97 | + # we expect all quota values to be numbers: the unit is implicit and |
| 98 | + # defined at usage time |
| 99 | + child_val = attribute_to_quota(child_attrs[key]) |
97 | 100 | except: |
98 | | - # value not a float, skip (use 0) |
99 | | - child_val = 0 |
| 101 | + # value not a float, skip |
| 102 | + continue |
100 | 103 | if not key in new_attrs or new_attrs[key] < child_val: |
101 | 104 | new_attrs.update({key: child_val}) |
102 | 105 | for key in new_attrs: |
103 | 106 | node.attrs.update({key: new_attrs[key]}) |
104 | 107 | return node.attrs |
105 | 108 |
|
106 | 109 |
|
| 110 | +def attribute_to_quota(attr_value: str): |
| 111 | + return float(re.sub("[^0-9.]", "", attr_value) if type(attr_value) is str else attr_value) |
| 112 | + |
| 113 | + |
107 | 114 | def get_user_quotas(application_config: ApplicationConfig = None, user_id: str = None) -> dict: |
108 | 115 | """Get the user quota from Keycloak and application |
109 | 116 |
|
@@ -142,5 +149,5 @@ def get_user_quotas(application_config: ApplicationConfig = None, user_id: str = |
142 | 149 | user_quotas.update({key: group_quotas[key]}) |
143 | 150 | for key in base_quotas: |
144 | 151 | if key not in user_quotas: |
145 | | - user_quotas.update({key: base_quotas[key]}) |
| 152 | + user_quotas.update({key: attribute_to_quota(base_quotas[key])}) |
146 | 153 | return user_quotas |
0 commit comments