Skip to content

Commit e5c1e9e

Browse files
Standardize logging (#105)
1 parent f5da89a commit e5c1e9e

29 files changed

+1672
-783
lines changed

.github/copilot-instructions.python.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ applyTo: "**/*.py"
2121
- Keep all imports at the top of the file.
2222
- Use type hints and concise docstrings (PEP 257).
2323
- Use 4-space indentation and PEP 8 conventions.
24+
- Surround an equal sign by a space on each side.
2425
- Use only straight quotes (U+0027 and U+0022), not typographic quotes.
2526
- Use whitespace to separate logical sections and add a blank line before `return` statements.
2627
- Use f-strings unless there is no interpolation.

.vscode/settings.json

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,7 @@
88
"./.venv/Scripts/python.exe"
99
],
1010
"jupyter.kernels.excludePythonEnvironments": [
11-
"**/anaconda3/**",
12-
"**/conda/**",
13-
"**/miniconda3/**",
14-
"**/python3.*",
15-
"*/site-packages/*",
16-
"/bin/python",
17-
"/bin/python3",
18-
"/opt/python/*/bin/python*",
19-
"/usr/bin/python",
20-
"/usr/bin/python3",
21-
"/usr/local/bin/python",
22-
"/usr/local/bin/python3",
23-
"python",
24-
"python3",
25-
"**/.venv/**/python*",
26-
"**/Scripts/python*",
27-
"**/bin/python*"
11+
"apim-samples"
2812
],
2913
"search.exclude": {
3014
"**/.venv": true,
@@ -66,11 +50,5 @@
6650
"plantuml.exportFormat": "svg",
6751
"plantuml.java": "C:\\Program Files\\OpenJDK\\jdk-22.0.2\\bin\\java.exe",
6852
"plantuml.diagramsRoot": "assets/diagrams/src",
69-
"plantuml.exportOutDir": "assets/diagrams/out",
70-
"jupyter.kernels.filter": [
71-
{
72-
"path": "apim-samples",
73-
"type": "pythonEnvironment"
74-
}
75-
]
76-
}
53+
"plantuml.exportOutDir": "assets/diagrams/out"
54+
}

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,16 @@ If you encounter import errors (e.g., `ModuleNotFoundError: No module named 'req
160160

161161
For detailed troubleshooting of setup issues, see [Import Troubleshooting Guide][import-troubleshooting].
162162

163+
---
164+
165+
## 🔎 Logging and Output
166+
167+
The Python helpers in this repo use standard-library `logging` (not ad-hoc `print()`), so you can control verbosity via environment variables.
168+
169+
- `APIM_SAMPLES_LOG_LEVEL`: Controls the overall verbosity (`DEBUG`, `INFO`, `WARNING`, `ERROR`, `CRITICAL`). Default: `INFO`.
170+
- When set to `DEBUG`, the Azure CLI runner in [shared/python/azure_resources.py](shared/python/azure_resources.py) will also add `--debug` to simple `az ...` commands.
171+
- `APIM_SAMPLES_CONSOLE_WIDTH`: Optional wrap width for long lines (defaults to `120`).
172+
163173
📘 **For comprehensive troubleshooting including deployment errors, authentication issues, and more, see our main [Troubleshooting Guide][troubleshooting].**
164174

165175
## 🚀 Running a Sample

infrastructure/afd-apim-pe/create_infrastructure.py

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,16 @@
1313

1414

1515
def create_infrastructure(location: str, index: int, apim_sku: APIM_SKU, no_aca: bool = False) -> None:
16-
try:
17-
# Check if infrastructure already exists to determine messaging
18-
infrastructure_exists = az.does_resource_group_exist(az.get_infra_rg_name(INFRASTRUCTURE.AFD_APIM_PE, index))
19-
20-
# Create custom APIs for AFD-APIM-PE with optional Container Apps backends
21-
custom_apis = _create_afd_specific_apis(not no_aca)
16+
# Check if infrastructure already exists to determine messaging
17+
infrastructure_exists = az.does_resource_group_exist(az.get_infra_rg_name(INFRASTRUCTURE.AFD_APIM_PE, index))
2218

23-
infra = AfdApimAcaInfrastructure(location, index, apim_sku, infra_apis = custom_apis)
24-
result = infra.deploy_infrastructure(infrastructure_exists)
19+
# Create custom APIs for AFD-APIM-PE with optional Container Apps backends
20+
custom_apis = _create_afd_specific_apis(not no_aca)
2521

26-
sys.exit(0 if result.success else 1)
22+
infra = AfdApimAcaInfrastructure(location, index, apim_sku, infra_apis = custom_apis)
23+
result = infra.deploy_infrastructure(infrastructure_exists)
2724

28-
except Exception as e:
29-
print(f'\n💥 Error: {str(e)}')
30-
sys.exit(1)
25+
raise SystemExit(0 if result.success else 1)
3126

3227
def _create_afd_specific_apis(use_aca: bool = True) -> list[API]:
3328
"""

infrastructure/apim-aca/create_infrastructure.py

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,16 @@
1313

1414

1515
def create_infrastructure(location: str, index: int, apim_sku: APIM_SKU) -> None:
16-
try:
17-
# Check if infrastructure already exists to determine messaging
18-
infrastructure_exists = az.does_resource_group_exist(az.get_infra_rg_name(INFRASTRUCTURE.APIM_ACA, index))
19-
20-
# Create custom APIs for APIM-ACA with Container Apps backends
21-
custom_apis = _create_aca_specific_apis()
16+
# Check if infrastructure already exists to determine messaging
17+
infrastructure_exists = az.does_resource_group_exist(az.get_infra_rg_name(INFRASTRUCTURE.APIM_ACA, index))
2218

23-
infra = ApimAcaInfrastructure(location, index, apim_sku, infra_apis = custom_apis)
24-
result = infra.deploy_infrastructure(infrastructure_exists)
19+
# Create custom APIs for APIM-ACA with Container Apps backends
20+
custom_apis = _create_aca_specific_apis()
2521

26-
sys.exit(0 if result.success else 1)
27-
28-
except Exception as e:
29-
print(f'\n💥 Error: {str(e)}')
30-
sys.exit(1)
22+
infra = ApimAcaInfrastructure(location, index, apim_sku, infra_apis = custom_apis)
23+
result = infra.deploy_infrastructure(infrastructure_exists)
3124

25+
raise SystemExit(0 if result.success else 1)
3226

3327
def _create_aca_specific_apis() -> list[API]:
3428
"""

infrastructure/appgw-apim-pe/create_infrastructure.py

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,16 @@
1313

1414

1515
def create_infrastructure(location: str, index: int, apim_sku: APIM_SKU, no_aca: bool = False) -> None:
16-
try:
17-
# Check if infrastructure already exists to determine messaging
18-
infrastructure_exists = az.does_resource_group_exist(az.get_infra_rg_name(INFRASTRUCTURE.APPGW_APIM_PE, index))
19-
20-
# Create custom APIs for APPGW-APIM-PE with optional Container Apps backends
21-
custom_apis = _create_appgw_specific_apis(not no_aca)
16+
# Check if infrastructure already exists to determine messaging
17+
infrastructure_exists = az.does_resource_group_exist(az.get_infra_rg_name(INFRASTRUCTURE.APPGW_APIM_PE, index))
2218

23-
infra = AppGwApimPeInfrastructure(location, index, apim_sku, infra_apis = custom_apis)
24-
result = infra.deploy_infrastructure(infrastructure_exists)
19+
# Create custom APIs for APPGW-APIM-PE with optional Container Apps backends
20+
custom_apis = _create_appgw_specific_apis(not no_aca)
2521

26-
sys.exit(0 if result.success else 1)
22+
infra = AppGwApimPeInfrastructure(location, index, apim_sku, infra_apis = custom_apis)
23+
result = infra.deploy_infrastructure(infrastructure_exists)
2724

28-
except Exception as e:
29-
print(f'\n💥 Error: {str(e)}')
30-
sys.exit(1)
25+
raise SystemExit(0 if result.success else 1)
3126

3227
def _create_appgw_specific_apis(use_aca: bool = True) -> list[API]:
3328
"""

infrastructure/appgw-apim-pe/main.bicep

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ resource nsgAppGw 'Microsoft.Network/networkSecurityGroups@2024-05-01' = {
146146
sourceAddressPrefix: 'AzureLoadBalancer'
147147
destinationAddressPrefix: '*'
148148
access: 'Allow'
149-
priority: 130
149+
priority: 120
150150
direction: 'Inbound'
151151
}
152152
}
@@ -221,10 +221,10 @@ module vnetModule '../../shared/bicep/modules/vnet/v1/vnet.bicep' = {
221221
}
222222
}
223223

224-
var apimSubnetResourceId = '${vnetModule.outputs.vnetId}/subnets/${apimSubnetName}'
225-
var acaSubnetResourceId = '${vnetModule.outputs.vnetId}/subnets/${acaSubnetName}'
226-
var appgwSubnetResourceId = '${vnetModule.outputs.vnetId}/subnets/${appgwSubnetName}'
227-
var privateEndpointSubnetResourceId = '${vnetModule.outputs.vnetId}/subnets/${privateEndpointSubnetName}'
224+
var apimSubnetResourceId = '${vnetModule.outputs.vnetId}/subnets/${apimSubnetName}'
225+
var acaSubnetResourceId = '${vnetModule.outputs.vnetId}/subnets/${acaSubnetName}'
226+
var appgwSubnetResourceId = '${vnetModule.outputs.vnetId}/subnets/${appgwSubnetName}'
227+
var peSubnetResourceId = '${vnetModule.outputs.vnetId}/subnets/${privateEndpointSubnetName}'
228228

229229
// 4. User Assigned Managed Identity
230230
// https://github.com/Azure/bicep-registry-modules/tree/main/avm/res/managed-identity/user-assigned-identity
@@ -340,6 +340,7 @@ module acaModule2 '../../shared/bicep/modules/aca/v1/containerapp.bicep' = if (u
340340
module apimModule '../../shared/bicep/modules/apim/v1/apim.bicep' = {
341341
name: 'apimModule'
342342
params: {
343+
apimName: apimName
343344
apimSku: apimSku
344345
appInsightsInstrumentationKey: appInsightsInstrumentationKey
345346
appInsightsId: appInsightsId
@@ -421,11 +422,13 @@ module apisModule '../../shared/bicep/modules/apim/v1/api.bicep' = [for api in a
421422
appInsightsId: appInsightsId
422423
api: api
423424
}
424-
dependsOn: [
425+
dependsOn: useACA ? [
425426
apimModule
426427
backendModule1
427428
backendModule2
428429
backendPoolModule
430+
] : [
431+
apimModule
429432
]
430433
}]
431434

@@ -436,7 +439,7 @@ resource apimPrivateEndpoint 'Microsoft.Network/privateEndpoints@2024-05-01' = {
436439
location: location
437440
properties: {
438441
subnet: {
439-
id: privateEndpointSubnetResourceId
442+
id: peSubnetResourceId
440443
}
441444
privateLinkServiceConnections: [
442445
{
@@ -476,7 +479,7 @@ module apimDnsPrivateLinkModule '../../shared/bicep/modules/dns/v1/dns-private-l
476479
dnsZoneName: 'privatelink.azure-api.net'
477480
vnetId: vnetModule.outputs.vnetId
478481
vnetLinkName: 'link-apim'
479-
enableDnsZoneGroup: true
482+
enableDnsZoneGroup: false
480483
dnsZoneGroupName: 'dnsZoneGroup-apim'
481484
dnsZoneConfigName: 'config-apim'
482485
}
@@ -553,7 +556,7 @@ module appgwModule 'br/public:avm/res/network/application-gateway:0.7.2' = {
553556
properties: {
554557
backendAddresses: [
555558
{
556-
fqdn: replace(apimModule.outputs.gatewayUrl, 'https://', '')
559+
fqdn: '${apimName}.azure-api.net'
557560
}
558561
]
559562
}
@@ -624,9 +627,6 @@ module appgwModule 'br/public:avm/res/network/application-gateway:0.7.2' = {
624627
}
625628
]
626629
}
627-
dependsOn: [
628-
apimPrivateDnsZoneGroup
629-
]
630630
}
631631

632632

infrastructure/simple-apim/clean-up.ipynb

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,27 +11,9 @@
1111
},
1212
{
1313
"cell_type": "code",
14-
"execution_count": 1,
14+
"execution_count": null,
1515
"metadata": {},
16-
"outputs": [
17-
{
18-
"name": "stdout",
19-
"output_type": "stream",
20-
"text": [
21-
"\n",
22-
"👉🏽 \u001b[1;34mCleaning up resources for simple-apim - 1\u001b[0m \n",
23-
"👉🏽 \u001b[1;34mResource group : apim-infra-simple-apim-1\u001b[0m \n",
24-
"👉🏽 \u001b[1;34mFound 1 resource(s) to clean up. Processing in parallel...\u001b[0m \n",
25-
"👉🏽 \u001b[1;34mStarting parallel cleanup of 1 resource(s) with 1 worker(s)...\u001b[0m \n",
26-
"👉🏽 \u001b[1;34mDeleting and purging apim 'apim-pwpn7jvwjh7em'...\u001b[0m \n",
27-
"\u001b[1;32m✓ Cleaned up apim 'apim-pwpn7jvwjh7em' (1/1)\u001b[0m ⌚ 15:32:07.104892 \n",
28-
"\n",
29-
"\u001b[1;32mAll 1 resource(s) cleaned up successfully!\u001b[0m ⌚ 15:32:07.104892 \n",
30-
"ℹ️ \u001b[1;32mDeleting resource group 'apim-infra-simple-apim-1'...\u001b[0m ⌚ 15:32:07.104892 \n",
31-
"ℹ️ \u001b[1;32mCleanup completed.\u001b[0m ⌚ 15:33:26.835597 \n"
32-
]
33-
}
34-
],
16+
"outputs": [],
3517
"source": [
3618
"from apimtypes import INFRASTRUCTURE\n",
3719
"from infrastructures import cleanup_infra_deployments\n",

infrastructure/simple-apim/create.ipynb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@
5454
],
5555
"metadata": {
5656
"kernelspec": {
57-
"display_name": "APIM Samples Python 3.12",
57+
"display_name": ".venv (3.12.10)",
5858
"language": "python",
59-
"name": "apim-samples"
59+
"name": "python3"
6060
},
6161
"language_info": {
6262
"codemirror_mode": {

infrastructure/simple-apim/create_infrastructure.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,15 @@
99
import azure_resources as az
1010
from apimtypes import APIM_SKU, INFRASTRUCTURE
1111
from infrastructures import SimpleApimInfrastructure
12+
from console import print_plain
1213

1314

1415
def create_infrastructure(location: str, index: int, apim_sku: APIM_SKU) -> None:
15-
try:
16-
# Check if infrastructure already exists to determine messaging
17-
infrastructure_exists = az.does_resource_group_exist(az.get_infra_rg_name(INFRASTRUCTURE.SIMPLE_APIM, index))
18-
19-
result = SimpleApimInfrastructure(location, index, apim_sku).deploy_infrastructure(infrastructure_exists)
20-
sys.exit(0 if result.success else 1)
16+
# Check if infrastructure already exists to determine messaging
17+
infrastructure_exists = az.does_resource_group_exist(az.get_infra_rg_name(INFRASTRUCTURE.SIMPLE_APIM, index))
2118

22-
except Exception as e:
23-
print(f'\n💥 Error: {str(e)}')
24-
sys.exit(1)
19+
result = SimpleApimInfrastructure(location, index, apim_sku).deploy_infrastructure(infrastructure_exists)
20+
raise SystemExit(0 if result.success else 1)
2521

2622
def main():
2723
"""
@@ -38,7 +34,7 @@ def main():
3834
try:
3935
apim_sku = APIM_SKU(args.sku)
4036
except ValueError:
41-
print(f"Error: Invalid SKU '{args.sku}'. Valid options are: {', '.join([sku.value for sku in APIM_SKU])}")
37+
print_plain(f"Error: Invalid SKU '{args.sku}'. Valid options are: {', '.join([sku.value for sku in APIM_SKU])}")
4238
sys.exit(1)
4339

4440
create_infrastructure(args.location, args.index, apim_sku)

0 commit comments

Comments
 (0)