]> mj.ucw.cz Git - pynsc.git/blobdiff - nsconfig/core.py
Implement alias zones
[pynsc.git] / nsconfig / core.py
index f8c96615c1246d0dfb4df1d9800189e42c1effd8..50fa325b526e9ae925d1b4791cc233fc2a4764e3 100644 (file)
@@ -193,6 +193,7 @@ class NscZoneState:
 class ZoneType(Enum):
     primary = auto()
     secondary = auto()
+    alias = auto()
 
 
 class NscZone:
@@ -216,6 +217,9 @@ class NscZone:
     def process(self) -> None:
         pass
 
+    def is_changed(self) -> bool:
+        return False
+
 
 class NscZonePrimary(NscZone):
     zone: Zone
@@ -223,6 +227,7 @@ class NscZonePrimary(NscZone):
     state_file: Path
     state: NscZoneState
     prev_state: NscZoneState
+    aliases: List['NscZoneAlias']
 
     def __init__(self, *args, **kwargs) -> None:
         super().__init__(*args, **kwargs)
@@ -235,6 +240,8 @@ class NscZonePrimary(NscZone):
         self.prev_state = NscZoneState()
         self.prev_state.load(self.state_file)
 
+        self.aliases = []
+
         self.zone = dns.zone.Zone(origin=self.name, rdclass=RdataClass.IN)
         self.update_soa()
 
@@ -383,6 +390,21 @@ class NscZoneSecondary(NscZone):
         self.secondary_file = self.nsc.secondary_dir / self.safe_name
 
 
+class NscZoneAlias(NscZone):
+    alias_for: NscZonePrimary
+
+    def __init__(self, *args, alias_for=NscZonePrimary, **kwargs) -> None:
+        assert isinstance(alias_for, NscZonePrimary)
+        super().__init__(*args, **kwargs)
+        self.zone_type = ZoneType.alias
+        self.alias_for = alias_for
+        self.zone_file = alias_for.zone_file
+        alias_for.aliases.append(self)
+
+    def is_changed(self) -> bool:
+        return self.alias_for.is_changed()
+
+
 class Nsc:
     start_time: datetime
     zones: Dict[str, NscZone]
@@ -422,6 +444,7 @@ class Nsc:
     def add_zone(self,
                  name: Optional[str] = None,
                  reverse_for: str | IPNetwork | None = None,
+                 alias_for: Optional[NscZonePrimary] = None,
                  follow_primary: str | IPAddress | None = None,
                  inherit_config: Optional[NscZoneConfig] = None,
                  **kwargs) -> Zone:
@@ -436,7 +459,10 @@ class Nsc:
         assert name not in self.zones
 
         z: NscZone
-        if follow_primary is None:
+        if alias_for is not None:
+            assert follow_primary is None
+            z = NscZoneAlias(self, name, reverse_for=reverse_for, alias_for=alias_for, inherit_config=inherit_config, **kwargs)
+        elif follow_primary is None:
             z = NscZonePrimary(self, name, reverse_for=reverse_for, inherit_config=inherit_config, **kwargs)
         else:
             if isinstance(follow_primary, str):