|
| 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 | +"""Pure functions for configuration merging operations.""" |
| 16 | + |
| 17 | +from typing import Any, Dict |
| 18 | + |
| 19 | +from snowflake.cli.api.config_ng.constants import ( |
| 20 | + INTERNAL_CLI_PARAMETERS, |
| 21 | + ConfigSection, |
| 22 | +) |
| 23 | + |
| 24 | + |
| 25 | +def extract_root_level_connection_params( |
| 26 | + config: Dict[str, Any], |
| 27 | +) -> tuple[Dict[str, Any], Dict[str, Any]]: |
| 28 | + """ |
| 29 | + Extract root-level connection parameters from config. |
| 30 | +
|
| 31 | + Connection parameters at root level (not under any section) should |
| 32 | + be treated as general connection parameters that apply to all connections. |
| 33 | +
|
| 34 | + Args: |
| 35 | + config: Configuration dictionary with mixed sections and parameters |
| 36 | +
|
| 37 | + Returns: |
| 38 | + Tuple of (connection_params, remaining_config) |
| 39 | +
|
| 40 | + Example: |
| 41 | + Input: {"account": "acc", "cli": {...}, "connections": {...}} |
| 42 | + Output: ({"account": "acc"}, {"cli": {...}, "connections": {...}}) |
| 43 | + """ |
| 44 | + known_sections = {s.value for s in ConfigSection} |
| 45 | + |
| 46 | + connection_params = {} |
| 47 | + remaining = {} |
| 48 | + |
| 49 | + for key, value in config.items(): |
| 50 | + # Check if this key is a known section or internal parameter |
| 51 | + is_section = key in known_sections or any( |
| 52 | + key.startswith(s + ".") for s in known_sections |
| 53 | + ) |
| 54 | + is_internal = key in INTERNAL_CLI_PARAMETERS |
| 55 | + |
| 56 | + if not is_section and not is_internal: |
| 57 | + # Root-level parameter that's not a section = connection parameter |
| 58 | + connection_params[key] = value |
| 59 | + else: |
| 60 | + remaining[key] = value |
| 61 | + |
| 62 | + return connection_params, remaining |
| 63 | + |
| 64 | + |
| 65 | +def merge_params_into_connections( |
| 66 | + connections: Dict[str, Dict[str, Any]], params: Dict[str, Any] |
| 67 | +) -> Dict[str, Dict[str, Any]]: |
| 68 | + """ |
| 69 | + Merge parameters into all existing connections. |
| 70 | +
|
| 71 | + Used for overlay sources where root-level connection params apply to all connections. |
| 72 | + The params overlay (override) values in each connection. |
| 73 | +
|
| 74 | + Args: |
| 75 | + connections: Dictionary of connection configurations |
| 76 | + params: Parameters to merge into each connection |
| 77 | +
|
| 78 | + Returns: |
| 79 | + Dictionary of connections with params merged in |
| 80 | +
|
| 81 | + Example: |
| 82 | + Input: |
| 83 | + connections = {"dev": {"account": "dev_acc", "user": "dev_user"}} |
| 84 | + params = {"user": "override_user", "password": "new_pass"} |
| 85 | + Output: |
| 86 | + {"dev": {"account": "dev_acc", "user": "override_user", "password": "new_pass"}} |
| 87 | + """ |
| 88 | + from snowflake.cli.api.config_ng.dict_utils import deep_merge |
| 89 | + |
| 90 | + result = {} |
| 91 | + for conn_name, conn_config in connections.items(): |
| 92 | + if isinstance(conn_config, dict): |
| 93 | + result[conn_name] = deep_merge(conn_config, params) |
| 94 | + else: |
| 95 | + result[conn_name] = conn_config |
| 96 | + |
| 97 | + return result |
| 98 | + |
| 99 | + |
| 100 | +def create_default_connection_from_params( |
| 101 | + params: Dict[str, Any], |
| 102 | +) -> Dict[str, Dict[str, Any]]: |
| 103 | + """ |
| 104 | + Create a default connection from connection parameters. |
| 105 | +
|
| 106 | + Args: |
| 107 | + params: Connection parameters |
| 108 | +
|
| 109 | + Returns: |
| 110 | + Dictionary with "default" connection containing the params |
| 111 | +
|
| 112 | + Example: |
| 113 | + Input: {"account": "acc", "user": "usr"} |
| 114 | + Output: {"default": {"account": "acc", "user": "usr"}} |
| 115 | + """ |
| 116 | + if not params: |
| 117 | + return {} |
| 118 | + return {"default": params.copy()} |
0 commit comments