mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2025-05-16 01:37:19 +02:00
Optimized defaults_redirect_domain_mappings for dynamic creating by roles
This commit is contained in:
parent
d0844ce44f
commit
026855b197
@ -1,2 +0,0 @@
|
|||||||
# Todos
|
|
||||||
- Refactor all 4 functions to one
|
|
37
filter_plugins/redirect_filters.py
Normal file
37
filter_plugins/redirect_filters.py
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
# roles/<your-role>/filter_plugins/redirect_filters.py
|
||||||
|
from ansible.errors import AnsibleFilterError
|
||||||
|
|
||||||
|
class FilterModule(object):
|
||||||
|
"""
|
||||||
|
Custom filters for redirect domain mappings
|
||||||
|
"""
|
||||||
|
|
||||||
|
def filters(self):
|
||||||
|
return {
|
||||||
|
"add_redirect_if_group": self.add_redirect_if_group,
|
||||||
|
}
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def add_redirect_if_group(redirect_list, group, source, target, group_names):
|
||||||
|
"""
|
||||||
|
Append {"source": source, "target": target} to *redirect_list*
|
||||||
|
**only** if *group* is contained in *group_names*.
|
||||||
|
|
||||||
|
Usage in Jinja:
|
||||||
|
{{ redirect_list
|
||||||
|
| add_redirect_if_group('lam',
|
||||||
|
'ldap.' ~ primary_domain,
|
||||||
|
domains.lam,
|
||||||
|
group_names) }}
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
# Make a copy so we don’t mutate the original list in place
|
||||||
|
redirects = list(redirect_list)
|
||||||
|
|
||||||
|
if group in group_names:
|
||||||
|
redirects.append({"source": source, "target": target})
|
||||||
|
|
||||||
|
return redirects
|
||||||
|
|
||||||
|
except Exception as exc:
|
||||||
|
raise AnsibleFilterError(f"add_redirect_if_group failed: {exc}")
|
@ -53,31 +53,31 @@ defaults_domains:
|
|||||||
- "blog.{{primary_domain}}"
|
- "blog.{{primary_domain}}"
|
||||||
|
|
||||||
## Domain Redirects
|
## Domain Redirects
|
||||||
defaults_redirect_domain_mappings:
|
defaults_redirect_domain_mappings: >-
|
||||||
- { source: "akaunting.{{primary_domain}}", target: "{{domains.akaunting}}" }
|
{{ []
|
||||||
- { source: "bbb.{{primary_domain}}", target: "{{domains.bigbluebutton}}" }
|
| add_redirect_if_group('akaunting', "akaunting." ~ primary_domain, domains.akaunting, group_names)
|
||||||
- { source: "discourse.{{primary_domain}}", target: "{{domains.discourse}}" }
|
| add_redirect_if_group('bigbluebutton', "bbb." ~ primary_domain, domains.bigbluebutton, group_names)
|
||||||
- { source: "crm.{{primary_domain}}", target: "{{domains.espocrm}}" }
|
| add_redirect_if_group('discourse', "discourse." ~ primary_domain, domains.discourse, group_names)
|
||||||
- { source: "funkwhale.{{primary_domain}}", target: "{{domains.funkwhale}}" }
|
| add_redirect_if_group('espocrm', "crm." ~ primary_domain, domains.espocrm, group_names)
|
||||||
- { source: "gitea.{{primary_domain}}", target: "{{domains.gitea}}" }
|
| add_redirect_if_group('funkwhale', "funkwhale." ~ primary_domain, domains.funkwhale, group_names)
|
||||||
- { source: "keycloak.{{primary_domain}}", target: "{{domains.keycloak}}" }
|
| add_redirect_if_group('gitea', "gitea." ~ primary_domain, domains.gitea, group_names)
|
||||||
- {
|
| add_redirect_if_group('keycloak', "keycloak." ~ primary_domain, domains.keycloak, group_names)
|
||||||
source: "{{ domains.ldap }}",
|
| add_redirect_if_group('lam', domains.ldap, domains.lam, group_names)
|
||||||
target: "{% if 'lam' in group_names %}{{ domains.lam }}{% elif 'phpmyldapadmin' in group_names %}{{ domains.phpmyldap }}{% else %}{{ primary_domain }}{% endif %}"
|
| add_redirect_if_group('phpmyldapadmin', domains.ldap, domains.phpmyldap, group_names)
|
||||||
}
|
| add_redirect_if_group('listmonk', "listmonk." ~ primary_domain, domains.listmonk, group_names)
|
||||||
- { source: "listmonk.{{primary_domain}}", target: "{{domains.listmonk}}" }
|
| add_redirect_if_group('mailu', "mailu." ~ primary_domain, domains.mailu, group_names)
|
||||||
- { source: "mailu.{{primary_domain}}", target: "{{domains.mailu}}" }
|
| add_redirect_if_group('moodle', "moodle." ~ primary_domain, domains.moodle, group_names)
|
||||||
- { source: "moodle.{{primary_domain}}", target: "{{domains.moodle}}" }
|
| add_redirect_if_group('nextcloud', "nextcloud." ~ primary_domain, domains.nextcloud, group_names)
|
||||||
- { source: "nextcloud.{{primary_domain}}", target: "{{domains.nextcloud}}" }
|
| add_redirect_if_group('openproject', "openproject." ~ primary_domain, domains.openproject, group_names)
|
||||||
- { source: "openproject.{{primary_domain}}", target: "{{domains.openproject}}" }
|
| add_redirect_if_group('peertube', "peertube." ~ primary_domain, domains.peertube, group_names)
|
||||||
- { source: "peertube.{{primary_domain}}", target: "{{domains.peertube}}" }
|
| add_redirect_if_group('pixelfed', "pictures." ~ primary_domain, domains.pixelfed, group_names)
|
||||||
- { source: "pictures.{{primary_domain}}", target: "{{domains.pixelfed}}" }
|
| add_redirect_if_group('pixelfed', "pixelfed." ~ primary_domain, domains.pixelfed, group_names)
|
||||||
- { source: "pixelfed.{{primary_domain}}", target: "{{domains.pixelfed}}" }
|
| add_redirect_if_group('yourls', "short." ~ primary_domain, domains.yourls, group_names)
|
||||||
- { source: "short.{{primary_domain}}", target: "{{domains.yourls}}" }
|
| add_redirect_if_group('snipe-it', "snipe-it." ~ primary_domain, domains.snipe_it, group_names)
|
||||||
- { source: "snipe-it.{{primary_domain}}", target: "{{domains.snipe_it}}" }
|
| add_redirect_if_group('taiga', "taiga." ~ primary_domain, domains.taiga, group_names)
|
||||||
- { source: "taiga.{{primary_domain}}", target: "{{domains.taiga}}" }
|
| add_redirect_if_group('peertube', "videos." ~ primary_domain, domains.peertube, group_names)
|
||||||
- { source: "videos.{{primary_domain}}", target: "{{domains.peertube}}" }
|
| add_redirect_if_group('wordpress', "wordpress." ~ primary_domain, domains.wordpress[0], group_names)
|
||||||
- { source: "wordpress.{{primary_domain}}", target: "{{domains.wordpress[0]}}" }
|
}}
|
||||||
|
|
||||||
# Domains which are deprecated and should be cleaned up
|
# Domains which are deprecated and should be cleaned up
|
||||||
deprecated_domains: []
|
deprecated_domains: []
|
53
tests/unit/test_redirect_filters.py
Normal file
53
tests/unit/test_redirect_filters.py
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
import os
|
||||||
|
import sys
|
||||||
|
import unittest
|
||||||
|
|
||||||
|
PROJECT_ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), "../../.."))
|
||||||
|
sys.path.insert(0, PROJECT_ROOT)
|
||||||
|
|
||||||
|
from filter_plugins.redirect_filters import FilterModule
|
||||||
|
|
||||||
|
|
||||||
|
class TestAddRedirectIfGroup(unittest.TestCase):
|
||||||
|
"""Unit-tests for the add_redirect_if_group filter."""
|
||||||
|
|
||||||
|
def setUp(self):
|
||||||
|
# Obtain the callable once for reuse
|
||||||
|
self.add_redirect = FilterModule().filters()["add_redirect_if_group"]
|
||||||
|
|
||||||
|
def test_appends_redirect_when_group_present(self):
|
||||||
|
original = [{"source": "a", "target": "b"}]
|
||||||
|
result = self.add_redirect(
|
||||||
|
original,
|
||||||
|
group="lam",
|
||||||
|
source="ldap.example.com",
|
||||||
|
target="lam.example.com",
|
||||||
|
group_names=["lam", "other"],
|
||||||
|
)
|
||||||
|
|
||||||
|
# Original list must stay unchanged
|
||||||
|
self.assertEqual(len(original), 1)
|
||||||
|
# Result list must contain the extra entry
|
||||||
|
self.assertEqual(len(result), 2)
|
||||||
|
self.assertIn(
|
||||||
|
{"source": "ldap.example.com", "target": "lam.example.com"}, result
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_keeps_list_unchanged_when_group_absent(self):
|
||||||
|
original = [{"source": "a", "target": "b"}]
|
||||||
|
result = self.add_redirect(
|
||||||
|
original,
|
||||||
|
group="lam",
|
||||||
|
source="ldap.example.com",
|
||||||
|
target="lam.example.com",
|
||||||
|
group_names=["unrelated"],
|
||||||
|
)
|
||||||
|
|
||||||
|
# No new entries
|
||||||
|
self.assertEqual(result, original)
|
||||||
|
# But ensure a new list object was returned (no in-place mutation)
|
||||||
|
self.assertIsNot(result, original)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
unittest.main()
|
Loading…
x
Reference in New Issue
Block a user