Skip to content

Commit f23099c

Browse files
EvanR-DevEvan Romangavin-aguiar
authored
feat: support CosmosDB SDK-type bindings (#109)
* Init * Fix * Add code qual * Add docs * Fix * Chng docs * Chng test * Rename * Fix * Fix * Fix * Fix * Fix * Fix * test * TODO --------- Co-authored-by: Evan Roman <[email protected]> Co-authored-by: Gavin Aguiar <[email protected]>
1 parent 3866dd6 commit f23099c

File tree

55 files changed

+1770
-39
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+1770
-39
lines changed

azurefunctions-extensions-bindings-blob/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ import logging
5656
import azure.functions as func
5757
import azurefunctions.extensions.bindings.blob as blob
5858

59-
app = func.FunctionApp(http_auth_level=func.AuthLevel.ANONYMOUS)
59+
app = func.FunctionApp(http_auth_level=func.AuthLevel.FUNCTION)
6060

6161
@app.blob_trigger(arg_name="client",
6262
path="PATH/TO/BLOB",

azurefunctions-extensions-bindings-blob/azurefunctions/extensions/bindings/blob/blobClient.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,4 @@ def get_sdk_type(self):
5656
blob=self._blobName,
5757
)
5858
else:
59-
return None
59+
raise ValueError(f"Unable to create {self.__class__.__name__} SDK type.")

azurefunctions-extensions-bindings-blob/azurefunctions/extensions/bindings/blob/containerClient.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,4 @@ def get_sdk_type(self):
4646
container=self._containerName
4747
)
4848
else:
49-
return None
49+
raise ValueError(f"Unable to create {self.__class__.__name__} SDK type.")

azurefunctions-extensions-bindings-blob/azurefunctions/extensions/bindings/blob/storageStreamDownloader.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,4 @@ def get_sdk_type(self):
4848
blob=self._blobName,
4949
).download_blob()
5050
else:
51-
return None
51+
raise ValueError(f"Unable to create {self.__class__.__name__} SDK type.")

azurefunctions-extensions-bindings-blob/samples/blob_samples_blobclient/function_app.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import azure.functions as func
1010
import azurefunctions.extensions.bindings.blob as blob
1111

12-
app = func.FunctionApp(http_auth_level=func.AuthLevel.ANONYMOUS)
12+
app = func.FunctionApp(http_auth_level=func.AuthLevel.FUNCTION)
1313

1414
"""
1515
FOLDER: blob_samples_blobclient

azurefunctions-extensions-bindings-blob/samples/blob_samples_containerclient/function_app.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import azure.functions as func
1010
import azurefunctions.extensions.bindings.blob as blob
1111

12-
app = func.FunctionApp(http_auth_level=func.AuthLevel.ANONYMOUS)
12+
app = func.FunctionApp(http_auth_level=func.AuthLevel.FUNCTION)
1313

1414
"""
1515
FOLDER: blob_samples_containerclient

azurefunctions-extensions-bindings-blob/samples/blob_samples_storagestreamdownloader/function_app.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import azure.functions as func
1010
import azurefunctions.extensions.bindings.blob as blob
1111

12-
app = func.FunctionApp(http_auth_level=func.AuthLevel.ANONYMOUS)
12+
app = func.FunctionApp(http_auth_level=func.AuthLevel.FUNCTION)
1313

1414
"""
1515
FOLDER: blob_samples_storagestreamdownloader

azurefunctions-extensions-bindings-blob/tests/test_blobclient.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,10 @@ def test_input_incorrect_type(self):
9494

9595
def test_input_empty(self):
9696
datum: Datum = Datum(value={}, type="model_binding_data")
97-
result: BlobClient = BlobClientConverter.decode(
98-
data=datum, trigger_metadata=None, pytype=BlobClient
99-
)
100-
self.assertIsNone(result)
97+
with self.assertRaises(ValueError):
98+
BlobClientConverter.decode(
99+
data=datum, trigger_metadata=None, pytype=BlobClient
100+
)
101101

102102
def test_input_populated(self):
103103
content = {

azurefunctions-extensions-bindings-blob/tests/test_containerclient.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,10 @@ def test_input_incorrect_type(self):
9494

9595
def test_input_empty(self):
9696
datum: Datum = Datum(value={}, type="model_binding_data")
97-
result: ContainerClient = BlobClientConverter.decode(
98-
data=datum, trigger_metadata=None, pytype=ContainerClient
99-
)
100-
self.assertIsNone(result)
97+
with self.assertRaises(ValueError):
98+
BlobClientConverter.decode(
99+
data=datum, trigger_metadata=None, pytype=ContainerClient
100+
)
101101

102102
def test_input_populated(self):
103103
content = {

azurefunctions-extensions-bindings-blob/tests/test_ssd.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,10 @@ def test_input_incorrect_type(self):
9797

9898
def test_input_empty(self):
9999
datum: Datum = Datum(value={}, type="model_binding_data")
100-
result: StorageStreamDownloader = BlobClientConverter.decode(
101-
data=datum, trigger_metadata=None, pytype=StorageStreamDownloader
102-
)
103-
self.assertIsNone(result)
100+
with self.assertRaises(ValueError):
101+
BlobClientConverter.decode(
102+
data=datum, trigger_metadata=None, pytype=StorageStreamDownloader
103+
)
104104

105105
def test_input_populated(self):
106106
content = {
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
Copyright (c) Microsoft Corporation.
2+
3+
MIT License
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
recursive-include azure *.py *.pyi
2+
recursive-include tests *.py
3+
include LICENSE README.md
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
# Azure Functions Extensions Bindings Cosmos DB library for Python
2+
This library allows Cosmos DB Input bindings in Python Function Apps to recognize and bind to client types from the
3+
Azure Cosmos DB SDK.
4+
5+
Cosmos DB client types can be generated from:
6+
7+
* Cosmos DB Input
8+
9+
[Source code](https://github.com/Azure/azure-functions-python-extensions/tree/dev/azurefunctions-extensions-bindings-cosmosdb)
10+
[Package (PyPi)](https://pypi.org/project/azurefunctions-extensions-bindings-cosmosdb/)
11+
| [Samples](https://github.com/Azure/azure-functions-python-extensions/tree/dev/azurefunctions-extensions-bindings-cosmosdb/samples)
12+
13+
14+
## Getting started
15+
16+
### Prerequisites
17+
* Python 3.9 or later is required to use this package. For more details, please read our page on [Python Functions version support policy](https://learn.microsoft.com/en-us/azure/azure-functions/functions-versions?tabs=isolated-process%2Cv4&pivots=programming-language-python#languages).
18+
19+
* You must have an [Azure subscription](https://azure.microsoft.com/free/) and an
20+
[Azure storage account](https://docs.microsoft.com/azure/storage/common/storage-account-overview) to use this package.
21+
22+
### Install the package
23+
Install the Azure Functions Extensions Bindings Cosmos DB library for Python with pip:
24+
25+
```bash
26+
pip install azurefunctions-extensions-bindings-cosmosdb
27+
```
28+
29+
### Create a storage account
30+
If you wish to create a new storage account, you can use the
31+
[Azure Portal](https://docs.microsoft.com/azure/storage/common/storage-quickstart-create-account?tabs=azure-portal),
32+
[Azure PowerShell](https://docs.microsoft.com/azure/storage/common/storage-quickstart-create-account?tabs=azure-powershell),
33+
or [Azure CLI](https://docs.microsoft.com/azure/storage/common/storage-quickstart-create-account?tabs=azure-cli):
34+
35+
```bash
36+
# Create a new resource group to hold the storage account -
37+
# if using an existing resource group, skip this step
38+
az group create --name my-resource-group --location westus2
39+
40+
# Create the storage account
41+
az storage account create -n my-storage-account-name -g my-resource-group
42+
```
43+
44+
### Bind to the SDK-type
45+
The Azure Functions Extensions Bindings Cosmos DB library for Python allows you to create a function app with
46+
Cosmos DB Input and define the type as a CosmosClient, DatabaseProxy, or ContainerProxy. Instead of receiving
47+
a DocumentList, when the function is executed, the type returned will be the defined SDK-type and have all of the
48+
properties and methods available as seen in the Azure Storage Cosmos DB library for Python.
49+
50+
51+
```python
52+
import logging
53+
import azure.functions as func
54+
import azurefunctions.extensions.bindings.cosmosdb as cosmos
55+
56+
app = func.FunctionApp(http_auth_level=func.AuthLevel.FUNCTION)
57+
58+
@app.route(route="cosmos")
59+
@app.cosmos_db_input(arg_name="container",
60+
connection="CosmosDBConnection",
61+
database_name="db_name",
62+
container_name="container_name")
63+
def get_docs(req: func.HttpRequest, client: cosmos.CosmosClient):
64+
databases = client.list_databases()
65+
for db in databases:
66+
logging.info(f"Found database with ID: {db.get('id')}")
67+
68+
return "ok"
69+
```
70+
71+
## Troubleshooting
72+
### General
73+
The SDK-types raise exceptions defined in [Azure Core](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/core/azure-core/README.md).
74+
75+
This list can be used for reference to catch thrown exceptions. To get the specific error code of the exception, use the `error_code` attribute, i.e, `exception.error_code`.
76+
77+
## Next steps
78+
79+
### More sample code
80+
81+
Get started with our [Cosmos DB samples](https://github.com/Azure/azure-functions-python-extensions/tree/dev/azurefunctions-extensions-bindings-cosmosdb/samples).
82+
83+
Several samples are available in this GitHub repository. These samples provide example code for additional scenarios commonly encountered while working with Cosmos DB:
84+
85+
* [cosmosdb_samples_cosmosclient](https://github.com/Azure/azure-functions-python-extensions/tree/dev/azurefunctions-extensions-binding-cosmosdb/samples/cosmosdb_samples_cosmosclient) - Examples for using the CosmosClient type:
86+
* From CosmosDBInput
87+
88+
* [cosmosdb_samples_databaseproxy](https://github.com/Azure/azure-functions-python-extensions/tree/dev/azurefunctions-extensions-bindings-cosmosdb/samples/cosmosdb_samples_databaseproxy) - Examples for using the DatabaseProxy type:
89+
* From CosmosDBInput
90+
91+
* [cosmosdb_samples_containerclient](https://github.com/Azure/azure-functions-python-extensions/tree/dev/azurefunctions-extensions-bindings-cosmosdb/samples/cosmosdb_samples_containerproxy) - Examples for using the ContainerProxy type:
92+
* From CosmosDBInput
93+
94+
### Additional documentation
95+
For more information on the Azure Cosmos DB SDK, see the [Azure Cosmos DB documentation](https://learn.microsoft.com/en-us/azure/cosmos-db/) on learn.microsoft.com
96+
and the [Azure Cosmos DB README](https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/cosmos/azure-cosmos).
97+
98+
## Contributing
99+
This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.microsoft.com.
100+
101+
When you submit a pull request, a CLA-bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.
102+
103+
This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/). For more information see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or contact [[email protected]](mailto:[email protected]) with any additional questions or comments.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
__path__ = __import__("pkgutil").extend_path(__path__, __name__)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
__path__ = __import__("pkgutil").extend_path(__path__, __name__)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
__path__ = __import__("pkgutil").extend_path(__path__, __name__)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Copyright (c) Microsoft Corporation. All rights reserved.
2+
# Licensed under the MIT License.
3+
4+
from .cosmosClient import CosmosClient
5+
from .databaseProxy import DatabaseProxy
6+
from .containerProxy import ContainerProxy
7+
from .cosmosClientConverter import CosmosClientConverter
8+
9+
__all__ = [
10+
"CosmosClient",
11+
"DatabaseProxy",
12+
"ContainerProxy",
13+
"CosmosClientConverter"
14+
]
15+
16+
__version__ = "1.0.0a1"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Copyright (c) Microsoft Corporation. All rights reserved.
2+
# Licensed under the MIT License.
3+
4+
import json
5+
6+
from azure.cosmos import ContainerProxy as ContainerProxySdk
7+
from azurefunctions.extensions.base import Datum, SdkType
8+
from .utils import get_connection_string, using_managed_identity, get_cosmos_client
9+
10+
11+
class ContainerProxy(SdkType):
12+
def __init__(self, *, data: Datum) -> None:
13+
# model_binding_data properties
14+
self._data = data
15+
self._version = None
16+
self._source = None
17+
self._content_type = None
18+
self._database_name = None
19+
self._container_name = None
20+
self._connection = None
21+
self._using_managed_identity = False
22+
self._preferred_locations = None
23+
if self._data:
24+
self._version = data.version
25+
self._source = data.source
26+
self._content_type = data.content_type
27+
content_json = json.loads(data.content)
28+
self._database_name = content_json.get("DatabaseName")
29+
self._container_name = content_json.get("ContainerName")
30+
self._connection = get_connection_string(content_json.get("Connection"))
31+
self._using_managed_identity = using_managed_identity(
32+
content_json.get("Connection")
33+
)
34+
self._preferred_locations = content_json.get("PreferredLocations")
35+
36+
def get_sdk_type(self) -> ContainerProxySdk:
37+
"""
38+
There are two ways to create a CosmosClient:
39+
1. Through the constructor: this is the only option when using Managed Identity
40+
2. Through from_connection_string: when not using Managed Identity
41+
42+
We track if Managed Identity is being used through a flag.
43+
"""
44+
if not self._data:
45+
raise ValueError(f"Unable to create {self.__class__.__name__} SDK type.")
46+
47+
cosmos_client = get_cosmos_client(self._using_managed_identity,
48+
self._connection, self._preferred_locations)
49+
db_client = cosmos_client.get_database_client(self._database_name)
50+
return db_client.get_container_client(self._container_name)
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Copyright (c) Microsoft Corporation. All rights reserved.
2+
# Licensed under the MIT License.
3+
4+
import json
5+
6+
from azure.cosmos import CosmosClient as CosmosClientSdk
7+
from azurefunctions.extensions.base import Datum, SdkType
8+
from .utils import get_connection_string, using_managed_identity, get_cosmos_client
9+
10+
11+
class CosmosClient(SdkType):
12+
def __init__(self, *, data: Datum) -> None:
13+
# model_binding_data properties
14+
self._data = data
15+
self._version = None
16+
self._source = None
17+
self._content_type = None
18+
self._database_name = None
19+
self._container_name = None
20+
self._connection = None
21+
self._using_managed_identity = False
22+
self._preferred_locations = None
23+
if self._data:
24+
self._version = data.version
25+
self._source = data.source
26+
self._content_type = data.content_type
27+
content_json = json.loads(data.content)
28+
self._database_name = content_json.get("DatabaseName")
29+
self._container_name = content_json.get("ContainerName")
30+
self._connection = get_connection_string(content_json.get("Connection"))
31+
self._using_managed_identity = using_managed_identity(
32+
content_json.get("Connection")
33+
)
34+
self._preferred_locations = content_json.get("PreferredLocations")
35+
36+
def get_sdk_type(self) -> CosmosClientSdk:
37+
"""
38+
There are two ways to create a CosmosClient:
39+
1. Through the constructor: this is the only option when using Managed Identity
40+
2. Through from_connection_string: when not using Managed Identity
41+
42+
We track if Managed Identity is being used through a flag.
43+
"""
44+
if not self._data:
45+
raise ValueError(f"Unable to create {self.__class__.__name__} SDK type.")
46+
47+
return get_cosmos_client(self._using_managed_identity,
48+
self._connection, self._preferred_locations)

0 commit comments

Comments
 (0)