mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2025-07-21 15:51:10 +02:00
Set entity name as default domain instead of application_id
This commit is contained in:
parent
4cbd29735f
commit
257d0c4673
@ -1,4 +1,9 @@
|
|||||||
from ansible.errors import AnsibleFilterError
|
from ansible.errors import AnsibleFilterError
|
||||||
|
import sys
|
||||||
|
import os
|
||||||
|
|
||||||
|
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
|
||||||
|
from module_utils.entity_name_utils import get_entity_name
|
||||||
|
|
||||||
class FilterModule(object):
|
class FilterModule(object):
|
||||||
def filters(self):
|
def filters(self):
|
||||||
@ -13,19 +18,20 @@ class FilterModule(object):
|
|||||||
seen_domains = {}
|
seen_domains = {}
|
||||||
|
|
||||||
for app_id, cfg in apps.items():
|
for app_id, cfg in apps.items():
|
||||||
if not isinstance(cfg, dict):
|
if app_id.startswith(("web-","svc-")):
|
||||||
raise AnsibleFilterError(
|
if not isinstance(cfg, dict):
|
||||||
f"Invalid configuration for application '{app_id}': "
|
raise AnsibleFilterError(
|
||||||
f"expected a dict, got {cfg!r}"
|
f"Invalid configuration for application '{app_id}': "
|
||||||
)
|
f"expected a dict, got {cfg!r}"
|
||||||
|
)
|
||||||
domains_cfg = cfg.get('domains')
|
|
||||||
if not domains_cfg or 'canonical' not in domains_cfg:
|
domains_cfg = cfg.get('domains')
|
||||||
self._add_default_domain(app_id, primary_domain, seen_domains, result)
|
if not domains_cfg or 'canonical' not in domains_cfg:
|
||||||
continue
|
self._add_default_domain(app_id, primary_domain, seen_domains, result)
|
||||||
|
continue
|
||||||
|
|
||||||
canonical_domains = domains_cfg['canonical']
|
canonical_domains = domains_cfg['canonical']
|
||||||
self._process_canonical_domains(app_id, canonical_domains, seen_domains, result)
|
self._process_canonical_domains(app_id, canonical_domains, seen_domains, result)
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
@ -34,7 +40,8 @@ class FilterModule(object):
|
|||||||
Add the default domain for an application if no canonical domains are defined.
|
Add the default domain for an application if no canonical domains are defined.
|
||||||
Ensures the domain is unique across applications.
|
Ensures the domain is unique across applications.
|
||||||
"""
|
"""
|
||||||
default_domain = f"{app_id}.{primary_domain}"
|
entity_name = get_entity_name(app_id)
|
||||||
|
default_domain = f"{entity_name}.{primary_domain}"
|
||||||
if default_domain in seen_domains:
|
if default_domain in seen_domains:
|
||||||
raise AnsibleFilterError(
|
raise AnsibleFilterError(
|
||||||
f"Domain '{default_domain}' is already configured for "
|
f"Domain '{default_domain}' is already configured for "
|
||||||
|
@ -24,39 +24,39 @@ class TestDomainFilters(unittest.TestCase):
|
|||||||
self.assertEqual(result, expected)
|
self.assertEqual(result, expected)
|
||||||
|
|
||||||
def test_canonical_without_domains(self):
|
def test_canonical_without_domains(self):
|
||||||
apps = {'app1': {}}
|
apps = {'web-app-app1': {}}
|
||||||
expected = {'app1': ['app1.example.com']}
|
expected = {'web-app-app1': ['app1.example.com']}
|
||||||
result = self.filter_module.canonical_domains_map(apps, self.primary)
|
result = self.filter_module.canonical_domains_map(apps, self.primary)
|
||||||
self.assertEqual(result, expected)
|
self.assertEqual(result, expected)
|
||||||
|
|
||||||
def test_canonical_with_list(self):
|
def test_canonical_with_list(self):
|
||||||
apps = {
|
apps = {
|
||||||
'app1': {
|
'web-app-app1': {
|
||||||
'domains': {'canonical': ['foo.com', 'bar.com']}
|
'domains': {'canonical': ['foo.com', 'bar.com']}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
result = self.filter_module.canonical_domains_map(apps, self.primary)
|
result = self.filter_module.canonical_domains_map(apps, self.primary)
|
||||||
self.assertCountEqual(
|
self.assertCountEqual(
|
||||||
result['app1'],
|
result['web-app-app1'],
|
||||||
['foo.com', 'bar.com']
|
['foo.com', 'bar.com']
|
||||||
)
|
)
|
||||||
|
|
||||||
def test_canonical_with_dict(self):
|
def test_canonical_with_dict(self):
|
||||||
apps = {
|
apps = {
|
||||||
'app1': {
|
'web-app-app1': {
|
||||||
'domains': {'canonical': {'one': 'one.com', 'two': 'two.com'}}
|
'domains': {'canonical': {'one': 'one.com', 'two': 'two.com'}}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
result = self.filter_module.canonical_domains_map(apps, self.primary)
|
result = self.filter_module.canonical_domains_map(apps, self.primary)
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
result['app1'],
|
result['web-app-app1'],
|
||||||
{'one': 'one.com', 'two': 'two.com'}
|
{'one': 'one.com', 'two': 'two.com'}
|
||||||
)
|
)
|
||||||
|
|
||||||
def test_canonical_duplicate_raises(self):
|
def test_canonical_duplicate_raises(self):
|
||||||
apps = {
|
apps = {
|
||||||
'app1': {'domains': {'canonical': ['dup.com']}},
|
'web-app-app1': {'domains': {'canonical': ['dup.com']}},
|
||||||
'app2': {'domains': {'canonical': ['dup.com']}},
|
'web-app-app2': {'domains': {'canonical': ['dup.com']}},
|
||||||
}
|
}
|
||||||
with self.assertRaises(AnsibleFilterError) as cm:
|
with self.assertRaises(AnsibleFilterError) as cm:
|
||||||
self.filter_module.canonical_domains_map(apps, self.primary)
|
self.filter_module.canonical_domains_map(apps, self.primary)
|
||||||
@ -65,10 +65,48 @@ class TestDomainFilters(unittest.TestCase):
|
|||||||
|
|
||||||
def test_invalid_canonical_type(self):
|
def test_invalid_canonical_type(self):
|
||||||
apps = {
|
apps = {
|
||||||
'app1': {'domains': {'canonical': 123}}
|
'web-app-app1': {'domains': {'canonical': 123}}
|
||||||
}
|
}
|
||||||
with self.assertRaises(AnsibleFilterError):
|
with self.assertRaises(AnsibleFilterError):
|
||||||
self.filter_module.canonical_domains_map(apps, self.primary)
|
self.filter_module.canonical_domains_map(apps, self.primary)
|
||||||
|
|
||||||
|
def test_non_web_apps_are_ignored(self):
|
||||||
|
"""
|
||||||
|
Applications not starting with 'web-' should be skipped entirely,
|
||||||
|
resulting in an empty mapping when only non-web apps are provided.
|
||||||
|
"""
|
||||||
|
apps = {
|
||||||
|
'db-app-app1': {'domains': {'canonical': ['db.example.com']}},
|
||||||
|
'service-app-app2': {}
|
||||||
|
}
|
||||||
|
result = self.filter_module.canonical_domains_map(apps, self.primary)
|
||||||
|
self.assertEqual(result, {})
|
||||||
|
|
||||||
|
def test_mixed_web_and_non_web_apps(self):
|
||||||
|
"""
|
||||||
|
Only 'web-' prefixed applications should be processed;
|
||||||
|
non-web apps should be ignored alongside valid web apps.
|
||||||
|
"""
|
||||||
|
apps = {
|
||||||
|
'db-app-app1': {'domains': {'canonical': ['db.example.com']}},
|
||||||
|
'web-app-app1': {}
|
||||||
|
}
|
||||||
|
expected = {'web-app-app1': ['app1.example.com']}
|
||||||
|
result = self.filter_module.canonical_domains_map(apps, self.primary)
|
||||||
|
self.assertEqual(result, expected)
|
||||||
|
|
||||||
|
def test_non_web_invalid_config_no_error(self):
|
||||||
|
"""
|
||||||
|
Invalid configurations for non-web apps should not raise errors
|
||||||
|
since they are ignored by the filter.
|
||||||
|
"""
|
||||||
|
apps = {
|
||||||
|
'nonweb-app-app1': 'not-a-dict',
|
||||||
|
'another': 12345
|
||||||
|
}
|
||||||
|
# Should simply return an empty result without exceptions
|
||||||
|
result = self.filter_module.canonical_domains_map(apps, self.primary)
|
||||||
|
self.assertEqual(result, {})
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
unittest.main()
|
unittest.main()
|
Loading…
x
Reference in New Issue
Block a user