Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions airflow/cli/cli_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,9 @@ def positive_int(value):
ARG_CONN_TYPE = Arg(
('--conn-type',), help='Connection type, required to add a connection without conn_uri', type=str
)
ARG_CONN_DESCRIPTION = Arg(
('--conn-description',), help='Connection description, optional when adding a connection', type=str
)
ARG_CONN_HOST = Arg(('--conn-host',), help='Connection host, optional when adding a connection', type=str)
ARG_CONN_LOGIN = Arg(('--conn-login',), help='Connection login, optional when adding a connection', type=str)
ARG_CONN_PASSWORD = Arg(
Expand Down Expand Up @@ -654,6 +657,7 @@ def positive_int(value):

ALTERNATIVE_CONN_SPECS_ARGS = [
ARG_CONN_TYPE,
ARG_CONN_DESCRIPTION,
ARG_CONN_HOST,
ARG_CONN_LOGIN,
ARG_CONN_PASSWORD,
Expand Down
6 changes: 5 additions & 1 deletion airflow/cli/commands/connection_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ def _tabulate_connection(conns: List[Connection], tablefmt: str):
{
'Conn Id': conn.conn_id,
'Conn Type': conn.conn_type,
'Description': conn.description,
'Host': conn.host,
'Port': conn.port,
'Is Encrypted': conn.is_encrypted,
Expand All @@ -60,6 +61,7 @@ def _yamulate_connection(conn: Connection):
'Id': conn.id,
'Conn Id': conn.conn_id,
'Conn Type': conn.conn_type,
'Description': conn.description,
'Host': conn.host,
'Schema': conn.schema,
'Login': conn.login,
Expand Down Expand Up @@ -113,6 +115,7 @@ def _format_connections(conns: List[Connection], fmt: str) -> str:
for conn in conns:
connections_dict[conn.conn_id] = {
'conn_type': conn.conn_type,
'description': conn.description,
'host': conn.host,
'login': conn.login,
'password': conn.password,
Expand Down Expand Up @@ -202,11 +205,12 @@ def connections_add(args):
raise SystemExit(msg)

if args.conn_uri:
new_conn = Connection(conn_id=args.conn_id, uri=args.conn_uri)
new_conn = Connection(conn_id=args.conn_id, description=args.conn_description, uri=args.conn_uri)
else:
new_conn = Connection(
conn_id=args.conn_id,
conn_type=args.conn_type,
description=args.conn_description,
host=args.conn_host,
login=args.conn_login,
password=args.conn_password,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

"""Add description field to connection

Revision ID: 61ec73d9401f
Revises: 2c6edca13270
Create Date: 2020-09-10 14:56:30.279248

"""

import sqlalchemy as sa
from alembic import op

# revision identifiers, used by Alembic.
revision = '61ec73d9401f'
down_revision = '2c6edca13270'
branch_labels = None
depends_on = None


def upgrade():
"""Apply Add description field to connection"""
with op.batch_alter_table('connection') as batch_op:
batch_op.add_column(sa.Column('description', sa.String(length=5000), nullable=True))


def downgrade():
"""Unapply Add description field to connection"""
with op.batch_alter_table('connection', schema=None) as batch_op:
batch_op.drop_column('description')
7 changes: 6 additions & 1 deletion airflow/models/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ class Connection(Base, LoggingMixin): # pylint: disable=too-many-instance-attri
:type conn_id: str
:param conn_type: The connection type.
:type conn_type: str
:param description: The connection description.
:type description: str
:param host: The host.
:type host: str
:param login: The login.
Expand All @@ -152,6 +154,7 @@ class Connection(Base, LoggingMixin): # pylint: disable=too-many-instance-attri
id = Column(Integer(), primary_key=True)
conn_id = Column(String(ID_LEN), unique=True, nullable=False)
conn_type = Column(String(500), nullable=False)
description = Column(String(5000))
host = Column(String(500))
schema = Column(String(500))
login = Column(String(500))
Expand All @@ -161,10 +164,11 @@ class Connection(Base, LoggingMixin): # pylint: disable=too-many-instance-attri
is_extra_encrypted = Column(Boolean, unique=False, default=False)
_extra = Column('extra', String(5000))

def __init__(
def __init__( # pylint: disable=too-many-arguments
self,
conn_id: Optional[str] = None,
conn_type: Optional[str] = None,
description: Optional[str] = None,
host: Optional[str] = None,
login: Optional[str] = None,
password: Optional[str] = None,
Expand All @@ -175,6 +179,7 @@ def __init__(
):
super().__init__()
self.conn_id = conn_id
self.description = description
if uri and ( # pylint: disable=too-many-boolean-expressions
conn_type or host or login or password or schema or port or extra
):
Expand Down
1 change: 1 addition & 0 deletions airflow/www/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ class ConnectionForm(DynamicForm):
choices=sorted(_connection_types, key=itemgetter(1)), # pylint: disable=protected-access
widget=Select2Widget(),
)
description = StringField(lazy_gettext('Description'), widget=BS3TextAreaFieldWidget())
host = StringField(lazy_gettext('Host'), widget=BS3TextFieldWidget())
schema = StringField(lazy_gettext('Schema'), widget=BS3TextFieldWidget())
login = StringField(lazy_gettext('Login'), widget=BS3TextFieldWidget())
Expand Down
11 changes: 10 additions & 1 deletion airflow/www/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2852,10 +2852,19 @@ class ConnectionModelView(AirflowModelView):
'extra__kubernetes__kube_config',
'extra__kubernetes__namespace',
]
list_columns = ['conn_id', 'conn_type', 'host', 'port', 'is_encrypted', 'is_extra_encrypted']
list_columns = [
'conn_id',
'conn_type',
'description',
'host',
'port',
'is_encrypted',
'is_extra_encrypted',
]
add_columns = edit_columns = [
'conn_id',
'conn_type',
'description',
'host',
'schema',
'login',
Expand Down
2 changes: 2 additions & 0 deletions docs/howto/connection/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ The above URI would produce a ``Connection`` object equivalent to the following:
Connection(
conn_id='',
conn_type='my_conn_type',
description=None,
login='my-login',
password='my-password',
host='my-host',
Expand Down Expand Up @@ -257,6 +258,7 @@ convenience method :py:meth:`~airflow.models.connection.Connection.get_uri`. It
>>> c = Connection(
>>> conn_id='some_conn',
>>> conn_type='mysql',
>>> description='connection description',
>>> host='myhost.com',
>>> login='myname',
>>> password='mypassword',
Expand Down
43 changes: 40 additions & 3 deletions tests/cli/commands/test_connection_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ def setUp(self, session=None):
Connection(
conn_id="airflow_db",
conn_type="mysql",
description="mysql conn description",
host="mysql",
login="root",
password="plainpassword",
Expand All @@ -163,6 +164,7 @@ def setUp(self, session=None):
Connection(
conn_id="druid_broker_default",
conn_type="druid",
description="druid-broker conn description",
host="druid-broker",
port=8082,
extra='{"endpoint": "druid/v2/sql"}',
Expand Down Expand Up @@ -313,6 +315,7 @@ def test_cli_connections_export_should_export_as_json(self, mock_file_open, mock
{
"airflow_db": {
"conn_type": "mysql",
"description": "mysql conn description",
"host": "mysql",
"login": "root",
"password": "plainpassword",
Expand All @@ -322,6 +325,7 @@ def test_cli_connections_export_should_export_as_json(self, mock_file_open, mock
},
"druid_broker_default": {
"conn_type": "druid",
"description": "druid-broker conn description",
"host": "druid-broker",
"login": None,
"password": None,
Expand Down Expand Up @@ -355,6 +359,7 @@ def test_cli_connections_export_should_export_as_yaml(self, mock_file_open, mock
expected_connections = (
"airflow_db:\n"
" conn_type: mysql\n"
" description: mysql conn description\n"
" extra: null\n"
" host: mysql\n"
" login: root\n"
Expand All @@ -363,6 +368,7 @@ def test_cli_connections_export_should_export_as_yaml(self, mock_file_open, mock
" schema: airflow\n"
"druid_broker_default:\n"
" conn_type: druid\n"
" description: druid-broker conn description\n"
" extra: \'{\"endpoint\": \"druid/v2/sql\"}\'\n"
" host: druid-broker\n"
" login: null\n"
Expand Down Expand Up @@ -452,6 +458,7 @@ def test_cli_connections_export_should_force_export_as_specified_format(
{
"airflow_db": {
"conn_type": "mysql",
"description": "mysql conn description",
"host": "mysql",
"login": "root",
"password": "plainpassword",
Expand All @@ -461,6 +468,7 @@ def test_cli_connections_export_should_force_export_as_specified_format(
},
"druid_broker_default": {
"conn_type": "druid",
"description": "druid-broker conn description",
"host": "druid-broker",
"login": None,
"password": None,
Expand Down Expand Up @@ -492,10 +500,17 @@ def tearDownClass(cls):
@parameterized.expand(
[
(
["connections", "add", "new0", "--conn-uri=%s" % TEST_URL],
[
"connections",
"add",
"new0",
"--conn-uri=%s" % TEST_URL,
"--conn-description=new0 description",
],
"\tSuccessfully added `conn_id`=new0 : postgresql://airflow:airflow@host:5432/airflow",
{
"conn_type": "postgres",
"description": "new0 description",
"host": "host",
"is_encrypted": True,
"is_extra_encrypted": False,
Expand All @@ -505,10 +520,17 @@ def tearDownClass(cls):
},
),
(
["connections", "add", "new1", "--conn-uri=%s" % TEST_URL],
[
"connections",
"add",
"new1",
"--conn-uri=%s" % TEST_URL,
"--conn-description=new1 description",
],
"\tSuccessfully added `conn_id`=new1 : postgresql://airflow:airflow@host:5432/airflow",
{
"conn_type": "postgres",
"description": "new1 description",
"host": "host",
"is_encrypted": True,
"is_extra_encrypted": False,
Expand All @@ -529,6 +551,7 @@ def tearDownClass(cls):
"\tSuccessfully added `conn_id`=new2 : postgresql://airflow:airflow@host:5432/airflow",
{
"conn_type": "postgres",
"description": None,
"host": "host",
"is_encrypted": True,
"is_extra_encrypted": True,
Expand All @@ -545,10 +568,13 @@ def tearDownClass(cls):
"--conn-uri=%s" % TEST_URL,
"--conn-extra",
"{'extra': 'yes'}",
"--conn-description",
"new3 description",
],
"\tSuccessfully added `conn_id`=new3 : postgresql://airflow:airflow@host:5432/airflow",
{
"conn_type": "postgres",
"description": "new3 description",
"host": "host",
"is_encrypted": True,
"is_extra_encrypted": True,
Expand All @@ -568,10 +594,12 @@ def tearDownClass(cls):
"--conn-host=host",
"--conn-port=9083",
"--conn-schema=airflow",
"--conn-description= new4 description ",
],
"\tSuccessfully added `conn_id`=new4 : hive_metastore://airflow:******@host:9083/airflow",
{
"conn_type": "hive_metastore",
"description": " new4 description ",
"host": "host",
"is_encrypted": True,
"is_extra_encrypted": False,
Expand All @@ -590,10 +618,12 @@ def tearDownClass(cls):
"--conn-type=google_cloud_platform",
"--conn-extra",
"{'extra': 'yes'}",
"--conn-description=new5 description",
],
"\tSuccessfully added `conn_id`=new5 : google_cloud_platform://:@:",
{
"conn_type": "google_cloud_platform",
"description": "new5 description",
"host": None,
"is_encrypted": False,
"is_extra_encrypted": True,
Expand All @@ -615,6 +645,7 @@ def test_cli_connection_add(self, cmd, expected_output, expected_conn):
with create_session() as session:
comparable_attrs = [
"conn_type",
"description",
"host",
"is_encrypted",
"is_extra_encrypted",
Expand Down Expand Up @@ -665,7 +696,13 @@ def tearDownClass(cls):
def test_cli_delete_connections(self, session=None):
merge_conn(
Connection(
conn_id="new1", conn_type="mysql", host="mysql", login="root", password="", schema="airflow"
conn_id="new1",
conn_type="mysql",
description="mysql description",
host="mysql",
login="root",
password="",
schema="airflow",
),
session=session,
)
Expand Down
1 change: 1 addition & 0 deletions tests/www/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ def setUp(self):
self.connection = {
'conn_id': 'test_conn',
'conn_type': 'http',
'description': 'description',
'host': 'localhost',
'port': 8080,
'username': 'root',
Expand Down