diff options
| author | termie <github@anarkystic.com> | 2011-03-24 12:42:46 -0700 |
|---|---|---|
| committer | termie <github@anarkystic.com> | 2011-03-24 12:42:46 -0700 |
| commit | ef5c9e11595a00de468783adbb60cfbc2cbbf13d (patch) | |
| tree | a4ff720fb7bbbd4dd74eca4ae24950e2bf0c2449 /nova/api | |
| parent | ac44b8a9c5ed6a761793e1fa997768bd00a6c2da (diff) | |
add Limited, an API limiting/versioning wrapper
Diffstat (limited to 'nova/api')
| -rw-r--r-- | nova/api/direct.py | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/nova/api/direct.py b/nova/api/direct.py index dfca250e0..1011091a6 100644 --- a/nova/api/direct.py +++ b/nova/api/direct.py @@ -211,6 +211,42 @@ class ServiceWrapper(wsgi.Controller): return result +class Limited(object): + __notdoc = """Limit the available methods on a given object. + + (Not a docstring so that the docstring can be conditionally overriden.) + + Useful when defining a public API that only exposes a subset of an + internal API. + + Expected usage of this class is to define a subclass that lists the allowed + methods in the 'allowed' variable. + + Additionally where appropriate methods can be added or overwritten, for + example to provide backwards compatibility. + + """ + + _allowed = None + + def __init__(self, proxy): + self._proxy = proxy + if not self.__doc__: + self.__doc__ = proxy.__doc__ + if not self._allowed: + self._allowed = [] + + def __getattr__(self, key): + """Only return methods that are named in self._allowed.""" + if key not in self._allowed: + raise AttributeError() + return getattr(self._proxy, key) + + def __dir__(self): + """Only return methods that are named in self._allowed.""" + return [x for x in dir(self._proxy) if x in self._allowed] + + class Proxy(object): """Pretend a Direct API endpoint is an object.""" def __init__(self, app, prefix=None): |
