From c23573f41180794a936ba7a59c2063937cad35c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Veli=20Kerim=20=C3=87elik?= Date: Fri, 13 Dec 2024 13:51:50 +0100 Subject: [PATCH 1/3] allow and handle role list --- src/snowflake/snowpark/session.py | 32 +++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/src/snowflake/snowpark/session.py b/src/snowflake/snowpark/session.py index 0231ad9c870..a8e20279a9a 100644 --- a/src/snowflake/snowpark/session.py +++ b/src/snowflake/snowpark/session.py @@ -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 `_. + 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 `_. + 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: [ , ... ] + 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: From d5f9792e0c770f890bcb2509054fcfda44ca7957 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Veli=20Kerim=20=C3=87elik?= Date: Thu, 26 Dec 2024 05:55:09 +0100 Subject: [PATCH 2/3] remove inner function argument --- src/snowflake/snowpark/session.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/snowflake/snowpark/session.py b/src/snowflake/snowpark/session.py index a8e20279a9a..6aac9832260 100644 --- a/src/snowflake/snowpark/session.py +++ b/src/snowflake/snowpark/session.py @@ -3611,7 +3611,7 @@ def use_secondary_roles(self, roles: Optional[Union[List[str], Literal['all', 'n References: `Snowflake command USE SECONDARY ROLES `_. Args: - roles: list of specific roles or "all" or "none". ``None`` means "none". + roles: 'all' or list of specific roles or 'none'. ``None`` means 'none'. Example 1 Use specific roles as secondary roles: @@ -3629,16 +3629,16 @@ def use_secondary_roles(self, roles: Optional[Union[List[str], Literal['all', 'n >>> session.use_secondary_roles('none') """ - def format_roles(value): + def format_roles(): if not roles: return 'none' - if isinstance(value, List): + if isinstance(roles, List): # format the list according to syntax: [ , ... ] return ', '.join(roles) - return value.lower() + return roles.lower() self._run_query( - f"use secondary roles {format_roles(roles)}" + f"use secondary roles {format_roles()}" ) def _use_object(self, object_name: str, object_type: str) -> None: From 52e66bb20493a717e5f9187a88e8abe3576eed12 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Veli=20Kerim=20=C3=87elik?= Date: Sun, 29 Dec 2024 16:26:36 +0100 Subject: [PATCH 3/3] change order of examples --- src/snowflake/snowpark/session.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/snowflake/snowpark/session.py b/src/snowflake/snowpark/session.py index e6648528bd5..ee8d7581939 100644 --- a/src/snowflake/snowpark/session.py +++ b/src/snowflake/snowpark/session.py @@ -3629,16 +3629,16 @@ def use_secondary_roles(self, roles: Optional[Union[List[str], Literal['all', 'n roles: 'all' or list of specific roles or 'none'. ``None`` means 'none'. Example 1 + Use all roles that have been granted in addition to the current active primary role: + + >>> session.use_secondary_roles('all') + + Example 2 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: