Skip to content

Commit 761447b

Browse files
Add infrastructure tags (#46)
1 parent 0c96d6f commit 761447b

File tree

5 files changed

+305
-7
lines changed

5 files changed

+305
-7
lines changed

infrastructure/afd-apim/create.ipynb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"\n",
3030
"# 2) Service-defined parameters (please do not change these unless you know what you're doing)\n",
3131
"rg_name = utils.get_infra_rg_name(deployment, index)\n",
32+
"rg_tags = utils.build_infrastructure_tags(deployment)\n",
3233
"apim_network_mode = APIMNetworkMode.EXTERNAL_VNET\n",
3334
"\n",
3435
"# 3) Define the APIs and their operations and policies\n",
@@ -95,7 +96,7 @@
9596
"}\n",
9697
"\n",
9798
"# 2) Run the deployment\n",
98-
"output = utils.create_bicep_deployment_group(rg_name, rg_location, deployment, bicep_parameters)\n",
99+
"output = utils.create_bicep_deployment_group(rg_name, rg_location, deployment, bicep_parameters, rg_tags = rg_tags)\n",
99100
"\n",
100101
"# 3) Print a deployment summary, if successful; otherwise, exit with an error\n",
101102
"if not output.success:\n",

infrastructure/apim-aca/create.ipynb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"\n",
2929
"# 2) Service-defined parameters (please do not change these)\n",
3030
"rg_name = utils.get_infra_rg_name(deployment, index)\n",
31+
"rg_tags = utils.build_infrastructure_tags(deployment)\n",
3132
"\n",
3233
"# 3) Define the APIs and their operations and policies\n",
3334
"\n",
@@ -83,7 +84,7 @@
8384
"}\n",
8485
"\n",
8586
"# 2) Run the deployment\n",
86-
"output = utils.create_bicep_deployment_group(rg_name, rg_location, deployment, bicep_parameters)\n",
87+
"output = utils.create_bicep_deployment_group(rg_name, rg_location, deployment, bicep_parameters, rg_tags = rg_tags)\n",
8788
"\n",
8889
"# 3) Check the deployment outputs\n",
8990
"if not output.success:\n",

infrastructure/simple-apim/create.ipynb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"\n",
2929
"# 2) Service-defined parameters (please do not change these)\n",
3030
"rg_name = utils.get_infra_rg_name(deployment, index)\n",
31+
"rg_tags = utils.build_infrastructure_tags(deployment)\n",
3132
"\n",
3233
"# 3) Define the APIs and their operations and policies\n",
3334
"\n",
@@ -68,7 +69,7 @@
6869
"}\n",
6970
"\n",
7071
"# 2) Run the deployment\n",
71-
"output = utils.create_bicep_deployment_group(rg_name, rg_location, deployment, bicep_parameters)\n",
72+
"output = utils.create_bicep_deployment_group(rg_name, rg_location, deployment, bicep_parameters, rg_tags = rg_tags)\n",
7273
"\n",
7374
"# 3) Check the deployment outputs\n",
7475
"if not output.success:\n",

shared/python/utils.py

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,40 @@
3333
CONSOLE_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

Comments
 (0)