summaryrefslogtreecommitdiff
path: root/openstackclient
diff options
context:
space:
mode:
authorJenkins <jenkins@review.openstack.org>2014-03-08 03:50:54 +0000
committerGerrit Code Review <review@openstack.org>2014-03-08 03:50:54 +0000
commit3293138121c0ad1b91d63eae219c9aae41d758c3 (patch)
treeb17900deb94f37e9e7974c0f565f1ca8e843f013 /openstackclient
parentb03680aea5a3759e38d8b46b0c99df65738f98d7 (diff)
parente6e0dbf754c4dbc631e5c797b50d8032481a1a27 (diff)
downloadpython-openstackclient-3293138121c0ad1b91d63eae219c9aae41d758c3.tar.gz
Merge "Add --volume option to image create command"
Diffstat (limited to 'openstackclient')
-rw-r--r--openstackclient/image/v1/image.py59
-rw-r--r--openstackclient/tests/image/v1/test_image.py76
2 files changed, 119 insertions, 16 deletions
diff --git a/openstackclient/image/v1/image.py b/openstackclient/image/v1/image.py
index 026b583c..8c1501bd 100644
--- a/openstackclient/image/v1/image.py
+++ b/openstackclient/image/v1/image.py
@@ -112,6 +112,19 @@ class CreateImage(show.ShowOne):
" should immediately be copied from the data store",
)
parser.add_argument(
+ "--volume",
+ metavar="<volume>",
+ help="Create the image from the specified volume",
+ )
+ parser.add_argument(
+ "--force",
+ dest='force',
+ action='store_true',
+ default=False,
+ help="If the image is created from a volume, force creation of the"
+ " image even if volume is in use.",
+ )
+ parser.add_argument(
"--property",
dest="properties",
metavar="<key=value>",
@@ -162,7 +175,9 @@ class CreateImage(show.ShowOne):
args.pop("variables")
if "location" not in args and "copy_from" not in args:
- if "file" in args:
+ if "volume" in args:
+ pass
+ elif "file" in args:
args["data"] = open(args.pop("file"), "rb")
else:
args["data"] = None
@@ -171,23 +186,35 @@ class CreateImage(show.ShowOne):
msvcrt.setmode(sys.stdin.fileno(), os.O_BINARY)
args["data"] = sys.stdin
- image_client = self.app.client_manager.image
- try:
- image = utils.find_resource(
- image_client.images,
- parsed_args.name,
- )
- except exceptions.CommandError:
- # This is normal for a create or reserve (create w/o an image)
- image = image_client.images.create(**args)
+ if "volume" in args:
+ volume_client = self.app.client_manager.volume
+ source_volume = utils.find_resource(volume_client.volumes,
+ parsed_args.volume)
+ response, body = volume_client.volumes.upload_to_image(
+ source_volume,
+ parsed_args.force,
+ parsed_args.name,
+ parsed_args.container_format,
+ parsed_args.disk_format)
+ info = body['os-volume_upload_image']
else:
- # It must be an update
- # If an image is specified via --file, --location or --copy-from
- # let the API handle it
- image = image_client.images.update(image, **args)
+ image_client = self.app.client_manager.image
+ try:
+ image = utils.find_resource(
+ image_client.images,
+ parsed_args.name,
+ )
+ except exceptions.CommandError:
+ # This is normal for a create or reserve (create w/o an image)
+ image = image_client.images.create(**args)
+ else:
+ # It must be an update
+ # If an image is specified via --file, --location or
+ # --copy-from let the API handle it
+ image = image_client.images.update(image, **args)
- info = {}
- info.update(image._info)
+ info = {}
+ info.update(image._info)
return zip(*sorted(six.iteritems(info)))
diff --git a/openstackclient/tests/image/v1/test_image.py b/openstackclient/tests/image/v1/test_image.py
index a410674d..d7547f76 100644
--- a/openstackclient/tests/image/v1/test_image.py
+++ b/openstackclient/tests/image/v1/test_image.py
@@ -14,6 +14,7 @@
#
import copy
+import mock
from openstackclient.image.v1 import image
from openstackclient.tests import fakes
@@ -30,6 +31,81 @@ class TestImage(image_fakes.TestImagev1):
self.images_mock.reset_mock()
+class TestImageCreate(TestImage):
+
+ def setUp(self):
+ super(TestImageCreate, self).setUp()
+ self.images_mock.get.return_value = fakes.FakeResource(
+ None,
+ copy.deepcopy(image_fakes.IMAGE),
+ loaded=True,
+ )
+ self.cmd = image.CreateImage(self.app, None)
+
+ def test_create_volume(self):
+ arglist = [
+ '--volume', 'volly',
+ image_fakes.image_name,
+ ]
+ verifylist = [
+ ('volume', 'volly'),
+ ('name', image_fakes.image_name),
+ ]
+ parsed_args = self.check_parser(self.cmd, arglist, verifylist)
+ self.app.client_manager.volume = mock.Mock()
+ self.app.client_manager.volume.volumes = mock.Mock()
+ volumes = self.app.client_manager.volume.volumes
+ volumes.upload_to_image = mock.Mock()
+ response = {"id": 'volume_id',
+ "updated_at": 'updated_at',
+ "status": 'uploading',
+ "display_description": 'desc',
+ "size": 'size',
+ "volume_type": 'volume_type',
+ "image_id": 'image1',
+ "container_format": parsed_args.container_format,
+ "disk_format": parsed_args.disk_format,
+ "image_name": parsed_args.name}
+ full_response = {"os-volume_upload_image": response}
+ volumes.upload_to_image.return_value = (201, full_response)
+ volume_resource = fakes.FakeResource(
+ None,
+ copy.deepcopy({'id': 'vol1', 'name': 'volly'}),
+ loaded=True,
+ )
+ volumes.get.return_value = volume_resource
+ results = self.cmd.take_action(parsed_args)
+ volumes.upload_to_image.assert_called_with(
+ volume_resource,
+ False,
+ image_fakes.image_name,
+ 'bare',
+ 'raw',
+ )
+ expects = [('container_format',
+ 'disk_format',
+ 'display_description',
+ 'id',
+ 'image_id',
+ 'image_name',
+ 'size',
+ 'status',
+ 'updated_at',
+ 'volume_type'),
+ ('bare',
+ 'raw',
+ 'desc',
+ 'volume_id',
+ 'image1',
+ 'graven',
+ 'size',
+ 'uploading',
+ 'updated_at',
+ 'volume_type')]
+ for expected, result in zip(expects, results):
+ self.assertEqual(expected, result)
+
+
class TestImageDelete(TestImage):
def setUp(self):