|
| 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 | + |
| 60 | + |
| 61 | +class QuotaModelMixin: |
| 62 | + def _quota_limit(self, session) -> tp.Optional[int]: |
| 63 | + limit = QuotaLimit.objects.get_one_or_none( |
| 64 | + session=session, |
| 65 | + filters={ |
| 66 | + "project_id": dm_filters.EQ(self.project_id), |
| 67 | + "resource_name": dm_filters.EQ(self.__tablename__), |
| 68 | + }, |
| 69 | + ) |
| 70 | + if limit is not None: |
| 71 | + return limit.limit |
| 72 | + return DEFAULT_QUOTA_LIMITS.get(self.__tablename__) |
| 73 | + |
| 74 | + def _quota_check(self, session) -> None: |
| 75 | + # Check limit by counting entities in the table. |
| 76 | + # If limit exceeded, raises QuotaExceededError. |
| 77 | + limit = self._quota_limit(session) |
| 78 | + if limit is None: |
| 79 | + return |
| 80 | + |
| 81 | + current = self.objects.count( |
| 82 | + session=session, filters={"project_id": dm_filters.EQ(self.project_id)} |
| 83 | + ) |
| 84 | + |
| 85 | + if current + 1 > limit: |
| 86 | + raise QuotaExceededError( |
| 87 | + resource_name=self.__tablename__, |
| 88 | + limit=limit, |
| 89 | + current=current + 1, |
| 90 | + project_id=self.project_id, |
| 91 | + ) |
| 92 | + |
| 93 | + def insert(self, session=None): |
| 94 | + # Reserve quota slot by checking entity count, then insert entity. |
| 95 | + if session is None: |
| 96 | + with self._get_engine().session_manager(session=session) as s: |
| 97 | + self._quota_check(s) |
| 98 | + super().insert(session=s) |
| 99 | + else: |
| 100 | + self._quota_check(session) |
| 101 | + super().insert(session=session) |
| 102 | + |
| 103 | + |
| 104 | +class QuotaLimit( |
| 105 | + models.ModelWithUUID, |
| 106 | + models.ModelWithTimestamp, |
| 107 | + models.ModelWithProject, |
| 108 | + orm.SQLStorableMixin, |
| 109 | +): |
| 110 | + __tablename__ = "quota_limits" |
| 111 | + |
| 112 | + resource_name = properties.property( |
| 113 | + types.String(max_length=255), |
| 114 | + required=True, |
| 115 | + ) |
| 116 | + limit = properties.property( |
| 117 | + types.Integer(min_value=0), |
| 118 | + required=True, |
| 119 | + ) |
0 commit comments