Refactor constructing request body

Add a private static method to construct a request body
for create requests in the
novaclient.v2.volumes.VolumeManager class.

Change-Id: I884ad4b471e3d196255901499c75a1a2f0535f65
Signed-off-by: Takashi Natsume <takanattie@gmail.com>
This commit is contained in:
Takashi Natsume 2021-05-06 17:31:18 +09:00
parent 3dc9ad974e
commit 935fe75a66
1 changed files with 27 additions and 24 deletions

View File

@ -39,6 +39,20 @@ class VolumeManager(base.Manager):
""" """
resource_class = Volume resource_class = Volume
@staticmethod
def _get_request_body_for_create(volume_id, device=None, tag=None,
delete_on_termination=False):
body = {'volumeAttachment': {'volumeId': volume_id}}
if device is not None:
body['volumeAttachment']['device'] = device
if tag is not None:
body['volumeAttachment']['tag'] = tag
if delete_on_termination:
body['volumeAttachment']['delete_on_termination'] = (
delete_on_termination)
return body
@api_versions.wraps("2.0", "2.48") @api_versions.wraps("2.0", "2.48")
def create_server_volume(self, server_id, volume_id, device=None): def create_server_volume(self, server_id, volume_id, device=None):
""" """
@ -49,11 +63,10 @@ class VolumeManager(base.Manager):
:param device: The device name (optional) :param device: The device name (optional)
:rtype: :class:`Volume` :rtype: :class:`Volume`
""" """
body = {'volumeAttachment': {'volumeId': volume_id}} return self._create(
if device is not None: "/servers/%s/os-volume_attachments" % server_id,
body['volumeAttachment']['device'] = device VolumeManager._get_request_body_for_create(volume_id, device),
return self._create("/servers/%s/os-volume_attachments" % server_id, "volumeAttachment")
body, "volumeAttachment")
@api_versions.wraps("2.49", "2.78") @api_versions.wraps("2.49", "2.78")
def create_server_volume(self, server_id, volume_id, device=None, def create_server_volume(self, server_id, volume_id, device=None,
@ -67,13 +80,10 @@ class VolumeManager(base.Manager):
:param tag: The tag (optional) :param tag: The tag (optional)
:rtype: :class:`Volume` :rtype: :class:`Volume`
""" """
body = {'volumeAttachment': {'volumeId': volume_id}} return self._create(
if device is not None: "/servers/%s/os-volume_attachments" % server_id,
body['volumeAttachment']['device'] = device VolumeManager._get_request_body_for_create(volume_id, device, tag),
if tag is not None: "volumeAttachment")
body['volumeAttachment']['tag'] = tag
return self._create("/servers/%s/os-volume_attachments" % server_id,
body, "volumeAttachment")
@api_versions.wraps("2.79") @api_versions.wraps("2.79")
def create_server_volume(self, server_id, volume_id, device=None, def create_server_volume(self, server_id, volume_id, device=None,
@ -90,18 +100,11 @@ class VolumeManager(base.Manager):
(optional). (optional).
:rtype: :class:`Volume` :rtype: :class:`Volume`
""" """
# TODO(mriedem): Move this body construction into a private common return self._create(
# helper method for all versions of create_server_volume to use. "/servers/%s/os-volume_attachments" % server_id,
body = {'volumeAttachment': {'volumeId': volume_id}} VolumeManager._get_request_body_for_create(volume_id, device, tag,
if device is not None: delete_on_termination),
body['volumeAttachment']['device'] = device "volumeAttachment")
if tag is not None:
body['volumeAttachment']['tag'] = tag
if delete_on_termination:
body['volumeAttachment']['delete_on_termination'] = (
delete_on_termination)
return self._create("/servers/%s/os-volume_attachments" % server_id,
body, "volumeAttachment")
@api_versions.wraps("2.0", "2.84") @api_versions.wraps("2.0", "2.84")
def update_server_volume(self, server_id, src_volid, dest_volid): def update_server_volume(self, server_id, src_volid, dest_volid):