azure-sdk-for-python
azure-sdk-for-python copied to clipboard
Using Azure Python CORE library to list CosmosDB for MongoDB Connection String from Azure Function App via a managed identity
Hi,
I would like to list and retrieve connection string on CosmosDB for MongoDB via Azure Function App (Python) and its Managed Identity. I am adapting some part of the code from this article: https://learn.microsoft.com/en-us/azure/service-connector/how-to-integrate-cosmos-db?tabs=python Here is my code:
import os
import pymongo
import requests
from azure.core.pipeline.policies import BearerTokenCredentialPolicy, RetryPolicy
from azure.identity import ManagedIdentityCredential
from azure.core.pipeline import Pipeline
from azure.core.transport import RequestsTransport
from azure.core.rest import HttpRequest
from azure.core.serialization import deserialize_data
endpoint = os.getenv('AZURE_COSMOS_RESOURCEENDPOINT')
listConnectionStringUrl = os.getenv('AZURE_COSMOS_LISTCONNECTIONSTRINGURL')
scope = os.getenv('AZURE_COSMOS_SCOPE')
cred = ManagedIdentityCredential()
bearer_policy = BearerTokenCredentialPolicy(cred, scope)
retry_policy = RetryPolicy(retry_total=5,timeout=60)
transport = RequestsTransport()
pipeline = Pipeline(transport, [bearer_policy, retry_policy])
request = HttpRequest(
"POST", listConnectionStringUrl,
headers={'Content-type':'application/json'}
)
response = pipeline.run(request) #returns pipelineResponse class
keys_dict= deserialize_data(response.http_response)
conn_str = keys_dict["connectionStrings"][0]["connectionString"]
client = pymongo.MongoClient(conn_str)
However, seems deserialize_data function is not in azure.core.serialization module no more.
So how do I consume PipelineResponse object? From which module, do I import the deserialize_data?
Also the environment variables are like these:
AZURE_COSMOS_LISTCONNECTIONSTRINGURL = https://management.azure.com/subscriptions/<subscription-ID>/resourceGroups/<resource-group-name>/providers/Microsoft.DocumentDB/databaseAccounts/<Azure-Cosmos-DB-API-for-MongoDB-account>/listConnectionStrings?api-version=2021-04-15
AZURE_COSMOS_SCOPE=https://management.azure.com/.default
AZURE_COSMOS_RESOURCEENDPOINT = https://<Azure-Cosmos-DB-API-for-MongoDB-account>.mongo.cosmos.azure.com:443/
Thank you.
Thanks for the feedback! We are routing this to the appropriate team for follow-up. cc @AbhinavTrips @bambriz @pjohari-ms @simorenoh.
@xiangyan99 I believe the question here has to do with the deserialization of response data from the azure.core package - the azure.cosmos package is not used in the shared code. I looked through the core changelog to see if there was a mention of this being removed but couldn't find anything - I'd think asking someone from the core team about this would be useful to move forward.
@simorenoh, as you said, there is no deserialize_data method exposed from azure.core.
IOW, the code snippet fails by design.
Please provide some guidance/sample for the user's question:
list and retrieve connection string on CosmosDB for MongoDB via Azure Function App (Python) and its Managed Identity.
Or it will be even better if you can update your documentation.
And that was the reason I assigned the issue to your team.
Thanks for the feedback! We are routing this to the appropriate team for follow-up. cc @AbhinavTrips @bambriz @pjohari-ms @simorenoh.
Hi @LumineLittlelight, I am not entirely sure where you got the code you're using from, but in the public documentation you shared it seems as though all you would need is this? https://learn.microsoft.com/en-us/azure/service-connector/how-to-integrate-cosmos-db?tabs=python#sample-code-1
you should not be needing to deserialize/serialize things on your own if you're using SDKs - and I don't believe this code was ever public either. Please take a look at the link you shared again and the snippet I shared above. I also don't see how you are trying to use Azure Functions in the code you shared.
Hi @LumineLittlelight. Thank you for opening this issue and giving us the opportunity to assist. To help our team better understand your issue and the details of your scenario please provide a response to the question asked above or the information requested above. This will help us more accurately address your issue.
Hi @LumineLittlelight, we're sending this friendly reminder because we haven't heard back from you in 7 days. We need more information about this issue to help address it. Please be sure to give us your input. If we don't hear back from you within 14 days of this comment the issue will be automatically closed. Thank you!