summaryrefslogtreecommitdiff
path: root/openstackclient/compute
diff options
context:
space:
mode:
authorChen Hanxiao <chenhx@certusnet.com.cn>2018-01-19 14:30:18 +0800
committerChen Hanxiao <chenhx@certusnet.com.cn>2018-03-28 16:03:45 +0800
commit447d5d9e344060be4f284ad56b430b20eb190c2b (patch)
treea5321ea94d9118b42761af2148139ef7dfa4b5bc /openstackclient/compute
parent3f99dbac342b52ccecc78c32f54047b18726e320 (diff)
downloadpython-openstackclient-447d5d9e344060be4f284ad56b430b20eb190c2b.tar.gz
Add --image-property parameter in 'server create'
add --image-property option, just like --image-with of novaclient did. Change-Id: Ic1a8976559255529a8785b1b301a0307812433cb Signed-off-by: Chen Hanxiao <chenhx@certusnet.com.cn>
Diffstat (limited to 'openstackclient/compute')
-rw-r--r--openstackclient/compute/v2/server.py45
1 files changed, 45 insertions, 0 deletions
diff --git a/openstackclient/compute/v2/server.py b/openstackclient/compute/v2/server.py
index 85c20aee..f60cbe56 100644
--- a/openstackclient/compute/v2/server.py
+++ b/openstackclient/compute/v2/server.py
@@ -443,6 +443,12 @@ class CreateServer(command.ShowOne):
help=_('Create server boot disk from this image (name or ID)'),
)
disk_group.add_argument(
+ '--image-property',
+ metavar='<key=value>',
+ action=parseractions.KeyValueAction,
+ help=_("Image property to be matched"),
+ )
+ disk_group.add_argument(
'--volume',
metavar='<volume>',
help=_('Create server using this volume as the boot disk (name '
@@ -610,6 +616,45 @@ class CreateServer(command.ShowOne):
parsed_args.image,
)
+ if not image and parsed_args.image_property:
+ def emit_duplicated_warning(img, image_property):
+ img_uuid_list = [str(image.id) for image in img]
+ LOG.warning(_('Multiple matching images: %(img_uuid_list)s\n'
+ 'Using image: %(chosen_one)s') %
+ {'img_uuid_list': img_uuid_list,
+ 'chosen_one': img_uuid_list[0]})
+
+ def _match_image(image_api, wanted_properties):
+ image_list = image_api.image_list()
+ images_matched = []
+ for img in image_list:
+ img_dict = {}
+ # exclude any unhashable entries
+ for key, value in img.items():
+ try:
+ set([key, value])
+ except TypeError:
+ pass
+ else:
+ img_dict[key] = value
+ if all(k in img_dict and img_dict[k] == v
+ for k, v in wanted_properties.items()):
+ images_matched.append(img)
+ else:
+ return []
+ return images_matched
+
+ images = _match_image(image_client.api, parsed_args.image_property)
+ if len(images) > 1:
+ emit_duplicated_warning(images,
+ parsed_args.image_property)
+ if images:
+ image = images[0]
+ else:
+ raise exceptions.CommandError(_("No images match the "
+ "property expected by "
+ "--image-property"))
+
# Lookup parsed_args.volume
volume = None
if parsed_args.volume: