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: |
| 9 | ctrls_by_site[ctrl_site.site].append(ctrl_site.controller) |
| 10 | sites = Site.objects.all() |
| 11 | for site in sites: |
| 12 | if site not in ctrls_by_site or \ |
| 13 | controller not in ctrls_by_site[site]: |
| 14 | controller_site = ControllerSite(controller=controller, site=site) |
| 15 | controller_site.save() |
| 16 | # relations for all slices |
| 17 | ctrls_by_slice = defaultdict(list) |
| 18 | ctrl_slices = ControllerSlice.objects.all() |
| 19 | for ctrl_slice in ctrl_slices: |
| 20 | ctrls_by_slice[ctrl_slice.slice].append(ctrl_slice.controller) |
| 21 | slices = Slice.objects.all() |
| 22 | for slice in slices: |
| 23 | if slice not in ctrls_by_slice or \ |
| 24 | controller not in ctrls_by_slice[slice]: |
| 25 | controller_slice = ControllerSlice(controller=controller, slice=slice) |
| 26 | controller_slice.save() |
| 27 | # relations for all users |
| 28 | ctrls_by_user = defaultdict(list) |
| 29 | ctrl_users = ControllerUser.objects.all() |
| 30 | for ctrl_user in ctrl_users: |
| 31 | ctrls_by_user[ctrl_user.user].append(ctrl_user.controller) |
| 32 | users = User.objects.all() |
| 33 | for user in users: |
| 34 | if user not in ctrls_by_user or \ |
| 35 | controller not in ctrls_by_user[user]: |
| 36 | controller_user = ControllerUser(controller=controller, user=user) |
| 37 | controller_user.save() |
| 38 | # relations for all networks |
| 39 | ctrls_by_network = defaultdict(list) |
| 40 | ctrl_networks = ControllerNetwork.objects.all() |
| 41 | for ctrl_network in ctrl_networks: |
| 42 | ctrls_by_network[ctrl_network.network].append(ctrl_network.controller) |
| 43 | networks = Network.objects.all() |
| 44 | for network in networks: |
| 45 | if network not in ctrls_by_network or \ |
| 46 | controller not in ctrls_by_network[network]: |
| 47 | controller_network = ControllerNetwork(controller=controller, network=network) |
| 48 | if network.subnet and network.subnet.strip(): |
| 49 | controller_network.subnet = network.subnet.strip() |
| 50 | controller_network.save() |
| 51 | # relations for all images |
| 52 | ctrls_by_image = defaultdict(list) |
| 53 | ctrl_images = ControllerImages.objects.all() |
| 54 | for ctrl_image in ctrl_images: |
| 55 | ctrls_by_image[ctrl_image.image].append(ctrl_image.controller) |
| 56 | images = Image.objects.all() |
| 57 | for image in images: |
| 58 | if image not in ctrls_by_image or \ |
| 59 | controller not in ctrls_by_image[image]: |
| 60 | controller_image = ControllerImages(controller=controller, image=image) |
| 61 | controller_image.save() |