-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathcreate_infrastructure.py
More file actions
91 lines (69 loc) · 3.64 KB
/
Copy pathcreate_infrastructure.py
File metadata and controls
91 lines (69 loc) · 3.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
"""
This module provides a reusable way to create API Management with Azure Container Apps infrastructure
that can be called from notebooks or other scripts.
"""
import argparse
import sys
# APIM Samples imports
import azure_resources as az
from apimtypes import API, APIM_SKU, BACKEND_XML_POLICY_PATH, INFRASTRUCTURE, GET_APIOperation, Region
from infrastructures import ApimAcaInfrastructure
from utils import read_policy_xml
def create_infrastructure(location: str, index: int, apim_sku: APIM_SKU, rg_exists: bool | None = None) -> None:
"""Create the APIM + Azure Container Apps infrastructure."""
if rg_exists is None:
infrastructure_exists = az.does_resource_group_exist(az.get_infra_rg_name(INFRASTRUCTURE.APIM_ACA, index))
else:
infrastructure_exists = rg_exists
# Create custom APIs for APIM-ACA with Container Apps backends
custom_apis = _create_aca_specific_apis()
infra = ApimAcaInfrastructure(location, index, apim_sku, infra_apis=custom_apis, rg_exists=rg_exists)
result = infra.deploy_infrastructure(infrastructure_exists)
raise SystemExit(0 if result.success else 1)
def _create_aca_specific_apis() -> list[API]:
"""
Create APIM-ACA specific APIs with Container Apps backends.
Returns:
list[API]: List of ACA-specific APIs.
"""
# Define the APIs with Container Apps backends
pol_backend = read_policy_xml(BACKEND_XML_POLICY_PATH)
pol_aca_backend_1 = pol_backend.format(backend_id='aca-backend-1')
pol_aca_backend_2 = pol_backend.format(backend_id='aca-backend-2')
pol_aca_backend_pool = pol_backend.format(backend_id='aca-backend-pool')
# API 1: Hello World (ACA Backend 1)
api_hwaca_1_get = GET_APIOperation('This is a GET for Hello World on ACA Backend 1')
api_hwaca_1 = API('hello-world-aca-1', 'Hello World (ACA 1)', '/aca-1', 'This is the ACA API for Backend 1', pol_aca_backend_1, [api_hwaca_1_get])
# API 2: Hello World (ACA Backend 2)
api_hwaca_2_get = GET_APIOperation('This is a GET for Hello World on ACA Backend 2')
api_hwaca_2 = API('hello-world-aca-2', 'Hello World (ACA 2)', '/aca-2', 'This is the ACA API for Backend 2', pol_aca_backend_2, [api_hwaca_2_get])
# API 3: Hello World (ACA Backend Pool)
api_hwaca_pool_get = GET_APIOperation('This is a GET for Hello World on ACA Backend Pool')
api_hwaca_pool = API(
'hello-world-aca-pool',
'Hello World (ACA Pool)',
'/aca-pool',
'This is the ACA API for Backend Pool',
pol_aca_backend_pool,
[api_hwaca_pool_get],
)
return [api_hwaca_1, api_hwaca_2, api_hwaca_pool]
def main():
"""
Main entry point for command-line usage.
"""
parser = argparse.ArgumentParser(description='Create APIM-ACA infrastructure')
parser.add_argument('--location', default=Region.EAST_US_2, help=f'Azure region (default: {Region.EAST_US_2})')
parser.add_argument('--index', type=int, help='Infrastructure index')
parser.add_argument('--sku', choices=['Basicv2', 'Standardv2', 'Premiumv2'], default='Basicv2', help='APIM SKU (default: Basicv2)')
parser.add_argument('--rg-exists', action=argparse.BooleanOptionalAction, default=None, help='Pre-checked resource group existence state')
args = parser.parse_args()
# Convert SKU string to enum using the enum's built-in functionality
try:
apim_sku = APIM_SKU(args.sku)
except ValueError:
print(f"Error: Invalid SKU '{args.sku}'. Valid options are: {', '.join([sku.value for sku in APIM_SKU])}")
sys.exit(1)
create_infrastructure(args.location, args.index, apim_sku, rg_exists=args.rg_exists)
if __name__ == '__main__': # pragma: no cover
main()