Skip to content

Commit

Permalink
Enable asJob for group update (#1276)
Browse files Browse the repository at this point in the history
  • Loading branch information
jorwoods authored Sep 21, 2023
1 parent 9afc0b3 commit 81af54a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
5 changes: 4 additions & 1 deletion tableauserverclient/server/endpoint/groups_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,17 @@ def update(
)
group_item.minimum_site_role = default_site_role

url = "{0}/{1}".format(self.baseurl, group_item.id)

if not group_item.id:
error = "Group item missing ID."
raise MissingRequiredFieldError(error)
if as_job and (group_item.domain_name is None or group_item.domain_name == "local"):
error = "Local groups cannot be updated asynchronously."
raise ValueError(error)
elif as_job:
url = "?".join([url, "asJob=True"])

url = "{0}/{1}".format(self.baseurl, group_item.id)
update_req = RequestFactory.Group.update_req(group_item, None)
server_response = self.put_request(url, update_req)
logger.info("Updated group item (ID: {0})".format(group_item.id))
Expand Down
10 changes: 10 additions & 0 deletions test/assets/group_update_async.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version='1.0' encoding='UTF-8'?>
<tsResponse xmlns="http://tableau.com/api"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://tableau.com/api http://tableau.com/api/ts-api-2.3.xsd">
<job id="c2566efc-0767-4f15-89cb-56acb4349c1b"
mode="Asynchronous"
type="GroupSync"
progress="0"
createdAt="time-job-created" />
</tsResponse>
19 changes: 18 additions & 1 deletion test/test_group.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
# encoding=utf-8
from pathlib import Path
import unittest
import os
import requests_mock
import tableauserverclient as TSC
from tableauserverclient.datetime_helpers import format_datetime

TEST_ASSET_DIR = os.path.join(os.path.dirname(__file__), "assets")
TEST_ASSET_DIR = Path(__file__).absolute().parent / "assets"

# TEST_ASSET_DIR = os.path.join(os.path.dirname(__file__), "assets")

GET_XML = os.path.join(TEST_ASSET_DIR, "group_get.xml")
POPULATE_USERS = os.path.join(TEST_ASSET_DIR, "group_populate_users.xml")
Expand All @@ -16,6 +19,7 @@
CREATE_GROUP_AD = os.path.join(TEST_ASSET_DIR, "group_create_ad.xml")
CREATE_GROUP_ASYNC = os.path.join(TEST_ASSET_DIR, "group_create_async.xml")
UPDATE_XML = os.path.join(TEST_ASSET_DIR, "group_update.xml")
UPDATE_ASYNC_XML = TEST_ASSET_DIR / "group_update_async.xml"


class GroupTests(unittest.TestCase):
Expand Down Expand Up @@ -245,3 +249,16 @@ def test_update_local_async(self) -> None:
# mimic group returned from server where domain name is set to 'local'
group.domain_name = "local"
self.assertRaises(ValueError, self.server.groups.update, group, as_job=True)

def test_update_ad_async(self) -> None:
group = TSC.GroupItem("myGroup", "example.com")
group._id = "ef8b19c0-43b6-11e6-af50-63f5805dbe3c"
group.minimum_site_role = TSC.UserItem.Roles.Viewer

with requests_mock.mock() as m:
m.put(f"{self.baseurl}/{group.id}?asJob=True", text=UPDATE_ASYNC_XML.read_bytes().decode("utf8"))
job = self.server.groups.update(group, as_job=True)

self.assertEqual(job.id, "c2566efc-0767-4f15-89cb-56acb4349c1b")
self.assertEqual(job.mode, "Asynchronous")
self.assertEqual(job.type, "GroupSync")

0 comments on commit 81af54a

Please sign in to comment.