blob: 680f252c00c1b56a36837be5b901f79b86c5a769 [file] [log] [blame]
Scott Bakeraf599eb2017-03-21 12:43:26 -07001from collections import defaultdict
2from synchronizers.new_base.modelaccessor import *
Scott Bakerb63ea792016-08-11 10:24:48 -07003
4def handle(controller):
Scott Bakerb63ea792016-08-11 10:24:48 -07005 # relations for all sites
6 ctrls_by_site = defaultdict(list)
7 ctrl_sites = ControllerSite.objects.all()
8 for ctrl_site in ctrl_sites:
Scott Bakera0a688a2017-03-28 11:59:56 -07009 ctrls_by_site[ctrl_site.site.id].append(ctrl_site.controller.id)
10
Scott Bakerb63ea792016-08-11 10:24:48 -070011 sites = Site.objects.all()
12 for site in sites:
Scott Bakera0a688a2017-03-28 11:59:56 -070013 if site.id not in ctrls_by_site or controller.id not in ctrls_by_site[site.id]:
Scott Bakerb63ea792016-08-11 10:24:48 -070014 controller_site = ControllerSite(controller=controller, site=site)
15 controller_site.save()
Scott Bakera0a688a2017-03-28 11:59:56 -070016
Scott Bakerb63ea792016-08-11 10:24:48 -070017 # relations for all slices
18 ctrls_by_slice = defaultdict(list)
19 ctrl_slices = ControllerSlice.objects.all()
20 for ctrl_slice in ctrl_slices:
Scott Bakera0a688a2017-03-28 11:59:56 -070021 ctrls_by_slice[ctrl_slice.slice.id].append(ctrl_slice.controller.id)
22
Scott Bakerb63ea792016-08-11 10:24:48 -070023 slices = Slice.objects.all()
24 for slice in slices:
Scott Bakera0a688a2017-03-28 11:59:56 -070025 if slice.id not in ctrls_by_slice or controller.id not in ctrls_by_slice[slice.id]:
Scott Bakerb63ea792016-08-11 10:24:48 -070026 controller_slice = ControllerSlice(controller=controller, slice=slice)
27 controller_slice.save()
Scott Bakera0a688a2017-03-28 11:59:56 -070028
Scott Bakerb63ea792016-08-11 10:24:48 -070029 # relations for all users
30 ctrls_by_user = defaultdict(list)
31 ctrl_users = ControllerUser.objects.all()
32 for ctrl_user in ctrl_users:
Scott Bakera0a688a2017-03-28 11:59:56 -070033 ctrls_by_user[ctrl_user.user.id].append(ctrl_user.controller.id)
34
Scott Bakerb63ea792016-08-11 10:24:48 -070035 users = User.objects.all()
36 for user in users:
Scott Bakera0a688a2017-03-28 11:59:56 -070037 if user.id not in ctrls_by_user or controller.id not in ctrls_by_user[user.id]:
Scott Bakerb63ea792016-08-11 10:24:48 -070038 controller_user = ControllerUser(controller=controller, user=user)
39 controller_user.save()
Scott Bakera0a688a2017-03-28 11:59:56 -070040
Scott Bakerb63ea792016-08-11 10:24:48 -070041 # relations for all networks
42 ctrls_by_network = defaultdict(list)
43 ctrl_networks = ControllerNetwork.objects.all()
44 for ctrl_network in ctrl_networks:
Scott Bakera0a688a2017-03-28 11:59:56 -070045 ctrls_by_network[ctrl_network.network.id].append(ctrl_network.controller.id)
46
Scott Bakerb63ea792016-08-11 10:24:48 -070047 networks = Network.objects.all()
48 for network in networks:
Scott Bakera0a688a2017-03-28 11:59:56 -070049 if network.id not in ctrls_by_network or controller.id not in ctrls_by_network[network.id]:
Scott Bakerb63ea792016-08-11 10:24:48 -070050 controller_network = ControllerNetwork(controller=controller, network=network)
51 if network.subnet and network.subnet.strip():
52 controller_network.subnet = network.subnet.strip()
53 controller_network.save()
Scott Bakera0a688a2017-03-28 11:59:56 -070054
Scott Bakerb63ea792016-08-11 10:24:48 -070055 # relations for all images
56 ctrls_by_image = defaultdict(list)
57 ctrl_images = ControllerImages.objects.all()
58 for ctrl_image in ctrl_images:
Scott Bakera0a688a2017-03-28 11:59:56 -070059 ctrls_by_image[ctrl_image.image.id].append(ctrl_image.controller.id)
60
Scott Bakerb63ea792016-08-11 10:24:48 -070061 images = Image.objects.all()
62 for image in images:
Scott Bakera0a688a2017-03-28 11:59:56 -070063 if image.id not in ctrls_by_image or controller.id not in ctrls_by_image[image.id]:
Scott Bakerb63ea792016-08-11 10:24:48 -070064 controller_image = ControllerImages(controller=controller, image=image)
65 controller_image.save()
Scott Bakera0a688a2017-03-28 11:59:56 -070066