|
| 1 | +# pylint: disable=line-too-long,useless-suppression |
| 2 | +# coding=utf-8 |
| 3 | +# -------------------------------------------------------------------------- |
| 4 | +# Copyright (c) Microsoft Corporation. All rights reserved. |
| 5 | +# Licensed under the MIT License. See License.txt in the project root for license information. |
| 6 | +# Code generated by Microsoft (R) AutoRest Code Generator. |
| 7 | +# Changes may cause incorrect behavior and will be lost if the code is regenerated. |
| 8 | +# -------------------------------------------------------------------------- |
| 9 | + |
| 10 | +from azure.identity import DefaultAzureCredential |
| 11 | +from azure.mgmt.network import NetworkManagementClient |
| 12 | +from azure.mgmt.resource.resources import ResourceManagementClient |
| 13 | +from azure.core.exceptions import ResourceNotFoundError |
| 14 | +from dotenv import load_dotenv |
| 15 | +import os |
| 16 | + |
| 17 | +import sys |
| 18 | +import logging |
| 19 | + |
| 20 | +logging.basicConfig( |
| 21 | + level=logging.DEBUG, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", stream=sys.stdout |
| 22 | +) |
| 23 | + |
| 24 | + |
| 25 | +def main(): |
| 26 | + load_dotenv() |
| 27 | + |
| 28 | + # Try to get subscription ID from environment variables |
| 29 | + subscription_id = os.environ.get("AZURE_SUBSCRIPTION_ID") |
| 30 | + if not subscription_id: |
| 31 | + raise ValueError( |
| 32 | + "Subscription ID not found. Please set either SUBSCRIPTION_ID or " |
| 33 | + "AZURE_SUBSCRIPTION_ID environment variable." |
| 34 | + ) |
| 35 | + |
| 36 | + credential = DefaultAzureCredential() |
| 37 | + |
| 38 | + # Initialize clients |
| 39 | + network_client = NetworkManagementClient( |
| 40 | + credential=credential, subscription_id=subscription_id, logging_enable=True |
| 41 | + ) |
| 42 | + resource_client = ResourceManagementClient( |
| 43 | + credential=credential, |
| 44 | + subscription_id=subscription_id, |
| 45 | + ) |
| 46 | + |
| 47 | + resource_group_name = "rg4-yyc" |
| 48 | + location = "eastus2" |
| 49 | + vnet_name = f"bastion-vnet-{resource_group_name}" |
| 50 | + vm_subnet_name = f"vm-subnet-{resource_group_name}" |
| 51 | + |
| 52 | + # Check if resource group exists, create if not |
| 53 | + try: |
| 54 | + resource_client.resource_groups.get(resource_group_name) |
| 55 | + print(f"Resource group '{resource_group_name}' exists.") |
| 56 | + except ResourceNotFoundError: |
| 57 | + print(f"Resource group '{resource_group_name}' does not exist. Creating...") |
| 58 | + resource_client.resource_groups.create_or_update(resource_group_name, {"location": location}) |
| 59 | + print(f"Resource group '{resource_group_name}' created.") |
| 60 | + |
| 61 | + try: |
| 62 | + # get virtual network |
| 63 | + network_client.virtual_networks.get(resource_group_name, vnet_name) |
| 64 | + print(f"Virtual network '{vnet_name}' already exists.") |
| 65 | + except ResourceNotFoundError: |
| 66 | + print(f"Virtual network '{vnet_name}' does not exist. Creating...") |
| 67 | + |
| 68 | + # Create virtual network for bastion |
| 69 | + subnet_name = "AzureBastionSubnet" |
| 70 | + |
| 71 | + # Create VNet with both bastion and VM subnets |
| 72 | + vnet_params = { |
| 73 | + "location": location, |
| 74 | + "address_space": {"address_prefixes": ["10.0.0.0/16"]}, |
| 75 | + "subnets": [ |
| 76 | + {"name": subnet_name, "address_prefix": "10.0.1.0/27"}, |
| 77 | + {"name": vm_subnet_name, "address_prefix": "10.0.2.0/24"}, |
| 78 | + ], |
| 79 | + } |
| 80 | + |
| 81 | + print("Creating virtual network...") |
| 82 | + poller = network_client.virtual_networks.begin_create_or_update(resource_group_name, vnet_name, vnet_params) |
| 83 | + |
| 84 | + # get continuation token from poller |
| 85 | + continuation_token = poller.continuation_token() |
| 86 | + |
| 87 | + # reconstruct poller from continuation token |
| 88 | + poller_from_token = network_client.virtual_networks.begin_create_or_update( |
| 89 | + None, None, None, continuation_token=continuation_token |
| 90 | + ) |
| 91 | + |
| 92 | + # poll until done |
| 93 | + vnet_result = poller_from_token.result() |
| 94 | + print(vnet_result) |
| 95 | + |
| 96 | + print(f"Virtual network '{vnet_name}' created.") |
| 97 | + |
| 98 | + |
| 99 | +if __name__ == "__main__": |
| 100 | + main() |
0 commit comments