/
vnc_quota.py
57 lines (47 loc) · 2.05 KB
/
vnc_quota.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import copy
from gen.resource_xsd import *
from gen.resource_common import *
from gen.resource_server import *
class QuotaHelper(object):
default_quota = {
'defaults': -1
}
@classmethod
def get_project_dict(cls, proj_uuid, db_conn):
(ok, proj_dict) = db_conn.dbe_read('project', {'uuid': proj_uuid})
return (ok, proj_dict)
@classmethod
def get_quota_limit(cls, proj_dict, obj_type):
quota = proj_dict.get('quota') or cls.default_quota
quota_type = obj_type.replace('-','_')
quota_limit = quota.get(quota_type)
if quota_limit is None:
quota_limit = cls.default_quota.get(quota_type)
if quota_limit is None:
quota_limit = cls.default_quota['defaults']
return quota_limit
@classmethod
def check_quota_limit(cls, proj_dict, obj_type, quota_count):
quota_limit = cls.get_quota_limit(proj_dict, obj_type)
if quota_limit > 0 and quota_count >= quota_limit:
return (False, 'quota limit (%d) exceeded for resource %s' % (quota_limit, obj_type))
return (True, quota_limit)
@classmethod
def verify_quota_for_resource(cls, db_conn, resource, obj_type,
user_visibility, proj_uuid=None, fq_name=[]):
if not proj_uuid and fq_name:
try:
proj_uuid = db_conn.fq_name_to_uuid('project', fq_name[0:2])
except cfgm_common.exceptions.NoIdError:
return (False, (500, 'No Project ID error : ' + proj_uuid))
(ok, proj_dict) = cls.get_project_dict(proj_uuid, db_conn)
if not ok:
return (False, (500, 'Internal error : ' + pformat(proj_dict)))
if not user_visibility:
return True, ""
quota_count = len(proj_dict.get(resource, []))
(ok, quota_limit) = cls.check_quota_limit(proj_dict, obj_type,
quota_count)
if not ok:
return (False, (403, pformat(fq_name) + ' : ' + quota_limit))
return True, ""