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

[((506, 519), 'dotenv.load_dotenv', 'load_dotenv', ([], {}), '()\n', (517, 519), False, 'from dotenv import load_dotenv\n'), ((531, 548), 'os.getenv', 'getenv', (['"""CASS_DB"""'], {}), "('CASS_DB')\n", (537, 548), False, 'from os import getenv\n'), ((565, 588), 'os.getenv', 'getenv', (['"""CASS_PASSWORD"""'], {}), "('CASS_PASSWORD')\n", (571, 588), False, 'from os import getenv\n'), ((601, 620), 'os.getenv', 'getenv', (['"""CASS_USER"""'], {}), "('CASS_USER')\n", (607, 620), False, 'from os import getenv\n'), ((663, 728), 'cassandra.auth.PlainTextAuthProvider', 'PlainTextAuthProvider', ([], {'username': 'cass_user', 'password': 'cass_password'}), '(username=cass_user, password=cass_password)\n', (684, 728), False, 'from cassandra.auth import PlainTextAuthProvider\n'), ((743, 790), 'cassandra.cluster.Cluster', 'Cluster', (['[cass_db]'], {'auth_provider': 'auth_provider'}), '([cass_db], auth_provider=auth_provider)\n', (750, 790), False, 'from cassandra.cluster import Cluster\n'), ((1080, 1174), 'cassandra.cqlengine.connection.setup', 'connection.setup', (['[cass_db]', '"""migration"""'], {'protocol_version': '(3)', 'auth_provider': 'auth_provider'}), "([cass_db], 'migration', protocol_version=3, auth_provider=\n    auth_provider)\n", (1096, 1174), False, 'from cassandra.cqlengine import connection\n'), ((1172, 1193), 'cassandra.cqlengine.management.sync_table', 'sync_table', (['BluePrint'], {}), '(BluePrint)\n', (1182, 1193), False, 'from cassandra.cqlengine.management import sync_table\n'), ((1198, 1218), 'cassandra.cqlengine.management.sync_table', 'sync_table', (['Discover'], {}), '(Discover)\n', (1208, 1218), False, 'from cassandra.cqlengine.management import sync_table\n'), ((1223, 1242), 'cassandra.cqlengine.management.sync_table', 'sync_table', (['Project'], {}), '(Project)\n', (1233, 1242), False, 'from cassandra.cqlengine.management import sync_table\n'), ((1247, 1266), 'cassandra.cqlengine.management.sync_table', 'sync_table', (['Network'], {}), '(Network)\n', (1257, 1266), False, 'from cassandra.cqlengine.management import sync_table\n'), ((1271, 1289), 'cassandra.cqlengine.management.sync_table', 'sync_table', (['Subnet'], {}), '(Subnet)\n', (1281, 1289), False, 'from cassandra.cqlengine.management import sync_table\n'), ((1294, 1313), 'cassandra.cqlengine.management.sync_table', 'sync_table', (['Storage'], {}), '(Storage)\n', (1304, 1313), False, 'from cassandra.cqlengine.management import sync_table\n'), ((1318, 1336), 'cassandra.cqlengine.management.sync_table', 'sync_table', (['Bucket'], {}), '(Bucket)\n', (1328, 1336), False, 'from cassandra.cqlengine.management import sync_table\n'), ((1341, 1362), 'cassandra.cqlengine.management.sync_table', 'sync_table', (['GcpBucket'], {}), '(GcpBucket)\n', (1351, 1362), False, 'from cassandra.cqlengine.management import sync_table\n'), ((1367, 1383), 'cassandra.cqlengine.management.sync_table', 'sync_table', (['User'], {}), '(User)\n', (1377, 1383), False, 'from cassandra.cqlengine.management import sync_table\n'), ((1388, 1404), 'cassandra.cqlengine.management.sync_table', 'sync_table', (['Disk'], {}), '(Disk)\n', (1398, 1404), False, 'from cassandra.cqlengine.management import sync_table\n')]