diff options
author | Jenkins <jenkins@review.openstack.org> | 2015-02-25 18:03:22 +0000 |
---|---|---|
committer | Gerrit Code Review <review@openstack.org> | 2015-02-25 18:03:22 +0000 |
commit | c964a12e01b115afcab40849ea72aa0eddc9b3dc (patch) | |
tree | 560cbfde05aacceaa39be7e116a62f8147246b8b /neutronclient/v2_0 | |
parent | e6153883c1bff125238e9dba9f5220943c4ffb79 (diff) | |
parent | 779b02e480d8bb3bb1147f3fe0e101d6f1e802d5 (diff) | |
download | python-neutronclient-c964a12e01b115afcab40849ea72aa0eddc9b3dc.tar.gz |
Merge "Client command extension support"
Diffstat (limited to 'neutronclient/v2_0')
-rw-r--r-- | neutronclient/v2_0/client.py | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/neutronclient/v2_0/client.py b/neutronclient/v2_0/client.py index 9640572..0feac68 100644 --- a/neutronclient/v2_0/client.py +++ b/neutronclient/v2_0/client.py @@ -15,6 +15,8 @@ # under the License. # +import inspect +import itertools import logging import time @@ -24,6 +26,7 @@ import six.moves.urllib.parse as urlparse from neutronclient import client from neutronclient.common import constants from neutronclient.common import exceptions +from neutronclient.common import extension as client_extension from neutronclient.common import serializer from neutronclient.common import utils from neutronclient.i18n import _ @@ -454,6 +457,36 @@ class Client(ClientBase): } @APIParamsCall + def list_ext(self, path, **_params): + """Client extension hook for lists. + """ + return self.get(path, params=_params) + + @APIParamsCall + def show_ext(self, path, id, **_params): + """Client extension hook for shows. + """ + return self.get(path % id, params=_params) + + @APIParamsCall + def create_ext(self, path, body=None): + """Client extension hook for creates. + """ + return self.post(path, body=body) + + @APIParamsCall + def update_ext(self, path, id, body=None): + """Client extension hook for updates. + """ + return self.put(path % id, body=body) + + @APIParamsCall + def delete_ext(self, path, id): + """Client extension hook for deletes. + """ + return self.delete(path % id) + + @APIParamsCall def get_quotas_tenant(self, **_params): """Fetch tenant info in server's context for following quota operation. """ @@ -1523,3 +1556,59 @@ class Client(ClientBase): def delete_packet_filter(self, packet_filter_id): """Delete the specified packet filter.""" return self.delete(self.packet_filter_path % packet_filter_id) + + def __init__(self, **kwargs): + """Initialize a new client for the Neutron v2.0 API.""" + super(Client, self).__init__(**kwargs) + self._register_extensions(self.version) + + def extend_show(self, resource_plural, path): + def _fx(obj, **_params): + return self.show_ext(path, obj, **_params) + setattr(self, "show_%s" % resource_plural, _fx) + + def extend_list(self, resource_plural, path): + def _fx(**_params): + return self.list_ext(path, **_params) + setattr(self, "list_%s" % resource_plural, _fx) + + def extend_create(self, resource_singular, path): + def _fx(body=None): + return self.create_ext(path, body) + setattr(self, "create_%s" % resource_singular, _fx) + + def extend_delete(self, resource_singular, path): + def _fx(obj): + return self.delete_ext(path, obj) + setattr(self, "delete_%s" % resource_singular, _fx) + + def extend_update(self, resource_singular, path): + def _fx(obj, body=None): + return self.update_ext(path, obj, body) + setattr(self, "update_%s" % resource_singular, _fx) + + def _extend_client_with_module(self, module, version): + classes = inspect.getmembers(module, inspect.isclass) + for cls_name, cls in classes: + if hasattr(cls, 'versions'): + if version not in cls.versions: + continue + if issubclass(cls, client_extension.ClientExtensionList): + self.extend_list(cls.resource_plural, cls.object_path) + elif issubclass(cls, client_extension.ClientExtensionCreate): + self.extend_create(cls.resource, cls.object_path) + elif issubclass(cls, client_extension.ClientExtensionUpdate): + self.extend_update(cls.resource, cls.resource_path) + elif issubclass(cls, client_extension.ClientExtensionDelete): + self.extend_delete(cls.resource, cls.resource_path) + elif issubclass(cls, client_extension.ClientExtensionShow): + self.extend_show(cls.resource, cls.resource_path) + elif issubclass(cls, client_extension.NeutronClientExtension): + setattr(self, "%s_path" % cls.resource_plural, + cls.object_path) + setattr(self, "%s_path" % cls.resource, cls.resource_path) + + def _register_extensions(self, version): + for name, module in itertools.chain( + client_extension._discover_via_entry_points()): + self._extend_client_with_module(module, version) |