Skip to content

Commit 40ccff2

Browse files
Merge pull request #190 from microsoft/dev
fix: removed the container creation from code, added all the env variable in bicep for local setup
2 parents 3ebc0e6 + af1e563 commit 40ccff2

File tree

4 files changed

+52
-33
lines changed

4 files changed

+52
-33
lines changed

docs/DeploymentGuide.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,3 +228,8 @@ Additional Steps
228228
## Running the application
229229
230230
To help you get started, sample Informix queries have been included in the data/informix/functions and data/informix/simple directories. You can choose to upload these files to test the application.
231+
232+
## Environment configuration for local development & debugging
233+
> Set APP_ENV in your .env file to control Azure authentication. Use dev to enable to use Azure CLI credential, Prod to enable Managed Identity (for production). **Ensure you're logged in via az login when using dev in local**.
234+
235+
To configure your environment, navigate to the `src\backend` folder to create a `.env` file based on the `.env.sample`, fill it using deployment output or the Azure Portal under "Deployments" in your resource group, and ensure APP_ENV is set to "**dev**".

infra/main.bicep

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -647,3 +647,26 @@ module containerAppFrontend 'br/public:avm/res/app/container-app:0.17.0' = {
647647
@description('The resource group the resources were deployed into.')
648648
output resourceGroupName string = resourceGroup().name
649649
output WEB_APP_URL string = 'https://${containerAppFrontend.outputs.fqdn}'
650+
output COSMOSDB_ENDPOINT string = cosmosDb.outputs.endpoint
651+
output AZURE_BLOB_ACCOUNT_NAME string = storageAccount.outputs.name
652+
output AZURE_BLOB_ENDPOINT string = 'https://${storageAccount.outputs.name}.blob.core.windows.net/'
653+
output AZURE_OPENAI_ENDPOINT string = 'https://${aiServices.outputs.name}.openai.azure.com/'
654+
output AZURE_AI_AGENT_PROJECT_NAME string = aiServices.outputs.aiProjectInfo.name
655+
output AZURE_AI_AGENT_ENDPOINT string = aiServices.outputs.aiProjectInfo.apiEndpoint
656+
output AZURE_AI_AGENT_PROJECT_CONNECTION_STRING string = aiServices.outputs.aiProjectInfo.apiEndpoint
657+
output AZURE_AI_AGENT_RESOURCE_GROUP_NAME string = resourceGroup().name
658+
output AZURE_AI_AGENT_SUBSCRIPTION_ID string = subscription().subscriptionId
659+
output AI_PROJECT_ENDPOINT string = aiServices.outputs.aiProjectInfo.apiEndpoint
660+
output AZURE_CLIENT_ID string = appIdentity.outputs.clientId
661+
output AZURE_AI_AGENT_MODEL_DEPLOYMENT_NAME string = modelDeployment.name
662+
output AZURE_BLOB_CONTAINER_NAME string = appStorageContainerName
663+
output COSMOSDB_DATABASE string = cosmosDb.outputs.databaseName
664+
output COSMOSDB_BATCH_CONTAINER string = cosmosDb.outputs.containerNames.batch
665+
output COSMOSDB_FILE_CONTAINER string = cosmosDb.outputs.containerNames.file
666+
output COSMOSDB_LOG_CONTAINER string = cosmosDb.outputs.containerNames.log
667+
output APPLICATIONINSIGHTS_CONNECTION_STRING string = enableMonitoring ? applicationInsights.outputs.connectionString : ''
668+
output MIGRATOR_AGENT_MODEL_DEPLOY string = modelDeployment.name
669+
output PICKER_AGENT_MODEL_DEPLOY string = modelDeployment.name
670+
output FIXER_AGENT_MODEL_DEPLOY string = modelDeployment.name
671+
output SEMANTIC_VERIFIER_AGENT_MODEL_DEPLOY string = modelDeployment.name
672+
output SYNTAX_CHECKER_AGENT_MODEL_DEPLOY string = modelDeployment.name

src/backend/common/database/cosmosdb.py

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
from typing import Dict, List, Optional
33
from uuid import UUID, uuid4
44

5-
from azure.cosmos import PartitionKey, exceptions
65
from azure.cosmos.aio import CosmosClient
76
from azure.cosmos.aio._database import DatabaseProxy
87
from azure.cosmos.exceptions import (
@@ -49,32 +48,27 @@ async def initialize_cosmos(self):
4948
try:
5049
self.client = CosmosClient(url=self.endpoint, credential=self.credential)
5150
database = self.client.get_database_client(self.database_name)
52-
53-
self.batch_container = await self._get_or_create_container(
54-
database, self.batch_container_name, "/batch_id"
51+
self.batch_container = await self._get_container(
52+
database, self.batch_container_name
5553
)
56-
self.file_container = await self._get_or_create_container(
57-
database, self.file_container_name, "/file_id"
54+
self.file_container = await self._get_container(
55+
database, self.file_container_name
5856
)
59-
self.log_container = await self._get_or_create_container(
60-
database, self.log_container_name, "/log_id"
57+
self.log_container = await self._get_container(
58+
database, self.log_container_name
6159
)
6260
except Exception as e:
6361
self.logger.error("Failed to initialize Cosmos DB", error=str(e))
6462
raise
6563

66-
async def _get_or_create_container(
67-
self, database: DatabaseProxy, container_name, partition_key
64+
async def _get_container(
65+
self, database: DatabaseProxy, container_name
6866
):
6967
try:
70-
return await database.create_container(
71-
id=container_name, partition_key=PartitionKey(path=partition_key)
72-
)
73-
except exceptions.CosmosResourceExistsError:
7468
return database.get_container_client(container_name)
7569

7670
except Exception as e:
77-
self.logger.error("Failed to Get/Create cosmosdb container", error=str(e))
71+
self.logger.error("Failed to Get cosmosdb container", error=str(e))
7872
raise
7973

8074
async def create_batch(self, user_id: str, batch_id: UUID) -> BatchRecord:

src/tests/backend/common/database/cosmosdb_test.py

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ async def test_initialize_cosmos(cosmos_db_client, mocker):
5959
mock_file_container = mock.MagicMock()
6060
mock_log_container = mock.MagicMock()
6161

62-
# Use AsyncMock to mock asynchronous container creation
63-
mock_database.create_container = AsyncMock(side_effect=[
62+
# Mock get_container_client method (since _get_container uses this)
63+
mock_database.get_container_client = mock.MagicMock(side_effect=[
6464
mock_batch_container,
6565
mock_file_container,
6666
mock_log_container
@@ -69,10 +69,10 @@ async def test_initialize_cosmos(cosmos_db_client, mocker):
6969
# Call the initialize_cosmos method
7070
await cosmos_db_client.initialize_cosmos()
7171

72-
# Assert that the containers were created or fetched successfully
73-
mock_database.create_container.assert_any_call(id=batch_container, partition_key=mock.ANY)
74-
mock_database.create_container.assert_any_call(id=file_container, partition_key=mock.ANY)
75-
mock_database.create_container.assert_any_call(id=log_container, partition_key=mock.ANY)
72+
# Assert that the containers were fetched successfully
73+
mock_database.get_container_client.assert_any_call(batch_container)
74+
mock_database.get_container_client.assert_any_call(file_container)
75+
mock_database.get_container_client.assert_any_call(log_container)
7676

7777
# Check the client and containers were set
7878
assert cosmos_db_client.client is not None
@@ -87,15 +87,15 @@ async def test_initialize_cosmos_with_error(cosmos_db_client, mocker):
8787
mock_client = mocker.patch.object(CosmosClient, 'get_database_client', return_value=mock.MagicMock())
8888
mock_database = mock_client.return_value
8989

90-
# Simulate a general exception during container creation
91-
mock_database.create_container = AsyncMock(side_effect=Exception("Failed to create container"))
90+
# Simulate a general exception during container access
91+
mock_database.get_container_client = mock.MagicMock(side_effect=Exception("Failed to get container"))
9292

9393
# Call the initialize_cosmos method and expect it to raise an error
9494
with pytest.raises(Exception) as exc_info:
9595
await cosmos_db_client.initialize_cosmos()
9696

9797
# Assert that the exception message matches the expected message
98-
assert str(exc_info.value) == "Failed to create container"
98+
assert str(exc_info.value) == "Failed to get container"
9999

100100

101101
@pytest.mark.asyncio
@@ -104,16 +104,13 @@ async def test_initialize_cosmos_container_exists_error(cosmos_db_client, mocker
104104
mock_client = mocker.patch.object(CosmosClient, 'get_database_client', return_value=mock.MagicMock())
105105
mock_database = mock_client.return_value
106106

107-
# Simulating CosmosResourceExistsError for container creation
108-
mock_database.create_container = AsyncMock(side_effect=CosmosResourceExistsError)
109-
110107
# Use AsyncMock for asynchronous methods
111108
mock_batch_container = mock.MagicMock()
112109
mock_file_container = mock.MagicMock()
113110
mock_log_container = mock.MagicMock()
114111

115-
# Use AsyncMock to mock asynchronous container creation
116-
mock_database.create_container = AsyncMock(side_effect=[
112+
# Mock get_container_client method to return existing containers
113+
mock_database.get_container_client = mock.MagicMock(side_effect=[
117114
mock_batch_container,
118115
mock_file_container,
119116
mock_log_container
@@ -122,10 +119,10 @@ async def test_initialize_cosmos_container_exists_error(cosmos_db_client, mocker
122119
# Call the initialize_cosmos method
123120
await cosmos_db_client.initialize_cosmos()
124121

125-
# Assert that the container creation method was called with the correct arguments
126-
mock_database.create_container.assert_any_call(id='batch_container', partition_key=mock.ANY)
127-
mock_database.create_container.assert_any_call(id='file_container', partition_key=mock.ANY)
128-
mock_database.create_container.assert_any_call(id='log_container', partition_key=mock.ANY)
122+
# Assert that the container access method was called with the correct arguments
123+
mock_database.get_container_client.assert_any_call('batch_container')
124+
mock_database.get_container_client.assert_any_call('file_container')
125+
mock_database.get_container_client.assert_any_call('log_container')
129126

130127
# Check that existing containers are returned (mocked containers)
131128
assert cosmos_db_client.batch_container == mock_batch_container

0 commit comments

Comments
 (0)