Skip to content

Commit 7eea165

Browse files
Merge pull request #188 from microsoft/psl-bug-21554
fix: removed the container creation from code to avoid permission issue
2 parents 3042b8f + 18a103a commit 7eea165

2 files changed

Lines changed: 24 additions & 33 deletions

File tree

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)