diff --git a/.travis.yml b/.travis.yml index cbbed6b..ca1b02f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,3 +1,4 @@ +dist: trusty services: - cassandra diff --git a/README.md b/README.md index 0506473..0333714 100644 --- a/README.md +++ b/README.md @@ -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** @@ -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 diff --git a/flask_cqlalchemy/__init__.py b/flask_cqlalchemy/__init__.py index 6f4ce5e..67e4cbd 100644 --- a/flask_cqlalchemy/__init__.py +++ b/flask_cqlalchemy/__init__.py @@ -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: @@ -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.