names: Specify priority order among domain types

- This can be used to ensure that apps that can deal with only a single domain
auto-configure the best domain by default.

Tests:

- Unit tests run.

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
Sunil Mohan Adapa 2025-01-17 16:04:09 -08:00 committed by James Valleroy
parent 49d16412f8
commit 045b336a9b
No known key found for this signature in database
GPG Key ID: 77C0C75E7B650808
7 changed files with 16 additions and 8 deletions

View File

@ -61,7 +61,7 @@ class AvahiApp(app_module.App):
domain_type = DomainType('domain-type-local',
_('Local Network Domain'),
configuration_url='names:hostname',
can_have_certificate=False)
can_have_certificate=False, priority=10)
self.add(domain_type)
firewall = Firewall('firewall-avahi', info.name, ports=['mdns'],

View File

@ -78,7 +78,7 @@ class DynamicDNSApp(app_module.App):
edit_url='dynamicdns:domain-edit',
delete_url='dynamicdns:domain-delete',
add_url='dynamicdns:domain-add',
can_have_certificate=True)
can_have_certificate=True, priority=70)
self.add(domain_type)
users_and_groups = UsersAndGroups('users-and-groups-dynamicdns',

View File

@ -67,7 +67,7 @@ class NamesApp(app_module.App):
domain_type = DomainType('domain-type-static', _('Domain (regular)'),
delete_url='names:domain-delete',
add_url='names:domain-add',
can_have_certificate=True)
can_have_certificate=True, priority=100)
self.add(domain_type)
daemon = ResolvedDaemon('daemon-names', 'systemd-resolved')

View File

@ -46,8 +46,8 @@ class DomainType(app.FollowerComponent):
def __init__(self, component_id: str, display_name: str,
configuration_url: str | None = None,
edit_url: str | None = None, delete_url: str | None = None,
add_url: str | None = None,
can_have_certificate: bool = True):
add_url: str | None = None, can_have_certificate: bool = True,
priority: int = 50):
"""Initialize the domain type component.
component_id should be a unique ID across all components of an app and
@ -73,6 +73,10 @@ class DomainType(app.FollowerComponent):
can_have_certificate indicates if this type of domain can have a TLS
certificate that can be validated by a typical browser.
priority decides which domain will be used (by default) when an app can
only deal with a single domain. The domain with highest value will be
chosen by the app by default.
"""
super().__init__(component_id)
@ -82,6 +86,7 @@ class DomainType(app.FollowerComponent):
self.delete_url = delete_url
self.add_url = add_url
self.can_have_certificate = can_have_certificate
self.priority = priority
self._all[component_id] = self

View File

@ -26,13 +26,15 @@ def test_domain_type_init():
"""Test initialization of domain type object."""
component = DomainType('test-component', 'x-display-name',
configuration_url='config_url', edit_url='edit_url',
delete_url='delete_url', add_url='add_url')
delete_url='delete_url', add_url='add_url',
priority=80)
assert component.component_id == 'test-component'
assert component.display_name == 'x-display-name'
assert component.configuration_url == 'config_url'
assert component.edit_url == 'edit_url'
assert component.delete_url == 'delete_url'
assert component.add_url == 'add_url'
assert component.priority == 80
assert component.can_have_certificate
assert len(DomainType._all)
assert DomainType._all['test-component'] == component
@ -43,6 +45,7 @@ def test_domain_type_init():
assert component.edit_url is None
assert component.delete_url is None
assert component.add_url is None
assert component.priority == 50
assert not component.can_have_certificate

View File

@ -71,7 +71,7 @@ class PagekiteApp(app_module.App):
domain_type = DomainType('domain-type-pagekite', _('PageKite Domain'),
configuration_url='pagekite:index',
can_have_certificate=True)
can_have_certificate=True, priority=40)
self.add(domain_type)
daemon = Daemon('daemon-pagekite', self.DAEMON)

View File

@ -76,7 +76,7 @@ class TorApp(app_module.App):
domain_type = DomainType('domain-type-tor', _('Tor Onion Service'),
configuration_url='tor:index',
can_have_certificate=False)
can_have_certificate=False, priority=30)
self.add(domain_type)
firewall = Firewall('firewall-tor-relay', _('Tor Bridge Relay'),