-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds mechanism for provider package discovery. (#12383)
This is a simple mechanism that will allow us to dynamically discover and register all provider packages in the Airflow core. Closes: #11422 GitOrigin-RevId: 2c0920fba5d2f05d2e29cead91127686af277ec2
- Loading branch information
Showing
12 changed files
with
224 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
# | ||
# 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. | ||
"""Manages all providers.""" | ||
import importlib | ||
import json | ||
import logging | ||
import pkgutil | ||
import traceback | ||
from typing import Dict | ||
|
||
import jsonschema | ||
import yaml | ||
|
||
try: | ||
import importlib.resources as importlib_resources | ||
except ImportError: | ||
# Try backported to PY<37 `importlib_resources`. | ||
import importlib_resources | ||
|
||
|
||
log = logging.getLogger(__name__) | ||
|
||
|
||
def _load_schema() -> Dict: | ||
return json.loads(importlib_resources.read_text('airflow', 'provider.yaml.schema.json')) | ||
|
||
|
||
class ProvidersManager: | ||
"""Manages all provider packages.""" | ||
|
||
def __init__(self): | ||
self._provider_directory = {} | ||
try: | ||
from airflow import providers | ||
except ImportError as e: | ||
log.warning("No providers are present or error when importing them! :%s", e) | ||
return | ||
self._schema = _load_schema() | ||
self.__find_all_providers(providers.__path__) | ||
|
||
def __find_all_providers(self, paths: str): | ||
def onerror(_): | ||
exception_string = traceback.format_exc() | ||
log.warning(exception_string) | ||
|
||
for module_info in pkgutil.walk_packages(paths, prefix="airflow.providers.", onerror=onerror): | ||
try: | ||
imported_module = importlib.import_module(module_info.name) | ||
except Exception as e: # noqa pylint: disable=broad-except | ||
log.warning("Error when importing %s:%s", module_info.name, e) | ||
continue | ||
try: | ||
provider = importlib_resources.read_text(imported_module, 'provider.yaml') | ||
provider_info = yaml.safe_load(provider) | ||
jsonschema.validate(provider_info, schema=self._schema) | ||
self._provider_directory[provider_info['package-name']] = provider_info | ||
except FileNotFoundError: | ||
# This is OK - this is not a provider package | ||
pass | ||
except TypeError as e: | ||
if "is not a package" not in str(e): | ||
log.warning("Error when loading 'provider.yaml' file from %s:%s}", module_info.name, e) | ||
# Otherwise this is OK - this is likely a module | ||
except Exception as e: # noqa pylint: disable=broad-except | ||
log.warning("Error when loading 'provider.yaml' file from %s:%s", module_info.name, e) | ||
|
||
@property | ||
def providers(self): | ||
"""Returns information about available providers.""" | ||
return self._provider_directory |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
# | ||
# 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. | ||
import unittest | ||
|
||
from airflow.providers_manager import ProvidersManager | ||
|
||
ALL_PROVIDERS = [ | ||
'apache-airflow-providers-amazon', | ||
'apache-airflow-providers-apache-cassandra', | ||
'apache-airflow-providers-apache-druid', | ||
'apache-airflow-providers-apache-hdfs', | ||
'apache-airflow-providers-apache-hive', | ||
'apache-airflow-providers-apache-kylin', | ||
'apache-airflow-providers-apache-livy', | ||
'apache-airflow-providers-apache-pig', | ||
'apache-airflow-providers-apache-pinot', | ||
'apache-airflow-providers-apache-spark', | ||
'apache-airflow-providers-apache-sqoop', | ||
'apache-airflow-providers-celery', | ||
'apache-airflow-providers-cloudant', | ||
'apache-airflow-providers-cncf-kubernetes', | ||
'apache-airflow-providers-databricks', | ||
'apache-airflow-providers-datadog', | ||
'apache-airflow-providers-dingding', | ||
'apache-airflow-providers-discord', | ||
'apache-airflow-providers-docker', | ||
'apache-airflow-providers-elasticsearch', | ||
'apache-airflow-providers-exasol', | ||
'apache-airflow-providers-facebook', | ||
'apache-airflow-providers-ftp', | ||
'apache-airflow-providers-google', | ||
'apache-airflow-providers-grpc', | ||
'apache-airflow-providers-hashicorp', | ||
'apache-airflow-providers-http', | ||
'apache-airflow-providers-imap', | ||
'apache-airflow-providers-jdbc', | ||
'apache-airflow-providers-jenkins', | ||
'apache-airflow-providers-jira', | ||
'apache-airflow-providers-microsoft-azure', | ||
'apache-airflow-providers-microsoft-mssql', | ||
'apache-airflow-providers-microsoft-winrm', | ||
'apache-airflow-providers-mongo', | ||
'apache-airflow-providers-mysql', | ||
'apache-airflow-providers-odbc', | ||
'apache-airflow-providers-openfaas', | ||
'apache-airflow-providers-opsgenie', | ||
'apache-airflow-providers-oracle', | ||
'apache-airflow-providers-pagerduty', | ||
'apache-airflow-providers-papermill', | ||
'apache-airflow-providers-plexus', | ||
'apache-airflow-providers-postgres', | ||
'apache-airflow-providers-presto', | ||
'apache-airflow-providers-qubole', | ||
'apache-airflow-providers-redis', | ||
'apache-airflow-providers-salesforce', | ||
'apache-airflow-providers-samba', | ||
'apache-airflow-providers-segment', | ||
'apache-airflow-providers-sendgrid', | ||
'apache-airflow-providers-sftp', | ||
'apache-airflow-providers-singularity', | ||
'apache-airflow-providers-slack', | ||
'apache-airflow-providers-snowflake', | ||
'apache-airflow-providers-sqlite', | ||
'apache-airflow-providers-ssh', | ||
'apache-airflow-providers-vertica', | ||
'apache-airflow-providers-yandex', | ||
'apache-airflow-providers-zendesk', | ||
] | ||
|
||
|
||
class TestProviderManager(unittest.TestCase): | ||
def test_providers_are_loaded(self): | ||
provider_manager = ProvidersManager() | ||
provider_list = list(provider_manager.providers.keys()) | ||
provider_list.sort() | ||
self.assertEqual(ALL_PROVIDERS, provider_list) |