From 9a064c3d5c09bca9c5cb7e9ffc4277ac053820e0 Mon Sep 17 00:00:00 2001 From: Avishay Traeger Date: Wed, 16 Jan 2013 15:45:29 +0200 Subject: [PATCH] Add upload-to-image function to client. Support uploading a volume to an image in the image service. Change-Id: Ief996f92aaf6d7a4fee0bce5ca45b92bee3f6990 Fixes: bug #1099866 --- cinderclient/v1/shell.py | 34 +++++++++++++++++++++++++++++++++- cinderclient/v1/volumes.py | 22 ++++++++++++++++++++++ tests/test_shell.py | 4 ++-- 3 files changed, 57 insertions(+), 3 deletions(-) diff --git a/cinderclient/v1/shell.py b/cinderclient/v1/shell.py index 670bd13fb..651235370 100644 --- a/cinderclient/v1/shell.py +++ b/cinderclient/v1/shell.py @@ -356,7 +356,7 @@ def do_snapshot_show(cs, args): @utils.arg('--force', metavar='', help='Optional flag to indicate whether ' - 'to snapshot a volume even if its ' + 'to snapshot a volume even if it\'s ' 'attached to an instance. (Default=False)', default=False) @utils.arg( @@ -600,3 +600,35 @@ def _print_type_extra_specs(vol_type): def _find_volume_type(cs, vtype): """Get a volume type by name or ID.""" return utils.find_resource(cs.volume_types, vtype) + + +@utils.arg('volume_id', + metavar='', + help='ID of the volume to snapshot') +@utils.arg('--force', + metavar='', + help='Optional flag to indicate whether ' + 'to upload a volume even if it\'s ' + 'attached to an instance. (Default=False)', + default=False) +@utils.arg('--container-format', + metavar='', + help='Optional type for container format ' + '(Default=bare)', + default='bare') +@utils.arg('--disk-format', + metavar='', + help='Optional type for disk format ' + '(Default=raw)', + default='raw') +@utils.arg('image_name', + metavar='', + help='Name for created image') +@utils.service_type('volume') +def do_upload_to_image(cs, args): + """Upload volume to image service as image.""" + volume = _find_volume(cs, args.volume_id) + volume.upload_to_image(args.force, + args.image_name, + args.container_format, + args.disk_format) diff --git a/cinderclient/v1/volumes.py b/cinderclient/v1/volumes.py index 203c0f3d2..930435494 100644 --- a/cinderclient/v1/volumes.py +++ b/cinderclient/v1/volumes.py @@ -104,6 +104,14 @@ class Volume(base.Resource): """ return self.manager.set_metadata(self, metadata) + def upload_to_image(self, force, image_name, container_format, + disk_format): + """ + Upload a volume to image service as an image. + """ + self.manager.upload_to_image(self, force, image_name, container_format, + disk_format) + class VolumeManager(base.ManagerWithFind): """ @@ -316,3 +324,17 @@ class VolumeManager(base.ManagerWithFind): """ for k in keys: self._delete("/volumes/%s/metadata/%s" % (base.getid(volume), k)) + + def upload_to_image(self, volume, force, image_name, container_format, + disk_format): + """ + Upload volume to image service as image. + + :param volume: The :class:`Volume` to upload. + """ + return self._action('os-volume_upload_image', + volume, + {'force': force, + 'image_name': image_name, + 'container_format': container_format, + 'disk_format': disk_format}) diff --git a/tests/test_shell.py b/tests/test_shell.py index ce3d4294a..580dd25f7 100644 --- a/tests/test_shell.py +++ b/tests/test_shell.py @@ -55,7 +55,7 @@ class ShellTest(utils.TestCase): help_text = self.shell('help') for r in required: self.assertThat(help_text, - matchers.MatchesRegex(r, re.DOTALL|re.MULTILINE)) + matchers.MatchesRegex(r, re.DOTALL | re.MULTILINE)) def test_help_on_subcommand(self): required = [ @@ -65,4 +65,4 @@ class ShellTest(utils.TestCase): help_text = self.shell('help list') for r in required: self.assertThat(help_text, - matchers.MatchesRegex(r, re.DOTALL|re.MULTILINE)) + matchers.MatchesRegex(r, re.DOTALL | re.MULTILINE))