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,312 @@
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
+ import sys
18
+ from pathlib import Path
19
+ from typing import List, Optional
20
+
21
+ import typer
22
+ from click import ClickException
23
+ from snowflake.cli.api.commands.flags import (
24
+ IfNotExistsOption,
25
+ OverrideableOption,
26
+ identifier_argument,
27
+ like_option,
28
+ )
29
+ from snowflake.cli.api.commands.snow_typer import SnowTyperFactory
30
+ from snowflake.cli.api.constants import ObjectType
31
+ from snowflake.cli.api.identifiers import FQN
32
+ from snowflake.cli.api.output.types import (
33
+ CommandResult,
34
+ QueryJsonValueResult,
35
+ QueryResult,
36
+ SingleQueryResult,
37
+ )
38
+ from snowflake.cli.api.project.util import is_valid_object_name
39
+ from snowflake.cli.plugins.object.command_aliases import (
40
+ add_object_command_aliases,
41
+ scope_option,
42
+ )
43
+ from snowflake.cli.plugins.object.common import CommentOption, Tag, TagOption
44
+ from snowflake.cli.plugins.spcs.common import (
45
+ print_log_lines,
46
+ validate_and_set_instances,
47
+ )
48
+ from snowflake.cli.plugins.spcs.services.manager import ServiceManager
49
+
50
+ app = SnowTyperFactory(
51
+ name="service",
52
+ help="Manages Snowpark Container Services services.",
53
+ short_help="Manages services.",
54
+ )
55
+
56
+
57
+ def _service_name_callback(name: FQN) -> FQN:
58
+ if not is_valid_object_name(name.identifier, max_depth=2, allow_quoted=False):
59
+ raise ClickException(
60
+ f"'{name}' is not a valid service name. Note service names must be unquoted identifiers. The same constraint also applies to database and schema names where you create a service."
61
+ )
62
+ return name
63
+
64
+
65
+ ServiceNameArgument = identifier_argument(
66
+ sf_object="service pool",
67
+ example="my_service",
68
+ callback=_service_name_callback,
69
+ )
70
+
71
+ SpecPathOption = typer.Option(
72
+ ...,
73
+ "--spec-path",
74
+ help="Path to service specification file.",
75
+ file_okay=True,
76
+ dir_okay=False,
77
+ exists=True,
78
+ show_default=False,
79
+ )
80
+
81
+ _MIN_INSTANCES_HELP = "Minimum number of service instances to run."
82
+ MinInstancesOption = OverrideableOption(
83
+ 1, "--min-instances", help=_MIN_INSTANCES_HELP, min=1
84
+ )
85
+
86
+ _MAX_INSTANCES_HELP = "Maximum number of service instances to run."
87
+ MaxInstancesOption = OverrideableOption(
88
+ None, "--max-instances", help=_MAX_INSTANCES_HELP, min=1
89
+ )
90
+
91
+ _QUERY_WAREHOUSE_HELP = "Warehouse to use if a service container connects to Snowflake to execute a query without explicitly specifying a warehouse to use."
92
+ QueryWarehouseOption = OverrideableOption(
93
+ None,
94
+ "--query-warehouse",
95
+ help=_QUERY_WAREHOUSE_HELP,
96
+ )
97
+
98
+ _AUTO_RESUME_HELP = "The service will automatically resume when a service function or ingress is called."
99
+ AutoResumeOption = OverrideableOption(
100
+ True,
101
+ "--auto-resume/--no-auto-resume",
102
+ help=_AUTO_RESUME_HELP,
103
+ )
104
+
105
+ _COMMENT_HELP = "Comment for the service."
106
+
107
+ add_object_command_aliases(
108
+ app=app,
109
+ object_type=ObjectType.SERVICE,
110
+ name_argument=ServiceNameArgument,
111
+ like_option=like_option(
112
+ help_example='`list --like "my%"` lists all services that begin with “my”.'
113
+ ),
114
+ scope_option=scope_option(help_example="`list --in compute-pool my_pool`"),
115
+ )
116
+
117
+
118
+ @app.command(requires_connection=True)
119
+ def create(
120
+ name: FQN = ServiceNameArgument,
121
+ compute_pool: str = typer.Option(
122
+ ...,
123
+ "--compute-pool",
124
+ help="Compute pool to run the service on.",
125
+ show_default=False,
126
+ ),
127
+ spec_path: Path = SpecPathOption,
128
+ min_instances: int = MinInstancesOption(),
129
+ max_instances: Optional[int] = MaxInstancesOption(),
130
+ auto_resume: bool = AutoResumeOption(),
131
+ external_access_integrations: Optional[List[str]] = typer.Option(
132
+ None,
133
+ "--eai-name",
134
+ help="Identifies External Access Integrations(EAI) that the service can access. This option may be specified multiple times for multiple EAIs.",
135
+ ),
136
+ query_warehouse: Optional[str] = QueryWarehouseOption(),
137
+ tags: Optional[List[Tag]] = TagOption(help="Tag for the service."),
138
+ comment: Optional[str] = CommentOption(help=_COMMENT_HELP),
139
+ if_not_exists: bool = IfNotExistsOption(),
140
+ **options,
141
+ ) -> CommandResult:
142
+ """
143
+ Creates a new service in the current schema.
144
+ """
145
+ max_instances = validate_and_set_instances(
146
+ min_instances, max_instances, "instances"
147
+ )
148
+ cursor = ServiceManager().create(
149
+ service_name=name.identifier,
150
+ min_instances=min_instances,
151
+ max_instances=max_instances,
152
+ compute_pool=compute_pool,
153
+ spec_path=spec_path,
154
+ external_access_integrations=external_access_integrations,
155
+ auto_resume=auto_resume,
156
+ query_warehouse=query_warehouse,
157
+ tags=tags,
158
+ comment=comment,
159
+ if_not_exists=if_not_exists,
160
+ )
161
+ return SingleQueryResult(cursor)
162
+
163
+
164
+ @app.command(requires_connection=True)
165
+ def status(name: FQN = ServiceNameArgument, **options) -> CommandResult:
166
+ """
167
+ Retrieves the status of a service.
168
+ """
169
+ cursor = ServiceManager().status(service_name=name.identifier)
170
+ return QueryJsonValueResult(cursor)
171
+
172
+
173
+ @app.command(requires_connection=True)
174
+ def logs(
175
+ name: FQN = ServiceNameArgument,
176
+ container_name: str = typer.Option(
177
+ ...,
178
+ "--container-name",
179
+ help="Name of the container.",
180
+ show_default=False,
181
+ ),
182
+ instance_id: str = typer.Option(
183
+ ...,
184
+ "--instance-id",
185
+ help="ID of the service instance, starting with 0.",
186
+ show_default=False,
187
+ ),
188
+ num_lines: int = typer.Option(
189
+ 500, "--num-lines", help="Number of lines to retrieve."
190
+ ),
191
+ **options,
192
+ ):
193
+ """
194
+ Retrieves local logs from a service container.
195
+ """
196
+ results = ServiceManager().logs(
197
+ service_name=name.identifier,
198
+ instance_id=instance_id,
199
+ container_name=container_name,
200
+ num_lines=num_lines,
201
+ )
202
+ cursor = results.fetchone()
203
+ logs = next(iter(cursor)).split("\n")
204
+ print_log_lines(sys.stdout, name, "0", logs)
205
+
206
+
207
+ @app.command(requires_connection=True)
208
+ def upgrade(
209
+ name: FQN = ServiceNameArgument,
210
+ spec_path: Path = SpecPathOption,
211
+ **options,
212
+ ):
213
+ """
214
+ Updates an existing service with a new specification file.
215
+ """
216
+ return SingleQueryResult(
217
+ ServiceManager().upgrade_spec(service_name=name.identifier, spec_path=spec_path)
218
+ )
219
+
220
+
221
+ @app.command("list-endpoints", requires_connection=True)
222
+ def list_endpoints(name: FQN = ServiceNameArgument, **options):
223
+ """
224
+ Lists the endpoints in a service.
225
+ """
226
+ return QueryResult(ServiceManager().list_endpoints(service_name=name.identifier))
227
+
228
+
229
+ @app.command(requires_connection=True)
230
+ def suspend(name: FQN = ServiceNameArgument, **options) -> CommandResult:
231
+ """
232
+ Suspends the service, shutting down and deleting all its containers.
233
+ """
234
+ return SingleQueryResult(ServiceManager().suspend(name))
235
+
236
+
237
+ @app.command(requires_connection=True)
238
+ def resume(name: FQN = ServiceNameArgument, **options) -> CommandResult:
239
+ """
240
+ Resumes the service from a SUSPENDED state.
241
+ """
242
+ return SingleQueryResult(ServiceManager().resume(name))
243
+
244
+
245
+ @app.command("set", requires_connection=True)
246
+ def set_property(
247
+ name: FQN = ServiceNameArgument,
248
+ min_instances: Optional[int] = MinInstancesOption(default=None, show_default=False),
249
+ max_instances: Optional[int] = MaxInstancesOption(show_default=False),
250
+ query_warehouse: Optional[str] = QueryWarehouseOption(show_default=False),
251
+ auto_resume: Optional[bool] = AutoResumeOption(default=None, show_default=False),
252
+ comment: Optional[str] = CommentOption(help=_COMMENT_HELP, show_default=False),
253
+ **options,
254
+ ):
255
+ """
256
+ Sets one or more properties for the service.
257
+ """
258
+ cursor = ServiceManager().set_property(
259
+ service_name=name.identifier,
260
+ min_instances=min_instances,
261
+ max_instances=max_instances,
262
+ query_warehouse=query_warehouse,
263
+ auto_resume=auto_resume,
264
+ comment=comment,
265
+ )
266
+ return SingleQueryResult(cursor)
267
+
268
+
269
+ @app.command("unset", requires_connection=True)
270
+ def unset_property(
271
+ name: FQN = ServiceNameArgument,
272
+ min_instances: bool = MinInstancesOption(
273
+ default=False,
274
+ help=f"Reset the MIN_INSTANCES property - {_MIN_INSTANCES_HELP}",
275
+ show_default=False,
276
+ ),
277
+ max_instances: bool = MaxInstancesOption(
278
+ default=False,
279
+ help=f"Reset the MAX_INSTANCES property - {_MAX_INSTANCES_HELP}",
280
+ show_default=False,
281
+ ),
282
+ query_warehouse: bool = QueryWarehouseOption(
283
+ default=False,
284
+ help=f"Reset the QUERY_WAREHOUSE property - {_QUERY_WAREHOUSE_HELP}",
285
+ show_default=False,
286
+ ),
287
+ auto_resume: bool = AutoResumeOption(
288
+ default=False,
289
+ param_decls=["--auto-resume"],
290
+ help=f"Reset the AUTO_RESUME property - {_AUTO_RESUME_HELP}",
291
+ show_default=False,
292
+ ),
293
+ comment: bool = CommentOption(
294
+ default=False,
295
+ help=f"Reset the COMMENT property - {_COMMENT_HELP}",
296
+ callback=None,
297
+ show_default=False,
298
+ ),
299
+ **options,
300
+ ):
301
+ """
302
+ Resets one or more properties for the service to their default value(s).
303
+ """
304
+ cursor = ServiceManager().unset_property(
305
+ service_name=name.identifier,
306
+ min_instances=min_instances,
307
+ max_instances=max_instances,
308
+ query_warehouse=query_warehouse,
309
+ auto_resume=auto_resume,
310
+ comment=comment,
311
+ )
312
+ return SingleQueryResult(cursor)
@@ -0,0 +1,170 @@
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
+ import json
18
+ from pathlib import Path
19
+ from typing import List, Optional
20
+
21
+ import yaml
22
+ from snowflake.cli.api.constants import DEFAULT_SIZE_LIMIT_MB, ObjectType
23
+ from snowflake.cli.api.secure_path import SecurePath
24
+ from snowflake.cli.api.sql_execution import SqlExecutionMixin
25
+ from snowflake.cli.plugins.object.common import Tag
26
+ from snowflake.cli.plugins.spcs.common import (
27
+ NoPropertiesProvidedError,
28
+ handle_object_already_exists,
29
+ strip_empty_lines,
30
+ )
31
+ from snowflake.connector.cursor import SnowflakeCursor
32
+ from snowflake.connector.errors import ProgrammingError
33
+
34
+
35
+ class ServiceManager(SqlExecutionMixin):
36
+ def create(
37
+ self,
38
+ service_name: str,
39
+ compute_pool: str,
40
+ spec_path: Path,
41
+ min_instances: int,
42
+ max_instances: int,
43
+ auto_resume: bool,
44
+ external_access_integrations: Optional[List[str]],
45
+ query_warehouse: Optional[str],
46
+ tags: Optional[List[Tag]],
47
+ comment: Optional[str],
48
+ if_not_exists: bool,
49
+ ) -> SnowflakeCursor:
50
+ spec = self._read_yaml(spec_path)
51
+ create_statement = "CREATE SERVICE"
52
+ if if_not_exists:
53
+ create_statement = f"{create_statement} IF NOT EXISTS"
54
+ query = f"""\
55
+ {create_statement} {service_name}
56
+ IN COMPUTE POOL {compute_pool}
57
+ FROM SPECIFICATION $$
58
+ {spec}
59
+ $$
60
+ MIN_INSTANCES = {min_instances}
61
+ MAX_INSTANCES = {max_instances}
62
+ AUTO_RESUME = {auto_resume}
63
+ """.splitlines()
64
+
65
+ if external_access_integrations:
66
+ external_access_integration_list = ",".join(
67
+ f"{e}" for e in external_access_integrations
68
+ )
69
+ query.append(
70
+ f"EXTERNAL_ACCESS_INTEGRATIONS = ({external_access_integration_list})"
71
+ )
72
+
73
+ if query_warehouse:
74
+ query.append(f"QUERY_WAREHOUSE = {query_warehouse}")
75
+
76
+ if comment:
77
+ query.append(f"COMMENT = {comment}")
78
+
79
+ if tags:
80
+ tag_list = ",".join(f"{t.name}={t.value_string_literal()}" for t in tags)
81
+ query.append(f"WITH TAG ({tag_list})")
82
+
83
+ try:
84
+ return self._execute_query(strip_empty_lines(query))
85
+ except ProgrammingError as e:
86
+ handle_object_already_exists(e, ObjectType.SERVICE, service_name)
87
+
88
+ def _read_yaml(self, path: Path) -> str:
89
+ # TODO(aivanou): Add validation towards schema
90
+ with SecurePath(path).open("r", read_file_limit_mb=DEFAULT_SIZE_LIMIT_MB) as fh:
91
+ data = yaml.safe_load(fh)
92
+ return json.dumps(data)
93
+
94
+ def status(self, service_name: str) -> SnowflakeCursor:
95
+ return self._execute_query(f"CALL SYSTEM$GET_SERVICE_STATUS('{service_name}')")
96
+
97
+ def logs(
98
+ self, service_name: str, instance_id: str, container_name: str, num_lines: int
99
+ ):
100
+ return self._execute_query(
101
+ f"call SYSTEM$GET_SERVICE_LOGS('{service_name}', '{instance_id}', '{container_name}', {num_lines});"
102
+ )
103
+
104
+ def upgrade_spec(self, service_name: str, spec_path: Path):
105
+ spec = self._read_yaml(spec_path)
106
+ query = f"alter service {service_name} from specification $$ {spec} $$"
107
+ return self._execute_query(query)
108
+
109
+ def list_endpoints(self, service_name: str) -> SnowflakeCursor:
110
+ return self._execute_query(f"show endpoints in service {service_name}")
111
+
112
+ def suspend(self, service_name: str):
113
+ return self._execute_query(f"alter service {service_name} suspend")
114
+
115
+ def resume(self, service_name: str):
116
+ return self._execute_query(f"alter service {service_name} resume")
117
+
118
+ def set_property(
119
+ self,
120
+ service_name: str,
121
+ min_instances: Optional[int],
122
+ max_instances: Optional[int],
123
+ query_warehouse: Optional[str],
124
+ auto_resume: Optional[bool],
125
+ comment: Optional[str],
126
+ ):
127
+ property_pairs = [
128
+ ("min_instances", min_instances),
129
+ ("max_instances", max_instances),
130
+ ("query_warehouse", query_warehouse),
131
+ ("auto_resume", auto_resume),
132
+ ("comment", comment),
133
+ ]
134
+
135
+ # Check if all provided properties are set to None (no properties are being set)
136
+ if all([value is None for property_name, value in property_pairs]):
137
+ raise NoPropertiesProvidedError(
138
+ f"No properties specified for service '{service_name}'. Please provide at least one property to set."
139
+ )
140
+ query: List[str] = [f"alter service {service_name} set"]
141
+ for property_name, value in property_pairs:
142
+ if value is not None:
143
+ query.append(f"{property_name} = {value}")
144
+ return self._execute_query(strip_empty_lines(query))
145
+
146
+ def unset_property(
147
+ self,
148
+ service_name: str,
149
+ min_instances: bool,
150
+ max_instances: bool,
151
+ query_warehouse: bool,
152
+ auto_resume: bool,
153
+ comment: bool,
154
+ ):
155
+ property_pairs = [
156
+ ("min_instances", min_instances),
157
+ ("max_instances", max_instances),
158
+ ("query_warehouse", query_warehouse),
159
+ ("auto_resume", auto_resume),
160
+ ("comment", comment),
161
+ ]
162
+
163
+ # Check if all properties provided are False (no properties are being unset)
164
+ if not any([value for property_name, value in property_pairs]):
165
+ raise NoPropertiesProvidedError(
166
+ f"No properties specified for service '{service_name}'. Please provide at least one property to reset to its default value."
167
+ )
168
+ unset_list = [property_name for property_name, value in property_pairs if value]
169
+ query = f"alter service {service_name} unset {','.join(unset_list)}"
170
+ return self._execute_query(query)
@@ -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,83 @@
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 pathlib import Path
18
+ from typing import List, Optional
19
+
20
+ import typer
21
+ from snowflake.cli.api.commands.decorators import with_project_definition
22
+ from snowflake.cli.api.commands.flags import (
23
+ parse_key_value_variables,
24
+ variables_option,
25
+ )
26
+ from snowflake.cli.api.commands.snow_typer import SnowTyperFactory
27
+ from snowflake.cli.api.output.types import CommandResult, MultipleResults, QueryResult
28
+ from snowflake.cli.plugins.sql.manager import SqlManager
29
+
30
+ # simple Typer with defaults because it won't become a command group as it contains only one command
31
+ app = SnowTyperFactory()
32
+
33
+
34
+ @app.command(name="sql", requires_connection=True, no_args_is_help=True)
35
+ @with_project_definition(is_optional=True)
36
+ def execute_sql(
37
+ query: Optional[str] = typer.Option(
38
+ None,
39
+ "--query",
40
+ "-q",
41
+ help="Query to execute.",
42
+ ),
43
+ files: Optional[List[Path]] = typer.Option(
44
+ None,
45
+ "--filename",
46
+ "-f",
47
+ exists=True,
48
+ file_okay=True,
49
+ dir_okay=False,
50
+ readable=True,
51
+ help="File to execute.",
52
+ ),
53
+ std_in: Optional[bool] = typer.Option(
54
+ False,
55
+ "--stdin",
56
+ "-i",
57
+ help="Read the query from standard input. Use it when piping input to this command.",
58
+ ),
59
+ data_override: List[str] = variables_option(
60
+ "String in format of key=value. If provided the SQL content will "
61
+ "be treated as template and rendered using provided data.",
62
+ ),
63
+ **options,
64
+ ) -> CommandResult:
65
+ """
66
+ Executes Snowflake query.
67
+
68
+ Use either query, filename or input option.
69
+
70
+ Query to execute can be specified using query option, filename option (all queries from file will be executed)
71
+ or via stdin by piping output from other command. For example `cat my.sql | snow sql -i`.
72
+
73
+ The command supports variable substitution that happens on client-side. Both &VARIABLE or &{ VARIABLE }
74
+ syntax are supported.
75
+ """
76
+ data = {}
77
+ if data_override:
78
+ data = {v.key: v.value for v in parse_key_value_variables(data_override)}
79
+
80
+ single_statement, cursors = SqlManager().execute(query, files, std_in, data=data)
81
+ if single_statement:
82
+ return QueryResult(next(cursors))
83
+ return MultipleResults((QueryResult(c) for c in cursors))
@@ -0,0 +1,92 @@
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
+ import sys
18
+ from io import StringIO
19
+ from itertools import chain
20
+ from pathlib import Path
21
+ from typing import Dict, Iterable, List, Tuple
22
+
23
+ from click import ClickException, UsageError
24
+ from jinja2 import UndefinedError
25
+ from snowflake.cli.api.rendering.sql_templates import snowflake_sql_jinja_render
26
+ from snowflake.cli.api.secure_path import UNLIMITED, SecurePath
27
+ from snowflake.cli.api.sql_execution import SqlExecutionMixin, VerboseCursor
28
+ from snowflake.cli.plugins.sql.snowsql_templating import transpile_snowsql_templates
29
+ from snowflake.connector.cursor import SnowflakeCursor
30
+ from snowflake.connector.util_text import split_statements
31
+
32
+ IsSingleStatement = bool
33
+
34
+
35
+ class SqlManager(SqlExecutionMixin):
36
+ def execute(
37
+ self,
38
+ query: str | None,
39
+ files: List[Path] | None,
40
+ std_in: bool,
41
+ data: Dict | None = None,
42
+ ) -> Tuple[IsSingleStatement, Iterable[SnowflakeCursor]]:
43
+ inputs = [query, files, std_in]
44
+ # Check if any two inputs were provided simultaneously
45
+ if len([i for i in inputs if i]) > 1:
46
+ raise UsageError(
47
+ "Multiple input sources specified. Please specify only one."
48
+ )
49
+
50
+ if std_in:
51
+ query = sys.stdin.read()
52
+ if query:
53
+ return self._execute_single_query(query=query, data=data)
54
+
55
+ if files:
56
+ # Multiple files
57
+ results = []
58
+ single_statement = False
59
+ for file in files:
60
+ query_from_file = SecurePath(file).read_text(
61
+ file_size_limit_mb=UNLIMITED
62
+ )
63
+ single_statement, result = self._execute_single_query(
64
+ query=query_from_file, data=data
65
+ )
66
+ results.append(result)
67
+
68
+ # Use single_statement if there's only one, otherwise this is multi statement result
69
+ single_statement = len(files) == 1 and single_statement
70
+ return single_statement, chain.from_iterable(results)
71
+
72
+ # At that point, no stdin, query or files were provided
73
+ raise UsageError("Use either query, filename or input option.")
74
+
75
+ def _execute_single_query(
76
+ self, query: str, data: Dict | None = None
77
+ ) -> Tuple[IsSingleStatement, Iterable[SnowflakeCursor]]:
78
+ try:
79
+ query = transpile_snowsql_templates(query)
80
+ query = snowflake_sql_jinja_render(content=query, data=data)
81
+ except UndefinedError as err:
82
+ raise ClickException(f"SQL template rendering error: {err}")
83
+
84
+ statements = tuple(
85
+ statement
86
+ for statement, _ in split_statements(StringIO(query), remove_comments=True)
87
+ )
88
+ single_statement = len(statements) == 1
89
+
90
+ return single_statement, self._execute_string(
91
+ "\n".join(statements), cursor_class=VerboseCursor
92
+ )
@@ -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.sql 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.SINGLE_COMMAND,
29
+ typer_instance=commands.app.create_instance(),
30
+ )