Exemple #1
0
def load_connections_dict(file_path: str) -> Dict[str, Any]:
    """
    Load connection from text file.

    ``JSON``, `YAML` and ``.env`` files are supported.

    :return: A dictionary where the key contains a connection ID and the value contains the connection.
    :rtype: Dict[str, airflow.models.connection.Connection]
    """
    log.debug("Loading connection")

    secrets: Dict[str, Any] = _parse_secret_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:
                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)
        else:
            connection_by_conn_id[key] = _create_connection(key, secret_values)

    num_conn = len(connection_by_conn_id)
    log.debug("Loaded %d connections", num_conn)

    return connection_by_conn_id
def load_connections(file: _io.TextIOWrapper):
    """
    Load connection from text file.

    Both ``JSON`` and ``.env`` files are supported.

    :param file: TextIOWrapper object of the file that will be processed.
    :type file: _io.TextIOWrapper

    :return: A dictionary where the key contains a connection ID and the value contains a list of connections.
    :rtype: Dict[str, List[airflow.models.connection.Connection]]
    """
    log.debug("Loading connection")
    secrets: Dict[str, Any] = _parse_secret_file(file)
    connections_by_conn_id = defaultdict(list)
    for key, secret_values in list(secrets.items()):
        if isinstance(secret_values, list):
            for secret_value in secret_values:
                connections_by_conn_id[key].append(_create_connection(key, secret_value))
        else:
            connections_by_conn_id[key].append(_create_connection(key, secret_values))

        if len(connections_by_conn_id[key]) > 1:
            raise ConnectionNotUnique(f"Found multiple values for {key} in {file.name}")

    num_conn = sum(map(len, connections_by_conn_id.values()))
    log.debug("Loaded %d connections", num_conn)

    return connections_by_conn_id