Skip to content
Open
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
11 changes: 10 additions & 1 deletion tableauserverclient/models/schedule_item.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import xml.etree.ElementTree as ET
from datetime import datetime
from typing import Optional, Union
from typing import Optional, Union, TYPE_CHECKING

from defusedxml.ElementTree import fromstring

Expand All @@ -16,6 +16,10 @@
property_is_enum,
)

if TYPE_CHECKING:
from requests import Response


Interval = Union[HourlyInterval, DailyInterval, WeeklyInterval, MonthlyInterval]


Expand Down Expand Up @@ -407,3 +411,8 @@ def _read_warnings(parsed_response, ns):
for warning_xml in all_warning_xml:
warnings.append(warning_xml.get("message", None))
return warnings


def parse_batch_schedule_state(response: "Response", ns) -> list[str]:
xml = fromstring(response.content)
return [text for tag in xml.findall(".//t:scheduleLuid", namespaces=ns) if (text := tag.text)]
49 changes: 48 additions & 1 deletion tableauserverclient/server/endpoint/schedules_endpoint.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
from collections.abc import Iterable
import copy
import logging
import warnings
from collections import namedtuple
from typing import TYPE_CHECKING, Callable, Optional, Union
from typing import TYPE_CHECKING, Any, Callable, Literal, Optional, Union, overload

from .endpoint import Endpoint, api, parameter_added_in
from .exceptions import MissingRequiredFieldError
from tableauserverclient.server import RequestFactory
from tableauserverclient.models import PaginationItem, ScheduleItem, TaskItem, ExtractItem
from tableauserverclient.models.schedule_item import parse_batch_schedule_state

from tableauserverclient.helpers.logging import logger

Expand Down Expand Up @@ -279,3 +281,48 @@ def get_extract_refresh_tasks(
extract_items = ExtractItem.from_response(server_response.content, self.parent_srv.namespace)

return extract_items, pagination_item

@overload
def batch_update_state(
self,
schedules: Iterable[ScheduleItem | str],
state: Literal["active", "suspended"],
update_all: Literal[False] = False,
) -> list[str]: ...

@overload
def batch_update_state(
self, schedules: Any, state: Literal["active", "suspended"], update_all: Literal[True]
) -> list[str]: ...

@api(version="3.27")
def batch_update_state(self, schedules, state, update_all=False) -> list[str]:
"""
Batch update the status of one or more scheudles. If update_all is set,
all schedules on the Tableau Server are affected.

Parameters
----------
schedules: Iterable[ScheudleItem | str] | Any
The schedules to be updated. If update_all=True, this is ignored.

state: Literal["active", "suspended"]
The state of the schedules, whether active or suspended.

update_all: bool
Whether or not to apply the status to all schedules.

Returns
-------
List[str]
The IDs of the affected schedules.
"""
params = {"state": state}
if update_all:
params["updateAll"] = "true"
payload = RequestFactory.Empty.empty_req()
else:
payload = RequestFactory.Schedule.batch_update_state(schedules)

response = self.put_request(self.baseurl, payload, parameters={"params": params})
return parse_batch_schedule_state(response, self.parent_srv.namespace)
10 changes: 10 additions & 0 deletions tableauserverclient/server/request_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,16 @@ def add_datasource_req(self, id_: Optional[str], task_type: str = TaskItem.Type.
def add_flow_req(self, id_: Optional[str], task_type: str = TaskItem.Type.RunFlow) -> bytes:
return self._add_to_req(id_, "flow", task_type)

@_tsrequest_wrapped
def batch_update_state(self, xml: ET.Element, schedules: Iterable[ScheduleItem | str]) -> None:
luids = ET.SubElement(xml, "scheduleLuids")
for schedule in schedules:
luid = getattr(schedule, "id", schedule)
if not isinstance(luid, str):
continue
luid_tag = ET.SubElement(luids, "scheduleLuid")
luid_tag.text = luid


class SiteRequest:
def update_req(self, site_item: "SiteItem", parent_srv: Optional["Server"] = None):
Expand Down
7 changes: 7 additions & 0 deletions test/assets/schedule_batch_update_state.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<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">
<scheduleLuids>
<scheduleLuid>593d2ebf-0d18-4deb-9d21-b113a4902583</scheduleLuid>
<scheduleLuid>cecbb71e-def0-4030-8068-5ae50f51db1c</scheduleLuid>
<scheduleLuid>f39a6e7d-405e-4c07-8c18-95845f9da80e</scheduleLuid>
</scheduleLuids>
</tsResponse>
3 changes: 2 additions & 1 deletion test/test_datasource.py
Original file line number Diff line number Diff line change
Expand Up @@ -895,7 +895,8 @@ def test_publish_description(server: TSC.Server) -> None:
ds_elem = body.find(".//datasource")
assert ds_elem is not None
assert ds_elem.attrib["description"] == "Sample description"



def test_get_datasource_no_owner(server: TSC.Server) -> None:
with requests_mock.mock() as m:
m.get(server.datasources.baseurl, text=GET_NO_OWNER.read_text())
Expand Down
Loading
Loading