snowflake-cli 2.8.2__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (240) hide show
  1. snowflake/cli/__about__.py +17 -0
  2. snowflake/cli/__init__.py +13 -0
  3. snowflake/cli/api/__init__.py +48 -0
  4. snowflake/cli/api/cli_global_context.py +390 -0
  5. snowflake/cli/api/commands/__init__.py +13 -0
  6. snowflake/cli/api/commands/alias.py +23 -0
  7. snowflake/cli/api/commands/decorators.py +354 -0
  8. snowflake/cli/api/commands/execution_metadata.py +40 -0
  9. snowflake/cli/api/commands/experimental_behaviour.py +19 -0
  10. snowflake/cli/api/commands/flags.py +662 -0
  11. snowflake/cli/api/commands/project_initialisation.py +65 -0
  12. snowflake/cli/api/commands/snow_typer.py +237 -0
  13. snowflake/cli/api/commands/typer_pre_execute.py +26 -0
  14. snowflake/cli/api/config.py +348 -0
  15. snowflake/cli/api/console/__init__.py +17 -0
  16. snowflake/cli/api/console/abc.py +89 -0
  17. snowflake/cli/api/console/console.py +134 -0
  18. snowflake/cli/api/console/enum.py +17 -0
  19. snowflake/cli/api/constants.py +79 -0
  20. snowflake/cli/api/errno.py +27 -0
  21. snowflake/cli/api/exceptions.py +164 -0
  22. snowflake/cli/api/feature_flags.py +55 -0
  23. snowflake/cli/api/identifiers.py +167 -0
  24. snowflake/cli/api/output/__init__.py +13 -0
  25. snowflake/cli/api/output/formats.py +20 -0
  26. snowflake/cli/api/output/types.py +118 -0
  27. snowflake/cli/api/plugins/__init__.py +13 -0
  28. snowflake/cli/api/plugins/command/__init__.py +72 -0
  29. snowflake/cli/api/plugins/command/plugin_hook_specs.py +21 -0
  30. snowflake/cli/api/plugins/plugin_config.py +32 -0
  31. snowflake/cli/api/project/__init__.py +13 -0
  32. snowflake/cli/api/project/definition.py +84 -0
  33. snowflake/cli/api/project/definition_manager.py +134 -0
  34. snowflake/cli/api/project/errors.py +56 -0
  35. snowflake/cli/api/project/project_verification.py +23 -0
  36. snowflake/cli/api/project/schemas/__init__.py +13 -0
  37. snowflake/cli/api/project/schemas/entities/application_entity.py +44 -0
  38. snowflake/cli/api/project/schemas/entities/application_package_entity.py +66 -0
  39. snowflake/cli/api/project/schemas/entities/common.py +78 -0
  40. snowflake/cli/api/project/schemas/entities/entities.py +30 -0
  41. snowflake/cli/api/project/schemas/identifier_model.py +49 -0
  42. snowflake/cli/api/project/schemas/native_app/__init__.py +13 -0
  43. snowflake/cli/api/project/schemas/native_app/application.py +62 -0
  44. snowflake/cli/api/project/schemas/native_app/native_app.py +93 -0
  45. snowflake/cli/api/project/schemas/native_app/package.py +78 -0
  46. snowflake/cli/api/project/schemas/native_app/path_mapping.py +65 -0
  47. snowflake/cli/api/project/schemas/project_definition.py +199 -0
  48. snowflake/cli/api/project/schemas/snowpark/__init__.py +13 -0
  49. snowflake/cli/api/project/schemas/snowpark/argument.py +28 -0
  50. snowflake/cli/api/project/schemas/snowpark/callable.py +69 -0
  51. snowflake/cli/api/project/schemas/snowpark/snowpark.py +36 -0
  52. snowflake/cli/api/project/schemas/streamlit/__init__.py +13 -0
  53. snowflake/cli/api/project/schemas/streamlit/streamlit.py +46 -0
  54. snowflake/cli/api/project/schemas/template.py +77 -0
  55. snowflake/cli/api/project/schemas/updatable_model.py +194 -0
  56. snowflake/cli/api/project/util.py +261 -0
  57. snowflake/cli/api/rendering/__init__.py +13 -0
  58. snowflake/cli/api/rendering/jinja.py +112 -0
  59. snowflake/cli/api/rendering/project_definition_templates.py +39 -0
  60. snowflake/cli/api/rendering/project_templates.py +98 -0
  61. snowflake/cli/api/rendering/sql_templates.py +60 -0
  62. snowflake/cli/api/rest_api.py +172 -0
  63. snowflake/cli/api/sanitizers.py +43 -0
  64. snowflake/cli/api/secure_path.py +362 -0
  65. snowflake/cli/api/secure_utils.py +29 -0
  66. snowflake/cli/api/sql_execution.py +260 -0
  67. snowflake/cli/api/utils/__init__.py +13 -0
  68. snowflake/cli/api/utils/cursor.py +34 -0
  69. snowflake/cli/api/utils/definition_rendering.py +383 -0
  70. snowflake/cli/api/utils/dict_utils.py +73 -0
  71. snowflake/cli/api/utils/error_handling.py +23 -0
  72. snowflake/cli/api/utils/graph.py +97 -0
  73. snowflake/cli/api/utils/models.py +63 -0
  74. snowflake/cli/api/utils/naming_utils.py +13 -0
  75. snowflake/cli/api/utils/path_utils.py +36 -0
  76. snowflake/cli/api/utils/templating_functions.py +144 -0
  77. snowflake/cli/api/utils/types.py +35 -0
  78. snowflake/cli/app/__init__.py +22 -0
  79. snowflake/cli/app/__main__.py +31 -0
  80. snowflake/cli/app/api_impl/__init__.py +13 -0
  81. snowflake/cli/app/api_impl/plugin/__init__.py +13 -0
  82. snowflake/cli/app/api_impl/plugin/plugin_config_provider_impl.py +66 -0
  83. snowflake/cli/app/build_and_push.sh +8 -0
  84. snowflake/cli/app/cli_app.py +243 -0
  85. snowflake/cli/app/commands_registration/__init__.py +33 -0
  86. snowflake/cli/app/commands_registration/builtin_plugins.py +54 -0
  87. snowflake/cli/app/commands_registration/command_plugins_loader.py +169 -0
  88. snowflake/cli/app/commands_registration/commands_registration_with_callbacks.py +105 -0
  89. snowflake/cli/app/commands_registration/exception_logging.py +26 -0
  90. snowflake/cli/app/commands_registration/threadsafe.py +48 -0
  91. snowflake/cli/app/commands_registration/typer_registration.py +153 -0
  92. snowflake/cli/app/constants.py +19 -0
  93. snowflake/cli/app/dev/__init__.py +13 -0
  94. snowflake/cli/app/dev/commands_structure.py +48 -0
  95. snowflake/cli/app/dev/docs/__init__.py +13 -0
  96. snowflake/cli/app/dev/docs/commands_docs_generator.py +100 -0
  97. snowflake/cli/app/dev/docs/generator.py +35 -0
  98. snowflake/cli/app/dev/docs/project_definition_docs_generator.py +58 -0
  99. snowflake/cli/app/dev/docs/project_definition_generate_json_schema.py +227 -0
  100. snowflake/cli/app/dev/docs/template_utils.py +23 -0
  101. snowflake/cli/app/dev/docs/templates/definition_description.rst.jinja2 +38 -0
  102. snowflake/cli/app/dev/docs/templates/overview.rst.jinja2 +9 -0
  103. snowflake/cli/app/dev/docs/templates/usage.rst.jinja2 +57 -0
  104. snowflake/cli/app/dev/pycharm_remote_debug.py +46 -0
  105. snowflake/cli/app/loggers.py +199 -0
  106. snowflake/cli/app/main_typer.py +62 -0
  107. snowflake/cli/app/printing.py +181 -0
  108. snowflake/cli/app/snow_connector.py +243 -0
  109. snowflake/cli/app/telemetry.py +189 -0
  110. snowflake/cli/plugins/__init__.py +13 -0
  111. snowflake/cli/plugins/connection/__init__.py +13 -0
  112. snowflake/cli/plugins/connection/commands.py +330 -0
  113. snowflake/cli/plugins/connection/plugin_spec.py +30 -0
  114. snowflake/cli/plugins/connection/util.py +179 -0
  115. snowflake/cli/plugins/cortex/__init__.py +13 -0
  116. snowflake/cli/plugins/cortex/commands.py +327 -0
  117. snowflake/cli/plugins/cortex/constants.py +17 -0
  118. snowflake/cli/plugins/cortex/manager.py +189 -0
  119. snowflake/cli/plugins/cortex/plugin_spec.py +30 -0
  120. snowflake/cli/plugins/cortex/types.py +22 -0
  121. snowflake/cli/plugins/git/__init__.py +13 -0
  122. snowflake/cli/plugins/git/commands.py +354 -0
  123. snowflake/cli/plugins/git/manager.py +105 -0
  124. snowflake/cli/plugins/git/plugin_spec.py +30 -0
  125. snowflake/cli/plugins/init/__init__.py +13 -0
  126. snowflake/cli/plugins/init/commands.py +248 -0
  127. snowflake/cli/plugins/init/plugin_spec.py +30 -0
  128. snowflake/cli/plugins/nativeapp/__init__.py +13 -0
  129. snowflake/cli/plugins/nativeapp/artifacts.py +742 -0
  130. snowflake/cli/plugins/nativeapp/codegen/__init__.py +13 -0
  131. snowflake/cli/plugins/nativeapp/codegen/artifact_processor.py +91 -0
  132. snowflake/cli/plugins/nativeapp/codegen/compiler.py +130 -0
  133. snowflake/cli/plugins/nativeapp/codegen/sandbox.py +306 -0
  134. snowflake/cli/plugins/nativeapp/codegen/setup/native_app_setup_processor.py +172 -0
  135. snowflake/cli/plugins/nativeapp/codegen/setup/setup_driver.py.source +56 -0
  136. snowflake/cli/plugins/nativeapp/codegen/snowpark/callback_source.py.jinja +181 -0
  137. snowflake/cli/plugins/nativeapp/codegen/snowpark/extension_function_utils.py +217 -0
  138. snowflake/cli/plugins/nativeapp/codegen/snowpark/models.py +61 -0
  139. snowflake/cli/plugins/nativeapp/codegen/snowpark/python_processor.py +528 -0
  140. snowflake/cli/plugins/nativeapp/commands.py +439 -0
  141. snowflake/cli/plugins/nativeapp/common_flags.py +44 -0
  142. snowflake/cli/plugins/nativeapp/constants.py +27 -0
  143. snowflake/cli/plugins/nativeapp/exceptions.py +122 -0
  144. snowflake/cli/plugins/nativeapp/feature_flags.py +24 -0
  145. snowflake/cli/plugins/nativeapp/init.py +345 -0
  146. snowflake/cli/plugins/nativeapp/manager.py +823 -0
  147. snowflake/cli/plugins/nativeapp/plugin_spec.py +30 -0
  148. snowflake/cli/plugins/nativeapp/policy.py +50 -0
  149. snowflake/cli/plugins/nativeapp/project_model.py +195 -0
  150. snowflake/cli/plugins/nativeapp/run_processor.py +389 -0
  151. snowflake/cli/plugins/nativeapp/teardown_processor.py +301 -0
  152. snowflake/cli/plugins/nativeapp/utils.py +98 -0
  153. snowflake/cli/plugins/nativeapp/v2_conversions/v2_to_v1_decorator.py +135 -0
  154. snowflake/cli/plugins/nativeapp/version/__init__.py +13 -0
  155. snowflake/cli/plugins/nativeapp/version/commands.py +170 -0
  156. snowflake/cli/plugins/nativeapp/version/version_processor.py +362 -0
  157. snowflake/cli/plugins/notebook/__init__.py +13 -0
  158. snowflake/cli/plugins/notebook/commands.py +85 -0
  159. snowflake/cli/plugins/notebook/exceptions.py +20 -0
  160. snowflake/cli/plugins/notebook/manager.py +71 -0
  161. snowflake/cli/plugins/notebook/plugin_spec.py +30 -0
  162. snowflake/cli/plugins/notebook/types.py +15 -0
  163. snowflake/cli/plugins/object/__init__.py +13 -0
  164. snowflake/cli/plugins/object/command_aliases.py +95 -0
  165. snowflake/cli/plugins/object/commands.py +181 -0
  166. snowflake/cli/plugins/object/common.py +85 -0
  167. snowflake/cli/plugins/object/manager.py +97 -0
  168. snowflake/cli/plugins/object/plugin_spec.py +30 -0
  169. snowflake/cli/plugins/object_stage_deprecated/__init__.py +15 -0
  170. snowflake/cli/plugins/object_stage_deprecated/commands.py +122 -0
  171. snowflake/cli/plugins/object_stage_deprecated/plugin_spec.py +32 -0
  172. snowflake/cli/plugins/snowpark/__init__.py +13 -0
  173. snowflake/cli/plugins/snowpark/commands.py +546 -0
  174. snowflake/cli/plugins/snowpark/common.py +307 -0
  175. snowflake/cli/plugins/snowpark/manager.py +109 -0
  176. snowflake/cli/plugins/snowpark/models.py +157 -0
  177. snowflake/cli/plugins/snowpark/package/__init__.py +13 -0
  178. snowflake/cli/plugins/snowpark/package/anaconda_packages.py +233 -0
  179. snowflake/cli/plugins/snowpark/package/commands.py +256 -0
  180. snowflake/cli/plugins/snowpark/package/manager.py +44 -0
  181. snowflake/cli/plugins/snowpark/package/utils.py +26 -0
  182. snowflake/cli/plugins/snowpark/package_utils.py +354 -0
  183. snowflake/cli/plugins/snowpark/plugin_spec.py +30 -0
  184. snowflake/cli/plugins/snowpark/snowpark_package_paths.py +65 -0
  185. snowflake/cli/plugins/snowpark/snowpark_shared.py +95 -0
  186. snowflake/cli/plugins/snowpark/zipper.py +81 -0
  187. snowflake/cli/plugins/spcs/__init__.py +35 -0
  188. snowflake/cli/plugins/spcs/common.py +99 -0
  189. snowflake/cli/plugins/spcs/compute_pool/__init__.py +13 -0
  190. snowflake/cli/plugins/spcs/compute_pool/commands.py +241 -0
  191. snowflake/cli/plugins/spcs/compute_pool/manager.py +121 -0
  192. snowflake/cli/plugins/spcs/image_registry/__init__.py +13 -0
  193. snowflake/cli/plugins/spcs/image_registry/commands.py +65 -0
  194. snowflake/cli/plugins/spcs/image_registry/manager.py +105 -0
  195. snowflake/cli/plugins/spcs/image_repository/__init__.py +13 -0
  196. snowflake/cli/plugins/spcs/image_repository/commands.py +202 -0
  197. snowflake/cli/plugins/spcs/image_repository/manager.py +84 -0
  198. snowflake/cli/plugins/spcs/jobs/__init__.py +13 -0
  199. snowflake/cli/plugins/spcs/jobs/commands.py +78 -0
  200. snowflake/cli/plugins/spcs/jobs/manager.py +53 -0
  201. snowflake/cli/plugins/spcs/plugin_spec.py +30 -0
  202. snowflake/cli/plugins/spcs/services/__init__.py +13 -0
  203. snowflake/cli/plugins/spcs/services/commands.py +312 -0
  204. snowflake/cli/plugins/spcs/services/manager.py +170 -0
  205. snowflake/cli/plugins/sql/__init__.py +13 -0
  206. snowflake/cli/plugins/sql/commands.py +83 -0
  207. snowflake/cli/plugins/sql/manager.py +92 -0
  208. snowflake/cli/plugins/sql/plugin_spec.py +30 -0
  209. snowflake/cli/plugins/sql/snowsql_templating.py +28 -0
  210. snowflake/cli/plugins/stage/__init__.py +13 -0
  211. snowflake/cli/plugins/stage/commands.py +263 -0
  212. snowflake/cli/plugins/stage/diff.py +326 -0
  213. snowflake/cli/plugins/stage/manager.py +577 -0
  214. snowflake/cli/plugins/stage/md5.py +160 -0
  215. snowflake/cli/plugins/stage/plugin_spec.py +30 -0
  216. snowflake/cli/plugins/streamlit/__init__.py +13 -0
  217. snowflake/cli/plugins/streamlit/commands.py +179 -0
  218. snowflake/cli/plugins/streamlit/manager.py +222 -0
  219. snowflake/cli/plugins/streamlit/plugin_spec.py +30 -0
  220. snowflake/cli/plugins/workspace/__init__.py +13 -0
  221. snowflake/cli/plugins/workspace/commands.py +35 -0
  222. snowflake/cli/plugins/workspace/plugin_spec.py +30 -0
  223. snowflake/cli/templates/default_snowpark/.gitignore +4 -0
  224. snowflake/cli/templates/default_snowpark/app/__init__.py +0 -0
  225. snowflake/cli/templates/default_snowpark/app/common.py +2 -0
  226. snowflake/cli/templates/default_snowpark/app/functions.py +15 -0
  227. snowflake/cli/templates/default_snowpark/app/procedures.py +22 -0
  228. snowflake/cli/templates/default_snowpark/requirements.txt +1 -0
  229. snowflake/cli/templates/default_snowpark/snowflake.yml +23 -0
  230. snowflake/cli/templates/default_streamlit/.gitignore +4 -0
  231. snowflake/cli/templates/default_streamlit/common/hello.py +2 -0
  232. snowflake/cli/templates/default_streamlit/environment.yml +6 -0
  233. snowflake/cli/templates/default_streamlit/pages/my_page.py +3 -0
  234. snowflake/cli/templates/default_streamlit/snowflake.yml +10 -0
  235. snowflake/cli/templates/default_streamlit/streamlit_app.py +4 -0
  236. snowflake_cli-2.8.2.dist-info/METADATA +325 -0
  237. snowflake_cli-2.8.2.dist-info/RECORD +240 -0
  238. snowflake_cli-2.8.2.dist-info/WHEEL +4 -0
  239. snowflake_cli-2.8.2.dist-info/entry_points.txt +2 -0
  240. snowflake_cli-2.8.2.dist-info/licenses/LICENSE +201 -0
@@ -0,0 +1,71 @@
1
+ # Copyright (c) 2024 Snowflake Inc.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ from pathlib import Path
16
+ from textwrap import dedent
17
+
18
+ from snowflake.cli.api.cli_global_context import cli_context
19
+ from snowflake.cli.api.identifiers import FQN
20
+ from snowflake.cli.api.sql_execution import SqlExecutionMixin
21
+ from snowflake.cli.plugins.connection.util import make_snowsight_url
22
+ from snowflake.cli.plugins.notebook.exceptions import NotebookStagePathError
23
+ from snowflake.cli.plugins.notebook.types import NotebookStagePath
24
+
25
+
26
+ class NotebookManager(SqlExecutionMixin):
27
+ def execute(self, notebook_name: FQN):
28
+ query = f"EXECUTE NOTEBOOK {notebook_name.sql_identifier}()"
29
+ return self._execute_query(query=query)
30
+
31
+ def get_url(self, notebook_name: FQN):
32
+ fqn = notebook_name.using_connection(self._conn)
33
+ return make_snowsight_url(
34
+ self._conn,
35
+ f"/#/notebooks/{fqn.url_identifier}",
36
+ )
37
+
38
+ @staticmethod
39
+ def parse_stage_as_path(notebook_file: str) -> Path:
40
+ """Parses notebook file path to pathlib.Path."""
41
+ if not notebook_file.endswith(".ipynb"):
42
+ raise NotebookStagePathError(notebook_file)
43
+ stage_path = Path(notebook_file)
44
+ if len(stage_path.parts) < 2:
45
+ raise NotebookStagePathError(notebook_file)
46
+
47
+ return stage_path
48
+
49
+ def create(
50
+ self,
51
+ notebook_name: FQN,
52
+ notebook_file: NotebookStagePath,
53
+ ) -> str:
54
+ notebook_fqn = notebook_name.using_connection(self._conn)
55
+ stage_path = self.parse_stage_as_path(notebook_file)
56
+
57
+ queries = dedent(
58
+ f"""
59
+ CREATE OR REPLACE NOTEBOOK {notebook_fqn.sql_identifier}
60
+ FROM '{stage_path.parent}'
61
+ QUERY_WAREHOUSE = '{cli_context.connection.warehouse}'
62
+ MAIN_FILE = '{stage_path.name}';
63
+ // Cannot use IDENTIFIER(...)
64
+ ALTER NOTEBOOK {notebook_fqn.identifier} ADD LIVE VERSION FROM LAST;
65
+ """
66
+ )
67
+ self._execute_queries(queries=queries)
68
+
69
+ return make_snowsight_url(
70
+ self._conn, f"/#/notebooks/{notebook_fqn.url_identifier}"
71
+ )
@@ -0,0 +1,30 @@
1
+ # Copyright (c) 2024 Snowflake Inc.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ from snowflake.cli.api.plugins.command import (
16
+ SNOWCLI_ROOT_COMMAND_PATH,
17
+ CommandSpec,
18
+ CommandType,
19
+ plugin_hook_impl,
20
+ )
21
+ from snowflake.cli.plugins.notebook import commands
22
+
23
+
24
+ @plugin_hook_impl
25
+ def command_spec():
26
+ return CommandSpec(
27
+ parent_command_path=SNOWCLI_ROOT_COMMAND_PATH,
28
+ command_type=CommandType.COMMAND_GROUP,
29
+ typer_instance=commands.app.create_instance(),
30
+ )
@@ -0,0 +1,15 @@
1
+ # Copyright (c) 2024 Snowflake Inc.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ NotebookStagePath = str
@@ -0,0 +1,13 @@
1
+ # Copyright (c) 2024 Snowflake Inc.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
@@ -0,0 +1,95 @@
1
+ # Copyright (c) 2024 Snowflake Inc.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ from __future__ import annotations
16
+
17
+ from typing import List, Optional, Tuple
18
+
19
+ import typer
20
+ from click import ClickException
21
+ from snowflake.cli.api.commands.snow_typer import SnowTyperFactory
22
+ from snowflake.cli.api.constants import ObjectType
23
+ from snowflake.cli.api.identifiers import FQN
24
+ from snowflake.cli.plugins.object.commands import (
25
+ ScopeOption,
26
+ describe,
27
+ drop,
28
+ list_,
29
+ scope_option, # noqa: F401
30
+ )
31
+
32
+
33
+ def add_object_command_aliases(
34
+ app: SnowTyperFactory,
35
+ object_type: ObjectType,
36
+ name_argument: typer.Argument,
37
+ like_option: Optional[typer.Option],
38
+ scope_option: Optional[typer.Option],
39
+ ommit_commands: List[str] = [],
40
+ ):
41
+ if "list" not in ommit_commands:
42
+ if not like_option:
43
+ raise ClickException('[like_option] have to be defined for "list" command')
44
+
45
+ if not scope_option:
46
+
47
+ @app.command("list", requires_connection=True)
48
+ def list_cmd(like: str = like_option, **options): # type: ignore
49
+ return list_(
50
+ object_type=object_type.value.cli_name,
51
+ like=like,
52
+ scope=ScopeOption.default,
53
+ **options,
54
+ )
55
+
56
+ else:
57
+
58
+ @app.command("list", requires_connection=True)
59
+ def list_cmd(
60
+ like: str = like_option, # type: ignore
61
+ scope: Tuple[str, str] = scope_option, # type: ignore
62
+ **options,
63
+ ):
64
+ return list_(
65
+ object_type=object_type.value.cli_name,
66
+ like=like,
67
+ scope=scope,
68
+ **options,
69
+ )
70
+
71
+ list_cmd.__doc__ = f"Lists all available {object_type.value.sf_plural_name}."
72
+
73
+ if "drop" not in ommit_commands:
74
+
75
+ @app.command("drop", requires_connection=True)
76
+ def drop_cmd(name: FQN = name_argument, **options):
77
+ return drop(
78
+ object_type=object_type.value.cli_name,
79
+ object_name=name,
80
+ **options,
81
+ )
82
+
83
+ drop_cmd.__doc__ = f"Drops {object_type.value.sf_name} with given name."
84
+
85
+ if "describe" not in ommit_commands:
86
+
87
+ @app.command("describe", requires_connection=True)
88
+ def describe_cmd(name: FQN = name_argument, **options):
89
+ return describe(
90
+ object_type=object_type.value.cli_name,
91
+ object_name=name,
92
+ **options,
93
+ )
94
+
95
+ describe_cmd.__doc__ = f"Provides description of {object_type.value.sf_name}."
@@ -0,0 +1,181 @@
1
+ # Copyright (c) 2024 Snowflake Inc.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ from __future__ import annotations
16
+
17
+ from typing import List, Optional, Tuple
18
+
19
+ import typer
20
+ from click import ClickException
21
+ from snowflake.cli.api.commands.flags import (
22
+ IdentifierType,
23
+ like_option,
24
+ parse_key_value_variables,
25
+ )
26
+ from snowflake.cli.api.commands.snow_typer import SnowTyperFactory
27
+ from snowflake.cli.api.constants import SUPPORTED_OBJECTS, VALID_SCOPES
28
+ from snowflake.cli.api.identifiers import FQN
29
+ from snowflake.cli.api.output.types import MessageResult, QueryResult
30
+ from snowflake.cli.api.project.util import is_valid_identifier
31
+ from snowflake.cli.plugins.object.manager import ObjectManager
32
+
33
+ app = SnowTyperFactory(
34
+ name="object",
35
+ help="Manages Snowflake objects like warehouses and stages",
36
+ )
37
+
38
+
39
+ NameArgument = typer.Argument(
40
+ help="Name of the object.", show_default=False, click_type=IdentifierType()
41
+ )
42
+ ObjectArgument = typer.Argument(
43
+ help="Type of object. For example table, database, compute-pool.",
44
+ case_sensitive=False,
45
+ show_default=False,
46
+ )
47
+ # TODO: add documentation link
48
+ ObjectAttributesArgument = typer.Argument(
49
+ None,
50
+ help="""Object attributes provided as a list of key=value pairs,
51
+ for example name=my_db comment='created with Snowflake CLI'.
52
+
53
+ Check documentation for the full list of available parameters
54
+ for every object.
55
+ """,
56
+ show_default=False,
57
+ )
58
+ # TODO: add documentation link
59
+ ObjectDefinitionJsonOption = typer.Option(
60
+ None,
61
+ "--json",
62
+ help="""Object definition in JSON format, for example
63
+ \'{"name": "my_db", "comment": "created with Snowflake CLI"}\'.
64
+
65
+ Check documentation for the full list of available parameters
66
+ for every object.
67
+ """,
68
+ show_default=False,
69
+ )
70
+ LikeOption = like_option(
71
+ help_example='`list function --like "my%"` lists all functions that begin with “my”',
72
+ )
73
+
74
+
75
+ def _scope_validate(object_type: str, scope: Tuple[str, str]):
76
+ if scope[1] is not None and not is_valid_identifier(scope[1]):
77
+ raise ClickException("scope name must be a valid identifier")
78
+ if scope[0] is not None and scope[0].lower() not in VALID_SCOPES:
79
+ raise ClickException(
80
+ f"scope must be one of the following: {', '.join(VALID_SCOPES)}"
81
+ )
82
+ if scope[0] == "compute-pool" and object_type != "service":
83
+ raise ClickException("compute-pool scope is only supported for listing service")
84
+
85
+
86
+ def scope_option(help_example: str):
87
+ return typer.Option(
88
+ (None, None),
89
+ "--in",
90
+ help=f"Specifies the scope of this command using '--in <scope> <name>', for example {help_example}.",
91
+ )
92
+
93
+
94
+ ScopeOption = scope_option(
95
+ help_example="`list table --in database my_db`. Some object types have specialized scopes (e.g. list service --in compute-pool my_pool)"
96
+ )
97
+
98
+ SUPPORTED_TYPES_MSG = "\n\nSupported types: " + ", ".join(SUPPORTED_OBJECTS)
99
+
100
+
101
+ @app.command(
102
+ "list",
103
+ help=f"Lists all available Snowflake objects of given type.{SUPPORTED_TYPES_MSG}",
104
+ requires_connection=True,
105
+ )
106
+ def list_(
107
+ object_type: str = ObjectArgument,
108
+ like: str = LikeOption,
109
+ scope: Tuple[str, str] = ScopeOption,
110
+ **options,
111
+ ):
112
+ _scope_validate(object_type, scope)
113
+ return QueryResult(
114
+ ObjectManager().show(object_type=object_type, like=like, scope=scope)
115
+ )
116
+
117
+
118
+ @app.command(
119
+ help=f"Drops Snowflake object of given name and type. {SUPPORTED_TYPES_MSG}",
120
+ requires_connection=True,
121
+ )
122
+ def drop(object_type: str = ObjectArgument, object_name: FQN = NameArgument, **options):
123
+ return QueryResult(ObjectManager().drop(object_type=object_type, fqn=object_name))
124
+
125
+
126
+ # Image repository is the only supported object that does not have a DESCRIBE command.
127
+ DESCRIBE_SUPPORTED_TYPES_MSG = f"\n\nSupported types: {', '.join(obj for obj in SUPPORTED_OBJECTS if obj != 'image-repository')}"
128
+
129
+
130
+ @app.command(
131
+ help=f"Provides description of an object of given type. {DESCRIBE_SUPPORTED_TYPES_MSG}",
132
+ requires_connection=True,
133
+ )
134
+ def describe(
135
+ object_type: str = ObjectArgument, object_name: FQN = NameArgument, **options
136
+ ):
137
+ return QueryResult(
138
+ ObjectManager().describe(object_type=object_type, fqn=object_name)
139
+ )
140
+
141
+
142
+ @app.command(name="create", requires_connection=True)
143
+ def create(
144
+ object_type: str = ObjectArgument,
145
+ object_attributes: Optional[List[str]] = ObjectAttributesArgument,
146
+ object_json: str = ObjectDefinitionJsonOption,
147
+ **options,
148
+ ):
149
+ """
150
+ Create an object of a given type. Check documentation for the list of supported objects
151
+ and parameters.
152
+ """
153
+ import json
154
+
155
+ if object_attributes and object_json:
156
+ raise ClickException(
157
+ "Conflict: both object attributes and JSON definition are provided"
158
+ )
159
+
160
+ if object_json:
161
+ object_data = json.loads(object_json)
162
+ elif object_attributes:
163
+
164
+ def _parse_if_json(value: str):
165
+ try:
166
+ return json.loads(value)
167
+ except json.JSONDecodeError:
168
+ return value
169
+
170
+ object_data = {
171
+ v.key: _parse_if_json(v.value)
172
+ for v in parse_key_value_variables(object_attributes)
173
+ }
174
+
175
+ else:
176
+ raise ClickException(
177
+ "Provide either list of object attributes, or object definition in JSON format"
178
+ )
179
+
180
+ result = ObjectManager().create(object_type=object_type, object_data=object_data)
181
+ return MessageResult(result)
@@ -0,0 +1,85 @@
1
+ # Copyright (c) 2024 Snowflake Inc.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ from __future__ import annotations
16
+
17
+ from dataclasses import dataclass
18
+ from typing import Optional
19
+
20
+ from click import ClickException
21
+ from snowflake.cli.api.commands.flags import OverrideableOption
22
+ from snowflake.cli.api.project.util import (
23
+ VALID_IDENTIFIER_REGEX,
24
+ is_valid_identifier,
25
+ to_string_literal,
26
+ )
27
+
28
+
29
+ @dataclass
30
+ class Tag:
31
+ name: str
32
+ value: str
33
+
34
+ def __post_init__(self):
35
+ if not is_valid_identifier(self.name):
36
+ raise ValueError("name of a tag must be a valid Snowflake identifier")
37
+
38
+ def value_string_literal(self):
39
+ return to_string_literal(self.value)
40
+
41
+
42
+ class TagError(ClickException):
43
+ def __init__(self):
44
+ super().__init__(
45
+ "tag must be in the format <name>=<value> where 'name' is a valid identifier and value is a string"
46
+ )
47
+
48
+
49
+ def _parse_tag(tag: str) -> Tag:
50
+ import re
51
+
52
+ identifier_pattern = rf"(?P<tag_name>{VALID_IDENTIFIER_REGEX})"
53
+ value_pattern = r"(?P<tag_value>.+)"
54
+ result = re.fullmatch(rf"{identifier_pattern}={value_pattern}", tag)
55
+ if result is not None:
56
+ try:
57
+ return Tag(result.group("tag_name"), result.group("tag_value"))
58
+ except ValueError:
59
+ raise TagError()
60
+ else:
61
+ raise TagError()
62
+
63
+
64
+ """
65
+ Provides a common interface for all commands that accept a tag option (e.g. when altering the tag of an object).
66
+ Parses the input string in the format "name=value" into a Tag object with 'name' and 'value' properties.
67
+ """
68
+ TagOption = OverrideableOption(
69
+ None, "--tag", help=f"Tag for the object.", parser=_parse_tag, metavar="NAME=VALUE"
70
+ )
71
+
72
+
73
+ def _comment_callback(comment: Optional[str]):
74
+ if comment is None:
75
+ return comment
76
+ return to_string_literal(comment)
77
+
78
+
79
+ """
80
+ Provides a common interface for all commands that accept a comment option (e.g. when creating a new object).
81
+ Parses the input string into a string literal.
82
+ """
83
+ CommentOption = OverrideableOption(
84
+ None, "--comment", help="Comment for the object.", callback=_comment_callback
85
+ )
@@ -0,0 +1,97 @@
1
+ # Copyright (c) 2024 Snowflake Inc.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ from __future__ import annotations
16
+
17
+ from textwrap import dedent
18
+ from typing import Any, Dict, Optional, Tuple, Union
19
+
20
+ from click import ClickException
21
+ from snowflake.cli.api.constants import (
22
+ OBJECT_TO_NAMES,
23
+ ObjectNames,
24
+ )
25
+ from snowflake.cli.api.identifiers import FQN
26
+ from snowflake.cli.api.rest_api import RestApi
27
+ from snowflake.cli.api.sql_execution import SqlExecutionMixin
28
+ from snowflake.connector import ProgrammingError
29
+ from snowflake.connector.cursor import SnowflakeCursor
30
+ from snowflake.connector.errors import BadRequest
31
+
32
+
33
+ def _get_object_names(object_type: str) -> ObjectNames:
34
+ object_type = object_type.lower()
35
+ if object_type.lower() not in OBJECT_TO_NAMES:
36
+ raise ClickException(f"Object of type {object_type} is not supported.")
37
+ return OBJECT_TO_NAMES[object_type]
38
+
39
+
40
+ class ObjectManager(SqlExecutionMixin):
41
+ def show(
42
+ self,
43
+ *,
44
+ object_type: str,
45
+ like: Optional[str] = None,
46
+ scope: Union[Tuple[str, str], Tuple[None, None]] = (None, None),
47
+ **kwargs,
48
+ ) -> SnowflakeCursor:
49
+ object_name = _get_object_names(object_type).sf_plural_name
50
+ query = f"show {object_name}"
51
+ if like:
52
+ query += f" like '{like}'"
53
+ if scope[0] is not None:
54
+ query += f" in {scope[0].replace('-', ' ')} {scope[1]}"
55
+ return self._execute_query(query, **kwargs)
56
+
57
+ def drop(self, *, object_type: str, fqn: FQN) -> SnowflakeCursor:
58
+ object_name = _get_object_names(object_type).sf_name
59
+ return self._execute_query(f"drop {object_name} {fqn.sql_identifier}")
60
+
61
+ def describe(self, *, object_type: str, fqn: FQN):
62
+ # Image repository is the only supported object that does not have a DESCRIBE command.
63
+ if object_type == "image-repository":
64
+ raise ClickException(
65
+ f"Describe is currently not supported for object of type image-repository"
66
+ )
67
+ object_name = _get_object_names(object_type).sf_name
68
+ return self._execute_query(f"describe {object_name} {fqn.sql_identifier}")
69
+
70
+ def object_exists(self, *, object_type: str, fqn: FQN):
71
+ try:
72
+ self.describe(object_type=object_type, fqn=fqn)
73
+ return True
74
+ except ProgrammingError:
75
+ return False
76
+
77
+ def create(self, object_type: str, object_data: Dict[str, Any]) -> str:
78
+ rest = RestApi(self._conn)
79
+ url = rest.determine_url_for_create_query(object_type=object_type)
80
+
81
+ try:
82
+ response = rest.send_rest_request(url=url, method="post", data=object_data)
83
+ # workaround as SnowflakeRestful class ignores some errors, dropping their info and returns {} instead.
84
+ if not response:
85
+ raise ClickException(
86
+ dedent(
87
+ """ An unexpected error occurred while creating the object. Try again with --debug for more info.
88
+ Most probable reasons:
89
+ * object you are trying to create already exists
90
+ * role you are using do not have permissions to create this object"""
91
+ )
92
+ )
93
+ return response["status"]
94
+ except BadRequest:
95
+ raise ClickException(
96
+ "Incorrect object definition (arguments misspelled or malformatted)."
97
+ )
@@ -0,0 +1,30 @@
1
+ # Copyright (c) 2024 Snowflake Inc.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ from snowflake.cli.api.plugins.command import (
16
+ SNOWCLI_ROOT_COMMAND_PATH,
17
+ CommandSpec,
18
+ CommandType,
19
+ plugin_hook_impl,
20
+ )
21
+ from snowflake.cli.plugins.object import commands
22
+
23
+
24
+ @plugin_hook_impl
25
+ def command_spec():
26
+ return CommandSpec(
27
+ parent_command_path=SNOWCLI_ROOT_COMMAND_PATH,
28
+ command_type=CommandType.COMMAND_GROUP,
29
+ typer_instance=commands.app.create_instance(),
30
+ )
@@ -0,0 +1,15 @@
1
+ # Copyright (c) 2024 Snowflake Inc.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ # TODO 3.0: remove this file