-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
62 lines (52 loc) · 2.25 KB
/
main.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
import json
import uuid
import os
CURRENT_PATH = os.getcwd()
KEA_DHCP_INPUT_FILE_PATH = os.path.join(CURRENT_PATH, 'config', 'kea_dhcp_input.json')
KEA_DHCP_OUTPUT_FILE_PATH = os.path.join(CURRENT_PATH, 'config', 'kea_dhcp_output.json')
SITE_ID_GUID_MAP_PATH = os.path.join(CURRENT_PATH, 'config', 'fits_id_guid_map.json')
# Define the dhcp option data structure.
option_data = [
{
"name": "delivery-optimisation",
"space": "dhcp4",
"code": 234,
"data": "",
}
]
def main():
# Open the kea dhcp config fie, load the json into a python dictionary and close the input file.
kea_config_file = open(KEA_DHCP_INPUT_FILE_PATH, 'r')
kea_data = json.load(kea_config_file)
kea_config_file.close()
# Open the fits id guid map config fie, load the json into a python dictionary and close the input file.
fits_id_guid_map_file = open(SITE_ID_GUID_MAP_PATH, 'r')
fits_id_guid_map_data = json.load(fits_id_guid_map_file)
fits_id_guid_map_file.close()
# Break out the shared networks element from the kea config.
shared_nets = kea_data['Dhcp4']['shared-networks']
# Iterate through the shared networks.
for shared_net in shared_nets:
subnets = shared_net['subnet4']
if not subnets:
continue
for subnet in subnets:
site_id = subnet['user-context']['site-id']
if site_id not in fits_id_guid_map_data:
fits_id_guid_map_data[site_id] = str(uuid.uuid4())
if 'option-data' in subnet:
continue
# Populate the option data field with the guid based on the site id.
option_data[0]['data'] = fits_id_guid_map_data[site_id]
# Populate the subnet with the option data.
subnet["option-data"] = option_data
# Open the file and Write the site_id_map_json data back to a file.
site_id_map_json = open(SITE_ID_GUID_MAP_PATH, 'w')
site_id_map_json.write(json.dumps(fits_id_guid_map_data))
site_id_map_json.close()
# Open the file and Write the updated kea dhcp config to a new file.
kea_dhcp_output_file = open(KEA_DHCP_OUTPUT_FILE_PATH, 'w')
kea_dhcp_output_file.write(json.dumps(kea_data))
kea_dhcp_output_file.close()
if __name__ == '__main__':
main()