3333CONSOLE_WIDTH = 175
3434
3535
36+ # ------------------------------
37+ # HELPER FUNCTIONS
38+ # ------------------------------
39+
40+ def build_infrastructure_tags (infrastructure : str | INFRASTRUCTURE , custom_tags : dict | None = None ) -> dict :
41+ """
42+ Build standard tags for infrastructure resource groups, including required 'infrastructure' and infrastructure name tags.
43+
44+ Args:
45+ infrastructure (str | INFRASTRUCTURE): The infrastructure type/name.
46+ custom_tags (dict, optional): Additional custom tags to include.
47+
48+ Returns:
49+ dict: Combined tags dictionary with standard and custom tags.
50+ """
51+
52+ # Convert infrastructure enum to string value if needed
53+ if hasattr (infrastructure , 'value' ):
54+ infra_name = infrastructure .value
55+ else :
56+ infra_name = str (infrastructure )
57+
58+ # Build standard tags
59+ standard_tags = {
60+ 'infrastructure' : infra_name
61+ }
62+
63+ # Add custom tags if provided
64+ if custom_tags :
65+ standard_tags .update (custom_tags )
66+
67+ return standard_tags
68+
69+
3670# ------------------------------
3771# CLASSES
3872# ------------------------------
@@ -272,7 +306,7 @@ def get_azure_role_guid(role_name: str) -> Optional[str]:
272306 return None
273307
274308
275- def create_bicep_deployment_group (rg_name : str , rg_location : str , deployment : str | INFRASTRUCTURE , bicep_parameters : dict , bicep_parameters_file : str = 'params.json' ) -> Output :
309+ def create_bicep_deployment_group (rg_name : str , rg_location : str , deployment : str | INFRASTRUCTURE , bicep_parameters : dict , bicep_parameters_file : str = 'params.json' , rg_tags : dict | None = None ) -> Output :
276310 """
277311 Create a Bicep deployment in a resource group, writing parameters to a file and running the deployment.
278312 Creates the resource group if it does not exist.
@@ -283,13 +317,14 @@ def create_bicep_deployment_group(rg_name: str, rg_location: str, deployment: st
283317 deployment (str | INFRASTRUCTURE): Deployment name or enum value.
284318 bicep_parameters: Parameters for the Bicep template.
285319 bicep_parameters_file (str, optional): File to write parameters to.
320+ rg_tags (dict, optional): Additional tags to apply to the resource group.
286321
287322 Returns:
288323 Output: The result of the deployment command.
289324 """
290325
291326 # Create the resource group if doesn't exist
292- create_resource_group (rg_name , rg_location )
327+ create_resource_group (rg_name , rg_location , rg_tags )
293328
294329 if hasattr (deployment , 'value' ):
295330 deployment_name = deployment .value
@@ -311,13 +346,14 @@ def create_bicep_deployment_group(rg_name: str, rg_location: str, deployment: st
311346 return run (f"az deployment group create --name { deployment_name } --resource-group { rg_name } --template-file main.bicep --parameters { bicep_parameters_file } --query \" properties.outputs\" " ,
312347 f"Deployment '{ deployment_name } ' succeeded" , f"Deployment '{ deployment_name } ' failed." )
313348
314- def create_resource_group (rg_name : str , resource_group_location : str | None = None ) -> None :
349+ def create_resource_group (rg_name : str , resource_group_location : str | None = None , tags : dict | None = None ) -> None :
315350 """
316351 Create a resource group in Azure if it does not already exist.
317352
318353 Args:
319354 rg_name (str): Name of the resource group.
320355 resource_group_location (str, optional): Azure region for the resource group.
356+ tags (dict, optional): Additional tags to apply to the resource group.
321357
322358 Returns:
323359 None
@@ -326,7 +362,15 @@ def create_resource_group(rg_name: str, resource_group_location: str | None = No
326362 if not does_resource_group_exist (rg_name ):
327363 print_info (f"Creating the resource group now..." )
328364
329- run (f"az group create --name { rg_name } --location { resource_group_location } --tags source=apim-sample" ,
365+ # Build the tags string for the Azure CLI command
366+ tag_string = "source=apim-sample"
367+ if tags :
368+ for key , value in tags .items ():
369+ # Escape values that contain spaces or special characters
370+ escaped_value = value .replace ('"' , '\\ "' ) if isinstance (value , str ) else str (value )
371+ tag_string += f" { key } =\" { escaped_value } \" "
372+
373+ run (f"az group create --name { rg_name } --location { resource_group_location } --tags { tag_string } " ,
330374 f"Resource group '{ rg_name } ' created" ,
331375 f"Failed to create the resource group '{ rg_name } '" ,
332376 False , True , False , False )
0 commit comments