from diagrams import Cluster, Diagram, Edge

from diagrams.azure.database import SQLDatabases, DatabaseForMariadbServers
from diagrams.onprem.inmemory import Redis
from diagrams.onprem.network import Internet
from diagrams.onprem.groupware import Nextcloud
from diagrams.onprem.monitoring import Grafana, Prometheus
from diagrams.onprem.storage import Ceph, CephOsd
from diagrams.onprem.network import Haproxy
from diagrams.onprem.database import Mariadb

from diagrams.digitalocean.network import LoadBalancer

import diagrams.generic.storage as storage_1

from diagrams.azure.identity import Groups
from diagrams.custom import Custom

with Diagram(filename="./assets/dia_nc_dev_improved", show=True, direction="TB"):
    out = Internet("Intranet")

    with Cluster("Load Balancing"):
        openstack_lb = Custom("", "../assets/vagrant.png")
        lb = [Haproxy("SSL proxy 1"), Haproxy("SSL proxy 2")]

    with Cluster("Web server"):
        openstack_web = Custom("", "../assets/vagrant.png")
        nc_frontend = [ Nextcloud(), Nextcloud()]     

    with Cluster("Caching"):
        openstack_cach = Custom("", "../assets/vagrant.png")
        redis = [Redis("Redis 1"), Redis("Redis 2")]

    with Cluster("Control / Monitor"):
        openstack_ctrl = Custom("", "../assets/vagrant.png")
        ctrl = [Prometheus(), Grafana()]

    with Cluster("Database cluster"):
        Custom("", "../assets/vagrant.png")

        with Cluster("Load balancer"):
            paSQL = Custom("ProxySQL Active", "../assets/proxysql.png")
            ppSQL = Custom("ProxySQL Passive", "../assets/proxysql.png")
        
        with Cluster("Galera cluster"):
            bckp1 = DatabaseForMariadbServers("Backup writer")
            primary = Mariadb("Primary writer")
            bckp2 = DatabaseForMariadbServers("Backup writer")            
    
        paSQL - Edge(color="firebrick") - primary
        bckp1 - Edge(style="dashed") - paSQL - Edge(style="dashed") - bckp2
        
        ppSQL - Edge(color="firebrick") - primary
        bckp1 - Edge(style="dashed") - ppSQL - Edge(style="dashed") - bckp2

    openstack_cach << nc_frontend
    paSQL << openstack_web >> ppSQL  
    openstack_ctrl << openstack_lb >> openstack_web
    out >> lb
    openstack_web >> openstack_ctrl << paSQL