Skip to content

Commit ae8debf

Browse files
author
Evan Roman
committed
Init
1 parent cb8a6e3 commit ae8debf

36 files changed

+1785
-0
lines changed

azurefunctions-extensions-bindings-cosmos/CHANGELOG.md

Whitespace-only changes.
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: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
# Azure Functions Extensions Bindings Cosmos library for Python
2+
This library allows Cosmos Input bindings in Python Function Apps to recognize and bind to client types from the
3+
Azure Cosmos sdk.
4+
5+
Cosmos client types can be generated from:
6+
7+
* Cosmos Input
8+
9+
[Source code](https://github.com/Azure/azure-functions-python-extensions/tree/main/azurefunctions-extensions-bindings-cosmos)
10+
[Package (PyPi)](https://pypi.org/project/azurefunctions-extensions-bindings-cosmos/)
11+
| API reference documentation
12+
| Product documentation
13+
| [Samples](hhttps://github.com/Azure/azure-functions-python-extensions/tree/main/azurefunctions-extensions-bindings-cosmos/samples)
14+
15+
16+
## Getting started
17+
18+
### Prerequisites
19+
* 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).
20+
21+
* You must have an [Azure subscription](https://azure.microsoft.com/free/) and an
22+
[Azure storage account](https://docs.microsoft.com/azure/storage/common/storage-account-overview) to use this package.
23+
24+
### Install the package
25+
Install the Azure Functions Extensions Bindings Cosmos library for Python with pip:
26+
27+
```bash
28+
pip install azurefunctions-extensions-bindings-cosmos
29+
```
30+
31+
### Create a storage account
32+
If you wish to create a new storage account, you can use the
33+
[Azure Portal](https://docs.microsoft.com/azure/storage/common/storage-quickstart-create-account?tabs=azure-portal),
34+
[Azure PowerShell](https://docs.microsoft.com/azure/storage/common/storage-quickstart-create-account?tabs=azure-powershell),
35+
or [Azure CLI](https://docs.microsoft.com/azure/storage/common/storage-quickstart-create-account?tabs=azure-cli):
36+
37+
```bash
38+
# Create a new resource group to hold the storage account -
39+
# if using an existing resource group, skip this step
40+
az group create --name my-resource-group --location westus2
41+
42+
# Create the storage account
43+
az storage account create -n my-storage-account-name -g my-resource-group
44+
```
45+
46+
### Bind to the SDK-type
47+
The Azure Functions Extensions Bindings Cosmos library for Python allows you to create a function app with
48+
Cosmos Input and define the type as a CosmosClient, DatabaseProxy, or ContainerProxy. Instead of receiving
49+
an InputStream, when the function is executed, the type returned will be the defined SDK-type and have all of the
50+
properties and methods available as seen in the Azure Storage Cosmos library for Python.
51+
52+
53+
```python
54+
import logging
55+
import azure.functions as func
56+
import azurefunctions.extensions.bindings.blob as blob
57+
58+
@app.blob_trigger(arg_name="client",
59+
path="PATH/TO/BLOB",
60+
connection="AzureWebJobsStorage")
61+
def blob_trigger(client: blob.BlobClient):
62+
logging.info(f"Python blob trigger function processed blob \n"
63+
f"Properties: {client.get_blob_properties()}\n"
64+
f"Blob content head: {client.download_blob(encoding="utf-8").read(size=1)}")
65+
66+
67+
@app.route(route="file")
68+
@app.blob_input(arg_name="client",
69+
path="PATH/TO/BLOB",
70+
connection="AzureWebJobsStorage")
71+
def blob_input(req: func.HttpRequest, client: blob.BlobClient):
72+
logging.info(f"Python blob input function processed blob \n"
73+
f"Properties: {client.get_blob_properties()}\n"
74+
f"Blob content head: {client.download_blob(encoding="utf-8").read(size=1)}")
75+
```
76+
77+
## Troubleshooting
78+
### General
79+
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).
80+
81+
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`.
82+
83+
## Next steps
84+
85+
### More sample code
86+
87+
Get started with our [Cosmos samples](hhttps://github.com/Azure/azure-functions-python-extensions/tree/main/azurefunctions-extensions-bindings-cosmos/samples).
88+
89+
Several samples are available in this GitHub repository. These samples provide example code for additional scenarios commonly encountered while working with Cosmos:
90+
91+
* [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:
92+
* From CosmosInput
93+
94+
* [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:
95+
* From CosmosInput
96+
97+
* [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:
98+
* From CosmosInput
99+
100+
### Additional documentation
101+
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
102+
and the [Azure Cosmos DB README](https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/cosmos/azure-cosmos).
103+
104+
## Contributing
105+
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.
106+
107+
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.
108+
109+
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.0b1"
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Copyright (c) Microsoft Corporation. All rights reserved.
2+
# Licensed under the MIT License.
3+
4+
import json
5+
6+
from azure.identity import DefaultAzureCredential
7+
from azure.cosmos import CosmosClient as CosmosClientSdk, ContainerProxy as ContainerProxySdk
8+
from azurefunctions.extensions.base import Datum, SdkType
9+
from .utils import get_connection_string, using_managed_identity
10+
11+
12+
class ContainerProxy(SdkType):
13+
def __init__(self, *, data: Datum) -> None:
14+
# model_binding_data properties
15+
self._data = data
16+
self._version = None
17+
self._source = None
18+
self._content_type = None
19+
self._database_name = None
20+
self._container_name = None
21+
self._connection = None
22+
self._using_managed_identity = False
23+
self._partition_key = None
24+
self._sql_query = None
25+
self._preferred_locations = None
26+
if self._data:
27+
self._version = data.version
28+
self._source = data.source
29+
self._content_type = data.content_type
30+
content_json = json.loads(data.content)
31+
self._database_name = content_json.get("DatabaseName")
32+
self._container_name = content_json.get("ContainerName")
33+
self._connection = get_connection_string(content_json.get("Connection"))
34+
self._using_managed_identity = using_managed_identity(
35+
content_json.get("Connection")
36+
)
37+
self._preferred_locations = content_json.get("PreferredLocations")
38+
39+
def get_sdk_type(self) -> ContainerProxySdk:
40+
"""
41+
When using Managed Identity, the only way to create a BlobClient is
42+
through a BlobServiceClient. There are two ways to create a
43+
BlobServiceClient:
44+
1. Through the constructor: this is the only option when using Managed Identity
45+
2. Through from_connection_string: this is the only option when not using Managed Identity
46+
47+
We track if Managed Identity is being used through a flag.
48+
"""
49+
if self._data:
50+
cosmos_client = (
51+
CosmosClientSdk(
52+
url=self._connection, credential=DefaultAzureCredential()
53+
)
54+
if self._using_managed_identity
55+
else CosmosClientSdk.from_connection_string(self._connection)
56+
)
57+
db_client = cosmos_client.get_database_client(self._database_name)
58+
return db_client.get_container_client(self._container_name)
59+
else:
60+
return None
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Copyright (c) Microsoft Corporation. All rights reserved.
2+
# Licensed under the MIT License.
3+
4+
import json
5+
6+
from azure.identity import DefaultAzureCredential
7+
from azure.cosmos import CosmosClient as CosmosClientSdk
8+
from azurefunctions.extensions.base import Datum, SdkType
9+
from .utils import get_connection_string, using_managed_identity
10+
11+
12+
class CosmosClient(SdkType):
13+
def __init__(self, *, data: Datum) -> None:
14+
# model_binding_data properties
15+
self._data = data
16+
self._version = None
17+
self._source = None
18+
self._content_type = None
19+
self._database_name = None
20+
self._container_name = None
21+
self._connection = None
22+
self._using_managed_identity = False
23+
self._id = None
24+
self._partition_key = None
25+
self._sql_query = None
26+
self._preferred_locations = None
27+
if self._data:
28+
self._version = data.version
29+
self._source = data.source
30+
self._content_type = data.content_type
31+
content_json = json.loads(data.content)
32+
self._database_name = content_json.get("DatabaseName")
33+
self._container_name = content_json.get("ContainerName")
34+
self._connection = get_connection_string(content_json.get("Connection"))
35+
self._using_managed_identity = using_managed_identity(
36+
content_json.get("Connection")
37+
)
38+
self._preferred_locations = content_json.get("PreferredLocations")
39+
40+
def get_sdk_type(self) -> CosmosClientSdk:
41+
"""
42+
When using Managed Identity, the only way to create a BlobClient is
43+
through a BlobServiceClient. There are two ways to create a
44+
BlobServiceClient:
45+
1. Through the constructor: this is the only option when using Managed Identity
46+
2. Through from_connection_string: this is the only option when not using Managed Identity
47+
48+
We track if Managed Identity is being used through a flag.
49+
"""
50+
if self._data:
51+
cosmos_client = (
52+
CosmosClientSdk(
53+
url=self._connection, credential=DefaultAzureCredential()
54+
)
55+
if self._using_managed_identity
56+
else CosmosClientSdk.from_connection_string(self._connection)
57+
)
58+
return cosmos_client
59+
else:
60+
return None
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Copyright (c) Microsoft Corporation. All rights reserved.
2+
# Licensed under the MIT License.
3+
4+
from typing import Any
5+
6+
from azurefunctions.extensions.base import Datum, InConverter, OutConverter
7+
8+
from .cosmosClient import CosmosClient
9+
from .databaseProxy import DatabaseProxy
10+
from .containerProxy import ContainerProxy
11+
12+
13+
class CosmosClientConverter(
14+
InConverter,
15+
OutConverter,
16+
binding="cosmosDB"
17+
):
18+
@classmethod
19+
def check_input_type_annotation(cls, pytype: type) -> bool:
20+
return issubclass(
21+
pytype, (CosmosClient, DatabaseProxy, ContainerProxy)
22+
)
23+
24+
@classmethod
25+
def decode(cls, data: Datum, *, trigger_metadata, pytype) -> Any:
26+
if data is None or data.type is None:
27+
return None
28+
29+
data_type = data.type
30+
31+
if data_type == "model_binding_data":
32+
data = data.value
33+
else:
34+
raise ValueError(
35+
f'unexpected type of data received for the "Cosmos" binding '
36+
f": {data_type!r}"
37+
)
38+
39+
# Determines which sdk type to return based on pytype
40+
if pytype == CosmosClient:
41+
return CosmosClient(data=data).get_sdk_type()
42+
elif pytype == DatabaseProxy:
43+
return DatabaseProxy(data=data).get_sdk_type()
44+
elif pytype == ContainerProxy:
45+
return ContainerProxy(data=data).get_sdk_type()
46+
else:
47+
return None
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Copyright (c) Microsoft Corporation. All rights reserved.
2+
# Licensed under the MIT License.
3+
4+
import json
5+
6+
from azure.identity import DefaultAzureCredential
7+
from azure.cosmos import CosmosClient as CosmosClientSdk, DatabaseProxy as DatabaseProxySdk
8+
from azurefunctions.extensions.base import Datum, SdkType
9+
from .utils import get_connection_string, using_managed_identity
10+
11+
12+
class DatabaseProxy(SdkType):
13+
def __init__(self, *, data: Datum) -> None:
14+
# model_binding_data properties
15+
self._data = data
16+
self._version = None
17+
self._source = None
18+
self._content_type = None
19+
self._database_name = None
20+
self._container_name = None
21+
self._connection = None
22+
self._using_managed_identity = False
23+
self._partition_key = None
24+
self._sql_query = None
25+
self._preferred_locations = None
26+
if self._data:
27+
self._version = data.version
28+
self._source = data.source
29+
self._content_type = data.content_type
30+
content_json = json.loads(data.content)
31+
self._database_name = content_json.get("DatabaseName")
32+
self._container_name = content_json.get("ContainerName")
33+
self._connection = get_connection_string(content_json.get("Connection"))
34+
self._using_managed_identity = using_managed_identity(
35+
content_json.get("Connection")
36+
)
37+
self._preferred_locations = content_json.get("PreferredLocations")
38+
39+
def get_sdk_type(self) -> DatabaseProxySdk:
40+
"""
41+
When using Managed Identity, the only way to create a BlobClient is
42+
through a BlobServiceClient. There are two ways to create a
43+
BlobServiceClient:
44+
1. Through the constructor: this is the only option when using Managed Identity
45+
2. Through from_connection_string: this is the only option when not using Managed Identity
46+
47+
We track if Managed Identity is being used through a flag.
48+
"""
49+
if self._data:
50+
cosmos_client = (
51+
CosmosClientSdk(
52+
url=self._connection, credential=DefaultAzureCredential()
53+
)
54+
if self._using_managed_identity
55+
else CosmosClientSdk.from_connection_string(self._connection)
56+
)
57+
return cosmos_client.get_database_client(self._database_name)
58+
else:
59+
return None

0 commit comments

Comments
 (0)