From 14d46fdaa2ffa1fedf354dc6b94d7e1fcccf1bcb Mon Sep 17 00:00:00 2001 From: Ranger Date: Wed, 21 Oct 2020 23:06:51 -0400 Subject: [PATCH 1/3] Added support for connecting for Astra database clusters --- README.md | 8 +++++--- flask_cqlalchemy/__init__.py | 22 +++++++++++++++++++++- 2 files changed, 26 insertions(+), 4 deletions(-) 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..684b4ad 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._hosts_ = app.config.get('CASSANDRA_HOSTS', '') self._keyspace_ = app.config['CASSANDRA_KEYSPACE'] + self._username = app.config['CASSANDRA_USERNAME'] + self._password = app.config['CASSANDRA_PASSWORD'] + self._cloud_bundle = app.config['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. From 50c07d87a0c186f55ea0b172deeecbee16811cc9 Mon Sep 17 00:00:00 2001 From: Ranger Date: Thu, 22 Oct 2020 00:01:45 -0400 Subject: [PATCH 2/3] fixing build error by adding getters for the app config properties. --- flask_cqlalchemy/__init__.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/flask_cqlalchemy/__init__.py b/flask_cqlalchemy/__init__.py index 684b4ad..67e4cbd 100644 --- a/flask_cqlalchemy/__init__.py +++ b/flask_cqlalchemy/__init__.py @@ -49,10 +49,10 @@ def init_app(self, app): the Cassandra cluster and creates a connection at startup. """ self._hosts_ = app.config.get('CASSANDRA_HOSTS', '') - self._keyspace_ = app.config['CASSANDRA_KEYSPACE'] - self._username = app.config['CASSANDRA_USERNAME'] - self._password = app.config['CASSANDRA_PASSWORD'] - self._cloud_bundle = app.config['ASTRA_SECURE_CONNECT_BUNDLE'] + 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) From 58dd75a5a88a931c6f78e7d47c9d1a936d6bac17 Mon Sep 17 00:00:00 2001 From: Ranger Date: Thu, 29 Oct 2020 23:13:44 -0400 Subject: [PATCH 3/3] updated .travis.yml to specify trusty --- .travis.yml | 1 + 1 file changed, 1 insertion(+) 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