Skip to content

Commit 6a6a5ef

Browse files
Jenkinsopenstack-gerrit
authored andcommitted
Merge "Allow --file in image-create with v2 Image API"
2 parents acb1659 + 8e02b2a commit 6a6a5ef

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

glanceclient/v2/shell.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ def get_image_schema():
4141
@utils.arg('--property', metavar="<key=value>", action='append',
4242
default=[], help=('Arbitrary property to associate with image.'
4343
' May be used multiple times.'))
44+
@utils.arg('--file', metavar='<FILE>',
45+
help='Local file to save downloaded image data to. '
46+
'If this is not specified the image data will be '
47+
'written to stdout.')
48+
@utils.arg('--progress', action='store_true', default=False,
49+
help='Show upload progress bar.')
4450
def do_image_create(gc, args):
4551
"""Create a new image."""
4652
schema = gc.schemas.get("image")
@@ -55,8 +61,19 @@ def do_image_create(gc, args):
5561
key, value = datum.split('=', 1)
5662
fields[key] = value
5763

64+
file_name = fields.pop('file', None)
65+
if file_name is not None and os.access(file_name, os.R_OK) is False:
66+
utils.exit("File %s does not exist or user does not have read "
67+
"privileges to it" % file_name)
5868
image = gc.images.create(**fields)
59-
utils.print_image(image)
69+
try:
70+
if file_name is not None:
71+
args.id = image['id']
72+
args.size = None
73+
do_image_upload(gc, args)
74+
image = gc.images.get(args.id)
75+
finally:
76+
utils.print_image(image)
6077

6178

6279
@utils.arg('id', metavar='<IMAGE_ID>', help='ID of image to update.')

tests/v2/test_shell_v2.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
# under the License.
1616
import json
1717
import mock
18+
import os
19+
import tempfile
1820
import testtools
1921

2022
from glanceclient.common import utils
@@ -150,6 +152,47 @@ def test_do_image_create_no_user_props(self):
150152
'id': 'pass', 'name': 'IMG-01', 'disk_format': 'vhd',
151153
'container_format': 'bare'})
152154

155+
def test_do_image_create_with_file(self):
156+
try:
157+
file_name = None
158+
with open(tempfile.mktemp(), 'w+') as f:
159+
f.write('Some data here')
160+
f.flush()
161+
f.seek(0)
162+
file_name = f.name
163+
temp_args = {'name': 'IMG-01',
164+
'disk_format': 'vhd',
165+
'container_format': 'bare',
166+
'file': file_name,
167+
'progress': False}
168+
args = self._make_args(temp_args)
169+
with mock.patch.object(self.gc.images, 'create') as mocked_create:
170+
with mock.patch.object(self.gc.images, 'get') as mocked_get:
171+
172+
ignore_fields = ['self', 'access', 'schema']
173+
expect_image = dict([(field, field) for field in
174+
ignore_fields])
175+
expect_image['id'] = 'pass'
176+
expect_image['name'] = 'IMG-01'
177+
expect_image['disk_format'] = 'vhd'
178+
expect_image['container_format'] = 'bare'
179+
mocked_create.return_value = expect_image
180+
mocked_get.return_value = expect_image
181+
182+
test_shell.do_image_create(self.gc, args)
183+
184+
temp_args.pop('file', None)
185+
mocked_create.assert_called_once_with(**temp_args)
186+
mocked_get.assert_called_once_with('pass')
187+
utils.print_dict.assert_called_once_with({
188+
'id': 'pass', 'name': 'IMG-01', 'disk_format': 'vhd',
189+
'container_format': 'bare'})
190+
finally:
191+
try:
192+
os.remove(f.name)
193+
except Exception:
194+
pass
195+
153196
def test_do_image_create_with_user_props(self):
154197
args = self._make_args({'name': 'IMG-01',
155198
'property': ['myprop=myval']})

0 commit comments

Comments
 (0)