Home > Net >  Can't connect DataStax AstraDB with Django app on Google Cloud
Can't connect DataStax AstraDB with Django app on Google Cloud

Time:10-05

I am trying to connect AstraDB with my application served by gcloud AppEngine. I am using Django and therefore have used django_cassandra_engine. I want to keep Postgres as my default DB and use cassandra as a second DB. Everything works as expected on localhost,but when I deploy to gcloud, I receive 502 Bad gateway error and in the logs it says:

cassandra.cqlengine.CQLEngineException: Connection name 'cassandra' doesn't exist in the registry.

I am using:

Django==4.1
django-cassandra-engine==1.7.0
cassandra-driver==3.25.0

My secure_connect_bundle (ZIP file) is in the same folder where manage.py is located. This is my settings.py:

# [START db_setup]
# [START gaestd_py_django_database_config]
# Use django-environ to parse the connection string
DATABASES = {
    "default": env.db(),
    'cassandra': {
        'ENGINE': 'django_cassandra_engine',
        'NAME': 'the_keyspace',
        'TEST_NAME': 'test_db',
        'OPTIONS': {
            'connection': {
                'auth_provider': PlainTextAuthProvider(username=env("ASTRA_CLIENT_ID"),password=env("ASTRA_SECRET")),
                'cloud': {
                    'secure_connect_bundle': os.path.join(BASE_DIR, "secure-connect-db.zip")
                }
            }
        }
    }  
    }

# If the flag as been set, configure to use proxy
if os.getenv("USE_CLOUD_SQL_AUTH_PROXY", None):
    DATABASES["default"]["HOST"] = "127.0.0.1"
    DATABASES["default"]["PORT"] = 5432

# [END gaestd_py_django_database_config]
# [END db_setup]

# Use a in-memory sqlite3 database when testing in CI systems
# TODO(glasnt) CHECK IF THIS IS REQUIRED because we're setting a val above
if os.getenv("TRAMPOLINE_CI", None):
    DATABASES = {
        "default": {
            "ENGINE": "django.db.backends.sqlite3",
            "NAME": os.path.join(BASE_DIR, "db.sqlite3"),
        }
    }

CodePudding user response:

Just a thought, but I don't think you need the extra cassandra group in the DATABASES section. Try this:

DATABASES = {
    "default": {
        'ENGINE': 'django_cassandra_engine',
        'NAME': 'brondau_keyspace',

We (DataStax) actually ran a workshop showing Django and Astra DB in December: https://www.youtube.com/watch?v=tUF_dX0lgt0

The Git repo can be found here: Build A Blog in Python with Django and Astra DB NoSQL Database

This section of the repo should help with your current issue: https://github.com/tomitokko/django-blog-with-astradb/blob/ddc1c2d608e8fc0d6569796775cdfaf503afab94/techblog/settings.py#L92

Anyway, give my suggestion a try and check the video and/or repo for additional help.

CodePudding user response:

So the problem was that secure_connect_bundle (ZIP file) couldn't be extracted on AppEngine. Setting 'use_default_tempdir' to True in cloud_config solves the issue. If you are using python driver to establish connection, it should look like this:

cloud_config= {
        'secure_connect_bundle': ASTRADB_CONNECT_BUNDLE,
        'use_default_tempdir': True
}

Or if you use django_cassandra_engine:

'OPTIONS': {
    'connection': {
        'auth_provider': PlainTextAuthProvider(username=env("ASTRA_CLIENT_ID"),password=env("ASTRA_SECRET")),
        'cloud': {
            'secure_connect_bundle': 'secure-brondau-db.zip',
            'use_default_tempdir': True
        }
    }
}
  • Related