summaryrefslogtreecommitdiff
path: root/novaclient/v2/limits.py
diff options
context:
space:
mode:
Diffstat (limited to 'novaclient/v2/limits.py')
-rw-r--r--novaclient/v2/limits.py100
1 files changed, 100 insertions, 0 deletions
diff --git a/novaclient/v2/limits.py b/novaclient/v2/limits.py
new file mode 100644
index 0000000..46d77de
--- /dev/null
+++ b/novaclient/v2/limits.py
@@ -0,0 +1,100 @@
1# Copyright 2011 OpenStack Foundation
2#
3# Licensed under the Apache License, Version 2.0 (the "License"); you may
4# not use this file except in compliance with the License. You may obtain
5# a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations
13# under the License.
14
15from six.moves.urllib import parse
16
17from novaclient import base
18
19
20class Limits(base.Resource):
21 """A collection of RateLimit and AbsoluteLimit objects."""
22
23 def __repr__(self):
24 return "<Limits>"
25
26 @property
27 def absolute(self):
28 for (name, value) in self._info['absolute'].items():
29 yield AbsoluteLimit(name, value)
30
31 @property
32 def rate(self):
33 for group in self._info['rate']:
34 uri = group['uri']
35 regex = group['regex']
36 for rate in group['limit']:
37 yield RateLimit(rate['verb'], uri, regex, rate['value'],
38 rate['remaining'], rate['unit'],
39 rate['next-available'])
40
41
42class RateLimit(object):
43 """Data model that represents a flattened view of a single rate limit."""
44
45 def __init__(self, verb, uri, regex, value, remain,
46 unit, next_available):
47 self.verb = verb
48 self.uri = uri
49 self.regex = regex
50 self.value = value
51 self.remain = remain
52 self.unit = unit
53 self.next_available = next_available
54
55 def __eq__(self, other):
56 return self.uri == other.uri \
57 and self.regex == other.regex \
58 and self.value == other.value \
59 and self.verb == other.verb \
60 and self.remain == other.remain \
61 and self.unit == other.unit \
62 and self.next_available == other.next_available
63
64 def __repr__(self):
65 return "<RateLimit: verb=%s uri=%s>" % (self.verb, self.uri)
66
67
68class AbsoluteLimit(object):
69 """Data model that represents a single absolute limit."""
70
71 def __init__(self, name, value):
72 self.name = name
73 self.value = value
74
75 def __eq__(self, other):
76 return self.value == other.value and self.name == other.name
77
78 def __repr__(self):
79 return "<AbsoluteLimit: name=%s>" % (self.name)
80
81
82class LimitsManager(base.Manager):
83 """Manager object used to interact with limits resource."""
84
85 resource_class = Limits
86
87 def get(self, reserved=False, tenant_id=None):
88 """
89 Get a specific extension.
90
91 :rtype: :class:`Limits`
92 """
93 opts = {}
94 if reserved:
95 opts['reserved'] = 1
96 if tenant_id:
97 opts['tenant_id'] = tenant_id
98 query_string = "?%s" % parse.urlencode(opts) if opts else ""
99
100 return self._get("/limits%s" % query_string, "limits")