forked from apache/airflow
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add rest API to query for providers (apache#13394)
* Add API to query for providers (apache#12468) * Improve tests speed (apache#12468)
- Loading branch information
Showing
7 changed files
with
264 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# 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 re | ||
from typing import Dict, List | ||
|
||
from airflow.api_connexion import security | ||
from airflow.api_connexion.schemas.provider_schema import ProviderCollection, provider_collection_schema | ||
from airflow.providers_manager import ProviderInfo, ProvidersManager | ||
from airflow.security import permissions | ||
|
||
|
||
def _remove_rst_syntax(value: str) -> str: | ||
return re.sub("[`_<>]", "", value.strip(" \n.")) | ||
|
||
|
||
def _provider_mapper(provider: ProviderInfo) -> Dict: | ||
return { | ||
"package_name": provider[1]["package-name"], | ||
"description": _remove_rst_syntax(provider[1]["description"]), | ||
"version": provider[0], | ||
} | ||
|
||
|
||
@security.requires_access([(permissions.ACTION_CAN_READ, permissions.RESOURCE_PROVIDER)]) | ||
def get_providers(): | ||
"""Get providers""" | ||
providers_info: List[ProviderInfo] = list(ProvidersManager().providers.values()) | ||
providers = [_provider_mapper(d) for d in providers_info] | ||
total_entries = len(providers) | ||
return provider_collection_schema.dump( | ||
ProviderCollection(providers=providers, total_entries=total_entries) | ||
) |
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,46 @@ | ||
# 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. | ||
|
||
from typing import List, NamedTuple | ||
|
||
from marshmallow import Schema, fields | ||
|
||
|
||
class ProviderSchema(Schema): | ||
"""Provider schema""" | ||
|
||
package_name = fields.String(required=True) | ||
description = fields.String(required=True) | ||
version = fields.String(required=True) | ||
|
||
|
||
class ProviderCollection(NamedTuple): | ||
"""List of Providers""" | ||
|
||
providers: List[ProviderSchema] | ||
total_entries: int | ||
|
||
|
||
class ProviderCollectionSchema(Schema): | ||
"""Provider Collection schema""" | ||
|
||
providers = fields.List(fields.Nested(ProviderSchema)) | ||
total_entries = fields.Int() | ||
|
||
|
||
provider_collection_schema = ProviderCollectionSchema() | ||
provider_schema = ProviderSchema() |
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
123 changes: 123 additions & 0 deletions
123
tests/api_connexion/endpoints/test_provider_endpoint.py
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,123 @@ | ||
# 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. | ||
|
||
from collections import OrderedDict | ||
from unittest import mock | ||
|
||
import pytest | ||
|
||
from airflow.security import permissions | ||
from tests.test_utils.api_connexion_utils import create_user, delete_user | ||
|
||
MOCK_PROVIDERS = OrderedDict( | ||
[ | ||
( | ||
'apache-airflow-providers-amazon', | ||
( | ||
'1.0.0', | ||
{ | ||
'package-name': 'apache-airflow-providers-amazon', | ||
'name': 'Amazon', | ||
'description': '`Amazon Web Services (AWS) <https://aws.amazon.com/>`__.\n', | ||
'versions': ['1.0.0'], | ||
}, | ||
), | ||
), | ||
( | ||
'apache-airflow-providers-apache-cassandra', | ||
( | ||
'1.0.0', | ||
{ | ||
'package-name': 'apache-airflow-providers-apache-cassandra', | ||
'name': 'Apache Cassandra', | ||
'description': '`Apache Cassandra <http://cassandra.apache.org/>`__.\n', | ||
'versions': ['1.0.0'], | ||
}, | ||
), | ||
), | ||
] | ||
) | ||
|
||
|
||
@pytest.fixture(scope="module") | ||
def configured_app(minimal_app_for_api): | ||
app = minimal_app_for_api | ||
create_user( | ||
app, # type: ignore | ||
username="test", | ||
role_name="Test", | ||
permissions=[(permissions.ACTION_CAN_READ, permissions.RESOURCE_PROVIDER)], | ||
) | ||
create_user(app, username="test_no_permissions", role_name="TestNoPermissions") # type: ignore | ||
|
||
yield app | ||
|
||
delete_user(app, username="test") # type: ignore | ||
delete_user(app, username="test_no_permissions") # type: ignore | ||
|
||
|
||
class TestBaseProviderEndpoint: | ||
@pytest.fixture(autouse=True) | ||
def setup_attrs(self, configured_app) -> None: | ||
self.app = configured_app | ||
self.client = self.app.test_client() # type:ignore | ||
|
||
|
||
class TestGetProviders(TestBaseProviderEndpoint): | ||
@mock.patch( | ||
"airflow.providers_manager.ProvidersManager.providers", | ||
new_callable=mock.PropertyMock, | ||
return_value={}, | ||
) | ||
def test_response_200_empty_list(self, mock_providers): | ||
response = self.client.get("/api/v1/providers", environ_overrides={'REMOTE_USER': "test"}) | ||
assert response.status_code == 200 | ||
assert response.json == {"providers": [], "total_entries": 0} | ||
|
||
@mock.patch( | ||
"airflow.providers_manager.ProvidersManager.providers", | ||
new_callable=mock.PropertyMock, | ||
return_value=MOCK_PROVIDERS, | ||
) | ||
def test_response_200(self, mock_providers): | ||
response = self.client.get("/api/v1/providers", environ_overrides={'REMOTE_USER': "test"}) | ||
assert response.status_code == 200 | ||
assert response.json == { | ||
'providers': [ | ||
{ | ||
'description': 'Amazon Web Services (AWS) https://aws.amazon.com/', | ||
'package_name': 'apache-airflow-providers-amazon', | ||
'version': '1.0.0', | ||
}, | ||
{ | ||
'description': 'Apache Cassandra http://cassandra.apache.org/', | ||
'package_name': 'apache-airflow-providers-apache-cassandra', | ||
'version': '1.0.0', | ||
}, | ||
], | ||
'total_entries': 2, | ||
} | ||
|
||
def test_should_raises_401_unauthenticated(self): | ||
response = self.client.get("/api/v1/providers") | ||
assert response.status_code == 401 | ||
|
||
def test_should_raise_403_forbidden(self): | ||
response = self.client.get( | ||
"/api/v1/providers", environ_overrides={'REMOTE_USER': "test_no_permissions"} | ||
) | ||
assert response.status_code == 403 |