2017-10-04 00:06:53 +00:00
|
|
|
from __future__ import print_function
|
2014-10-28 05:39:45 +00:00
|
|
|
import json
|
2016-02-18 15:32:49 +00:00
|
|
|
import jsonschema
|
|
|
|
from jsonschema import ValidationError
|
2014-10-28 05:39:45 +00:00
|
|
|
|
2015-03-02 05:34:06 +00:00
|
|
|
from redash import query_runner
|
2014-10-28 05:39:45 +00:00
|
|
|
from redash.models import DataSource
|
|
|
|
|
|
|
|
|
2016-02-18 15:32:49 +00:00
|
|
|
def validate_configuration(query_runner_type, configuration_json):
|
|
|
|
query_runner_class = query_runner.query_runners.get(query_runner_type, None)
|
|
|
|
if query_runner_class is None:
|
|
|
|
return False
|
|
|
|
|
|
|
|
try:
|
|
|
|
if isinstance(configuration_json, basestring):
|
|
|
|
configuration = json.loads(configuration_json)
|
|
|
|
else:
|
|
|
|
configuration = configuration_json
|
|
|
|
jsonschema.validate(configuration, query_runner_class.configuration_schema())
|
|
|
|
except (ValidationError, ValueError):
|
|
|
|
return False
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
2014-10-28 05:39:45 +00:00
|
|
|
def update(data_source):
|
2017-10-04 00:06:53 +00:00
|
|
|
print("[%s] Old options: %s" % (data_source.name, data_source.options))
|
2015-03-02 05:34:06 +00:00
|
|
|
|
2016-02-18 15:32:49 +00:00
|
|
|
if validate_configuration(data_source.type, data_source.options):
|
2017-10-04 00:06:53 +00:00
|
|
|
print("[%s] configuration already valid. skipping." % data_source.name)
|
2015-03-02 05:34:06 +00:00
|
|
|
return
|
|
|
|
|
2014-10-28 05:39:45 +00:00
|
|
|
if data_source.type == 'pg':
|
|
|
|
values = data_source.options.split(" ")
|
|
|
|
configuration = {}
|
|
|
|
for value in values:
|
|
|
|
k, v = value.split("=", 1)
|
|
|
|
configuration[k] = v
|
2015-03-09 06:57:40 +00:00
|
|
|
if k == 'port':
|
|
|
|
configuration[k] = int(v)
|
|
|
|
|
2014-10-28 05:39:45 +00:00
|
|
|
data_source.options = json.dumps(configuration)
|
|
|
|
|
|
|
|
elif data_source.type == 'mysql':
|
2015-03-09 06:57:40 +00:00
|
|
|
mapping = {
|
|
|
|
'Server': 'host',
|
|
|
|
'User': 'user',
|
|
|
|
'Pwd': 'passwd',
|
|
|
|
'Database': 'db'
|
|
|
|
}
|
|
|
|
|
2014-10-28 05:39:45 +00:00
|
|
|
values = data_source.options.split(";")
|
|
|
|
configuration = {}
|
|
|
|
for value in values:
|
|
|
|
k, v = value.split("=", 1)
|
2015-03-09 06:57:40 +00:00
|
|
|
configuration[mapping[k]] = v
|
2014-10-28 05:39:45 +00:00
|
|
|
data_source.options = json.dumps(configuration)
|
|
|
|
|
|
|
|
elif data_source.type == 'graphite':
|
|
|
|
old_config = json.loads(data_source.options)
|
|
|
|
|
|
|
|
configuration = {
|
|
|
|
"url": old_config["url"]
|
|
|
|
}
|
|
|
|
|
|
|
|
if "verify" in old_config:
|
|
|
|
configuration['verify'] = old_config['verify']
|
|
|
|
|
|
|
|
if "auth" in old_config:
|
|
|
|
configuration['username'], configuration['password'] = old_config["auth"]
|
|
|
|
|
|
|
|
data_source.options = json.dumps(configuration)
|
|
|
|
|
|
|
|
elif data_source.type == 'url':
|
|
|
|
data_source.options = json.dumps({"url": data_source.options})
|
|
|
|
|
|
|
|
elif data_source.type == 'script':
|
|
|
|
data_source.options = json.dumps({"path": data_source.options})
|
|
|
|
|
2015-01-29 09:10:28 +00:00
|
|
|
elif data_source.type == 'mongo':
|
|
|
|
data_source.type = 'mongodb'
|
|
|
|
|
2014-10-28 05:39:45 +00:00
|
|
|
else:
|
2017-10-04 00:06:53 +00:00
|
|
|
print("[%s] No need to convert type of: %s" % (data_source.name, data_source.type))
|
2014-10-28 05:39:45 +00:00
|
|
|
|
2017-10-04 00:06:53 +00:00
|
|
|
print("[%s] New options: %s" % (data_source.name, data_source.options))
|
2016-01-24 09:57:00 +00:00
|
|
|
data_source.save(only=data_source.dirty_fields)
|
2014-10-28 05:39:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2016-01-24 09:57:00 +00:00
|
|
|
for data_source in DataSource.select(DataSource.id, DataSource.name, DataSource.type, DataSource.options):
|
|
|
|
update(data_source)
|