snowflake-cli-labs 2.8.0rc1__py3-none-any.whl → 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.
- README.md +21 -0
- {snowflake_cli_labs-2.8.0rc1.dist-info → snowflake_cli_labs-2.8.2.dist-info}/METADATA +7 -95
- snowflake_cli_labs-2.8.2.dist-info/RECORD +5 -0
- snowflake/cli/__about__.py +0 -17
- snowflake/cli/__init__.py +0 -13
- snowflake/cli/api/__init__.py +0 -48
- snowflake/cli/api/cli_global_context.py +0 -390
- snowflake/cli/api/commands/__init__.py +0 -13
- snowflake/cli/api/commands/alias.py +0 -23
- snowflake/cli/api/commands/decorators.py +0 -354
- snowflake/cli/api/commands/execution_metadata.py +0 -40
- snowflake/cli/api/commands/experimental_behaviour.py +0 -19
- snowflake/cli/api/commands/flags.py +0 -640
- snowflake/cli/api/commands/project_initialisation.py +0 -65
- snowflake/cli/api/commands/snow_typer.py +0 -237
- snowflake/cli/api/commands/typer_pre_execute.py +0 -26
- snowflake/cli/api/config.py +0 -348
- snowflake/cli/api/console/__init__.py +0 -17
- snowflake/cli/api/console/abc.py +0 -89
- snowflake/cli/api/console/console.py +0 -134
- snowflake/cli/api/console/enum.py +0 -17
- snowflake/cli/api/constants.py +0 -79
- snowflake/cli/api/errno.py +0 -27
- snowflake/cli/api/exceptions.py +0 -164
- snowflake/cli/api/feature_flags.py +0 -55
- snowflake/cli/api/identifiers.py +0 -154
- snowflake/cli/api/output/__init__.py +0 -13
- snowflake/cli/api/output/formats.py +0 -20
- snowflake/cli/api/output/types.py +0 -118
- snowflake/cli/api/plugins/__init__.py +0 -13
- snowflake/cli/api/plugins/command/__init__.py +0 -72
- snowflake/cli/api/plugins/command/plugin_hook_specs.py +0 -21
- snowflake/cli/api/plugins/plugin_config.py +0 -32
- snowflake/cli/api/project/__init__.py +0 -13
- snowflake/cli/api/project/definition.py +0 -84
- snowflake/cli/api/project/definition_manager.py +0 -134
- snowflake/cli/api/project/errors.py +0 -56
- snowflake/cli/api/project/project_verification.py +0 -23
- snowflake/cli/api/project/schemas/__init__.py +0 -13
- snowflake/cli/api/project/schemas/entities/application_entity.py +0 -44
- snowflake/cli/api/project/schemas/entities/application_package_entity.py +0 -66
- snowflake/cli/api/project/schemas/entities/common.py +0 -78
- snowflake/cli/api/project/schemas/entities/entities.py +0 -30
- snowflake/cli/api/project/schemas/identifier_model.py +0 -49
- snowflake/cli/api/project/schemas/native_app/__init__.py +0 -13
- snowflake/cli/api/project/schemas/native_app/application.py +0 -62
- snowflake/cli/api/project/schemas/native_app/native_app.py +0 -93
- snowflake/cli/api/project/schemas/native_app/package.py +0 -78
- snowflake/cli/api/project/schemas/native_app/path_mapping.py +0 -65
- snowflake/cli/api/project/schemas/project_definition.py +0 -199
- snowflake/cli/api/project/schemas/snowpark/__init__.py +0 -13
- snowflake/cli/api/project/schemas/snowpark/argument.py +0 -28
- snowflake/cli/api/project/schemas/snowpark/callable.py +0 -69
- snowflake/cli/api/project/schemas/snowpark/snowpark.py +0 -36
- snowflake/cli/api/project/schemas/streamlit/__init__.py +0 -13
- snowflake/cli/api/project/schemas/streamlit/streamlit.py +0 -46
- snowflake/cli/api/project/schemas/template.py +0 -77
- snowflake/cli/api/project/schemas/updatable_model.py +0 -194
- snowflake/cli/api/project/util.py +0 -261
- snowflake/cli/api/rendering/__init__.py +0 -13
- snowflake/cli/api/rendering/jinja.py +0 -112
- snowflake/cli/api/rendering/project_definition_templates.py +0 -39
- snowflake/cli/api/rendering/project_templates.py +0 -98
- snowflake/cli/api/rendering/sql_templates.py +0 -60
- snowflake/cli/api/rest_api.py +0 -172
- snowflake/cli/api/sanitizers.py +0 -43
- snowflake/cli/api/secure_path.py +0 -362
- snowflake/cli/api/secure_utils.py +0 -29
- snowflake/cli/api/sql_execution.py +0 -260
- snowflake/cli/api/utils/__init__.py +0 -13
- snowflake/cli/api/utils/cursor.py +0 -34
- snowflake/cli/api/utils/definition_rendering.py +0 -383
- snowflake/cli/api/utils/dict_utils.py +0 -73
- snowflake/cli/api/utils/error_handling.py +0 -23
- snowflake/cli/api/utils/graph.py +0 -97
- snowflake/cli/api/utils/models.py +0 -63
- snowflake/cli/api/utils/naming_utils.py +0 -13
- snowflake/cli/api/utils/path_utils.py +0 -36
- snowflake/cli/api/utils/templating_functions.py +0 -144
- snowflake/cli/api/utils/types.py +0 -35
- snowflake/cli/app/__init__.py +0 -22
- snowflake/cli/app/__main__.py +0 -31
- snowflake/cli/app/api_impl/__init__.py +0 -13
- snowflake/cli/app/api_impl/plugin/__init__.py +0 -13
- snowflake/cli/app/api_impl/plugin/plugin_config_provider_impl.py +0 -66
- snowflake/cli/app/build_and_push.sh +0 -8
- snowflake/cli/app/cli_app.py +0 -243
- snowflake/cli/app/commands_registration/__init__.py +0 -33
- snowflake/cli/app/commands_registration/builtin_plugins.py +0 -54
- snowflake/cli/app/commands_registration/command_plugins_loader.py +0 -169
- snowflake/cli/app/commands_registration/commands_registration_with_callbacks.py +0 -105
- snowflake/cli/app/commands_registration/exception_logging.py +0 -26
- snowflake/cli/app/commands_registration/threadsafe.py +0 -48
- snowflake/cli/app/commands_registration/typer_registration.py +0 -153
- snowflake/cli/app/constants.py +0 -19
- snowflake/cli/app/dev/__init__.py +0 -13
- snowflake/cli/app/dev/commands_structure.py +0 -48
- snowflake/cli/app/dev/docs/__init__.py +0 -13
- snowflake/cli/app/dev/docs/commands_docs_generator.py +0 -100
- snowflake/cli/app/dev/docs/generator.py +0 -35
- snowflake/cli/app/dev/docs/project_definition_docs_generator.py +0 -58
- snowflake/cli/app/dev/docs/project_definition_generate_json_schema.py +0 -227
- snowflake/cli/app/dev/docs/template_utils.py +0 -23
- snowflake/cli/app/dev/docs/templates/definition_description.rst.jinja2 +0 -38
- snowflake/cli/app/dev/docs/templates/overview.rst.jinja2 +0 -9
- snowflake/cli/app/dev/docs/templates/usage.rst.jinja2 +0 -57
- snowflake/cli/app/dev/pycharm_remote_debug.py +0 -46
- snowflake/cli/app/loggers.py +0 -199
- snowflake/cli/app/main_typer.py +0 -62
- snowflake/cli/app/printing.py +0 -181
- snowflake/cli/app/snow_connector.py +0 -243
- snowflake/cli/app/telemetry.py +0 -189
- snowflake/cli/plugins/__init__.py +0 -13
- snowflake/cli/plugins/connection/__init__.py +0 -13
- snowflake/cli/plugins/connection/commands.py +0 -330
- snowflake/cli/plugins/connection/plugin_spec.py +0 -30
- snowflake/cli/plugins/connection/util.py +0 -179
- snowflake/cli/plugins/cortex/__init__.py +0 -13
- snowflake/cli/plugins/cortex/commands.py +0 -327
- snowflake/cli/plugins/cortex/constants.py +0 -17
- snowflake/cli/plugins/cortex/manager.py +0 -189
- snowflake/cli/plugins/cortex/plugin_spec.py +0 -30
- snowflake/cli/plugins/cortex/types.py +0 -22
- snowflake/cli/plugins/git/__init__.py +0 -13
- snowflake/cli/plugins/git/commands.py +0 -305
- snowflake/cli/plugins/git/manager.py +0 -96
- snowflake/cli/plugins/git/plugin_spec.py +0 -30
- snowflake/cli/plugins/init/__init__.py +0 -13
- snowflake/cli/plugins/init/commands.py +0 -244
- snowflake/cli/plugins/init/plugin_spec.py +0 -30
- snowflake/cli/plugins/nativeapp/__init__.py +0 -13
- snowflake/cli/plugins/nativeapp/artifacts.py +0 -742
- snowflake/cli/plugins/nativeapp/codegen/__init__.py +0 -13
- snowflake/cli/plugins/nativeapp/codegen/artifact_processor.py +0 -91
- snowflake/cli/plugins/nativeapp/codegen/compiler.py +0 -130
- snowflake/cli/plugins/nativeapp/codegen/sandbox.py +0 -306
- snowflake/cli/plugins/nativeapp/codegen/setup/native_app_setup_processor.py +0 -172
- snowflake/cli/plugins/nativeapp/codegen/setup/setup_driver.py.source +0 -56
- snowflake/cli/plugins/nativeapp/codegen/snowpark/callback_source.py.jinja +0 -181
- snowflake/cli/plugins/nativeapp/codegen/snowpark/extension_function_utils.py +0 -217
- snowflake/cli/plugins/nativeapp/codegen/snowpark/models.py +0 -61
- snowflake/cli/plugins/nativeapp/codegen/snowpark/python_processor.py +0 -528
- snowflake/cli/plugins/nativeapp/commands.py +0 -439
- snowflake/cli/plugins/nativeapp/common_flags.py +0 -44
- snowflake/cli/plugins/nativeapp/constants.py +0 -27
- snowflake/cli/plugins/nativeapp/exceptions.py +0 -122
- snowflake/cli/plugins/nativeapp/feature_flags.py +0 -24
- snowflake/cli/plugins/nativeapp/init.py +0 -345
- snowflake/cli/plugins/nativeapp/manager.py +0 -823
- snowflake/cli/plugins/nativeapp/plugin_spec.py +0 -30
- snowflake/cli/plugins/nativeapp/policy.py +0 -50
- snowflake/cli/plugins/nativeapp/project_model.py +0 -195
- snowflake/cli/plugins/nativeapp/run_processor.py +0 -389
- snowflake/cli/plugins/nativeapp/teardown_processor.py +0 -301
- snowflake/cli/plugins/nativeapp/utils.py +0 -98
- snowflake/cli/plugins/nativeapp/v2_conversions/v2_to_v1_decorator.py +0 -135
- snowflake/cli/plugins/nativeapp/version/__init__.py +0 -13
- snowflake/cli/plugins/nativeapp/version/commands.py +0 -170
- snowflake/cli/plugins/nativeapp/version/version_processor.py +0 -362
- snowflake/cli/plugins/notebook/__init__.py +0 -13
- snowflake/cli/plugins/notebook/commands.py +0 -84
- snowflake/cli/plugins/notebook/exceptions.py +0 -20
- snowflake/cli/plugins/notebook/manager.py +0 -71
- snowflake/cli/plugins/notebook/plugin_spec.py +0 -30
- snowflake/cli/plugins/notebook/types.py +0 -16
- snowflake/cli/plugins/object/__init__.py +0 -13
- snowflake/cli/plugins/object/command_aliases.py +0 -94
- snowflake/cli/plugins/object/commands.py +0 -174
- snowflake/cli/plugins/object/common.py +0 -85
- snowflake/cli/plugins/object/manager.py +0 -96
- snowflake/cli/plugins/object/plugin_spec.py +0 -30
- snowflake/cli/plugins/object_stage_deprecated/__init__.py +0 -15
- snowflake/cli/plugins/object_stage_deprecated/commands.py +0 -122
- snowflake/cli/plugins/object_stage_deprecated/plugin_spec.py +0 -32
- snowflake/cli/plugins/snowpark/__init__.py +0 -13
- snowflake/cli/plugins/snowpark/commands.py +0 -548
- snowflake/cli/plugins/snowpark/common.py +0 -307
- snowflake/cli/plugins/snowpark/manager.py +0 -109
- snowflake/cli/plugins/snowpark/models.py +0 -156
- snowflake/cli/plugins/snowpark/package/__init__.py +0 -13
- snowflake/cli/plugins/snowpark/package/anaconda_packages.py +0 -233
- snowflake/cli/plugins/snowpark/package/commands.py +0 -256
- snowflake/cli/plugins/snowpark/package/manager.py +0 -43
- snowflake/cli/plugins/snowpark/package/utils.py +0 -26
- snowflake/cli/plugins/snowpark/package_utils.py +0 -354
- snowflake/cli/plugins/snowpark/plugin_spec.py +0 -30
- snowflake/cli/plugins/snowpark/snowpark_package_paths.py +0 -65
- snowflake/cli/plugins/snowpark/snowpark_shared.py +0 -95
- snowflake/cli/plugins/snowpark/zipper.py +0 -81
- snowflake/cli/plugins/spcs/__init__.py +0 -35
- snowflake/cli/plugins/spcs/common.py +0 -99
- snowflake/cli/plugins/spcs/compute_pool/__init__.py +0 -13
- snowflake/cli/plugins/spcs/compute_pool/commands.py +0 -240
- snowflake/cli/plugins/spcs/compute_pool/manager.py +0 -121
- snowflake/cli/plugins/spcs/image_registry/__init__.py +0 -13
- snowflake/cli/plugins/spcs/image_registry/commands.py +0 -65
- snowflake/cli/plugins/spcs/image_registry/manager.py +0 -105
- snowflake/cli/plugins/spcs/image_repository/__init__.py +0 -13
- snowflake/cli/plugins/spcs/image_repository/commands.py +0 -196
- snowflake/cli/plugins/spcs/image_repository/manager.py +0 -84
- snowflake/cli/plugins/spcs/jobs/__init__.py +0 -13
- snowflake/cli/plugins/spcs/jobs/commands.py +0 -78
- snowflake/cli/plugins/spcs/jobs/manager.py +0 -53
- snowflake/cli/plugins/spcs/plugin_spec.py +0 -30
- snowflake/cli/plugins/spcs/services/__init__.py +0 -13
- snowflake/cli/plugins/spcs/services/commands.py +0 -311
- snowflake/cli/plugins/spcs/services/manager.py +0 -170
- snowflake/cli/plugins/sql/__init__.py +0 -13
- snowflake/cli/plugins/sql/commands.py +0 -83
- snowflake/cli/plugins/sql/manager.py +0 -92
- snowflake/cli/plugins/sql/plugin_spec.py +0 -30
- snowflake/cli/plugins/sql/snowsql_templating.py +0 -28
- snowflake/cli/plugins/stage/__init__.py +0 -13
- snowflake/cli/plugins/stage/commands.py +0 -261
- snowflake/cli/plugins/stage/diff.py +0 -326
- snowflake/cli/plugins/stage/manager.py +0 -544
- snowflake/cli/plugins/stage/md5.py +0 -160
- snowflake/cli/plugins/stage/plugin_spec.py +0 -30
- snowflake/cli/plugins/streamlit/__init__.py +0 -13
- snowflake/cli/plugins/streamlit/commands.py +0 -186
- snowflake/cli/plugins/streamlit/manager.py +0 -222
- snowflake/cli/plugins/streamlit/plugin_spec.py +0 -30
- snowflake/cli/plugins/workspace/__init__.py +0 -13
- snowflake/cli/plugins/workspace/commands.py +0 -35
- snowflake/cli/plugins/workspace/plugin_spec.py +0 -30
- snowflake/cli/templates/default_snowpark/.gitignore +0 -4
- snowflake/cli/templates/default_snowpark/app/__init__.py +0 -0
- snowflake/cli/templates/default_snowpark/app/common.py +0 -2
- snowflake/cli/templates/default_snowpark/app/functions.py +0 -15
- snowflake/cli/templates/default_snowpark/app/procedures.py +0 -22
- snowflake/cli/templates/default_snowpark/requirements.txt +0 -1
- snowflake/cli/templates/default_snowpark/snowflake.yml +0 -23
- snowflake/cli/templates/default_streamlit/.gitignore +0 -4
- snowflake/cli/templates/default_streamlit/common/hello.py +0 -2
- snowflake/cli/templates/default_streamlit/environment.yml +0 -6
- snowflake/cli/templates/default_streamlit/pages/my_page.py +0 -3
- snowflake/cli/templates/default_streamlit/snowflake.yml +0 -10
- snowflake/cli/templates/default_streamlit/streamlit_app.py +0 -4
- snowflake_cli_labs-2.8.0rc1.dist-info/RECORD +0 -240
- snowflake_cli_labs-2.8.0rc1.dist-info/entry_points.txt +0 -2
- {snowflake_cli_labs-2.8.0rc1.dist-info → snowflake_cli_labs-2.8.2.dist-info}/WHEEL +0 -0
- {snowflake_cli_labs-2.8.0rc1.dist-info → snowflake_cli_labs-2.8.2.dist-info}/licenses/LICENSE +0 -0
|
@@ -1,311 +0,0 @@
|
|
|
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
|
-
like_option,
|
|
27
|
-
)
|
|
28
|
-
from snowflake.cli.api.commands.snow_typer import SnowTyperFactory
|
|
29
|
-
from snowflake.cli.api.constants import ObjectType
|
|
30
|
-
from snowflake.cli.api.output.types import (
|
|
31
|
-
CommandResult,
|
|
32
|
-
QueryJsonValueResult,
|
|
33
|
-
QueryResult,
|
|
34
|
-
SingleQueryResult,
|
|
35
|
-
)
|
|
36
|
-
from snowflake.cli.api.project.util import is_valid_object_name
|
|
37
|
-
from snowflake.cli.plugins.object.command_aliases import (
|
|
38
|
-
add_object_command_aliases,
|
|
39
|
-
scope_option,
|
|
40
|
-
)
|
|
41
|
-
from snowflake.cli.plugins.object.common import CommentOption, Tag, TagOption
|
|
42
|
-
from snowflake.cli.plugins.spcs.common import (
|
|
43
|
-
print_log_lines,
|
|
44
|
-
validate_and_set_instances,
|
|
45
|
-
)
|
|
46
|
-
from snowflake.cli.plugins.spcs.services.manager import ServiceManager
|
|
47
|
-
|
|
48
|
-
app = SnowTyperFactory(
|
|
49
|
-
name="service",
|
|
50
|
-
help="Manages Snowpark Container Services services.",
|
|
51
|
-
short_help="Manages services.",
|
|
52
|
-
)
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
def _service_name_callback(name: str) -> str:
|
|
56
|
-
if not is_valid_object_name(name, max_depth=2, allow_quoted=False):
|
|
57
|
-
raise ClickException(
|
|
58
|
-
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."
|
|
59
|
-
)
|
|
60
|
-
return name
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
ServiceNameArgument = typer.Argument(
|
|
64
|
-
...,
|
|
65
|
-
help="Name of the service.",
|
|
66
|
-
callback=_service_name_callback,
|
|
67
|
-
show_default=False,
|
|
68
|
-
)
|
|
69
|
-
|
|
70
|
-
SpecPathOption = typer.Option(
|
|
71
|
-
...,
|
|
72
|
-
"--spec-path",
|
|
73
|
-
help="Path to service specification file.",
|
|
74
|
-
file_okay=True,
|
|
75
|
-
dir_okay=False,
|
|
76
|
-
exists=True,
|
|
77
|
-
show_default=False,
|
|
78
|
-
)
|
|
79
|
-
|
|
80
|
-
_MIN_INSTANCES_HELP = "Minimum number of service instances to run."
|
|
81
|
-
MinInstancesOption = OverrideableOption(
|
|
82
|
-
1, "--min-instances", help=_MIN_INSTANCES_HELP, min=1
|
|
83
|
-
)
|
|
84
|
-
|
|
85
|
-
_MAX_INSTANCES_HELP = "Maximum number of service instances to run."
|
|
86
|
-
MaxInstancesOption = OverrideableOption(
|
|
87
|
-
None, "--max-instances", help=_MAX_INSTANCES_HELP, min=1
|
|
88
|
-
)
|
|
89
|
-
|
|
90
|
-
_QUERY_WAREHOUSE_HELP = "Warehouse to use if a service container connects to Snowflake to execute a query without explicitly specifying a warehouse to use."
|
|
91
|
-
QueryWarehouseOption = OverrideableOption(
|
|
92
|
-
None,
|
|
93
|
-
"--query-warehouse",
|
|
94
|
-
help=_QUERY_WAREHOUSE_HELP,
|
|
95
|
-
)
|
|
96
|
-
|
|
97
|
-
_AUTO_RESUME_HELP = "The service will automatically resume when a service function or ingress is called."
|
|
98
|
-
AutoResumeOption = OverrideableOption(
|
|
99
|
-
True,
|
|
100
|
-
"--auto-resume/--no-auto-resume",
|
|
101
|
-
help=_AUTO_RESUME_HELP,
|
|
102
|
-
)
|
|
103
|
-
|
|
104
|
-
_COMMENT_HELP = "Comment for the service."
|
|
105
|
-
|
|
106
|
-
add_object_command_aliases(
|
|
107
|
-
app=app,
|
|
108
|
-
object_type=ObjectType.SERVICE,
|
|
109
|
-
name_argument=ServiceNameArgument,
|
|
110
|
-
like_option=like_option(
|
|
111
|
-
help_example='`list --like "my%"` lists all services that begin with “my”.'
|
|
112
|
-
),
|
|
113
|
-
scope_option=scope_option(help_example="`list --in compute-pool my_pool`"),
|
|
114
|
-
)
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
@app.command(requires_connection=True)
|
|
118
|
-
def create(
|
|
119
|
-
name: str = ServiceNameArgument,
|
|
120
|
-
compute_pool: str = typer.Option(
|
|
121
|
-
...,
|
|
122
|
-
"--compute-pool",
|
|
123
|
-
help="Compute pool to run the service on.",
|
|
124
|
-
show_default=False,
|
|
125
|
-
),
|
|
126
|
-
spec_path: Path = SpecPathOption,
|
|
127
|
-
min_instances: int = MinInstancesOption(),
|
|
128
|
-
max_instances: Optional[int] = MaxInstancesOption(),
|
|
129
|
-
auto_resume: bool = AutoResumeOption(),
|
|
130
|
-
external_access_integrations: Optional[List[str]] = typer.Option(
|
|
131
|
-
None,
|
|
132
|
-
"--eai-name",
|
|
133
|
-
help="Identifies External Access Integrations(EAI) that the service can access. This option may be specified multiple times for multiple EAIs.",
|
|
134
|
-
),
|
|
135
|
-
query_warehouse: Optional[str] = QueryWarehouseOption(),
|
|
136
|
-
tags: Optional[List[Tag]] = TagOption(help="Tag for the service."),
|
|
137
|
-
comment: Optional[str] = CommentOption(help=_COMMENT_HELP),
|
|
138
|
-
if_not_exists: bool = IfNotExistsOption(),
|
|
139
|
-
**options,
|
|
140
|
-
) -> CommandResult:
|
|
141
|
-
"""
|
|
142
|
-
Creates a new service in the current schema.
|
|
143
|
-
"""
|
|
144
|
-
max_instances = validate_and_set_instances(
|
|
145
|
-
min_instances, max_instances, "instances"
|
|
146
|
-
)
|
|
147
|
-
cursor = ServiceManager().create(
|
|
148
|
-
service_name=name,
|
|
149
|
-
min_instances=min_instances,
|
|
150
|
-
max_instances=max_instances,
|
|
151
|
-
compute_pool=compute_pool,
|
|
152
|
-
spec_path=spec_path,
|
|
153
|
-
external_access_integrations=external_access_integrations,
|
|
154
|
-
auto_resume=auto_resume,
|
|
155
|
-
query_warehouse=query_warehouse,
|
|
156
|
-
tags=tags,
|
|
157
|
-
comment=comment,
|
|
158
|
-
if_not_exists=if_not_exists,
|
|
159
|
-
)
|
|
160
|
-
return SingleQueryResult(cursor)
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
@app.command(requires_connection=True)
|
|
164
|
-
def status(name: str = ServiceNameArgument, **options) -> CommandResult:
|
|
165
|
-
"""
|
|
166
|
-
Retrieves the status of a service.
|
|
167
|
-
"""
|
|
168
|
-
cursor = ServiceManager().status(service_name=name)
|
|
169
|
-
return QueryJsonValueResult(cursor)
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
@app.command(requires_connection=True)
|
|
173
|
-
def logs(
|
|
174
|
-
name: str = ServiceNameArgument,
|
|
175
|
-
container_name: str = typer.Option(
|
|
176
|
-
...,
|
|
177
|
-
"--container-name",
|
|
178
|
-
help="Name of the container.",
|
|
179
|
-
show_default=False,
|
|
180
|
-
),
|
|
181
|
-
instance_id: str = typer.Option(
|
|
182
|
-
...,
|
|
183
|
-
"--instance-id",
|
|
184
|
-
help="ID of the service instance, starting with 0.",
|
|
185
|
-
show_default=False,
|
|
186
|
-
),
|
|
187
|
-
num_lines: int = typer.Option(
|
|
188
|
-
500, "--num-lines", help="Number of lines to retrieve."
|
|
189
|
-
),
|
|
190
|
-
**options,
|
|
191
|
-
):
|
|
192
|
-
"""
|
|
193
|
-
Retrieves local logs from a service container.
|
|
194
|
-
"""
|
|
195
|
-
results = ServiceManager().logs(
|
|
196
|
-
service_name=name,
|
|
197
|
-
instance_id=instance_id,
|
|
198
|
-
container_name=container_name,
|
|
199
|
-
num_lines=num_lines,
|
|
200
|
-
)
|
|
201
|
-
cursor = results.fetchone()
|
|
202
|
-
logs = next(iter(cursor)).split("\n")
|
|
203
|
-
print_log_lines(sys.stdout, name, "0", logs)
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
@app.command(requires_connection=True)
|
|
207
|
-
def upgrade(
|
|
208
|
-
name: str = ServiceNameArgument,
|
|
209
|
-
spec_path: Path = SpecPathOption,
|
|
210
|
-
**options,
|
|
211
|
-
):
|
|
212
|
-
"""
|
|
213
|
-
Updates an existing service with a new specification file.
|
|
214
|
-
"""
|
|
215
|
-
return SingleQueryResult(
|
|
216
|
-
ServiceManager().upgrade_spec(service_name=name, spec_path=spec_path)
|
|
217
|
-
)
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
@app.command("list-endpoints", requires_connection=True)
|
|
221
|
-
def list_endpoints(name: str = ServiceNameArgument, **options):
|
|
222
|
-
"""
|
|
223
|
-
Lists the endpoints in a service.
|
|
224
|
-
"""
|
|
225
|
-
return QueryResult(ServiceManager().list_endpoints(service_name=name))
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
@app.command(requires_connection=True)
|
|
229
|
-
def suspend(name: str = ServiceNameArgument, **options) -> CommandResult:
|
|
230
|
-
"""
|
|
231
|
-
Suspends the service, shutting down and deleting all its containers.
|
|
232
|
-
"""
|
|
233
|
-
return SingleQueryResult(ServiceManager().suspend(name))
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
@app.command(requires_connection=True)
|
|
237
|
-
def resume(name: str = ServiceNameArgument, **options) -> CommandResult:
|
|
238
|
-
"""
|
|
239
|
-
Resumes the service from a SUSPENDED state.
|
|
240
|
-
"""
|
|
241
|
-
return SingleQueryResult(ServiceManager().resume(name))
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
@app.command("set", requires_connection=True)
|
|
245
|
-
def set_property(
|
|
246
|
-
name: str = ServiceNameArgument,
|
|
247
|
-
min_instances: Optional[int] = MinInstancesOption(default=None, show_default=False),
|
|
248
|
-
max_instances: Optional[int] = MaxInstancesOption(show_default=False),
|
|
249
|
-
query_warehouse: Optional[str] = QueryWarehouseOption(show_default=False),
|
|
250
|
-
auto_resume: Optional[bool] = AutoResumeOption(default=None, show_default=False),
|
|
251
|
-
comment: Optional[str] = CommentOption(help=_COMMENT_HELP, show_default=False),
|
|
252
|
-
**options,
|
|
253
|
-
):
|
|
254
|
-
"""
|
|
255
|
-
Sets one or more properties for the service.
|
|
256
|
-
"""
|
|
257
|
-
cursor = ServiceManager().set_property(
|
|
258
|
-
service_name=name,
|
|
259
|
-
min_instances=min_instances,
|
|
260
|
-
max_instances=max_instances,
|
|
261
|
-
query_warehouse=query_warehouse,
|
|
262
|
-
auto_resume=auto_resume,
|
|
263
|
-
comment=comment,
|
|
264
|
-
)
|
|
265
|
-
return SingleQueryResult(cursor)
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
@app.command("unset", requires_connection=True)
|
|
269
|
-
def unset_property(
|
|
270
|
-
name: str = ServiceNameArgument,
|
|
271
|
-
min_instances: bool = MinInstancesOption(
|
|
272
|
-
default=False,
|
|
273
|
-
help=f"Reset the MIN_INSTANCES property - {_MIN_INSTANCES_HELP}",
|
|
274
|
-
show_default=False,
|
|
275
|
-
),
|
|
276
|
-
max_instances: bool = MaxInstancesOption(
|
|
277
|
-
default=False,
|
|
278
|
-
help=f"Reset the MAX_INSTANCES property - {_MAX_INSTANCES_HELP}",
|
|
279
|
-
show_default=False,
|
|
280
|
-
),
|
|
281
|
-
query_warehouse: bool = QueryWarehouseOption(
|
|
282
|
-
default=False,
|
|
283
|
-
help=f"Reset the QUERY_WAREHOUSE property - {_QUERY_WAREHOUSE_HELP}",
|
|
284
|
-
show_default=False,
|
|
285
|
-
),
|
|
286
|
-
auto_resume: bool = AutoResumeOption(
|
|
287
|
-
default=False,
|
|
288
|
-
param_decls=["--auto-resume"],
|
|
289
|
-
help=f"Reset the AUTO_RESUME property - {_AUTO_RESUME_HELP}",
|
|
290
|
-
show_default=False,
|
|
291
|
-
),
|
|
292
|
-
comment: bool = CommentOption(
|
|
293
|
-
default=False,
|
|
294
|
-
help=f"Reset the COMMENT property - {_COMMENT_HELP}",
|
|
295
|
-
callback=None,
|
|
296
|
-
show_default=False,
|
|
297
|
-
),
|
|
298
|
-
**options,
|
|
299
|
-
):
|
|
300
|
-
"""
|
|
301
|
-
Resets one or more properties for the service to their default value(s).
|
|
302
|
-
"""
|
|
303
|
-
cursor = ServiceManager().unset_property(
|
|
304
|
-
service_name=name,
|
|
305
|
-
min_instances=min_instances,
|
|
306
|
-
max_instances=max_instances,
|
|
307
|
-
query_warehouse=query_warehouse,
|
|
308
|
-
auto_resume=auto_resume,
|
|
309
|
-
comment=comment,
|
|
310
|
-
)
|
|
311
|
-
return SingleQueryResult(cursor)
|
|
@@ -1,170 +0,0 @@
|
|
|
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)
|
|
@@ -1,13 +0,0 @@
|
|
|
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.
|
|
@@ -1,83 +0,0 @@
|
|
|
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))
|
|
@@ -1,92 +0,0 @@
|
|
|
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
|
-
)
|
|
@@ -1,30 +0,0 @@
|
|
|
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
|
-
)
|