diff --git a/airflow/hooks/jdbc_hook.py b/airflow/hooks/jdbc_hook.py index 1556262753962..af83c49130655 100644 --- a/airflow/hooks/jdbc_hook.py +++ b/airflow/hooks/jdbc_hook.py @@ -38,8 +38,8 @@ def get_conn(self): host = conn.host login = conn.login psw = conn.password - jdbc_driver_loc = conn.extra_dejson.get('jdbc_drv_path') - jdbc_driver_name = conn.extra_dejson.get('jdbc_drv_clsname') + jdbc_driver_loc = conn.extra_dejson.get('extra__jdbc__drv_path') + jdbc_driver_name = conn.extra_dejson.get('extra__jdbc__drv_clsname') conn = jaydebeapi.connect(jdbc_driver_name, [str(host), str(login), str(psw)], @@ -52,4 +52,4 @@ def set_autocommit(self, conn, autocommit): :param conn: The connection :return: """ - conn.jconn.autocommit = autocommit \ No newline at end of file + conn.jconn.autocommit = autocommit diff --git a/airflow/www/app.py b/airflow/www/app.py index 5e05346d93bd3..35e2832cd055d 100644 --- a/airflow/www/app.py +++ b/airflow/www/app.py @@ -1586,9 +1586,15 @@ class ConnectionModelView(wwwutils.SuperUserMixin, AirflowModelView): column_default_sort = ('conn_id', False) column_list = ('conn_id', 'conn_type', 'host', 'port') form_overrides = dict(password=VisiblePasswordField) - form_extra_fields = { 'jdbc_drv_path' : StringField('Driver Path'), - 'jdbc_drv_clsname': StringField('Driver Class'), - } + # Used to customized the form, the forms elements get rendered + # and results are stored in the extra field as json. All of these + # need to be prefixed with extra__ and then the conn_type ___ as in + # extra__{conn_type}__name. You can also hide form elements and rename + # others from the connection_form.js file + form_extra_fields = { + 'extra__jdbc__drv_path' : StringField('Driver Path'), + 'extra__jdbc__drv_clsname': StringField('Driver Class'), + } form_choices = { 'conn_type': [ ('ftp', 'FTP',), @@ -1607,24 +1613,27 @@ class ConnectionModelView(wwwutils.SuperUserMixin, AirflowModelView): ('sqlite', 'Sqlite',), ('mssql', 'Microsoft SQL Server'), ] - } def on_model_change(self, form, model, is_created): formdata = form.data - if formdata['conn_type'] == 'jdbc': - jdbc = {key:formdata[key] for key in ('jdbc_drv_path','jdbc_drv_clsname' - #, 'jdbc_conn_url' - ) if key in formdata} - model.extra = json.dumps(jdbc) + if formdata['conn_type'] in ['jdbc']: + extra = { + key:formdata[key] + for key in self.form_extra_fields.keys() if key in formdata} + model.extra = json.dumps(extra) def on_form_prefill(self, form, id): - data = form.data - if 'extra' in data and data['extra'] != None: - d = json.loads(data['extra']) - #form.jdbc_conn_url.data = d['jdbc_conn_url'] - form.jdbc_drv_path.data = d['jdbc_drv_path'] - form.jdbc_drv_clsname.data = d['jdbc_drv_clsname'] + try: + d = json.loads(form.data.get('extra', '{}')) + except Exception as e: + d = {} + + for field in self.form_extra_fields.keys(): + value = d.get(field, '') + if value: + field = getattr(form, field) + field.data = value mv = ConnectionModelView( models.Connection, Session, diff --git a/airflow/www/static/connection_form.js b/airflow/www/static/connection_form.js new file mode 100644 index 0000000000000..0e5a057113e4f --- /dev/null +++ b/airflow/www/static/connection_form.js @@ -0,0 +1,42 @@ +/** + * Created by janomar on 23/07/15. + */ + + $(document).ready(function() { + var config = { + jdbc: { + hidden_fields: ['port', 'schema', 'extra'], + relabeling: {'host': 'Connection URL'}, + } + } + function connTypeChange(connectionType) { + $("div.form_group").removeClass("hide"); + $.each($("[id^='extra__']"), function() { + $(this).parent().parent().addClass('hide') + }); + // Somehow the previous command doesn't honor __ + $("#extra").parent().parent().removeClass('hide') + $.each($("[id^='extra__"+connectionType+"']"), function() { + $(this).parent().parent().removeClass('hide') + }); + $("label[orig_text]").each(function(){ + $(this).text($(this).attr("orig_text")); + }); + if (config[connectionType] != undefined){ + $.each(config[connectionType].hidden_fields, function(i, field){ + $("#" + field).parent().parent().addClass('hide') + }); + $.each(config[connectionType].relabeling, function(k, v){ + lbl = $("label[for='" + k + "']") + lbl.attr("orig_text", lbl.text()); + $("label[for='" + k + "']").text(v); + }); + } + } + var connectionType=$("#conn_type").val(); + $("#conn_type").on('change', function(e) { + connectionType = $("#conn_type").val(); + connTypeChange(connectionType); + }); + connTypeChange(connectionType); +}); diff --git a/airflow/www/static/jdbc.js b/airflow/www/static/jdbc.js deleted file mode 100644 index f3b42a637c517..0000000000000 --- a/airflow/www/static/jdbc.js +++ /dev/null @@ -1,26 +0,0 @@ -/** - * Created by janomar on 23/07/15. - */ - function toggleJdbc(connectionType) { - isJdbc = connectionType == 'jdbc' - $("#port").parent().parent().toggleClass('hide', isJdbc) - $("#schema").parent().parent().toggleClass('hide', isJdbc) - $("#extra").parent().parent().toggleClass('hide', isJdbc) - $("#jdbc_drv_clsname").parent().parent().toggleClass('hide', !isJdbc) - $("#jdbc_drv_path").parent().parent().toggleClass('hide', !isJdbc) - - if (isJdbc) { - $("label[for='host']").text("Connection URL"); - } else { - $("label[for='host']").text("Host"); - } - } - - jQuery(document).ready(function() { - var conn_type =jQuery("#conn_type").val(); - jQuery("#conn_type").on('change', function(e) { - conn_type = jQuery("#conn_type").val(); - toggleJdbc(conn_type); - }); - toggleJdbc(conn_type); -}); \ No newline at end of file diff --git a/airflow/www/templates/airflow/conn_create.html b/airflow/www/templates/airflow/conn_create.html index e878842bfdd7a..5c5987a1baa06 100644 --- a/airflow/www/templates/airflow/conn_create.html +++ b/airflow/www/templates/airflow/conn_create.html @@ -2,5 +2,5 @@ {% block tail %} {{ super() }} - + {% endblock %} diff --git a/airflow/www/templates/airflow/conn_edit.html b/airflow/www/templates/airflow/conn_edit.html index 2c45f6849aee0..2f988c70f5d3d 100644 --- a/airflow/www/templates/airflow/conn_edit.html +++ b/airflow/www/templates/airflow/conn_edit.html @@ -2,5 +2,5 @@ {% block tail %} {{ super() }} - + {% endblock %}