|
| 1 | +""" |
| 2 | +Reusable script to create Application Gateway + APIM (Internal) infrastructure. |
| 3 | +""" |
| 4 | + |
| 5 | +import sys |
| 6 | +import argparse |
| 7 | + |
| 8 | +# APIM Samples imports |
| 9 | +import azure_resources as az |
| 10 | +from apimtypes import APIM_SKU, API, GET_APIOperation, BACKEND_XML_POLICY_PATH, INFRASTRUCTURE |
| 11 | +from infrastructures import AppGwApimInfrastructure |
| 12 | +import utils |
| 13 | + |
| 14 | + |
| 15 | +def create_infrastructure(location: str, index: int, apim_sku: APIM_SKU, no_aca: bool = False) -> None: |
| 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, index)) |
| 18 | + |
| 19 | + # Create custom APIs for APPGW-APIM with optional Container Apps backends |
| 20 | + custom_apis = _create_appgw_specific_apis(not no_aca) |
| 21 | + |
| 22 | + infra = AppGwApimInfrastructure(location, index, apim_sku, infra_apis = custom_apis) |
| 23 | + result = infra.deploy_infrastructure(infrastructure_exists) |
| 24 | + |
| 25 | + raise SystemExit(0 if result.success else 1) |
| 26 | + |
| 27 | +def _create_appgw_specific_apis(use_aca: bool = True) -> list[API]: |
| 28 | + """ |
| 29 | + Create APPGW-APIM specific APIs with optional Container Apps backends. |
| 30 | +
|
| 31 | + Args: |
| 32 | + use_aca (bool): Whether to include Azure Container Apps backends. Defaults to true. |
| 33 | +
|
| 34 | + Returns: |
| 35 | + list[API]: List of AppGw-specific APIs. |
| 36 | + """ |
| 37 | + |
| 38 | + # If Container Apps is enabled, create the ACA APIs in APIM |
| 39 | + if use_aca: |
| 40 | + pol_backend = utils.read_policy_xml(BACKEND_XML_POLICY_PATH) |
| 41 | + pol_aca_backend_1 = pol_backend.format(backend_id = 'aca-backend-1') |
| 42 | + pol_aca_backend_2 = pol_backend.format(backend_id = 'aca-backend-2') |
| 43 | + pol_aca_backend_pool = pol_backend.format(backend_id = 'aca-backend-pool') |
| 44 | + |
| 45 | + # API 1: Hello World (ACA Backend 1) |
| 46 | + api_hwaca_1_get = GET_APIOperation('This is a GET for Hello World on ACA Backend 1') |
| 47 | + 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]) |
| 48 | + |
| 49 | + # API 2: Hello World (ACA Backend 2) |
| 50 | + api_hwaca_2_get = GET_APIOperation('This is a GET for Hello World on ACA Backend 2') |
| 51 | + 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]) |
| 52 | + |
| 53 | + # API 3: Hello World (ACA Backend Pool) |
| 54 | + api_hwaca_pool_get = GET_APIOperation('This is a GET for Hello World on ACA Backend Pool') |
| 55 | + 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]) |
| 56 | + |
| 57 | + return [api_hwaca_1, api_hwaca_2, api_hwaca_pool] |
| 58 | + |
| 59 | + return [] |
| 60 | + |
| 61 | +def main(): |
| 62 | + """ |
| 63 | + Main entry point for command-line usage. |
| 64 | + """ |
| 65 | + |
| 66 | + parser = argparse.ArgumentParser(description = 'Create APPGW-APIM infrastructure (VNet Internal)') |
| 67 | + parser.add_argument('--location', default = 'eastus2', help = 'Azure region (default: eastus2)') |
| 68 | + parser.add_argument('--index', type = int, help = 'Infrastructure index') |
| 69 | + parser.add_argument('--sku', choices = ['Developer', 'Basicv2', 'Standardv2'], default = 'Developer', help = 'APIM SKU (default: Developer)') |
| 70 | + parser.add_argument('--no-aca', action = 'store_true', help = 'Disable Azure Container Apps') |
| 71 | + args = parser.parse_args() |
| 72 | + |
| 73 | + # Convert SKU string to enum using the enum's built-in functionality |
| 74 | + try: |
| 75 | + apim_sku = APIM_SKU(args.sku) |
| 76 | + except ValueError: |
| 77 | + print(f"Error: Invalid SKU '{args.sku}'. Valid options are: {', '.join([sku.value for sku in APIM_SKU])}") |
| 78 | + sys.exit(1) |
| 79 | + |
| 80 | + create_infrastructure(args.location, args.index, apim_sku, args.no_aca) |
| 81 | + |
| 82 | +if __name__ == '__main__': |
| 83 | + main() |
0 commit comments