Skip to content

Commit

Permalink
Add https support for Druid (apache#4480)
Browse files Browse the repository at this point in the history
* Add https support for Druid

* addressing comment
  • Loading branch information
mistercrunch authored and michellethomas committed May 23, 2018
1 parent cde2695 commit db78c9d
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 16 deletions.
24 changes: 15 additions & 9 deletions superset/connectors/druid/models.py
Expand Up @@ -11,6 +11,7 @@
import json
import logging
from multiprocessing.pool import ThreadPool
import re

from dateutil.parser import parse as dparse
from flask import escape, Markup
Expand Down Expand Up @@ -107,24 +108,29 @@ def data(self):
'backend': 'druid',
}

@staticmethod
def get_base_url(host, port):
if not re.match('http(s)?://', host):
host = 'http://' + host
return '{0}:{1}'.format(host, port)

def get_base_coordinator_url(self):
base_url = self.get_base_url(
self.coordinator_host, self.coordinator_port)
return '{base_url}/{self.coordinator_endpoint}'.format(**locals())

def get_pydruid_client(self):
cli = PyDruid(
'http://{0}:{1}/'.format(self.broker_host, self.broker_port),
self.get_base_url(self.broker_host, self.broker_port),
self.broker_endpoint)
return cli

def get_datasources(self):
endpoint = (
'http://{obj.coordinator_host}:{obj.coordinator_port}/'
'{obj.coordinator_endpoint}/datasources'
).format(obj=self)

endpoint = self.get_base_coordinator_url() + '/datasources'
return json.loads(requests.get(endpoint).text)

def get_druid_version(self):
endpoint = (
'http://{obj.coordinator_host}:{obj.coordinator_port}/status'
).format(obj=self)
endpoint = self.get_base_coordinator_url() + '/status'
return json.loads(requests.get(endpoint).text)['version']

def refresh_datasources(
Expand Down
33 changes: 26 additions & 7 deletions tests/druid_tests.py
Expand Up @@ -77,6 +77,16 @@ class DruidTests(SupersetTestCase):
def __init__(self, *args, **kwargs):
super(DruidTests, self).__init__(*args, **kwargs)

def get_test_cluster_obj(self):
return DruidCluster(
cluster_name='test_cluster',
coordinator_host='localhost',
coordinator_endpoint='druid/coordinator/v1/metadata',
coordinator_port=7979,
broker_host='localhost',
broker_port=7980,
metadata_last_refreshed=datetime.now())

@patch('superset.connectors.druid.models.PyDruid')
def test_client(self, PyDruid):
self.login(username='admin')
Expand All @@ -95,13 +105,7 @@ def test_client(self, PyDruid):
db.session.delete(cluster)
db.session.commit()

cluster = DruidCluster(
cluster_name='test_cluster',
coordinator_host='localhost',
coordinator_port=7979,
broker_host='localhost',
broker_port=7980,
metadata_last_refreshed=datetime.now())
cluster = self.get_test_cluster_obj()

db.session.add(cluster)
cluster.get_datasources = PickableMock(return_value=['test_datasource'])
Expand Down Expand Up @@ -323,6 +327,21 @@ def test_sync_druid_perm(self, PyDruid):
permission=permission, view_menu=view_menu).first()
assert pv is not None

def test_urls(self):
cluster = self.get_test_cluster_obj()
self.assertEquals(
cluster.get_base_url('localhost', '9999'), 'http://localhost:9999')
self.assertEquals(
cluster.get_base_url('http://localhost', '9999'),
'http://localhost:9999')
self.assertEquals(
cluster.get_base_url('https://localhost', '9999'),
'https://localhost:9999')

self.assertEquals(
cluster.get_base_coordinator_url(),
'http://localhost:7979/druid/coordinator/v1/metadata')


if __name__ == '__main__':
unittest.main()

0 comments on commit db78c9d

Please sign in to comment.