-
Notifications
You must be signed in to change notification settings - Fork 1
/
create_udr.py
69 lines (53 loc) · 2.03 KB
/
create_udr.py
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
"""Create UDRs and Routes on Azure using Azure SDK."""
import os
from azure.identity import AzureCliCredential
from azure.mgmt.network import NetworkManagementClient
from generate_udr import read_csv_data, udr_list
# Retrieve subscription ID from environment variable.
subscription_id = os.environ["AZURE_SUBSCRIPTION_ID"]
print(f"Using subscription '{subscription_id}'")
# CONSTANTS
###########
RESOURCE_GROUP_NAME = "tzakisg-udr-test"
LOCATION = "West Europe"
FW_IP_ADDRESS = "10.1.1.1"
FILE_LOCATION = "/home/memos/Projects/azure-udr-generator/data.csv"
###########
# CONSTANTS
topology_data = read_csv_data(FILE_LOCATION)
udrs = udr_list(topology_data, FW_IP_ADDRESS)
# Acquire a credential object using CLI-based authentication.
credential = AzureCliCredential()
# Obtain the management object for networks
network_client = NetworkManagementClient(credential, subscription_id)
def create_route_tables(udr_table):
"""Create Route Table."""
result = network_client.route_tables.begin_create_or_update(
RESOURCE_GROUP_NAME,
udr_table.name,
{"location": LOCATION, "disable_bgp_route_propagation": True},
).result()
return result
def create_routes(udr_name, udr_route):
"""Create Route on a specific UDR."""
result = network_client.routes.begin_create_or_update(
RESOURCE_GROUP_NAME,
udr_name,
udr_route.name,
{
"address_prefix": udr_route.dest_subnet,
"next_hop_type": udr_route.next_hop_type,
"next_hop_ip_address": udr_route.next_hop_ip,
},
).result()
return result
for udr in udrs:
# Provision Route table and wait for completion
route_table_result = create_route_tables(udr)
print(f"Provisioned Route Table: {route_table_result.name}")
for udr in udrs:
# Iterate through all routes of table
for route in udr.routes:
# Provision route and wait for completion
route_result = create_routes(udr.name, route)
print(f"Provisioned route '{route_result.name}' on route table '{udr.name}'")