@@ -117,6 +117,73 @@ def get(self, key: str, label: str = '', secure: bool = False) -> str | None:
117117# PRIVATE METHODS
118118# ------------------------------
119119
120+ def _cleanup_resources (deployment_name : str , rg_name : str ) -> None :
121+ """
122+ Clean up resources associated with a deployment in a resource group.
123+ Deletes and purges Cognitive Services, API Management, and Key Vault resources, then deletes the resource group itself.
124+
125+ Args:
126+ deployment_name (str): The deployment name (string).
127+ rg_name (str): The resource group name.
128+
129+ Returns:
130+ None
131+
132+ Raises:
133+ Exception: If an error occurs during cleanup.
134+ """
135+ if not deployment_name :
136+ print_error ("Missing deployment name parameter." )
137+ return
138+
139+ if not rg_name :
140+ print_error ("Missing resource group name parameter." )
141+ return
142+
143+ try :
144+ print_info (f"🧹 Cleaning up resource group '{ rg_name } '..." )
145+
146+ # Show the deployment details
147+ output = run (f"az deployment group show --name { deployment_name } -g { rg_name } -o json" , "Deployment retrieved" , "Failed to retrieve the deployment" )
148+
149+ if output .success and output .json_data :
150+ provisioning_state = output .json_data .get ("properties" ).get ("provisioningState" )
151+ print_info (f"Deployment provisioning state: { provisioning_state } " )
152+
153+ # Delete and purge CognitiveService accounts
154+ output = run (f" az cognitiveservices account list -g { rg_name } " , f"Listed CognitiveService accounts" , f"Failed to list CognitiveService accounts" )
155+ if output .success and output .json_data :
156+ for resource in output .json_data :
157+ print_info (f"Deleting and purging Cognitive Service Account '{ resource ['name' ]} ' in resource group '{ rg_name } '..." )
158+ output = run (f"az cognitiveservices account delete -g { rg_name } -n { resource ['name' ]} " , f"Cognitive Services '{ resource ['name' ]} ' deleted" , f"Failed to delete Cognitive Services '{ resource ['name' ]} '" )
159+ output = run (f"az cognitiveservices account purge -g { rg_name } -n { resource ['name' ]} -l \" { resource ['location' ]} \" " , f"Cognitive Services '{ resource ['name' ]} ' purged" , f"Failed to purge Cognitive Services '{ resource ['name' ]} '" )
160+
161+ # Delete and purge APIM resources
162+ output = run (f" az apim list -g { rg_name } " , f"Listed APIM resources" , f"Failed to list APIM resources" )
163+ if output .success and output .json_data :
164+ for resource in output .json_data :
165+ print_info (f"Deleting and purging API Management '{ resource ['name' ]} ' in resource group '{ rg_name } '..." )
166+ output = run (f"az apim delete -n { resource ['name' ]} -g { rg_name } -y" , f"API Management '{ resource ['name' ]} ' deleted" , f"Failed to delete API Management '{ resource ['name' ]} '" )
167+ output = run (f"az apim deletedservice purge --service-name { resource ['name' ]} --location \" { resource ['location' ]} \" " , f"API Management '{ resource ['name' ]} ' purged" , f"Failed to purge API Management '{ resource ['name' ]} '" )
168+
169+ # Delete and purge Key Vault resources
170+ output = run (f" az keyvault list -g { rg_name } " , f"Listed Key Vault resources" , f"Failed to list Key Vault resources" )
171+ if output .success and output .json_data :
172+ for resource in output .json_data :
173+ print_info (f"Deleting and purging Key Vault '{ resource ['name' ]} ' in resource group '{ rg_name } '..." )
174+ output = run (f"az keyvault delete -n { resource ['name' ]} -g { rg_name } " , f"Key Vault '{ resource ['name' ]} ' deleted" , f"Failed to delete Key Vault '{ resource ['name' ]} '" )
175+ output = run (f"az keyvault purge -n { resource ['name' ]} --location \" { resource ['location' ]} \" " , f"Key Vault '{ resource ['name' ]} ' purged" , f"Failed to purge Key Vault '{ resource ['name' ]} '" )
176+
177+ # Delete the resource group last
178+ print_message (f"🧹 Deleting resource group '{ rg_name } '..." )
179+ output = run (f"az group delete --name { rg_name } -y" , f"Resource group '{ rg_name } ' deleted" , f"Failed to delete resource group '{ rg_name } '" )
180+
181+ print_message ("🧹 Cleanup completed." )
182+
183+ except Exception as e :
184+ print (f"An error occurred during cleanup: { e } " )
185+ traceback .print_exc ()
186+
120187def _print_log (message : str , prefix : str = '' , color : str = '' , output : str = '' , duration : str = '' , show_time : bool = False , blank_above : bool = False , blank_below : bool = False ) -> None :
121188 """
122189 Print a formatted log message with optional prefix, color, output, duration, and time.
@@ -163,12 +230,6 @@ def _print_log(message: str, prefix: str = '', color: str = '', output: str = ''
163230print_warning = lambda msg , output = '' , duration = '' : _print_log (msg , '⚠️ ' , BOLD_Y , output , duration , True )
164231print_val = lambda name , value , val_below = False : _print_log (f"{ name :<25} :{ '\n ' if val_below else ' ' } { value } " , '👉🏽 ' , BOLD_B )
165232
166- # Validation functions will raise ValueError if the value is not valid
167-
168- validate_http_verb = lambda val : HTTP_VERB (val )
169- validate_infrastructure = lambda val : INFRASTRUCTURE (val )
170- validate_sku = lambda val : APIM_SKU (val )
171-
172233def create_bicep_deployment_group (rg_name : str , rg_location : str , deployment : str | INFRASTRUCTURE , bicep_parameters : dict , bicep_parameters_file : str = 'params.json' ) -> Output :
173234 """
174235 Create a Bicep deployment in a resource group, writing parameters to a file and running the deployment.
@@ -279,78 +340,6 @@ def read_policy_xml(policy_xml_filepath: str) -> str:
279340
280341 return policy_template_xml
281342
282- def _cleanup_resources (deployment_name : str , rg_name : str ) -> None :
283- """
284- Clean up resources associated with a deployment in a resource group.
285- Deletes and purges Cognitive Services, API Management, and Key Vault resources, then deletes the resource group itself.
286-
287- Args:
288- deployment_name (str): The deployment name (string).
289- rg_name (str): The resource group name.
290-
291- Returns:
292- None
293-
294- Raises:
295- Exception: If an error occurs during cleanup.
296- """
297- if not deployment_name :
298- print_error ("Missing deployment name parameter." )
299- return
300-
301- if not rg_name :
302- print_error ("Missing resource group name parameter." )
303- return
304-
305- try :
306- print_info (f"🧹 Cleaning up resource group '{ rg_name } '..." )
307-
308- # Show the deployment details
309- output = run (f"az deployment group show --name { deployment_name } -g { rg_name } -o json" , "Deployment retrieved" , "Failed to retrieve the deployment" )
310-
311- if output .success and output .json_data :
312- provisioning_state = output .json_data .get ("properties" ).get ("provisioningState" )
313- print_info (f"Deployment provisioning state: { provisioning_state } " )
314-
315- # Delete and purge CognitiveService accounts
316- output = run (f" az cognitiveservices account list -g { rg_name } " , f"Listed CognitiveService accounts" , f"Failed to list CognitiveService accounts" )
317- if output .success and output .json_data :
318- for resource in output .json_data :
319- print_info (f"Deleting and purging Cognitive Service Account '{ resource ['name' ]} ' in resource group '{ rg_name } '..." )
320- output = run (f"az cognitiveservices account delete -g { rg_name } -n { resource ['name' ]} " , f"Cognitive Services '{ resource ['name' ]} ' deleted" , f"Failed to delete Cognitive Services '{ resource ['name' ]} '" )
321- output = run (f"az cognitiveservices account purge -g { rg_name } -n { resource ['name' ]} -l \" { resource ['location' ]} \" " , f"Cognitive Services '{ resource ['name' ]} ' purged" , f"Failed to purge Cognitive Services '{ resource ['name' ]} '" )
322-
323- # Delete and purge APIM resources
324- output = run (f" az apim list -g { rg_name } " , f"Listed APIM resources" , f"Failed to list APIM resources" )
325- if output .success and output .json_data :
326- for resource in output .json_data :
327- print_info (f"Deleting and purging API Management '{ resource ['name' ]} ' in resource group '{ rg_name } '..." )
328- output = run (f"az apim delete -n { resource ['name' ]} -g { rg_name } -y" , f"API Management '{ resource ['name' ]} ' deleted" , f"Failed to delete API Management '{ resource ['name' ]} '" )
329- output = run (f"az apim deletedservice purge --service-name { resource ['name' ]} --location \" { resource ['location' ]} \" " , f"API Management '{ resource ['name' ]} ' purged" , f"Failed to purge API Management '{ resource ['name' ]} '" )
330-
331- # Delete and purge Key Vault resources
332- output = run (f" az keyvault list -g { rg_name } " , f"Listed Key Vault resources" , f"Failed to list Key Vault resources" )
333- if output .success and output .json_data :
334- for resource in output .json_data :
335- print_info (f"Deleting and purging Key Vault '{ resource ['name' ]} ' in resource group '{ rg_name } '..." )
336- output = run (f"az keyvault delete -n { resource ['name' ]} -g { rg_name } " , f"Key Vault '{ resource ['name' ]} ' deleted" , f"Failed to delete Key Vault '{ resource ['name' ]} '" )
337- output = run (f"az keyvault purge -n { resource ['name' ]} --location \" { resource ['location' ]} \" " , f"Key Vault '{ resource ['name' ]} ' purged" , f"Failed to purge Key Vault '{ resource ['name' ]} '" )
338-
339- # Delete the resource group last
340- print_message (f"🧹 Deleting resource group '{ rg_name } '..." )
341- output = run (f"az group delete --name { rg_name } -y" , f"Resource group '{ rg_name } ' deleted" , f"Failed to delete resource group '{ rg_name } '" )
342-
343- print_message ("🧹 Cleanup completed." )
344-
345- except Exception as e :
346- print (f"An error occurred during cleanup: { e } " )
347- traceback .print_exc ()
348-
349-
350- # ------------------------------
351- # PUBLIC METHODS
352- # ------------------------------
353-
354343def cleanup_infra_deployments (deployment : INFRASTRUCTURE , indexes : int | list [int ] | None = None ) -> None :
355344 """
356345 Clean up infrastructure deployments by deployment enum and index/indexes.
@@ -360,7 +349,6 @@ def cleanup_infra_deployments(deployment: INFRASTRUCTURE, indexes: int | list[in
360349 deployment (INFRASTRUCTURE): The infrastructure deployment enum value.
361350 indexes (int | list[int] | None): A single index, a list of indexes, or None for no index.
362351 """
363- validate_infrastructure (deployment )
364352
365353 if indexes is None :
366354 indexes_list = [None ]
@@ -545,8 +533,6 @@ def get_infra_rg_name(deployment_name: INFRASTRUCTURE, index: int | None = None)
545533 str: The generated resource group name.
546534 """
547535
548- validate_infrastructure (deployment_name )
549-
550536 rg_name = f"apim-infra-{ deployment_name .value } "
551537
552538 if index is not None :
@@ -638,3 +624,23 @@ def run(command: str, ok_message: str = '', error_message: str = '', print_outpu
638624 print_message (ok_message if success else error_message , output_text if not success or print_output else "" , f"[{ int (minutes )} m:{ int (seconds )} s]" )
639625
640626 return Output (success , output_text )
627+
628+ # Validation functions will raise ValueError if the value is not valid
629+
630+ validate_http_verb = lambda val : HTTP_VERB (val )
631+ validate_sku = lambda val : APIM_SKU (val )
632+
633+ def validate_infrastructure (infra : INFRASTRUCTURE , supported_infras : list [INFRASTRUCTURE ]) -> None :
634+ """
635+ Validate that the provided infrastructure is a supported infrastructure.
636+
637+ Args:
638+ infra (INFRASTRUCTURE): The infrastructure deployment enum value.
639+ supported_infras (list[INFRASTRUCTURE]): List of supported infrastructures.
640+
641+ Raises:
642+ ValueError: If the infrastructure is not supported.
643+ """
644+
645+ if infra not in supported_infras :
646+ raise ValueError (f"Unsupported infrastructure: { infra } . Supported infrastructures are: { ', ' .join ([i .value for i in supported_infras ])} " )
0 commit comments