diff --git a/libraries/microsoft-agents-storage-cosmos/microsoft_agents/storage/cosmos/cosmos_db_storage.py b/libraries/microsoft-agents-storage-cosmos/microsoft_agents/storage/cosmos/cosmos_db_storage.py index 96df0352..ed56dd9f 100644 --- a/libraries/microsoft-agents-storage-cosmos/microsoft_agents/storage/cosmos/cosmos_db_storage.py +++ b/libraries/microsoft-agents-storage-cosmos/microsoft_agents/storage/cosmos/cosmos_db_storage.py @@ -53,33 +53,41 @@ def __init__(self, config: CosmosDBStorageConfig): self._lock: asyncio.Lock = asyncio.Lock() def _create_client(self) -> CosmosClient: - if self._config.url: - if not self._config.credential: - raise ValueError( - storage_errors.InvalidConfiguration.format( - "Credential is required when using a custom service URL" - ) + + if not self._config.cosmos_db_endpoint: + raise ValueError( + storage_errors.InvalidConfiguration.format( + "Cosmos DB Endpoint is required." ) - return CosmosClient( - url=self._config.url, credential=self._config.credential ) - connection_policy = self._config.cosmos_client_options.get( - "connection_policy", documents.ConnectionPolicy() - ) + if self._config.credential or self._config.auth_key: + cred = ( + self._config.credential + if self._config.credential + else self._config.auth_key + ) - # kwargs 'connection_verify' is to handle CosmosClient overwriting the - # ConnectionPolicy.DisableSSLVerification value. - return CosmosClient( - self._config.cosmos_db_endpoint, - self._config.auth_key, - consistency_level=self._config.cosmos_client_options.get( - "consistency_level", None - ), - **{ - "connection_policy": connection_policy, - "connection_verify": not connection_policy.DisableSSLVerification, - }, + connection_policy = self._config.cosmos_client_options.get( + "connection_policy", documents.ConnectionPolicy() + ) + + return CosmosClient( + url=self._config.cosmos_db_endpoint, + credential=cred, + consistency_level=self._config.cosmos_client_options.get( + "consistency_level", None + ), + **{ + "connection_policy": connection_policy, + "connection_verify": not connection_policy.DisableSSLVerification, + }, + ) + + raise ValueError( + storage_errors.InvalidConfiguration.format( + "Either Cosmos DB Credential or Auth Key is required." + ) ) def _sanitize(self, key: str) -> str: diff --git a/libraries/microsoft-agents-storage-cosmos/microsoft_agents/storage/cosmos/cosmos_db_storage_config.py b/libraries/microsoft-agents-storage-cosmos/microsoft_agents/storage/cosmos/cosmos_db_storage_config.py index 4e0e15ac..70c68d1e 100644 --- a/libraries/microsoft-agents-storage-cosmos/microsoft_agents/storage/cosmos/cosmos_db_storage_config.py +++ b/libraries/microsoft-agents-storage-cosmos/microsoft_agents/storage/cosmos/cosmos_db_storage_config.py @@ -20,7 +20,6 @@ def __init__( container_throughput: int | None = None, key_suffix: str = "", compatibility_mode: bool = False, - url: str = "", credential: Union[AsyncTokenCredential, None] = None, **kwargs, ): @@ -37,7 +36,6 @@ def __init__( key characters. (e.g. not: '\\', '?', '/', '#', '*') :param compatibility_mode: True if keys should be truncated in order to support previous CosmosDb max key length of 255. - :param url: The URL to the CosmosDB resource. :param credential: The TokenCredential to use for authentication. :return CosmosDBConfig: """ @@ -61,7 +59,6 @@ def __init__( self.compatibility_mode: bool = compatibility_mode or kwargs.get( "compatibility_mode", False ) - self.url = url or kwargs.get("url", "") self.credential: Union[AsyncTokenCredential, None] = credential @staticmethod diff --git a/tests/storage_cosmos/test_cosmos_db_storage.py b/tests/storage_cosmos/test_cosmos_db_storage.py index d71da51b..95117885 100644 --- a/tests/storage_cosmos/test_cosmos_db_storage.py +++ b/tests/storage_cosmos/test_cosmos_db_storage.py @@ -253,9 +253,9 @@ async def test_cosmos_db_from_azure_cred(self): load_dotenv() cred = DefaultAzureCredential() - url = os.environ.get("TEST_COSMOS_DB_ENDPOINT") + url = os.environ.get("TEST_COSMOS_DB_ENDPOINT", "") config = CosmosDBStorageConfig( - url=url, + cosmos_db_endpoint=url, credential=cred, database_id="test-db", container_id="bot-storage",