from mongoengine import *
from dotenv import load_dotenv
from os import getenv
from cassandra.cluster import Cluster
from cassandra.auth import PlainTextAuthProvider
from cassandra.cqlengine import connection
from cassandra.cqlengine.management import sync_table
from cassandra.query import ordered_dict_factory
from model.discover import *
from model.blueprint import *
from model.disk import *
from model.storage import *
from model.project import *
from model.network import *
from model.user import *

load_dotenv()

cass_db = getenv("CASS_DB")
cass_password = getenv("CASS_PASSWORD")
cass_user = getenv("CASS_USER")

def create_db_con():
    auth_provider = PlainTextAuthProvider(username=cass_user, password=cass_password)
    cluster = Cluster([cass_db],auth_provider=auth_provider)
    session = cluster.connect()
    session.execute("""
        CREATE KEYSPACE IF NOT EXISTS migration
        WITH replication = { 'class': 'SimpleStrategy', 'replication_factor': '2' }
        """)
    session.set_keyspace('migration')
    session.row_factory = ordered_dict_factory
    connection.setup([cass_db], "migration",protocol_version=3,auth_provider=auth_provider)
    sync_table(BluePrint)
    sync_table(Discover)
    sync_table(Project)
    sync_table(Network)
    sync_table(Subnet)
    sync_table(Storage)
    sync_table(Bucket)
    sync_table(GcpBucket)
    sync_table(User)
    sync_table(Disk)
    session.execute("CREATE INDEX IF NOT EXISTS ON blue_print (network);")
    session.execute("CREATE INDEX IF NOT EXISTS ON blue_print (subnet);")
    return session

[(17, 'dotenv.load_dotenv', 'load_dotenv', False, 'from dotenv import load_dotenv\n'), (19, 'os.getenv', 'getenv', False, 'from os import getenv\n'), (20, 'os.getenv', 'getenv', False, 'from os import getenv\n'), (21, 'os.getenv', 'getenv', False, 'from os import getenv\n'), (24, 'cassandra.auth.PlainTextAuthProvider', 'PlainTextAuthProvider', False, 'from cassandra.auth import PlainTextAuthProvider\n'), (25, 'cassandra.cluster.Cluster', 'Cluster', False, 'from cassandra.cluster import Cluster\n'), (33, 'cassandra.cqlengine.connection.setup', 'connection.setup', False, 'from cassandra.cqlengine import connection\n'), (34, 'cassandra.cqlengine.management.sync_table', 'sync_table', False, 'from cassandra.cqlengine.management import sync_table\n'), (35, 'cassandra.cqlengine.management.sync_table', 'sync_table', False, 'from cassandra.cqlengine.management import sync_table\n'), (36, 'cassandra.cqlengine.management.sync_table', 'sync_table', False, 'from cassandra.cqlengine.management import sync_table\n'), (37, 'cassandra.cqlengine.management.sync_table', 'sync_table', False, 'from cassandra.cqlengine.management import sync_table\n'), (38, 'cassandra.cqlengine.management.sync_table', 'sync_table', False, 'from cassandra.cqlengine.management import sync_table\n'), (39, 'cassandra.cqlengine.management.sync_table', 'sync_table', False, 'from cassandra.cqlengine.management import sync_table\n'), (40, 'cassandra.cqlengine.management.sync_table', 'sync_table', False, 'from cassandra.cqlengine.management import sync_table\n'), (41, 'cassandra.cqlengine.management.sync_table', 'sync_table', False, 'from cassandra.cqlengine.management import sync_table\n'), (42, 'cassandra.cqlengine.management.sync_table', 'sync_table', False, 'from cassandra.cqlengine.management import sync_table\n'), (43, 'cassandra.cqlengine.management.sync_table', 'sync_table', False, 'from cassandra.cqlengine.management import sync_table\n')]