-
Notifications
You must be signed in to change notification settings - Fork 3
feat: support CosmosDB SDK-type bindings #109
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
ae8debf
Init
0896771
Fix
b075c17
Add code qual
859ed62
Add docs
3e9c8b7
Fix
9c42d1b
Chng docs
64dde80
Chng test
6641ad2
Merge branch 'dev' into evanroman/cosmos
gavin-aguiar b656864
Merge remote-tracking branch 'origin/dev' into evanroman/cosmos
fd8021c
Merge branch 'evanroman/cosmos' of https://github.com/Azure/azure-fun…
e37aa73
Rename
05d5f47
Fix
75d5f8b
Fix
8a06cea
Fix
ea4600d
Fix
777dc8d
Merge remote-tracking branch 'origin/dev' into evanroman/cosmos
a0397a9
Fix
121e9c3
Fix
c388118
Merge branch 'dev' into evanroman/cosmos
EvanR-Dev 5220076
test
8f315b7
Merge branch 'evanroman/cosmos' of https://github.com/Azure/azure-fun…
bba4178
TODO
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Empty file.
EvanR-Dev marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or 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,21 @@ | ||
Copyright (c) Microsoft Corporation. | ||
|
||
MIT License | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains hidden or 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,3 @@ | ||
recursive-include azure *.py *.pyi | ||
recursive-include tests *.py | ||
include LICENSE README.md |
This file contains hidden or 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,105 @@ | ||
# Azure Functions Extensions Bindings Cosmos library for Python | ||
This library allows Cosmos Input bindings in Python Function Apps to recognize and bind to client types from the | ||
Azure Cosmos sdk. | ||
|
||
Cosmos client types can be generated from: | ||
|
||
* Cosmos Input | ||
|
||
[Source code](https://github.com/Azure/azure-functions-python-extensions/tree/main/azurefunctions-extensions-bindings-cosmos) | ||
EvanR-Dev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
[Package (PyPi)](https://pypi.org/project/azurefunctions-extensions-bindings-cosmos/) | ||
| API reference documentation | ||
| Product documentation | ||
| [Samples](hhttps://github.com/Azure/azure-functions-python-extensions/tree/main/azurefunctions-extensions-bindings-cosmos/samples) | ||
EvanR-Dev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
## Getting started | ||
|
||
### Prerequisites | ||
* 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). | ||
|
||
* You must have an [Azure subscription](https://azure.microsoft.com/free/) and an | ||
[Azure storage account](https://docs.microsoft.com/azure/storage/common/storage-account-overview) to use this package. | ||
|
||
### Install the package | ||
Install the Azure Functions Extensions Bindings Cosmos library for Python with pip: | ||
|
||
```bash | ||
pip install azurefunctions-extensions-bindings-cosmos | ||
``` | ||
|
||
### Create a storage account | ||
If you wish to create a new storage account, you can use the | ||
[Azure Portal](https://docs.microsoft.com/azure/storage/common/storage-quickstart-create-account?tabs=azure-portal), | ||
[Azure PowerShell](https://docs.microsoft.com/azure/storage/common/storage-quickstart-create-account?tabs=azure-powershell), | ||
or [Azure CLI](https://docs.microsoft.com/azure/storage/common/storage-quickstart-create-account?tabs=azure-cli): | ||
|
||
```bash | ||
# Create a new resource group to hold the storage account - | ||
# if using an existing resource group, skip this step | ||
az group create --name my-resource-group --location westus2 | ||
|
||
# Create the storage account | ||
az storage account create -n my-storage-account-name -g my-resource-group | ||
``` | ||
|
||
### Bind to the SDK-type | ||
The Azure Functions Extensions Bindings Cosmos library for Python allows you to create a function app with | ||
Cosmos Input and define the type as a CosmosClient, DatabaseProxy, or ContainerProxy. Instead of receiving | ||
an InputStream, when the function is executed, the type returned will be the defined SDK-type and have all of the | ||
EvanR-Dev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
properties and methods available as seen in the Azure Storage Cosmos library for Python. | ||
|
||
|
||
```python | ||
import logging | ||
import azure.functions as func | ||
import azurefunctions.extensions.bindings.cosmos as cosmos | ||
|
||
app = func.FunctionApp(http_auth_level=func.AuthLevel.ANONYMOUS) | ||
|
||
@app.route(route="cosmos") | ||
@app.cosmos_db_input(arg_name="container", | ||
EvanR-Dev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
connection="AzureWebJobsStorage", | ||
EvanR-Dev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
database_name="db_name", | ||
container_name="container_name") | ||
def get_docs(req: func.HttpRequest, client: cosmos.CosmosClient): | ||
databases = client.list_databases() | ||
for db in databases: | ||
logging.info(f"Found database with ID: {db.get('id')}") | ||
|
||
return "ok" | ||
``` | ||
|
||
## Troubleshooting | ||
### General | ||
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). | ||
|
||
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`. | ||
|
||
## Next steps | ||
|
||
### More sample code | ||
|
||
Get started with our [Cosmos samples](hhttps://github.com/Azure/azure-functions-python-extensions/tree/main/azurefunctions-extensions-bindings-cosmos/samples). | ||
EvanR-Dev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Several samples are available in this GitHub repository. These samples provide example code for additional scenarios commonly encountered while working with Cosmos: | ||
|
||
* [cosmos_samples_cosmosclient](https://github.com/Azure/azure-functions-python-extensions/tree/main/azurefunctions-extensions-binding-cosmos/samples/cosmos_samples_cosmosclient) - Examples for using the CosmosClient type: | ||
* From CosmosInput | ||
|
||
* [cosmos_samples_databaseproxy](https://github.com/Azure/azure-functions-python-extensions/tree/main/azurefunctions-extensions-bindings-cosmos/samples/cosmos_samples_databaseproxy) - Examples for using the DatabaseProxy type: | ||
* From CosmosInput | ||
|
||
* [cosmos_samples_containerclient](https://github.com/Azure/azure-functions-python-extensions/tree/main/azurefunctions-extensions-bindings-cosmos/samples/cosmos_samples_containerproxy) - Examples for using the ContainerProxy type: | ||
* From CosmosInput | ||
|
||
### Additional documentation | ||
For more information on the Azure Cosmos SDK, see the [Azure Cosmos DB documentation](https://learn.microsoft.com/en-us/azure/cosmos-db/) on learn.microsoft.com | ||
and the [Azure Cosmos DB README](https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/cosmos/azure-cosmos). | ||
|
||
## Contributing | ||
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. | ||
|
||
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. | ||
|
||
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. |
1 change: 1 addition & 0 deletions
1
azurefunctions-extensions-bindings-cosmos/azurefunctions/__init__.py
This file contains hidden or 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 @@ | ||
__path__ = __import__("pkgutil").extend_path(__path__, __name__) |
1 change: 1 addition & 0 deletions
1
azurefunctions-extensions-bindings-cosmos/azurefunctions/extensions/__init__.py
This file contains hidden or 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 @@ | ||
__path__ = __import__("pkgutil").extend_path(__path__, __name__) |
1 change: 1 addition & 0 deletions
1
azurefunctions-extensions-bindings-cosmos/azurefunctions/extensions/bindings/__init__.py
This file contains hidden or 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 @@ | ||
__path__ = __import__("pkgutil").extend_path(__path__, __name__) |
16 changes: 16 additions & 0 deletions
16
...unctions-extensions-bindings-cosmos/azurefunctions/extensions/bindings/cosmos/__init__.py
This file contains hidden or 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,16 @@ | ||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# Licensed under the MIT License. | ||
|
||
from .cosmosClient import CosmosClient | ||
from .databaseProxy import DatabaseProxy | ||
from .containerProxy import ContainerProxy | ||
from .cosmosClientConverter import CosmosClientConverter | ||
|
||
__all__ = [ | ||
"CosmosClient", | ||
"DatabaseProxy", | ||
"ContainerProxy", | ||
"CosmosClientConverter" | ||
] | ||
|
||
__version__ = "1.0.0b1" | ||
EvanR-Dev marked this conversation as resolved.
Show resolved
Hide resolved
|
60 changes: 60 additions & 0 deletions
60
...ns-extensions-bindings-cosmos/azurefunctions/extensions/bindings/cosmos/containerProxy.py
This file contains hidden or 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,60 @@ | ||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# Licensed under the MIT License. | ||
|
||
import json | ||
|
||
from azure.identity import DefaultAzureCredential | ||
from azure.cosmos import CosmosClient as CosmosClientSdk, ContainerProxy as ContainerProxySdk | ||
from azurefunctions.extensions.base import Datum, SdkType | ||
from .utils import get_connection_string, using_managed_identity | ||
|
||
|
||
class ContainerProxy(SdkType): | ||
def __init__(self, *, data: Datum) -> None: | ||
# model_binding_data properties | ||
self._data = data | ||
self._version = None | ||
self._source = None | ||
self._content_type = None | ||
self._database_name = None | ||
self._container_name = None | ||
self._connection = None | ||
self._using_managed_identity = False | ||
self._partition_key = None | ||
self._sql_query = None | ||
EvanR-Dev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
self._preferred_locations = None | ||
if self._data: | ||
self._version = data.version | ||
self._source = data.source | ||
self._content_type = data.content_type | ||
content_json = json.loads(data.content) | ||
self._database_name = content_json.get("DatabaseName") | ||
self._container_name = content_json.get("ContainerName") | ||
self._connection = get_connection_string(content_json.get("Connection")) | ||
self._using_managed_identity = using_managed_identity( | ||
content_json.get("Connection") | ||
) | ||
self._preferred_locations = content_json.get("PreferredLocations") | ||
|
||
def get_sdk_type(self) -> ContainerProxySdk: | ||
""" | ||
When using Managed Identity, the only way to create a BlobClient is | ||
through a BlobServiceClient. There are two ways to create a | ||
BlobServiceClient: | ||
1. Through the constructor: this is the only option when using Managed Identity | ||
2. Through from_connection_string: this is the only option when not using Managed Identity | ||
|
||
We track if Managed Identity is being used through a flag. | ||
""" | ||
if self._data: | ||
cosmos_client = ( | ||
CosmosClientSdk( | ||
url=self._connection, credential=DefaultAzureCredential() | ||
gavin-aguiar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) | ||
if self._using_managed_identity | ||
else CosmosClientSdk.from_connection_string(self._connection) | ||
) | ||
db_client = cosmos_client.get_database_client(self._database_name) | ||
return db_client.get_container_client(self._container_name) | ||
else: | ||
return None |
60 changes: 60 additions & 0 deletions
60
...ions-extensions-bindings-cosmos/azurefunctions/extensions/bindings/cosmos/cosmosClient.py
This file contains hidden or 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,60 @@ | ||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# Licensed under the MIT License. | ||
|
||
import json | ||
|
||
from azure.identity import DefaultAzureCredential | ||
from azure.cosmos import CosmosClient as CosmosClientSdk | ||
from azurefunctions.extensions.base import Datum, SdkType | ||
from .utils import get_connection_string, using_managed_identity | ||
|
||
|
||
class CosmosClient(SdkType): | ||
def __init__(self, *, data: Datum) -> None: | ||
# model_binding_data properties | ||
self._data = data | ||
self._version = None | ||
self._source = None | ||
self._content_type = None | ||
self._database_name = None | ||
self._container_name = None | ||
self._connection = None | ||
self._using_managed_identity = False | ||
self._id = None | ||
self._partition_key = None | ||
self._sql_query = None | ||
self._preferred_locations = None | ||
if self._data: | ||
self._version = data.version | ||
self._source = data.source | ||
self._content_type = data.content_type | ||
content_json = json.loads(data.content) | ||
self._database_name = content_json.get("DatabaseName") | ||
self._container_name = content_json.get("ContainerName") | ||
self._connection = get_connection_string(content_json.get("Connection")) | ||
self._using_managed_identity = using_managed_identity( | ||
content_json.get("Connection") | ||
) | ||
self._preferred_locations = content_json.get("PreferredLocations") | ||
|
||
def get_sdk_type(self) -> CosmosClientSdk: | ||
""" | ||
When using Managed Identity, the only way to create a BlobClient is | ||
through a BlobServiceClient. There are two ways to create a | ||
BlobServiceClient: | ||
1. Through the constructor: this is the only option when using Managed Identity | ||
2. Through from_connection_string: this is the only option when not using Managed Identity | ||
|
||
We track if Managed Identity is being used through a flag. | ||
""" | ||
if self._data: | ||
cosmos_client = ( | ||
CosmosClientSdk( | ||
url=self._connection, credential=DefaultAzureCredential() | ||
) | ||
if self._using_managed_identity | ||
else CosmosClientSdk.from_connection_string(self._connection) | ||
) | ||
return cosmos_client | ||
else: | ||
return None | ||
gavin-aguiar marked this conversation as resolved.
Show resolved
Hide resolved
|
47 changes: 47 additions & 0 deletions
47
...nsions-bindings-cosmos/azurefunctions/extensions/bindings/cosmos/cosmosClientConverter.py
This file contains hidden or 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 @@ | ||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# Licensed under the MIT License. | ||
|
||
from typing import Any | ||
|
||
from azurefunctions.extensions.base import Datum, InConverter, OutConverter | ||
|
||
from .cosmosClient import CosmosClient | ||
from .databaseProxy import DatabaseProxy | ||
from .containerProxy import ContainerProxy | ||
|
||
|
||
class CosmosClientConverter( | ||
InConverter, | ||
OutConverter, | ||
binding="cosmosDB" | ||
): | ||
@classmethod | ||
def check_input_type_annotation(cls, pytype: type) -> bool: | ||
return issubclass( | ||
pytype, (CosmosClient, DatabaseProxy, ContainerProxy) | ||
) | ||
|
||
@classmethod | ||
def decode(cls, data: Datum, *, trigger_metadata, pytype) -> Any: | ||
if data is None or data.type is None: | ||
return None | ||
|
||
data_type = data.type | ||
|
||
if data_type == "model_binding_data": | ||
data = data.value | ||
else: | ||
raise ValueError( | ||
f'unexpected type of data received for the "Cosmos" binding ' | ||
f": {data_type!r}" | ||
) | ||
|
||
# Determines which sdk type to return based on pytype | ||
if pytype == CosmosClient: | ||
return CosmosClient(data=data).get_sdk_type() | ||
elif pytype == DatabaseProxy: | ||
return DatabaseProxy(data=data).get_sdk_type() | ||
elif pytype == ContainerProxy: | ||
return ContainerProxy(data=data).get_sdk_type() | ||
else: | ||
return None |
59 changes: 59 additions & 0 deletions
59
...ons-extensions-bindings-cosmos/azurefunctions/extensions/bindings/cosmos/databaseProxy.py
This file contains hidden or 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,59 @@ | ||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# Licensed under the MIT License. | ||
|
||
import json | ||
|
||
from azure.identity import DefaultAzureCredential | ||
from azure.cosmos import CosmosClient as CosmosClientSdk, DatabaseProxy as DatabaseProxySdk | ||
from azurefunctions.extensions.base import Datum, SdkType | ||
from .utils import get_connection_string, using_managed_identity | ||
|
||
|
||
class DatabaseProxy(SdkType): | ||
def __init__(self, *, data: Datum) -> None: | ||
# model_binding_data properties | ||
self._data = data | ||
self._version = None | ||
self._source = None | ||
self._content_type = None | ||
self._database_name = None | ||
self._container_name = None | ||
self._connection = None | ||
self._using_managed_identity = False | ||
self._partition_key = None | ||
self._sql_query = None | ||
self._preferred_locations = None | ||
if self._data: | ||
self._version = data.version | ||
self._source = data.source | ||
self._content_type = data.content_type | ||
content_json = json.loads(data.content) | ||
self._database_name = content_json.get("DatabaseName") | ||
self._container_name = content_json.get("ContainerName") | ||
self._connection = get_connection_string(content_json.get("Connection")) | ||
self._using_managed_identity = using_managed_identity( | ||
content_json.get("Connection") | ||
) | ||
self._preferred_locations = content_json.get("PreferredLocations") | ||
|
||
def get_sdk_type(self) -> DatabaseProxySdk: | ||
""" | ||
When using Managed Identity, the only way to create a BlobClient is | ||
through a BlobServiceClient. There are two ways to create a | ||
BlobServiceClient: | ||
1. Through the constructor: this is the only option when using Managed Identity | ||
2. Through from_connection_string: this is the only option when not using Managed Identity | ||
|
||
We track if Managed Identity is being used through a flag. | ||
""" | ||
if self._data: | ||
cosmos_client = ( | ||
CosmosClientSdk( | ||
url=self._connection, credential=DefaultAzureCredential() | ||
) | ||
if self._using_managed_identity | ||
else CosmosClientSdk.from_connection_string(self._connection) | ||
) | ||
return cosmos_client.get_database_client(self._database_name) | ||
else: | ||
return None |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.