From 447d5d9e344060be4f284ad56b430b20eb190c2b Mon Sep 17 00:00:00 2001 From: Chen Hanxiao Date: Fri, 19 Jan 2018 14:30:18 +0800 Subject: 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 --- openstackclient/compute/v2/server.py | 45 ++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) (limited to 'openstackclient/compute') 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 @@ -442,6 +442,12 @@ class CreateServer(command.ShowOne): metavar='', help=_('Create server boot disk from this image (name or ID)'), ) + disk_group.add_argument( + '--image-property', + metavar='', + action=parseractions.KeyValueAction, + help=_("Image property to be matched"), + ) disk_group.add_argument( '--volume', metavar='', @@ -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: -- cgit v1.2.1