Raise a UnsupportedKind for unknown kinds
This commit is contained in:
parent
3c5bec6328
commit
4197b10c11
@ -16,10 +16,11 @@
|
|||||||
import json
|
import json
|
||||||
|
|
||||||
from kazoo import client
|
from kazoo import client
|
||||||
from kazoo import exceptions
|
from kazoo import exceptions as kazoo_exceptions
|
||||||
from kazoo.protocol import paths
|
from kazoo.protocol import paths
|
||||||
|
|
||||||
from delimiter import engine
|
from delimiter import engine
|
||||||
|
from delimiter import exceptions
|
||||||
from delimiter import processors
|
from delimiter import processors
|
||||||
|
|
||||||
|
|
||||||
@ -56,7 +57,7 @@ class ZookeeperQuotaEngine(engine.QuotaEngine):
|
|||||||
who_path = paths.join(self.uri.path, for_who)
|
who_path = paths.join(self.uri.path, for_who)
|
||||||
try:
|
try:
|
||||||
child_nodes = self.client.get_children(who_path)
|
child_nodes = self.client.get_children(who_path)
|
||||||
except exceptions.NoNodeError:
|
except kazoo_exceptions.NoNodeError:
|
||||||
return []
|
return []
|
||||||
else:
|
else:
|
||||||
limits = []
|
limits = []
|
||||||
@ -64,14 +65,15 @@ class ZookeeperQuotaEngine(engine.QuotaEngine):
|
|||||||
try:
|
try:
|
||||||
blob, _znode = self.client.get(paths.join(who_path,
|
blob, _znode = self.client.get(paths.join(who_path,
|
||||||
resource))
|
resource))
|
||||||
except exceptions.NoNodeError:
|
except kazoo_exceptions.NoNodeError:
|
||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
stored = json.loads(blob)
|
stored = json.loads(blob)
|
||||||
kind = stored['kind']
|
kind = stored['kind']
|
||||||
processor = self.processors.get(kind)
|
processor = self.processors.get(kind)
|
||||||
if not processor:
|
if not processor:
|
||||||
raise ValueError("Read unsupported kind '%s'" % kind)
|
raise exceptions.UnsupportedKind(
|
||||||
|
"Read unsupported kind '%s'" % kind)
|
||||||
limits.append((resource,
|
limits.append((resource,
|
||||||
processor.decode(stored['details'])))
|
processor.decode(stored['details'])))
|
||||||
return limits
|
return limits
|
||||||
@ -89,12 +91,13 @@ class ZookeeperQuotaEngine(engine.QuotaEngine):
|
|||||||
'kind': kind,
|
'kind': kind,
|
||||||
'details': processor.create(limit),
|
'details': processor.create(limit),
|
||||||
}))
|
}))
|
||||||
except exceptions.NodeExistsError:
|
except kazoo_exceptions.NodeExistsError:
|
||||||
blob, znode = self.client.get(resource_path)
|
blob, znode = self.client.get(resource_path)
|
||||||
stored = json.loads(blob)
|
stored = json.loads(blob)
|
||||||
if stored['kind'] != kind:
|
if stored['kind'] != kind:
|
||||||
raise ValueError("Can only update limits of the same"
|
raise exceptions.UnsupportedKind(
|
||||||
" kind, %s != %s" % (kind, stored['kind']))
|
"Can only update limits of the same"
|
||||||
|
" kind, %s != %s" % (kind, stored['kind']))
|
||||||
else:
|
else:
|
||||||
stored['details'] = processor.update(stored['details'], limit)
|
stored['details'] = processor.update(stored['details'], limit)
|
||||||
# Ensure we pass in the version that we read this on so
|
# Ensure we pass in the version that we read this on so
|
||||||
@ -108,9 +111,10 @@ class ZookeeperQuotaEngine(engine.QuotaEngine):
|
|||||||
kind = stored['kind']
|
kind = stored['kind']
|
||||||
processor = self.processors.get(kind)
|
processor = self.processors.get(kind)
|
||||||
if not processor:
|
if not processor:
|
||||||
raise ValueError("Unsupported kind '%s' encountered"
|
raise exceptions.UnsupportedKind(
|
||||||
" for resource '%s' owned by '%s'"
|
"Unsupported kind '%s' encountered"
|
||||||
% (kind, resource, for_who))
|
" for resource '%s' owned by '%s'"
|
||||||
|
% (kind, resource, for_who))
|
||||||
return processor.process(stored['details'], amount)
|
return processor.process(stored['details'], amount)
|
||||||
|
|
||||||
def consume_many(self, for_who, resources, amounts):
|
def consume_many(self, for_who, resources, amounts):
|
||||||
|
@ -18,5 +18,9 @@ class DelimiterException(Exception):
|
|||||||
"""Base class for *most* exceptions emitted from this library."""
|
"""Base class for *most* exceptions emitted from this library."""
|
||||||
|
|
||||||
|
|
||||||
|
class UnsupportedKind(DelimiterException):
|
||||||
|
"""Exception raised when an unknown resource/limit kind is found."""
|
||||||
|
|
||||||
|
|
||||||
class OverLimitException(DelimiterException):
|
class OverLimitException(DelimiterException):
|
||||||
"""Exception raised when over some limit."""
|
"""Exception raised when over some limit."""
|
||||||
|
Loading…
x
Reference in New Issue
Block a user