Scott Baker | af599eb | 2017-03-21 12:43:26 -0700 | [diff] [blame] | 1 | from collections import defaultdict |
| 2 | from synchronizers.new_base.modelaccessor import * |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 3 | |
| 4 | def handle(controller): |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 5 | # relations for all sites |
| 6 | ctrls_by_site = defaultdict(list) |
| 7 | ctrl_sites = ControllerSite.objects.all() |
| 8 | for ctrl_site in ctrl_sites: |
Scott Baker | a0a688a | 2017-03-28 11:59:56 -0700 | [diff] [blame] | 9 | ctrls_by_site[ctrl_site.site.id].append(ctrl_site.controller.id) |
| 10 | |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 11 | sites = Site.objects.all() |
| 12 | for site in sites: |
Scott Baker | a0a688a | 2017-03-28 11:59:56 -0700 | [diff] [blame] | 13 | if site.id not in ctrls_by_site or controller.id not in ctrls_by_site[site.id]: |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 14 | controller_site = ControllerSite(controller=controller, site=site) |
| 15 | controller_site.save() |
Scott Baker | a0a688a | 2017-03-28 11:59:56 -0700 | [diff] [blame] | 16 | |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 17 | # relations for all slices |
| 18 | ctrls_by_slice = defaultdict(list) |
| 19 | ctrl_slices = ControllerSlice.objects.all() |
| 20 | for ctrl_slice in ctrl_slices: |
Scott Baker | a0a688a | 2017-03-28 11:59:56 -0700 | [diff] [blame] | 21 | ctrls_by_slice[ctrl_slice.slice.id].append(ctrl_slice.controller.id) |
| 22 | |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 23 | slices = Slice.objects.all() |
| 24 | for slice in slices: |
Scott Baker | a0a688a | 2017-03-28 11:59:56 -0700 | [diff] [blame] | 25 | if slice.id not in ctrls_by_slice or controller.id not in ctrls_by_slice[slice.id]: |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 26 | controller_slice = ControllerSlice(controller=controller, slice=slice) |
| 27 | controller_slice.save() |
Scott Baker | a0a688a | 2017-03-28 11:59:56 -0700 | [diff] [blame] | 28 | |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 29 | # relations for all users |
| 30 | ctrls_by_user = defaultdict(list) |
| 31 | ctrl_users = ControllerUser.objects.all() |
| 32 | for ctrl_user in ctrl_users: |
Scott Baker | a0a688a | 2017-03-28 11:59:56 -0700 | [diff] [blame] | 33 | ctrls_by_user[ctrl_user.user.id].append(ctrl_user.controller.id) |
| 34 | |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 35 | users = User.objects.all() |
| 36 | for user in users: |
Scott Baker | a0a688a | 2017-03-28 11:59:56 -0700 | [diff] [blame] | 37 | if user.id not in ctrls_by_user or controller.id not in ctrls_by_user[user.id]: |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 38 | controller_user = ControllerUser(controller=controller, user=user) |
| 39 | controller_user.save() |
Scott Baker | a0a688a | 2017-03-28 11:59:56 -0700 | [diff] [blame] | 40 | |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 41 | # relations for all networks |
| 42 | ctrls_by_network = defaultdict(list) |
| 43 | ctrl_networks = ControllerNetwork.objects.all() |
| 44 | for ctrl_network in ctrl_networks: |
Scott Baker | a0a688a | 2017-03-28 11:59:56 -0700 | [diff] [blame] | 45 | ctrls_by_network[ctrl_network.network.id].append(ctrl_network.controller.id) |
| 46 | |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 47 | networks = Network.objects.all() |
| 48 | for network in networks: |
Scott Baker | a0a688a | 2017-03-28 11:59:56 -0700 | [diff] [blame] | 49 | if network.id not in ctrls_by_network or controller.id not in ctrls_by_network[network.id]: |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 50 | 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 Baker | a0a688a | 2017-03-28 11:59:56 -0700 | [diff] [blame] | 54 | |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 55 | # relations for all images |
| 56 | ctrls_by_image = defaultdict(list) |
| 57 | ctrl_images = ControllerImages.objects.all() |
| 58 | for ctrl_image in ctrl_images: |
Scott Baker | a0a688a | 2017-03-28 11:59:56 -0700 | [diff] [blame] | 59 | ctrls_by_image[ctrl_image.image.id].append(ctrl_image.controller.id) |
| 60 | |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 61 | images = Image.objects.all() |
| 62 | for image in images: |
Scott Baker | a0a688a | 2017-03-28 11:59:56 -0700 | [diff] [blame] | 63 | if image.id not in ctrls_by_image or controller.id not in ctrls_by_image[image.id]: |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 64 | controller_image = ControllerImages(controller=controller, image=image) |
| 65 | controller_image.save() |
Scott Baker | a0a688a | 2017-03-28 11:59:56 -0700 | [diff] [blame] | 66 | |