|
| 1 | +# Copyright 2026 Genesis Corporation. |
| 2 | +# |
| 3 | +# All Rights Reserved. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 6 | +# not use this file except in compliance with the License. You may obtain |
| 7 | +# a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 13 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 14 | +# License for the specific language governing permissions and limitations |
| 15 | +# under the License. |
| 16 | + |
| 17 | +import logging |
| 18 | +import typing as tp |
| 19 | +import uuid as sys_uuid |
| 20 | + |
| 21 | +from restalchemy.common import exceptions as ra_e |
| 22 | +from restalchemy.dm import filters as dm_filters |
| 23 | +from restalchemy.dm import models |
| 24 | +from restalchemy.dm import properties |
| 25 | +from restalchemy.dm import types |
| 26 | +from restalchemy.storage.sql import orm |
| 27 | + |
| 28 | +LOG = logging.getLogger(__name__) |
| 29 | + |
| 30 | + |
| 31 | +class QuotaExceededError(ra_e.ValidationErrorException): |
| 32 | + message = "Quota exceeded for resource '%(resource_name)s' in project %(project_id)s: %(current)s > %(limit)s" |
| 33 | + |
| 34 | + def __init__( |
| 35 | + self, resource_name: str, limit: int, current: int, project_id: sys_uuid.UUID |
| 36 | + ): |
| 37 | + super().__init__( |
| 38 | + resource_name=resource_name, |
| 39 | + limit=limit, |
| 40 | + current=current, |
| 41 | + project_id=project_id, |
| 42 | + ) |
| 43 | + self.resource_name = resource_name |
| 44 | + self.limit = limit |
| 45 | + self.current = current |
| 46 | + self.project_id = project_id |
| 47 | + |
| 48 | + |
| 49 | +DEFAULT_QUOTA_LIMIT = 1000 |
| 50 | +DEFAULT_QUOTA_LIMITS: tp.Dict[str, int] = { |
| 51 | + "net_lb": DEFAULT_QUOTA_LIMIT, |
| 52 | + "compute_sets": DEFAULT_QUOTA_LIMIT, |
| 53 | + "nodes": DEFAULT_QUOTA_LIMIT, |
| 54 | + "secret_passwords": DEFAULT_QUOTA_LIMIT, |
| 55 | + "secret_certificates": DEFAULT_QUOTA_LIMIT, |
| 56 | + "secret_rsa_keys": DEFAULT_QUOTA_LIMIT, |
| 57 | + "secret_ssh_keys": DEFAULT_QUOTA_LIMIT, |
| 58 | +} |
| 59 | +DEFAULT_QUOTA_FIELD_LIMITS: tp.Dict[str, tp.Dict[str, int]] = { |
| 60 | + "nodes": {"cores": 10000}, |
| 61 | +} |
| 62 | + |
| 63 | + |
| 64 | +class QuotaModelMixin: |
| 65 | + def _quota_limits(self, session) -> tp.Collection["QuotaLimit"]: |
| 66 | + limits = list( |
| 67 | + QuotaLimit.objects.get_all( |
| 68 | + session=session, |
| 69 | + filters={ |
| 70 | + "project_id": dm_filters.EQ(self.project_id), |
| 71 | + "resource_name": dm_filters.EQ(self.__tablename__), |
| 72 | + }, |
| 73 | + ) |
| 74 | + ) |
| 75 | + limit_fields = {limit.field_name for limit in limits} |
| 76 | + if "" not in limit_fields: |
| 77 | + default_limit = DEFAULT_QUOTA_LIMITS.get(self.__tablename__) |
| 78 | + if default_limit is not None: |
| 79 | + limits.append( |
| 80 | + QuotaLimit( |
| 81 | + project_id=self.project_id, |
| 82 | + resource_name=self.__tablename__, |
| 83 | + field_name="", |
| 84 | + limit=default_limit, |
| 85 | + ) |
| 86 | + ) |
| 87 | + |
| 88 | + for field_name, default_limit in DEFAULT_QUOTA_FIELD_LIMITS.get( |
| 89 | + self.__tablename__, {} |
| 90 | + ).items(): |
| 91 | + if field_name not in limit_fields: |
| 92 | + limits.append( |
| 93 | + QuotaLimit( |
| 94 | + project_id=self.project_id, |
| 95 | + resource_name=self.__tablename__, |
| 96 | + field_name=field_name, |
| 97 | + limit=default_limit, |
| 98 | + ) |
| 99 | + ) |
| 100 | + return limits |
| 101 | + |
| 102 | + def _quota_check(self, session) -> None: |
| 103 | + # Check entity-count and aggregate-field limits before inserting. |
| 104 | + limits = self._quota_limits(session) |
| 105 | + if not limits: |
| 106 | + return |
| 107 | + |
| 108 | + filters = {"project_id": dm_filters.EQ(self.project_id)} |
| 109 | + field_limits = [limit for limit in limits if limit.field_name] |
| 110 | + count_limits = [limit for limit in limits if not limit.field_name] |
| 111 | + |
| 112 | + if field_limits: |
| 113 | + field_names = {limit.field_name for limit in field_limits} |
| 114 | + invalid_field_names = field_names - set(self.properties.properties) |
| 115 | + if invalid_field_names: |
| 116 | + raise ValueError( |
| 117 | + f"Unknown quota field(s): {', '.join(sorted(invalid_field_names))}" |
| 118 | + ) |
| 119 | + |
| 120 | + fields = ", ".join(sorted(field_names)) |
| 121 | + result = session.execute( |
| 122 | + f"SELECT {fields} FROM {self.__tablename__} WHERE project_id = %s", |
| 123 | + (self.project_id,), |
| 124 | + ) |
| 125 | + rows = result.fetchall() |
| 126 | + for quota_limit in field_limits: |
| 127 | + current = sum(row[quota_limit.field_name] for row in rows) + getattr( |
| 128 | + self, quota_limit.field_name |
| 129 | + ) |
| 130 | + if current > quota_limit.limit: |
| 131 | + raise QuotaExceededError( |
| 132 | + resource_name=f"{self.__tablename__}.{quota_limit.field_name}", |
| 133 | + limit=quota_limit.limit, |
| 134 | + current=current, |
| 135 | + project_id=self.project_id, |
| 136 | + ) |
| 137 | + |
| 138 | + if count_limits: |
| 139 | + current = self.objects.count(session=session, filters=filters) + 1 |
| 140 | + for quota_limit in count_limits: |
| 141 | + if current > quota_limit.limit: |
| 142 | + raise QuotaExceededError( |
| 143 | + resource_name=self.__tablename__, |
| 144 | + limit=quota_limit.limit, |
| 145 | + current=current, |
| 146 | + project_id=self.project_id, |
| 147 | + ) |
| 148 | + |
| 149 | + def insert(self, session=None): |
| 150 | + # Reserve quota capacity by checking entity-count and field totals. |
| 151 | + if session is None: |
| 152 | + with self._get_engine().session_manager(session=session) as s: |
| 153 | + self._quota_check(s) |
| 154 | + super().insert(session=s) |
| 155 | + else: |
| 156 | + self._quota_check(session) |
| 157 | + super().insert(session=session) |
| 158 | + |
| 159 | + |
| 160 | +class QuotaLimit( |
| 161 | + models.ModelWithUUID, |
| 162 | + models.ModelWithTimestamp, |
| 163 | + models.ModelWithProject, |
| 164 | + orm.SQLStorableMixin, |
| 165 | +): |
| 166 | + __tablename__ = "quota_limits" |
| 167 | + |
| 168 | + resource_name = properties.property( |
| 169 | + types.String(max_length=255), |
| 170 | + required=True, |
| 171 | + ) |
| 172 | + field_name = properties.property( |
| 173 | + types.String(max_length=255), |
| 174 | + default="", |
| 175 | + ) |
| 176 | + limit = properties.property( |
| 177 | + types.Integer(min_value=0), |
| 178 | + required=True, |
| 179 | + ) |
0 commit comments