# Info: KCL CoreDNS task schemas for provisioning (Provisioning)
# Author: JesusPerezLorenzo 
# Release: 0.0.4
# Date: 29-12-2023

import regex

schema CoreDNSForward:
   source: str = "."
   forward_ip?: str

   check:
      forward_ip == Undefined or regex.match(forward_ip,"^\$.*$") or regex.match(forward_ip, "^((25[0-5]|2[0-4][0-9]|[0-1]?[0-9]?[0-9])\.){3}(25[0-5]|2[0-4][0-9]|[0-1]?[0-9]?[0-9])$"), "'network_private_ip = ${forward_ip}' check value definition (use $vaule or xx.xx.xx.xx)"

schema CoreDNSRecord:
   name: str
   ttl: int = 300
   rectype: "A" | "AAAA" | "CNAME" | "MX" | "TXT" | "NS" | "SOA" | "SRV" | "PTR" 
   server_pos?: int
   source?: str
   target_ip?: str
   # use for SRV like: "0 0 2380" (priority weight port)
   # user for MX like: "300 ip-or-FQND" (priority target)
   # user for TXT or CNAME with target content 
   value?: str
   comment?: str
   etcd_dns_ttl: int = 300 
   etcd_peer_port: int = 2380
   etcd_cli_port: int = 2379   

   check:
      target_ip == Undefined or regex.match(target_ip,"^\$.*$") or regex.match(target_ip, "^((25[0-5]|2[0-4][0-9]|[0-1]?[0-9]?[0-9])\.){3}(25[0-5]|2[0-4][0-9]|[0-1]?[0-9]?[0-9])$"), "'target_ip = ${target_ip}' check value definition (use $value or xx.xx.xx.xx)"

schema NameServer: 
   ns_ip: str
   check:
      ns_ip == Undefined or regex.match(ns_ip,"^\$.*$") or regex.match(ns_ip, "^((25[0-5]|2[0-4][0-9]|[0-1]?[0-9]?[0-9])\.){3}(25[0-5]|2[0-4][0-9]|[0-1]?[0-9]?[0-9])$"), "'ns_ip = ${ns_ip}' check value definition (use $value or xx.xx.xx.xx)"

schema CoreDNSEntry: 
   domain: str
   port: int = 53
   file?: str
   records?: [CoreDNSRecord]
   forward?: CoreDNSForward
   use_log: bool = True
   use_errors: bool = True
   use_cache: bool = True
   etcd_cluster_name: str = ""

schema COREDNS:
   version: str
   name: str
   hostname: str
   etc_corefile: str = "/etc/coredns/Corefile"
   nameservers: [NameServer]
   domains_search: str = ""
   entries: [ CoreDNSEntry ]
