# Info: KCL Local provider defaults schemas for provisioning (Provisioning)
# Author: JesusPerezLorenzo 
# Release: 0.0.4
# Date: 15-12-2023
import regex
import provisioning

schema Storage_local(provisioning.Storage):
    """
    Local Storage settings
    """
    volname: str = ""
    labels: str = ""
    encrypted: bool = False

    check:
        len(volname) > 0, "Check volume name"

schema ServerDefaults_local(provisioning.ServerDefaults):
    """
    Local Server Defaults settings
    """
    provider: "local" = "local"
    # Local provision data settings
    prov_settings: str = "defs/local_data.k"
    # Local provision data settings clean 
    prov_settings_clean: bool = False
    not_use: bool = False
    time_zone: str = "UTC"
    # Local Plan name
    plan?: str
    storage_os?: str
    storages?: [Storage_local]
    #storage_size: int
    # Add one or more SSH keys to the admin account. Accepted values are SSH public keys or filenames from
    # where to read the keys.
    # ssh public key to be included in /root/.ssh/authorized_keys
    ssh_key_path?: str
    # Public certificate must be created or imported as a key_name 
    # use: providers/aws/bin/on-ssh.sh (add -h to get info)
    ssh_key_name?: str
    # To use private network, IPs will be set in servers items
    priv_cidr_block?: str = "10.11.1.0/24"
    # ssh_key_mode: rewrite
    #network_utility_ipv4: bool = True
    #network_utility_ipv6: bool = False
    #network_public_ipv4?: bool = True
    #network_public_ipv6?: bool = False
    network_private_name?: str = "Private Network"
    liveness_ip?: str = "$network_public_ip"
    liveness_port: int = 22

    # Labels to describe the server in `key: value` format, multiple can be declared.
    # Usage: env: dev
    labels: str = "{Key=cluster,Value=k8s}"
    user: str = "root"

    check:
        len(user) > 0, "Check user value"
        priv_cidr_block == Undefined or regex.match(priv_cidr_block, "^(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}\/(?:3[0-2]|[0-2]?[0-9])$"), "'priv_cidr_block = ${priv_cidr_block}' check value definition"
        liveness_ip == Undefined or regex.match(liveness_ip, "^\$.*$") or regex.match(liveness_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])$"), "'liveness_ip = ${liveness_ip}' check value definition (use $vaule or xx.xx.xx.xx)"

