1+ """Azure cloud resource management for SeBS.
2+
3+ This module manages deployed special resources in Azure cloud, particularly
4+ CosmosDB accounts that require special handling for authentication and
5+ configuration management.
6+ """
7+
18import json
2- from typing import Optional
9+ from typing import Dict , Optional
310
411from sebs .azure .cli import AzureCLI
512
6- """
7- Keep a list of deployed special resources in Azure cloud.
813
9- Currently, we have here CosmosDB accounts that require special handling.
10- """
14+ class CosmosDBAccount :
15+ """Azure CosmosDB account configuration and management.
1116
17+ Manages CosmosDB account information including account name, endpoint URL,
18+ and authentication credentials. Provides methods for querying account
19+ details from Azure and serialization for caching.
20+
21+ Attributes:
22+ _account_name (str): Name of the CosmosDB account
23+ _url (str): Document endpoint URL for the account
24+ _credential (str): Primary master key for authentication
25+ """
1226
13- class CosmosDBAccount :
1427 @property
1528 def account_name (self ) -> str :
29+ """Get the CosmosDB account name.
30+
31+ Returns:
32+ str: The name of the CosmosDB account.
33+ """
1634 return self ._account_name
1735
1836 @property
1937 def url (self ) -> str :
38+ """Get the CosmosDB document endpoint URL.
39+
40+ Returns:
41+ str: The document endpoint URL for the CosmosDB account.
42+ """
2043 return self ._url
2144
2245 @property
2346 def credential (self ) -> str :
47+ """Get the CosmosDB authentication credential.
48+
49+ Returns:
50+ str: The primary master key for CosmosDB authentication.
51+ """
2452 return self ._credential
2553
26- def __init__ (self , account_name : str , url : str , credential : str ):
54+ def __init__ (self , account_name : str , url : str , credential : str ) -> None :
55+ """Initialize CosmosDB account configuration.
56+
57+ Args:
58+ account_name (str): Name of the CosmosDB account
59+ url (str): Document endpoint URL for the account
60+ credential (str): Primary master key for authentication
61+ """
2762 super ().__init__ ()
2863 self ._account_name = account_name
2964 self ._url = url
3065 self ._credential = credential
3166
3267 @staticmethod
3368 def from_cache (account_name : str , url : str , credential : str ) -> "CosmosDBAccount" :
69+ """Create CosmosDB account instance from cached data.
70+
71+ Args:
72+ account_name (str): Name of the CosmosDB account
73+ url (str): Document endpoint URL for the account
74+ credential (str): Primary master key for authentication
75+
76+ Returns:
77+ CosmosDBAccount: New instance with provided configuration.
78+ """
3479 return CosmosDBAccount (account_name , url , credential )
3580
3681 @staticmethod
3782 def from_allocation (
38- account_name : str , resource_group : str , cli_instance : AzureCLI , url : Optional [str ]
83+ account_name : str , resource_group : str , cli_instance : AzureCLI , url : Optional [str ] = None
3984 ) -> "CosmosDBAccount" :
85+ """Create CosmosDB account instance by querying Azure.
86+
87+ Queries Azure CLI to retrieve account configuration including
88+ endpoint URL and authentication credentials.
4089
90+ Args:
91+ account_name (str): Name of the CosmosDB account
92+ resource_group (str): Azure resource group containing the account
93+ cli_instance (AzureCLI): Azure CLI instance for executing commands
94+ url (Optional[str]): Pre-known URL, if None will query from Azure
95+
96+ Returns:
97+ CosmosDBAccount: New instance with queried configuration.
98+ """
4199 if url is None :
42100 url = CosmosDBAccount .query_url (
43101 account_name ,
@@ -55,7 +113,23 @@ def from_allocation(
55113
56114 @staticmethod
57115 def query_url (account_name : str , resource_group : str , cli_instance : AzureCLI ) -> str :
116+ """Query CosmosDB account endpoint URL from Azure.
117+
118+ Uses Azure CLI to retrieve the document endpoint URL for the
119+ specified CosmosDB account.
58120
121+ Args:
122+ account_name (str): Name of the CosmosDB account
123+ resource_group (str): Azure resource group containing the account
124+ cli_instance (AzureCLI): Azure CLI instance for executing commands
125+
126+ Returns:
127+ str: The document endpoint URL for the CosmosDB account.
128+
129+ Raises:
130+ RuntimeError: If Azure CLI command fails.
131+ KeyError: If the expected response structure is not found.
132+ """
59133 # Find the endpoint URL
60134 ret = cli_instance .execute (
61135 f" az cosmosdb show --name { account_name } " f" --resource-group { resource_group } "
@@ -65,7 +139,23 @@ def query_url(account_name: str, resource_group: str, cli_instance: AzureCLI) ->
65139
66140 @staticmethod
67141 def query_credentials (account_name : str , resource_group : str , cli_instance : AzureCLI ) -> str :
142+ """Query CosmosDB account authentication credentials from Azure.
143+
144+ Uses Azure CLI to retrieve the primary master key for the
145+ specified CosmosDB account.
68146
147+ Args:
148+ account_name (str): Name of the CosmosDB account
149+ resource_group (str): Azure resource group containing the account
150+ cli_instance (AzureCLI): Azure CLI instance for executing commands
151+
152+ Returns:
153+ str: The primary master key for CosmosDB authentication.
154+
155+ Raises:
156+ RuntimeError: If Azure CLI command fails.
157+ KeyError: If the expected response structure is not found.
158+ """
69159 # Read the master key to access CosmosDB account
70160 ret = cli_instance .execute (
71161 f" az cosmosdb keys list --name { account_name } " f" --resource-group { resource_group } "
@@ -75,13 +165,33 @@ def query_credentials(account_name: str, resource_group: str, cli_instance: Azur
75165
76166 return credential
77167
78- def serialize (self ) -> dict :
168+ def serialize (self ) -> Dict [str , str ]:
169+ """Serialize CosmosDB account configuration to dictionary.
170+
171+ Returns:
172+ Dict[str, str]: Dictionary containing account configuration with keys:
173+ - account_name: The CosmosDB account name
174+ - url: The document endpoint URL
175+ - credential: The primary master key
176+ """
79177 return {
80178 "account_name" : self ._account_name ,
81179 "url" : self ._url ,
82180 "credential" : self ._credential ,
83181 }
84182
85183 @staticmethod
86- def deserialize (obj : dict ) -> "CosmosDBAccount" :
184+ def deserialize (obj : Dict [str , str ]) -> "CosmosDBAccount" :
185+ """Deserialize CosmosDB account configuration from dictionary.
186+
187+ Args:
188+ obj (Dict[str, str]): Dictionary containing account configuration
189+ with required keys: account_name, url, credential
190+
191+ Returns:
192+ CosmosDBAccount: New instance with deserialized configuration.
193+
194+ Raises:
195+ KeyError: If required keys are missing from the dictionary.
196+ """
87197 return CosmosDBAccount .from_cache (obj ["account_name" ], obj ["url" ], obj ["credential" ])
0 commit comments