Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for connecting for Astra database clusters #33

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
dist: trusty
services:
- cassandra

Expand Down
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@
[![Code Climate](https://codeclimate.com/github/thegeorgeous/flask-cqlalchemy/badges/gpa.svg)](https://codeclimate.com/github/thegeorgeous/flask-cqlalchemy)


Flask-CQLAlchemy handles connections to Cassandra clusters
and gives a unified easier way to declare models and their
columns
Flask-CQLAlchemy handles connections to Cassandra and Astra clusters
and gives a unified easier way to declare models and their columns.

**Now with support for PyPy**

Expand Down Expand Up @@ -109,6 +108,9 @@ method
* `CASSANDRA_RETRY_CONNECT` - True if we should retry to connect even if there was
a connection failure initially
* `CASSANDRA_SETUP_KWARGS` - Pass-through keyword arguments for Cluster()
* `ASTRA_SECURE_CONNECT_BUNDLE` - Full path to the secure connect bundle for your Astra database.
* `CASSANDRA_USERNAME` - Username to used to connect to a Cassandra cluster.
* `CASSANDRA_PASSWORD` - Password to used to connect to a Cassandra cluster.

## Beta Features
Flask CQLAlchemy supports User Defined Types, provided you are using Cassandra
Expand Down
24 changes: 22 additions & 2 deletions flask_cqlalchemy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
from cassandra.cqlengine import models
from cassandra.cqlengine import usertype

from cassandra.cluster import Cluster
from cassandra.auth import PlainTextAuthProvider

try:
from flask import _app_ctx_stack as stack
except ImportError:
Expand Down Expand Up @@ -45,12 +48,29 @@ def init_app(self, app):
This method set all the config options for the connection to
the Cassandra cluster and creates a connection at startup.
"""
self._hosts_ = app.config['CASSANDRA_HOSTS']
self._keyspace_ = app.config['CASSANDRA_KEYSPACE']
self._hosts_ = app.config.get('CASSANDRA_HOSTS', '')
self._keyspace_ = app.config.get('CASSANDRA_KEYSPACE', '')
self._username = app.config.get('CASSANDRA_USERNAME', '')
self._password = app.config.get('CASSANDRA_PASSWORD', '')
self._cloud_bundle = app.config.get('ASTRA_SECURE_CONNECT_BUNDLE', '')
consistency = app.config.get('CASSANDRA_CONSISTENCY', 1)
lazy_connect = app.config.get('CASSANDRA_LAZY_CONNECT', False)
retry_connect = app.config.get('CASSANDRA_RETRY_CONNECT', False)
setup_kwargs = app.config.get('CASSANDRA_SETUP_KWARGS', {})
self._auth_provider = None

if self._username and self._password:
self._auth_provider = PlainTextAuthProvider(
username=self._username,
password=self._password
)

if self._cloud_bundle != None and self._auth_provider != None:
cloud = { 'secure_connect_bundle' : self._cloud_bundle }
cluster = Cluster(cloud=cloud, auth_provider=self._auth_provider)
session = cluster.connect(self._keyspace_)
connection.set_session(session)
return

if not self._hosts_ and self._keyspace_:
raise NoConfig("""No Configuration options defined.
Expand Down