Abstract out the name of the name key

Heat and Cinder each have different key names for their "name" concept.
Rather than keeping a list of them, make an argument so we can override
it in the instances we need to.

Change-Id: I6ba0b69129c74e976ea6762728a940b4670f3873
This commit is contained in:
Monty Taylor 2015-09-13 20:35:08 +02:00 committed by David Shrewsbury
parent 7aa043ec87
commit 0bb5192f0c
2 changed files with 11 additions and 8 deletions

View File

@ -960,7 +960,8 @@ class OpenStackCloud(object):
def search_volumes(self, name_or_id=None, filters=None):
volumes = self.list_volumes()
return _utils._filter_list(volumes, name_or_id, filters)
return _utils._filter_list(
volumes, name_or_id, filters, name_key='display_name')
def search_flavors(self, name_or_id=None, filters=None):
flavors = self.list_flavors()

View File

@ -37,13 +37,14 @@ def _iterate_timeout(timeout, message, wait=2):
raise exc.OpenStackCloudTimeout(message)
def _filter_list(data, name_or_id, filters):
def _filter_list(data, name_or_id, filters, name_key='name'):
"""Filter a list by name/ID and arbitrary meta data.
:param list data:
The list of dictionary data to filter. It is expected that
each dictionary contains an 'id', 'name' (or 'display_name')
key if a value for name_or_id is given.
each dictionary contains an 'id' and 'name'
key if a value for name_or_id is given. The 'name' key can be
overridden with the name_key parameter.
:param string name_or_id:
The name or ID of the entity being filtered.
:param dict filters:
@ -56,15 +57,16 @@ def _filter_list(data, name_or_id, filters):
'gender': 'Female'
}
}
:param string name_key:
The name of the name key. Cinder wants display_name. Heat wants
stack_name. Defaults to 'name'
"""
if name_or_id:
identifier_matches = []
for e in data:
e_id = str(e.get('id', None))
e_name = e.get('name', None)
# cinder likes to be different and use display_name
e_display_name = e.get('display_name', None)
if str(name_or_id) in (e_id, e_name, e_display_name):
e_name = e.get(name_key, None)
if str(name_or_id) in (e_id, e_name):
identifier_matches.append(e)
data = identifier_matches