ashb commented on a change in pull request #15425: URL: https://github.com/apache/airflow/pull/15425#discussion_r640553314
########## File path: airflow/utils/parse.py ########## @@ -0,0 +1,168 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# + +"""Parse data from a file if it uses a valid format.""" +import json +import logging +import os +from collections import defaultdict +from json import JSONDecodeError +from typing import Any, Dict, List, Tuple + +import airflow.utils.yaml as yaml +from airflow.exceptions import AirflowException, AirflowFileParseException, FileSyntaxError +from airflow.utils.file import COMMENT_PATTERN Review comment: ```suggestion from airflow.exceptions import AirflowException, AirflowFileParseException, FileSyntaxError from airflow.utils import yaml from airflow.utils.file import COMMENT_PATTERN ``` ########## File path: airflow/secrets/local_filesystem.py ########## @@ -263,19 +71,32 @@ def load_connections_dict(file_path: str) -> Dict[str, Any]: :return: A dictionary where the key contains a connection ID and the value contains the connection. :rtype: Dict[str, airflow.models.connection.Connection] """ + from airflow.models.connection import Connection + log.debug("Loading connection") - secrets: Dict[str, Any] = _parse_secret_file(file_path) + secrets: Dict[str, Any] = _parse_file(file_path) connection_by_conn_id = {} for key, secret_values in list(secrets.items()): if isinstance(secret_values, list): - if len(secret_values) > 1: + # secret_values is either length 0, 1 or 2+ -- only length 1 is valid + if not secret_values: + log.debug("No secret values for %s", key) + continue + + if len(secret_values) >= 2: raise ConnectionNotUnique(f"Found multiple values for {key} in {file_path}.") - for secret_value in secret_values: - connection_by_conn_id[key] = _create_connection(key, secret_value) + # secret_values must be of length one, so unpack it + elif secret_values: + secret_values = secret_values[0] + + if isinstance(secret_values, dict): + connection_by_conn_id[key] = Connection.from_dict(key, secret_values) Review comment: Oh, this handles the extra to, which `**secret_values` wouldn't. ########## File path: airflow/cli/commands/connection_command.py ########## @@ -28,9 +28,9 @@ from airflow.exceptions import AirflowNotFoundException from airflow.hooks.base import BaseHook from airflow.models import Connection -from airflow.secrets.local_filesystem import _create_connection, load_connections_dict from airflow.utils import cli as cli_utils, yaml from airflow.utils.cli import suppress_logs_and_warning +from airflow.utils.parse import parse_file Review comment: Hmmmm, I'm not really happy with this module name -- it doesn't _really_ tell me what its doing, not in specific enough terms. We've already got `airflow.utils.file` that we could include this in -- I think I prefer that over a new file. "parse_file" is also a bit ambigious, but I can't think of a better name. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected]
