diff --git a/novaclient/base.py b/novaclient/base.py index a07bd2497..96aaec1d1 100644 --- a/novaclient/base.py +++ b/novaclient/base.py @@ -89,13 +89,16 @@ class Manager(base.HookableMixin): for res in data if res] @contextlib.contextmanager - def alternate_service_type(self, service_type): + def alternate_service_type(self, default, allowed_types=()): original_service_type = self.api.client.service_type - self.api.client.service_type = service_type - try: + if original_service_type in allowed_types: yield - finally: - self.api.client.service_type = original_service_type + else: + self.api.client.service_type = default + try: + yield + finally: + self.api.client.service_type = original_service_type @contextlib.contextmanager def completion_cache(self, cache_type, obj_class, mode): diff --git a/novaclient/v2/volume_snapshots.py b/novaclient/v2/volume_snapshots.py index ffdb812c0..6cf597a1e 100644 --- a/novaclient/v2/volume_snapshots.py +++ b/novaclient/v2/volume_snapshots.py @@ -61,7 +61,8 @@ class SnapshotManager(base.ManagerWithFind): 'deprecated and will be removed after Nova 13.0.0 is ' 'released. Use python-cinderclient or ' 'python-openstacksdk instead.', DeprecationWarning) - with self.alternate_service_type('volume'): + with self.alternate_service_type( + 'volumev2', allowed_types=('volume', 'volumev2')): body = {'snapshot': {'volume_id': volume_id, 'force': force, 'display_name': display_name, @@ -79,7 +80,9 @@ class SnapshotManager(base.ManagerWithFind): 'deprecated and will be removed after Nova 13.0.0 is ' 'released. Use python-cinderclient or ' 'python-openstacksdk instead.', DeprecationWarning) - with self.alternate_service_type('volume'): + with self.alternate_service_type( + 'volumev2', allowed_types=('volume', 'volumev2')): + return self._get("/snapshots/%s" % snapshot_id, "snapshot") def list(self, detailed=True): @@ -92,7 +95,8 @@ class SnapshotManager(base.ManagerWithFind): 'deprecated and will be removed after Nova 13.0.0 is ' 'released. Use python-cinderclient or ' 'python-openstacksdk instead.', DeprecationWarning) - with self.alternate_service_type('volume'): + with self.alternate_service_type( + 'volumev2', allowed_types=('volume', 'volumev2')): if detailed is True: return self._list("/snapshots/detail", "snapshots") else: @@ -108,5 +112,6 @@ class SnapshotManager(base.ManagerWithFind): 'deprecated and will be removed after Nova 13.0.0 is ' 'released. Use python-cinderclient or ' 'python-openstacksdk instead.', DeprecationWarning) - with self.alternate_service_type('volume'): + with self.alternate_service_type( + 'volumev2', allowed_types=('volume', 'volumev2')): self._delete("/snapshots/%s" % base.getid(snapshot)) diff --git a/novaclient/v2/volume_types.py b/novaclient/v2/volume_types.py index 861c4a357..4cd629018 100644 --- a/novaclient/v2/volume_types.py +++ b/novaclient/v2/volume_types.py @@ -47,7 +47,8 @@ class VolumeTypeManager(base.ManagerWithFind): 'and will be removed after Nova 13.0.0 is released. Use ' 'python-cinderclient or python-openstacksdk instead.', DeprecationWarning) - with self.alternate_service_type('volume'): + with self.alternate_service_type( + 'volumev2', allowed_types=('volume', 'volumev2')): return self._list("/types", "volume_types") def get(self, volume_type): @@ -61,7 +62,8 @@ class VolumeTypeManager(base.ManagerWithFind): 'and will be removed after Nova 13.0.0 is released. Use ' 'python-cinderclient or python-openstacksdk instead.', DeprecationWarning) - with self.alternate_service_type('volume'): + with self.alternate_service_type( + 'volumev2', allowed_types=('volume', 'volumev2')): return self._get("/types/%s" % base.getid(volume_type), "volume_type") @@ -75,7 +77,8 @@ class VolumeTypeManager(base.ManagerWithFind): 'and will be removed after Nova 13.0.0 is released. Use ' 'python-cinderclient or python-openstacksdk instead.', DeprecationWarning) - with self.alternate_service_type('volume'): + with self.alternate_service_type( + 'volumev2', allowed_types=('volume', 'volumev2')): self._delete("/types/%s" % base.getid(volume_type)) def create(self, name): @@ -89,7 +92,8 @@ class VolumeTypeManager(base.ManagerWithFind): 'and will be removed after Nova 13.0.0 is released. Use ' 'python-cinderclient or python-openstacksdk instead.', DeprecationWarning) - with self.alternate_service_type('volume'): + with self.alternate_service_type( + 'volumev2', allowed_types=('volume', 'volumev2')): body = { "volume_type": { "name": name, diff --git a/novaclient/v2/volumes.py b/novaclient/v2/volumes.py index d0446c5fd..802cf500d 100644 --- a/novaclient/v2/volumes.py +++ b/novaclient/v2/volumes.py @@ -68,7 +68,8 @@ class VolumeManager(base.ManagerWithFind): '13.0.0 is released. Use python-cinderclient or ' 'python-openstacksdk instead.', DeprecationWarning) # NOTE(melwitt): Ensure we use the volume endpoint for this call - with self.alternate_service_type('volume'): + with self.alternate_service_type( + 'volumev2', allowed_types=('volume', 'volumev2')): body = {'volume': {'size': size, 'snapshot_id': snapshot_id, 'display_name': display_name, @@ -89,7 +90,8 @@ class VolumeManager(base.ManagerWithFind): 'method is deprecated and will be removed after Nova ' '13.0.0 is released. Use python-cinderclient or ' 'python-openstacksdk instead.', DeprecationWarning) - with self.alternate_service_type('volume'): + with self.alternate_service_type( + 'volumev2', allowed_types=('volume', 'volumev2')): return self._get("/volumes/%s" % volume_id, "volume") def list(self, detailed=True, search_opts=None): @@ -102,7 +104,8 @@ class VolumeManager(base.ManagerWithFind): 'method is deprecated and will be removed after Nova ' '13.0.0 is released. Use python-cinderclient or ' 'python-openstacksdk instead.', DeprecationWarning) - with self.alternate_service_type('volume'): + with self.alternate_service_type( + 'volumev2', allowed_types=('volume', 'volumev2')): search_opts = search_opts or {} if 'name' in search_opts.keys(): @@ -128,7 +131,8 @@ class VolumeManager(base.ManagerWithFind): 'method is deprecated and will be removed after Nova ' '13.0.0 is released. Use python-cinderclient or ' 'python-openstacksdk instead.', DeprecationWarning) - with self.alternate_service_type('volume'): + with self.alternate_service_type( + 'volumev2', allowed_types=('volume', 'volumev2')): self._delete("/volumes/%s" % base.getid(volume)) def create_server_volume(self, server_id, volume_id, device=None):