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 support for specific roles as secondary roles #2762

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
32 changes: 28 additions & 4 deletions src/snowflake/snowpark/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -3602,19 +3602,43 @@ def use_role(self, role: str) -> None:
"""
self._use_object(role, "role")

def use_secondary_roles(self, roles: Optional[Literal["all", "none"]]) -> None:
def use_secondary_roles(self, roles: Optional[Union[List[str], Literal['all', 'none']]]) -> None:
"""
Specifies the active/current secondary roles for the session.
The currently-active secondary roles set the context that determines whether
the current user has the necessary privileges to perform SQL actions.

References: `Snowflake command USE SECONDARY ROLES <https://docs.snowflake.com/en/sql-reference/sql/use-secondary-roles.html>`_.

Args:
roles: "all" or "none". ``None`` means "none".
roles: list of specific roles or "all" or "none". ``None`` means "none".

References: `Snowflake command USE SECONDARY ROLES <https://docs.snowflake.com/en/sql-reference/sql/use-secondary-roles.html>`_.
Example 1
Use specific roles as secondary roles:

>>> secondary_roles = ['READER_ROLE_A', 'READER_ROLE_B']
>>> session.use_secondary_roles(secondary_roles)

Example 2
Use all roles that have been granted in addition to the current active primary role:

>>> session.use_secondary_roles('all')

Example 3
Use no secondary roles. Disables secondary roles in the current session:

>>> session.use_secondary_roles('none')
"""
def format_roles(value):
if not roles:
return 'none'
if isinstance(value, List):
# format the list according to syntax: <role_name> [ , <role_name> ... ]
return ', '.join(roles)
return value.lower()

self._run_query(
f"use secondary roles {'none' if roles is None else roles.lower()}"
f"use secondary roles {format_roles(roles)}"
)

def _use_object(self, object_name: str, object_type: str) -> None:
Expand Down
Loading