filters: make get_domain_id more robust

- Return None if no domain name was given
- Raise a KeyError if the domain name wasn't found so lookup couldn't
  happen.

Change-Id: Iec0009eab5ae33b9dda1ffbabc9fbfc83b2c5909
This commit is contained in:
Emilien Macchi 2020-02-25 08:54:47 -05:00
parent bea0ae1cf9
commit a44c191e1a
3 changed files with 29 additions and 1 deletions

View File

@ -367,9 +367,12 @@ class FilterModule(object):
This filter taks in input a domain name and a dictionary with all
domain informations.
"""
if domain_name == '':
return
for d in all_domains:
if d.get('name') == domain_name:
return d.get('id')
raise KeyError('Could not get domain ID for "%s"' % domain_name)
def get_changed_containers(self, async_results):
"""Return a list of containers that changed.

View File

@ -21,7 +21,7 @@
project: "{{ lookup('dict', tripleo_keystone_resources_data_user).value.project | default(omit) }}"
# TODO(emilien) remove the custom filter once the module moved to OpenStack collections and we can make changes
domain: >-
{{ lookup('dict', tripleo_keystone_resources_data_user).value.domain | default(omit) |
{{ lookup('dict', tripleo_keystone_resources_data_user).value.domain | default('') |
get_domain_id(all_domains=openstack_domains) | default(omit) }}
role: "{{ batched_tripleo_keystone_resources_roles_data.key }}"
state: present

View File

@ -777,6 +777,31 @@ class TestHelperFilters(tests_base.TestCase):
result = self.filters.get_domain_id('heat_stack', openstack_domains)
self.assertEqual(result, 'fd85b560d4554fd8bf363728e4a3863e')
def test_get_domain_id_empty(self):
openstack_domains = []
result = self.filters.get_domain_id('', openstack_domains)
self.assertEqual(result, None)
def test_get_domain_id_not_found(self):
openstack_domains = [
{
"description": "The default domain",
"enabled": "true",
"id": "default",
"name": "Default"
},
{
"description": "The heat stack domain",
"enabled": "true",
"id": "fd85b560d4554fd8bf363728e4a3863e",
"name": "heat_stack"
}
]
self.assertRaises(
KeyError,
lambda: self.filters.get_domain_id('ghost', openstack_domains)
)
def test_get_changed_containers(self):
data = [
{