8000 Fix issues with connections publishing workbooks (#1171) · tableau/server-client-python@7ceed6c · GitHub
[go: up one dir, main page]

Skip to content

Commit 7ceed6c

Browse files
authored
Fix issues with connections publishing workbooks (#1171)
Allow publishing using connection credentials on ConnectionItem class without ConnectionCredentials instance, as documented Accept empty string for username or password in connection credentials Avoid Tableau Server internal server error when publishing with empty connection list by setting connections to None
1 parent 23d110f commit 7ceed6c

File tree

2 files changed

+62
-3
lines changed

2 files changed

+62
-3
lines changed

tableauserverclient/server/request_factory.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
from tableauserverclient.models.metric_item import MetricItem
99

10+
from ..models import ConnectionCredentials
1011
from ..models import ConnectionItem
1112
from ..models import DataAlertItem
1213
from ..models import FlowItem
@@ -55,6 +56,13 @@ def _add_connections_element(connections_element, connection):
5556
connection_element.attrib["serverPort"] = connection.server_port
5657
if connection.connection_credentials:
5758
connection_credentials = connection.connection_credentials
59+
elif connection.username is not None and connection.password is not None and connection.embed_password is not None:
60+
connection_credentials = ConnectionCredentials(
61+
connection.username, connection.password, embed=connection.embed_password
62+
)
63+
else:
64+
connection_credentials = None
65+
if connection_credentials:
5866
_add_credentials_element(connection_element, connection_credentials)
5967

6068

@@ -66,7 +74,7 @@ def _add_hiddenview_element(views_element, view_name):
6674

6775
def _add_credentials_element(parent_element, connection_credentials):
6876
credentials_element = ET.SubElement(parent_element, "connectionCredentials")
69-
if not connection_credentials.password or not connection_credentials.name:
77+
if connection_credentials.password is None or connection_credentials.name is None:
7078
raise ValueError("Connection Credentials must have a name and password")
7179
credentials_element.attrib["name"] = connection_credentials.name
7280
credentials_element.attrib["password"] = connection_credentials.password
@@ -177,7 +185,7 @@ def _generate_xml(self, datasource_item, connection_credentials=None, connection
177185
if connection_credentials is not None:
178186
_add_credentials_element(datasource_element, connection_credentials)
179187

180-
if connections is not None:
188+
if connections is not None and len(connections) > 0:
181189
connections_element = ET.SubElement(datasource_element, "connections")
182190
for connection in connections:
183191
_add_connections_element(connections_element, connection)
@@ -899,7 +907,7 @@ def _generate_xml(
899907
if connection_credentials is not None:
900908
_add_credentials_element(workbook_element, connection_credentials)
901909

902-
if connections is not None:
910+
if connections is not None and len(connections) > 0:
903911
connections_element = ET.SubElement(workbook_element, "connections")
904912
for connection in connections:
905913
_add_connections_element(connections_element, connection)

test/test_workbook.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -748,6 +748,30 @@ def test_publish_multi_connection(self) -> None:
748748
self.assertEqual(connection_results[1].get("serverAddress", None), "pgsql.test.com")
749749
self.assertEqual(connection_results[1].find("connectionCredentials").get("password", None), "secret") # type: ignore[union-attr]
750750

751+
def test_publish_multi_connection_flat(self) -> None:
752+
new_workbook = TSC.WorkbookItem(
753+
name="Sample", show_tabs=False, project_id="ee8c6e70-43b6-11e6-af4f-f7b0d8e20760"
754+
)
755+
connection1 = TSC.ConnectionItem()
756+
connection1.server_address = "mysql.test.com"
757+
connection1.username = "test"
758+
connection1.password = "secret"
759+
connection1.embed_password = True
760+
connection2 = TSC.ConnectionItem()
761+
connection2.server_address = "pgsql.test.com"
762+
connection2.username = "test"
763+
connection2.password = "secret"
764+
connection2.embed_password = True
765+
766+
response = RequestFactory.Workbook._generate_xml(new_workbook, connections=[connection1, connection2])
767+
# Can't use ConnectionItem parser due to xml namespace problems
768+
connection_results = fromstring(response).findall(".//connection")
769+
770+
self.assertEqual(connection_results[0].get("serverAddress", None), "mysql.test.com")
771+
self.assertEqual(connection_results[0].find("connectionCredentials").get("name", None), "test") # type: ignore[union-attr]
772+
self.assertEqual(connection_results[1].get("serverAddress", None), "pgsql.test.com")
773+
self.assertEqual(connection_results[1].find("connectionCredentials").get("password", None), "secret") # type: ignore[union-attr]
774+
751775
def test_publish_single_connection(self) -> None:
752776
new_workbook = TSC.WorkbookItem(
753777
name="Sample", show_tabs=False, project_id="ee8c6e70-43b6-11e6-af4f-f7b0d8e20760"
@@ -762,6 +786,33 @@ def test_publish_single_connection(self) -> None:
762786
self.assertEqual(credentials[0].get("password", None), "secret")
763787
self.assertEqual(credentials[0].get("embed", None), "true")
764788

789+
def test_publish_single_connection_username_none(self) -> None:
790+
new_workbook = TSC.WorkbookItem(
791+
name="Sample", show_tabs=False, project_id="ee8c6e70-43b6-11e6-af4f-f7b0d8e20760"
792+
)
793+
connection_creds = TSC.ConnectionCredentials(None, "secret", True)
794+
795+
self.assertRaises(
796+
ValueError,
797+
RequestFactory.Workbook._generate_xml,
798+
new_workbook,
799+
connection_credentials=connection_creds,
800+
)
801+
802+
def test_publish_single_connection_username_empty(self) -> None:
803+
new_workbook = TSC.WorkbookItem(
804+
name="Sample", show_tabs=False, project_id="ee8c6e70-43b6-11e6-af4f-f7b0d8e20760"
805+
)
806+
connection_creds = TSC.ConnectionCredentials("", "secret", True)
807+
808+
response = RequestFactory.Workbook._generate_xml(new_workbook, connection_credentials=connection_creds)
809+
# Can't use ConnectionItem parser due to xml namespace problems
810+
credentials = fromstring(response).findall(".//connectionCredentials")
811+
self.assertEqual(len(credentials), 1)
812+
self.assertEqual(credentials[0].get("name", None), "")
813+
self.assertEqual(credentials[0].get("password", None), "secret")
814+
self.assertEqual(credentials[0].get("embed", None), "true")
815+
765816
def test_credentials_and_multi_connect_raises_exception(self) -> None:
766817
new_workbook = TSC.WorkbookItem(
767818
name="Sample", show_tabs=False, project_id="ee8c6e70-43b6-11e6-af4f-f7b0d8e20760"

0 commit comments

Comments
 (0)
0