diff options
author | Takashi Natsume <takanattie@gmail.com> | 2021-05-06 17:31:18 +0900 |
---|---|---|
committer | Takashi Natsume <takanattie@gmail.com> | 2021-05-06 18:00:10 +0900 |
commit | 935fe75a660ac366f4780ef37658a599cdcc6a35 (patch) | |
tree | c3dd8fd166f2bf5c0024bd270a0a351a7a07ac7c | |
parent | 3dc9ad974e86bdf93177bc27e85978f943cc944c (diff) | |
download | python-novaclient-935fe75a660ac366f4780ef37658a599cdcc6a35.tar.gz |
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>
-rw-r--r-- | novaclient/v2/volumes.py | 51 |
1 files changed, 27 insertions, 24 deletions
diff --git a/novaclient/v2/volumes.py b/novaclient/v2/volumes.py index 7153c835..93fdd868 100644 --- a/novaclient/v2/volumes.py +++ b/novaclient/v2/volumes.py @@ -39,6 +39,20 @@ class VolumeManager(base.Manager): """ 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") 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) :rtype: :class:`Volume` """ - body = {'volumeAttachment': {'volumeId': volume_id}} - if device is not None: - body['volumeAttachment']['device'] = device - return self._create("/servers/%s/os-volume_attachments" % server_id, - body, "volumeAttachment") + return self._create( + "/servers/%s/os-volume_attachments" % server_id, + VolumeManager._get_request_body_for_create(volume_id, device), + "volumeAttachment") @api_versions.wraps("2.49", "2.78") def create_server_volume(self, server_id, volume_id, device=None, @@ -67,13 +80,10 @@ class VolumeManager(base.Manager): :param tag: The tag (optional) :rtype: :class:`Volume` """ - body = {'volumeAttachment': {'volumeId': volume_id}} - if device is not None: - body['volumeAttachment']['device'] = device - if tag is not None: - body['volumeAttachment']['tag'] = tag - return self._create("/servers/%s/os-volume_attachments" % server_id, - body, "volumeAttachment") + return self._create( + "/servers/%s/os-volume_attachments" % server_id, + VolumeManager._get_request_body_for_create(volume_id, device, tag), + "volumeAttachment") @api_versions.wraps("2.79") def create_server_volume(self, server_id, volume_id, device=None, @@ -90,18 +100,11 @@ class VolumeManager(base.Manager): (optional). :rtype: :class:`Volume` """ - # TODO(mriedem): Move this body construction into a private common - # helper method for all versions of create_server_volume to use. - 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 self._create("/servers/%s/os-volume_attachments" % server_id, - body, "volumeAttachment") + return self._create( + "/servers/%s/os-volume_attachments" % server_id, + VolumeManager._get_request_body_for_create(volume_id, device, tag, + delete_on_termination), + "volumeAttachment") @api_versions.wraps("2.0", "2.84") def update_server_volume(self, server_id, src_volid, dest_volid): |