Skip to content

Commit

Permalink
chore: type hint default permissions endpoints (#1493)
Browse files Browse the repository at this point in the history
Resource is not currently an actual type, but an enum-like
holder for literal values. Added a Union for str types to
make mypy happy.

Co-authored-by: Jordan Woods <[email protected]>
  • Loading branch information
jorwoods and jorwoods authored Oct 15, 2024
1 parent d880d52 commit 9f59af1
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 34 deletions.
2 changes: 1 addition & 1 deletion tableauserverclient/models/tableau_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class Resource:
TableauItem = Union[DatasourceItem, FlowItem, MetricItem, ProjectItem, ViewItem, WorkbookItem, VirtualConnectionItem]


def plural_type(content_type: Resource) -> str:
def plural_type(content_type: Union[Resource, str]) -> str:
if content_type == Resource.Lens:
return "lenses"
else:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def __str__(self):
__repr__ = __str__

def update_default_permissions(
self, resource: BaseItem, permissions: Sequence[PermissionsRule], content_type: Resource
self, resource: BaseItem, permissions: Sequence[PermissionsRule], content_type: Union[Resource, str]
) -> list[PermissionsRule]:
url = f"{self.owner_baseurl()}/{resource.id}/default-permissions/{plural_type(content_type)}"
update_req = RequestFactory.Permission.add_req(permissions)
Expand All @@ -50,7 +50,9 @@ def update_default_permissions(

return permissions

def delete_default_permission(self, resource: BaseItem, rule: PermissionsRule, content_type: Resource) -> None:
def delete_default_permission(
self, resource: BaseItem, rule: PermissionsRule, content_type: Union[Resource, str]
) -> None:
for capability, mode in rule.capabilities.items():
# Made readability better but line is too long, will make this look better
url = (
Expand All @@ -72,7 +74,7 @@ def delete_default_permission(self, resource: BaseItem, rule: PermissionsRule, c

logger.info(f"Deleted permission for {rule.grantee.tag_name} {rule.grantee.id} item {resource.id}")

def populate_default_permissions(self, item: BaseItem, content_type: Resource) -> None:
def populate_default_permissions(self, item: BaseItem, content_type: Union[Resource, str]) -> None:
if not item.id:
error = "Server item is missing ID. Item must be retrieved from server first."
raise MissingRequiredFieldError(error)
Expand All @@ -84,7 +86,7 @@ def permission_fetcher() -> list[PermissionsRule]:
logger.info(f"Populated default {content_type} permissions for item (ID: {item.id})")

def _get_default_permissions(
self, item: BaseItem, content_type: Resource, req_options: Optional["RequestOptions"] = None
self, item: BaseItem, content_type: Union[Resource, str], req_options: Optional["RequestOptions"] = None
) -> list[PermissionsRule]:
url = f"{self.owner_baseurl()}/{item.id}/default-permissions/{plural_type(content_type)}"
server_response = self.get_request(url, req_options)
Expand Down
73 changes: 44 additions & 29 deletions tableauserverclient/server/endpoint/projects_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from tableauserverclient.server.endpoint.exceptions import MissingRequiredFieldError
from tableauserverclient.server.endpoint.permissions_endpoint import _PermissionsEndpoint
from tableauserverclient.server import RequestFactory, RequestOptions
from tableauserverclient.models.permissions_item import PermissionsRule
from tableauserverclient.models import ProjectItem, PaginationItem, Resource

from typing import Optional, TYPE_CHECKING
Expand Down Expand Up @@ -78,119 +79,133 @@ def populate_permissions(self, item: ProjectItem) -> None:
self._permissions.populate(item)

@api(version="2.0")
def update_permissions(self, item, rules):
def update_permissions(self, item: ProjectItem, rules: list[PermissionsRule]) -> list[PermissionsRule]:
return self._permissions.update(item, rules)

@api(version="2.0")
def delete_permission(self, item, rules):
def delete_permission(self, item: ProjectItem, rules: list[PermissionsRule]) -> None:
self._permissions.delete(item, rules)

@api(version="2.1")
def populate_workbook_default_permissions(self, item):
def populate_workbook_default_permissions(self, item: ProjectItem) -> None:
self._default_permissions.populate_default_permissions(item, Resource.Workbook)

@api(version="2.1")
def populate_datasource_default_permissions(self, item):
def populate_datasource_default_permissions(self, item: ProjectItem) -> None:
self._default_permissions.populate_default_permissions(item, Resource.Datasource)

@api(version="3.2")
def populate_metric_default_permissions(self, item):
def populate_metric_default_permissions(self, item: ProjectItem) -> None:
self._default_permissions.populate_default_permissions(item, Resource.Metric)

@api(version="3.4")
def populate_datarole_default_permissions(self, item):
def populate_datarole_default_permissions(self, item: ProjectItem) -> None:
self._default_permissions.populate_default_permissions(item, Resource.Datarole)

@api(version="3.4")
def populate_flow_default_permissions(self, item):
def populate_flow_default_permissions(self, item: ProjectItem) -> None:
self._default_permissions.populate_default_permissions(item, Resource.Flow)

@api(version="3.4")
def populate_lens_default_permissions(self, item):
def populate_lens_default_permissions(self, item: ProjectItem) -> None:
self._default_permissions.populate_default_permissions(item, Resource.Lens)

@api(version="3.23")
def populate_virtualconnection_default_permissions(self, item):
def populate_virtualconnection_default_permissions(self, item: ProjectItem) -> None:
self._default_permissions.populate_default_permissions(item, Resource.VirtualConnection)

@api(version="3.23")
def populate_database_default_permissions(self, item):
def populate_database_default_permissions(self, item: ProjectItem) -> None:
self._default_permissions.populate_default_permissions(item, Resource.Database)

@api(version="3.23")
def populate_table_default_permissions(self, item):
def populate_table_default_permissions(self, item: ProjectItem) -> None:
self._default_permissions.populate_default_permissions(item, Resource.Table)

@api(version="2.1")
def update_workbook_default_permissions(self, item, rules):
def update_workbook_default_permissions(
self, item: ProjectItem, rules: list[PermissionsRule]
) -> list[PermissionsRule]:
return self._default_permissions.update_default_permissions(item, rules, Resource.Workbook)

@api(version="2.1")
def update_datasource_default_permissions(self, item, rules):
def update_datasource_default_permissions(
self, item: ProjectItem, rules: list[PermissionsRule]
) -> list[PermissionsRule]:
return self._default_permissions.update_default_permissions(item, rules, Resource.Datasource)

@api(version="3.2")
def update_metric_default_permissions(self, item, rules):
def update_metric_default_permissions(
self, item: ProjectItem, rules: list[PermissionsRule]
) -> list[PermissionsRule]:
return self._default_permissions.update_default_permissions(item, rules, Resource.Metric)

@api(version="3.4")
def update_datarole_default_permissions(self, item, rules):
def update_datarole_default_permissions(
self, item: ProjectItem, rules: list[PermissionsRule]
) -> list[PermissionsRule]:
return self._default_permissions.update_default_permissions(item, rules, Resource.Datarole)

@api(version="3.4")
def update_flow_default_permissions(self, item, rules):
def update_flow_default_permissions(self, item: ProjectItem, rules: list[PermissionsRule]) -> list[PermissionsRule]:
return self._default_permissions.update_default_permissions(item, rules, Resource.Flow)

@api(version="3.4")
def update_lens_default_permissions(self, item, rules):
def update_lens_default_permissions(self, item: ProjectItem, rules: list[PermissionsRule]) -> list[PermissionsRule]:
return self._default_permissions.update_default_permissions(item, rules, Resource.Lens)

@api(version="3.23")
def update_virtualconnection_default_permissions(self, item, rules):
def update_virtualconnection_default_permissions(
self, item: ProjectItem, rules: list[PermissionsRule]
) -> list[PermissionsRule]:
return self._default_permissions.update_default_permissions(item, rules, Resource.VirtualConnection)

@api(version="3.23")
def update_database_default_permissions(self, item, rules):
def update_database_default_permissions(
self, item: ProjectItem, rules: list[PermissionsRule]
) -> list[PermissionsRule]:
return self._default_permissions.update_default_permissions(item, rules, Resource.Database)

@api(version="3.23")
def update_table_default_permissions(self, item, rules):
def update_table_default_permissions(
self, item: ProjectItem, rules: list[PermissionsRule]
) -> list[PermissionsRule]:
return self._default_permissions.update_default_permissions(item, rules, Resource.Table)

@api(version="2.1")
def delete_workbook_default_permissions(self, item, rule):
def delete_workbook_default_permissions(self, item: ProjectItem, rule: PermissionsRule) -> None:
self._default_permissions.delete_default_permission(item, rule, Resource.Workbook)

@api(version="2.1")
def delete_datasource_default_permissions(self, item, rule):
def delete_datasource_default_permissions(self, item: ProjectItem, rule: PermissionsRule) -> None:
self._default_permissions.delete_default_permission(item, rule, Resource.Datasource)

@api(version="3.2")
def delete_metric_default_permissions(self, item, rule):
def delete_metric_default_permissions(self, item: ProjectItem, rule: PermissionsRule) -> None:
self._default_permissions.delete_default_permission(item, rule, Resource.Metric)

@api(version="3.4")
def delete_datarole_default_permissions(self, item, rule):
def delete_datarole_default_permissions(self, item: ProjectItem, rule: PermissionsRule) -> None:
self._default_permissions.delete_default_permission(item, rule, Resource.Datarole)

@api(version="3.4")
def delete_flow_default_permissions(self, item, rule):
def delete_flow_default_permissions(self, item: ProjectItem, rule: PermissionsRule) -> None:
self._default_permissions.delete_default_permission(item, rule, Resource.Flow)

@api(version="3.4")
def delete_lens_default_permissions(self, item, rule):
def delete_lens_default_permissions(self, item: ProjectItem, rule: PermissionsRule) -> None:
self._default_permissions.delete_default_permission(item, rule, Resource.Lens)

@api(version="3.23")
def delete_virtualconnection_default_permissions(self, item, rule):
def delete_virtualconnection_default_permissions(self, item: ProjectItem, rule: PermissionsRule) -> None:
self._default_permissions.delete_default_permission(item, rule, Resource.VirtualConnection)

@api(version="3.23")
def delete_database_default_permissions(self, item, rule):
def delete_database_default_permissions(self, item: ProjectItem, rule: PermissionsRule) -> None:
self._default_permissions.delete_default_permission(item, rule, Resource.Database)

@api(version="3.23")
def delete_table_default_permissions(self, item, rule):
def delete_table_default_permissions(self, item: ProjectItem, rule: PermissionsRule) -> None:
self._default_permissions.delete_default_permission(item, rule, Resource.Table)

def filter(self, *invalid, page_size: Optional[int] = None, **kwargs) -> QuerySet[ProjectItem]:
Expand Down

0 comments on commit 9f59af1

Please sign in to comment.