Skip to content

Commit fbd2c00

Browse files
committed
Fix --image-property option in 'create server'
There was a problem that the '-image-property' option, which can be used to create an instance, did not work as intended. I found that there were two problems with this option. First, I cannot select an image as its metadata. The second is that when there are multiple images available, the desired image may not be selected depending on the situation. This patch solves these two problems. I wrote the test case with these two problems considered together. Change-Id: Ib2745d7e067056ff4ca8bfaf6cff492d0dacb73a story: #2007860
1 parent 95cc05b commit fbd2c00

3 files changed

Lines changed: 76 additions & 3 deletions

File tree

openstackclient/compute/v2/server.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -751,19 +751,27 @@ def _match_image(image_api, wanted_properties):
751751
images_matched = []
752752
for img in image_list:
753753
img_dict = {}
754+
754755
# exclude any unhashable entries
755-
for key, value in img.items():
756+
img_dict_items = list(img.items())
757+
if img.properties:
758+
img_dict_items.extend(list(img.properties.items()))
759+
for key, value in img_dict_items:
756760
try:
757761
set([key, value])
758762
except TypeError:
763+
if key != 'properties':
764+
LOG.debug('Skipped the \'%s\' attribute. '
765+
'That cannot be compared. '
766+
'(image: %s, value: %s)',
767+
key, img.id, value)
759768
pass
760769
else:
761770
img_dict[key] = value
771+
762772
if all(k in img_dict and img_dict[k] == v
763773
for k, v in wanted_properties.items()):
764774
images_matched.append(img)
765-
else:
766-
return []
767775
return images_matched
768776

769777
images = _match_image(image_client, parsed_args.image_property)

openstackclient/tests/unit/compute/v2/test_server.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2048,6 +2048,65 @@ def test_server_create_image_property_missed(self):
20482048
self.cmd.take_action,
20492049
parsed_args)
20502050

2051+
def test_server_create_image_property_with_image_list(self):
2052+
arglist = [
2053+
'--image-property',
2054+
'owner_specified.openstack.object=image/cirros',
2055+
'--flavor', 'flavor1',
2056+
'--nic', 'none',
2057+
self.new_server.name,
2058+
]
2059+
2060+
verifylist = [
2061+
('image_property',
2062+
{'owner_specified.openstack.object': 'image/cirros'}),
2063+
('flavor', 'flavor1'),
2064+
('nic', ['none']),
2065+
('server_name', self.new_server.name),
2066+
]
2067+
# create a image_info as the side_effect of the fake image_list()
2068+
image_info = {
2069+
'properties': {
2070+
'owner_specified.openstack.object': 'image/cirros'
2071+
}
2072+
}
2073+
2074+
target_image = image_fakes.FakeImage.create_one_image(image_info)
2075+
another_image = image_fakes.FakeImage.create_one_image({})
2076+
self.images_mock.return_value = [target_image, another_image]
2077+
2078+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
2079+
2080+
columns, data = self.cmd.take_action(parsed_args)
2081+
2082+
# Set expected values
2083+
kwargs = dict(
2084+
files={},
2085+
reservation_id=None,
2086+
min_count=1,
2087+
max_count=1,
2088+
security_groups=[],
2089+
userdata=None,
2090+
key_name=None,
2091+
availability_zone=None,
2092+
block_device_mapping_v2=[],
2093+
nics='none',
2094+
meta=None,
2095+
scheduler_hints={},
2096+
config_drive=None,
2097+
)
2098+
2099+
# ServerManager.create(name, image, flavor, **kwargs)
2100+
self.servers_mock.create.assert_called_with(
2101+
self.new_server.name,
2102+
target_image,
2103+
self.flavor,
2104+
**kwargs
2105+
)
2106+
2107+
self.assertEqual(self.columns, columns)
2108+
self.assertEqual(self.datalist(), data)
2109+
20512110
def test_server_create_invalid_hint(self):
20522111
# Not a key-value pair
20532112
arglist = [
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
features:
3+
- Support for image search via properties of image. Currently
4+
"openstack server create --image-property" only takes image property.
5+
Now it can also search image via properties (user defined) too.
6+
Story https://storyboard.openstack.org/#!/story/2007860.

0 commit comments

Comments
 (0)