Merge "Add --volume option to image create command"
This commit is contained in:
commit
3293138121
openstackclient
@ -111,6 +111,19 @@ class CreateImage(show.ShowOne):
|
||||
help="Similar to --location, but this indicates that the image"
|
||||
" 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",
|
||||
@ -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)))
|
||||
|
||||
|
||||
|
@ -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):
|
||||
|
Loading…
x
Reference in New Issue
Block a user