Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add means to Duplicate connections from UI #15574

Merged
merged 30 commits into from
Jun 17, 2021
Merged
Show file tree
Hide file tree
Changes from 29 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
32d8d64
12401 - added changes in view.py to duplicate connections
pateash Apr 28, 2021
dd68fb8
12401 - pylint error fixed
pateash Apr 28, 2021
20d0532
12401 - test added
pateash Apr 28, 2021
7349a40
12401 - pylint fixed
pateash Apr 28, 2021
ece3352
Merge branch 'master' of https://github.com/apache/airflow into #1240…
pateash May 1, 2021
04750aa
12401 - test added
pateash May 4, 2021
5adfd66
Update tests/www/test_views.py
pateash May 4, 2021
ba7b9ef
12401 - removed formatting
pateash May 4, 2021
85edcc0
Merge branch '#12401-creating-duplicate-connection-in-ui' of https://…
pateash May 4, 2021
a6974bc
Merge branch 'master' of https://github.com/apache/airflow into #1240…
pateash May 4, 2021
d57bee9
Update test_views.py
pateash May 4, 2021
5e7b6d3
Create test_views.py
pateash May 4, 2021
3f04aea
12401 - fixed intermediate matching and used _copyN
pateash May 4, 2021
dfedd9d
Merge remote-tracking branch 'ap/#12401-creating-duplicate-connection…
pateash May 4, 2021
08fcd82
12401 - test implemented
pateash May 4, 2021
86853ac
12401 - test fixed
pateash May 5, 2021
038c1a6
12401 - re.search() used instead of findall()
pateash May 5, 2021
e4370b9
12401 - pylint fixed
pateash May 5, 2021
1372eb9
12401 - variable name changed as per suggestion.
pateash May 5, 2021
a5104b1
Update airflow/www/views.py
pateash May 5, 2021
1ab9f8a
Merge branch '#12401-creating-duplicate-connection-in-ui' of https://…
pateash May 5, 2021
d55c15d
12401 - variable name changed as per suggestion.
pateash May 5, 2021
06d1932
12401 - pylint fixed
pateash May 5, 2021
7014a50
Merge branch 'master' of https://github.com/apache/airflow into #1240…
pateash May 11, 2021
7281040
12401 - refactoring done as per new test refactoring.
pateash May 11, 2021
582347c
12401 - mock_form is not needed for now.
pateash May 11, 2021
43a0443
Merge branch 'main' into #12401-creating-duplicate-connection-in-ui
pateash Jun 2, 2021
2171c62
Update airflow/www/views.py
pateash Jun 12, 2021
ab22173
12401 - fixed pylint issue
pateash Jun 14, 2021
ac6841c
Merge branch 'main' of https://github.com/apache/airflow into #12401-…
pateash Jun 17, 2021
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
53 changes: 53 additions & 0 deletions airflow/www/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import json
import logging
import math
import re
import socket
import sys
import traceback
Expand Down Expand Up @@ -78,6 +79,7 @@
from pygments import highlight, lexers
from pygments.formatters import HtmlFormatter # noqa pylint: disable=no-name-in-module
from sqlalchemy import Date, and_, desc, func, or_, union_all
from sqlalchemy.exc import IntegrityError
from sqlalchemy.orm import joinedload
from wtforms import SelectField, validators
from wtforms.validators import InputRequired
Expand Down Expand Up @@ -3060,6 +3062,7 @@ class ConnectionModelView(AirflowModelView):
'edit': 'edit',
'delete': 'delete',
'action_muldelete': 'delete',
'action_mulduplicate': 'create',
}

base_permissions = [
Expand Down Expand Up @@ -3113,6 +3116,56 @@ def action_muldelete(self, items):
self.update_redirect()
return redirect(self.get_redirect())

@action(
'mulduplicate',
'Duplicate',
'Are you sure you want to duplicate the selected connections?',
single=False,
)
@provide_session
@auth.has_access(
[
(permissions.ACTION_CAN_CREATE, permissions.RESOURCE_CONNECTION),
pateash marked this conversation as resolved.
Show resolved Hide resolved
(permissions.ACTION_CAN_READ, permissions.RESOURCE_CONNECTION),
]
)
def action_mulduplicate(self, connections, session=None):
"""Duplicate Multiple connections"""
for selected_conn in connections:
new_conn_id = selected_conn.conn_id
match = re.search(r"_copy(\d+)$", selected_conn.conn_id)
if match:
conn_id_prefix = selected_conn.conn_id[: match.start()]
new_conn_id = f"{conn_id_prefix}_copy{int(match.group(1)) + 1}"
else:
new_conn_id += '_copy1'

dup_conn = Connection(
new_conn_id,
selected_conn.conn_type,
selected_conn.description,
selected_conn.host,
selected_conn.login,
selected_conn.password,
selected_conn.schema,
selected_conn.port,
selected_conn.extra,
)

try:
session.add(dup_conn)
session.commit()
flash(f"Connection {new_conn_id} added successfully.", "success")
except IntegrityError:
flash(
f"Connection {new_conn_id} can't be added. Integrity error, probably unique constraint.",
"warning",
)
session.rollback()
ashb marked this conversation as resolved.
Show resolved Hide resolved

self.update_redirect()
return redirect(self.get_redirect())

def process_form(self, form, is_created):
"""Process form data."""
conn_type = form.data['conn_type']
Expand Down
42 changes: 42 additions & 0 deletions tests/www/views/test_views_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,45 @@ def test_prefill_form_null_extra():

cmv = ConnectionModelView()
cmv.prefill_form(form=mock_form, pk=1)


def test_duplicate_connection(admin_client):
"""Test Duplicate multiple connection with suffix"""
conn1 = Connection(
conn_id='test_duplicate_gcp_connection',
conn_type='Google Cloud',
description='Google Cloud Connection',
)
conn2 = Connection(
conn_id='test_duplicate_mysql_connection',
conn_type='FTP',
description='MongoDB2',
host='localhost',
schema='airflow',
port=3306,
)
conn3 = Connection(
conn_id='test_duplicate_postgres_connection_copy1',
conn_type='FTP',
description='Postgres',
host='localhost',
schema='airflow',
port=3306,
)
with create_session() as session:
session.query(Connection).delete()
session.add_all([conn1, conn2, conn3])
session.commit()
pateash marked this conversation as resolved.
Show resolved Hide resolved

data = {"action": "mulduplicate", "rowid": [conn1.id, conn3.id]}
resp = admin_client.post('/connection/action_post', data=data, follow_redirects=True)
expected_result = {
'test_duplicate_gcp_connection',
'test_duplicate_gcp_connection_copy1',
'test_duplicate_mysql_connection',
'test_duplicate_postgres_connection_copy1',
'test_duplicate_postgres_connection_copy2',
}
response = {conn[0] for conn in session.query(Connection.conn_id).all()}
assert resp.status_code == 200
assert expected_result == response