2023-10-26 01:47:42 +00:00
|
|
|
import os
|
|
|
|
|
|
|
|
from cassandra.auth import PlainTextAuthProvider
|
2023-10-27 02:44:30 +00:00
|
|
|
from cassandra.cluster import Cluster
|
2023-10-26 01:47:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_cassandra_connection():
|
|
|
|
contact_points = [
|
|
|
|
cp.strip()
|
2023-10-27 02:44:30 +00:00
|
|
|
for cp in os.environ.get("CASSANDRA_CONTACT_POINTS", "").split(",")
|
2023-10-26 01:47:42 +00:00
|
|
|
if cp.strip()
|
|
|
|
]
|
|
|
|
CASSANDRA_KEYSPACE = os.environ["CASSANDRA_KEYSPACE"]
|
|
|
|
CASSANDRA_USERNAME = os.environ.get("CASSANDRA_USERNAME")
|
|
|
|
CASSANDRA_PASSWORD = os.environ.get("CASSANDRA_PASSWORD")
|
|
|
|
#
|
|
|
|
if CASSANDRA_USERNAME and CASSANDRA_PASSWORD:
|
|
|
|
auth_provider = PlainTextAuthProvider(
|
|
|
|
CASSANDRA_USERNAME,
|
|
|
|
CASSANDRA_PASSWORD,
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
auth_provider = None
|
|
|
|
|
2023-10-27 02:44:30 +00:00
|
|
|
c_cluster = Cluster(
|
|
|
|
contact_points if contact_points else None, auth_provider=auth_provider
|
|
|
|
)
|
2023-10-26 01:47:42 +00:00
|
|
|
session = c_cluster.connect()
|
|
|
|
return (session, CASSANDRA_KEYSPACE)
|