/
vnc_quota.py
82 lines (67 loc) · 3.03 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
from gen.resource_xsd import *
from gen.resource_common import *
from gen.resource_server import *
from pprint import pformat
class QuotaHelper(object):
default_quota = {
'defaults': -1
}
@classmethod
def get_project_dict_for_quota(cls, proj_uuid, db_conn):
(ok, proj_dict) = db_conn.dbe_read('project', {'uuid': proj_uuid},
obj_fields=['quota'])
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 user_visibility:
return True, ""
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_for_quota(proj_uuid, db_conn)
if not ok:
return (False, (500, 'Internal error : ' + pformat(proj_dict)))
quota_limit = cls.get_quota_limit(proj_dict, obj_type)
# Quota limit is not enabled for this resource
if quota_limit < 0:
return True, ""
if resource in Project.children_fields:
# Number of resources created under this project.
# Resouce is a child ref under project object
(ok, quota_count) = db_conn.dbe_count_children(
'project', proj_uuid, resource)
if not ok:
return (False, (500, 'Internal error : Failed to read current '
'resource count'))
else:
(ok, res_list) = db_conn.dbe_list(obj_type,
back_ref_uuids=[proj_uuid])
if not ok:
return (False, (500, 'Internal error : Failed to read %s '
'resource list' % obj_type))
quota_count = len(res_list)
if quota_count >= quota_limit:
msg = ('quota limit (%d) exceeded for resource %s'
% (quota_limit, obj_type))
return (False, (403, pformat(fq_name) + ' : ' + msg))
return True, ""