Add missing docstrings and examples
This commit is contained in:
@@ -37,6 +37,12 @@ def encode_dnsname(hostname):
|
|||||||
|
|
||||||
Raises:
|
Raises:
|
||||||
EncodingError: If hostname is invalid.
|
EncodingError: If hostname is invalid.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
>>> encode_dnsname("münchen")
|
||||||
|
'xn--mnchen-3ya'
|
||||||
|
>>> encode_dnsname("example.com.")
|
||||||
|
'example.com'
|
||||||
"""
|
"""
|
||||||
hostname = hostname.lower().strip()
|
hostname = hostname.lower().strip()
|
||||||
|
|
||||||
@@ -84,6 +90,24 @@ def encode_dnsname(hostname):
|
|||||||
|
|
||||||
|
|
||||||
def detect_ip_type(ip):
|
def detect_ip_type(ip):
|
||||||
|
"""
|
||||||
|
Detect IP address type and normalize.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
ip: IP address string.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Tuple of (record_type, normalized_ip).
|
||||||
|
|
||||||
|
Raises:
|
||||||
|
ValueError: If IP address is invalid.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
>>> detect_ip_type("192.168.1.1")
|
||||||
|
('A', '192.168.1.1')
|
||||||
|
>>> detect_ip_type("2001:db8::1")
|
||||||
|
('AAAA', '2001:db8::1')
|
||||||
|
"""
|
||||||
try:
|
try:
|
||||||
addr = ipaddress.ip_address(ip)
|
addr = ipaddress.ip_address(ip)
|
||||||
if isinstance(addr, ipaddress.IPv4Address):
|
if isinstance(addr, ipaddress.IPv4Address):
|
||||||
@@ -127,6 +151,16 @@ def parse_bind_key_file(path):
|
|||||||
|
|
||||||
Raises:
|
Raises:
|
||||||
DNSError: If parsing fails.
|
DNSError: If parsing fails.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
Key file contents::
|
||||||
|
|
||||||
|
key "ddns-key." {
|
||||||
|
algorithm hmac-sha256;
|
||||||
|
secret "base64secret==";
|
||||||
|
};
|
||||||
|
|
||||||
|
>>> keyring, algo = parse_bind_key_file("/etc/bind/ddns.key")
|
||||||
"""
|
"""
|
||||||
if not path:
|
if not path:
|
||||||
return None, None
|
return None, None
|
||||||
@@ -324,13 +358,17 @@ class DNSService:
|
|||||||
Update a DNS record for the given hostname.
|
Update a DNS record for the given hostname.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
hostname: Fully qualified hostname.
|
hostname: Hostname (without zone suffix).
|
||||||
zone: DNS zone name.
|
zone: DNS zone name.
|
||||||
ip: IP address to set.
|
ip: IP address to set.
|
||||||
ttl: DNS record TTL.
|
ttl: DNS record TTL.
|
||||||
|
|
||||||
Raises:
|
Raises:
|
||||||
DNSError: If update fails.
|
DNSError: If update fails.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
>>> dns_service.update_record("myhost", "example.com", "192.168.1.1", 60)
|
||||||
|
>>> dns_service.update_record("myhost", "example.com", "2001:db8::1", 60)
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
record_type, normalized_ip = detect_ip_type(ip)
|
record_type, normalized_ip = detect_ip_type(ip)
|
||||||
|
|||||||
@@ -19,6 +19,22 @@ from peewee import (
|
|||||||
)
|
)
|
||||||
from playhouse.pool import PooledMySQLDatabase
|
from playhouse.pool import PooledMySQLDatabase
|
||||||
|
|
||||||
|
# Re-export DoesNotExist and EncodingError for convenience
|
||||||
|
__all__ = [
|
||||||
|
'db',
|
||||||
|
'DATABASE_VERSION',
|
||||||
|
'User',
|
||||||
|
'Hostname',
|
||||||
|
'Version',
|
||||||
|
'init_database',
|
||||||
|
'create_tables',
|
||||||
|
'get_user',
|
||||||
|
'get_hostname',
|
||||||
|
'get_hostname_for_user',
|
||||||
|
'DoesNotExist',
|
||||||
|
'EncodingError',
|
||||||
|
]
|
||||||
|
|
||||||
# Database proxy (initialized later with actual backend)
|
# Database proxy (initialized later with actual backend)
|
||||||
db = DatabaseProxy()
|
db = DatabaseProxy()
|
||||||
|
|
||||||
@@ -269,6 +285,11 @@ def get_user(username: str):
|
|||||||
|
|
||||||
Raises:
|
Raises:
|
||||||
DoesNotExist: If user not found.
|
DoesNotExist: If user not found.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
>>> user = get_user("alice")
|
||||||
|
>>> print(user.email)
|
||||||
|
'alice@example.com'
|
||||||
"""
|
"""
|
||||||
return User.get(User.username == username)
|
return User.get(User.username == username)
|
||||||
|
|
||||||
@@ -286,6 +307,11 @@ def get_hostname(hostname, zone):
|
|||||||
|
|
||||||
Raises:
|
Raises:
|
||||||
DoesNotExist: If hostname not found.
|
DoesNotExist: If hostname not found.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
>>> host = get_hostname("myhost", "example.com")
|
||||||
|
>>> print(host.last_ipv4)
|
||||||
|
'192.168.1.1'
|
||||||
"""
|
"""
|
||||||
return Hostname.get(
|
return Hostname.get(
|
||||||
(Hostname.hostname == hostname) & (Hostname.zone == zone)
|
(Hostname.hostname == hostname) & (Hostname.zone == zone)
|
||||||
@@ -297,7 +323,7 @@ def get_hostname_for_user(hostname: str, user: User):
|
|||||||
Get hostname owned by specific user.
|
Get hostname owned by specific user.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
hostname: Hostname to look up.
|
hostname: Hostname to look up (FQDN).
|
||||||
user: User who should own the hostname.
|
user: User who should own the hostname.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
@@ -305,23 +331,10 @@ def get_hostname_for_user(hostname: str, user: User):
|
|||||||
|
|
||||||
Raises:
|
Raises:
|
||||||
DoesNotExist: If hostname not found or not owned by user.
|
DoesNotExist: If hostname not found or not owned by user.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
>>> user = get_user("alice")
|
||||||
|
>>> host = get_hostname_for_user("myhost.example.com", user)
|
||||||
"""
|
"""
|
||||||
fqdn = fn.Concat(Hostname.hostname, '.', Hostname.zone)
|
fqdn = fn.Concat(Hostname.hostname, '.', Hostname.zone)
|
||||||
return Hostname.get((fqdn == hostname) & (Hostname.user == user))
|
return Hostname.get((fqdn == hostname) & (Hostname.user == user))
|
||||||
|
|
||||||
|
|
||||||
# Re-export DoesNotExist and EncodingError for convenience
|
|
||||||
__all__ = [
|
|
||||||
'db',
|
|
||||||
'DATABASE_VERSION',
|
|
||||||
'User',
|
|
||||||
'Hostname',
|
|
||||||
'Version',
|
|
||||||
'init_database',
|
|
||||||
'create_tables',
|
|
||||||
'get_user',
|
|
||||||
'get_hostname',
|
|
||||||
'get_hostname_for_user',
|
|
||||||
'DoesNotExist',
|
|
||||||
'EncodingError',
|
|
||||||
]
|
|
||||||
|
|||||||
Reference in New Issue
Block a user