File size: 1,346 Bytes
254eff8 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
from azure.cosmos import CosmosClient, exceptions
import os
def diagnose_cosmos_connection():
endpoint = os.environ.get("COSMOS_ENDPOINT")
key = os.environ.get("COSMOS_KEY")
if not all([endpoint, key]):
raise ValueError("Please ensure COSMOS_ENDPOINT and COSMOS_KEY are set.")
client = CosmosClient(endpoint, key)
print("Attempting to connect to Cosmos DB...")
try:
# List databases
print("Listing databases:")
databases = list(client.list_databases())
for db in databases:
print(f"- {db['id']}")
# Try to list containers for each database
try:
containers = list(client.get_database_client(db['id']).list_containers())
print(f" Containers in {db['id']}:")
for container in containers:
print(f" - {container['id']}")
except exceptions.CosmosResourceNotFoundError:
print(f" Unable to list containers in {db['id']}")
except Exception as e:
print(f" Error listing containers in {db['id']}: {str(e)}")
print() # Add a blank line for readability
except Exception as e:
print(f"Error: {str(e)}")
if __name__ == "__main__":
diagnose_cosmos_connection() |