Sapan Bhatia | b3aeb1b | 2014-09-03 15:39:13 -0400 | [diff] [blame] | 1 | |
| 2 | def handle(slice): |
Tony Mack | a7dbd42 | 2015-01-05 22:48:11 -0500 | [diff] [blame] | 3 | from core.models import Controller, ControllerSlice, SiteDeployment, Network, NetworkSlice,NetworkTemplate |
| 4 | from collections import defaultdict |
| 5 | controller_slices = ControllerSlice.objects.filter(slice=slice) |
| 6 | existing_controllers = [cs.controller for cs in controller_slices] |
| 7 | |
| 8 | all_controllers = Controller.objects.all() |
| 9 | for controller in all_controllers: |
| 10 | if controller not in existing_controllers: |
Tony Mack | 2656436 | 2015-01-06 17:49:25 -0500 | [diff] [blame] | 11 | sd = ControllerSlice(slice=slice, controller=controller) |
Tony Mack | a7dbd42 | 2015-01-05 22:48:11 -0500 | [diff] [blame] | 12 | sd.save() |
Sapan Bhatia | b3aeb1b | 2014-09-03 15:39:13 -0400 | [diff] [blame] | 13 | |
Tony Mack | a7dbd42 | 2015-01-05 22:48:11 -0500 | [diff] [blame] | 14 | # make sure slice has at least 1 public and 1 private networkd |
| 15 | public_nets = [] |
| 16 | private_net = None |
| 17 | networks = Network.objects.filter(owner=slice) |
| 18 | for network in networks: |
| 19 | if network.template.name == 'Public dedicated IPv4': |
| 20 | public_nets.append(network) |
| 21 | elif network.template.name == 'Public shared IPv4': |
| 22 | public_nets.append(network) |
| 23 | elif network.template.name == 'Private': |
| 24 | private_net = network |
| 25 | if not public_nets: |
Scott Baker | cb7649c | 2014-10-16 00:43:54 -0700 | [diff] [blame] | 26 | # ensure there is at least one public network, and default it to dedicated |
Tony Mack | a7dbd42 | 2015-01-05 22:48:11 -0500 | [diff] [blame] | 27 | nat_net = Network( |
| 28 | name = slice.name+'-nat', |
| 29 | template = NetworkTemplate.objects.get(name='Public shared IPv4'), |
| 30 | owner = slice |
| 31 | ) |
| 32 | nat_net.save() |
| 33 | public_nets.append(nat_net) |
Scott Baker | cb7649c | 2014-10-16 00:43:54 -0700 | [diff] [blame] | 34 | |
Tony Mack | a7dbd42 | 2015-01-05 22:48:11 -0500 | [diff] [blame] | 35 | if not private_net: |
| 36 | private_net = Network( |
| 37 | name = slice.name+'-private', |
| 38 | template = NetworkTemplate.objects.get(name='Private'), |
| 39 | owner = slice |
| 40 | ) |
| 41 | private_net.save() |
| 42 | # create slice networks |
| 43 | public_net_slice = None |
| 44 | private_net_slice = None |
| 45 | net_slices = NetworkSlice.objects.filter(slice=slice, network__in=[private_net]+public_nets) |
| 46 | for net_slice in net_slices: |
| 47 | if net_slice.network in public_nets: |
| 48 | public_net_slice = net_slice |
| 49 | elif net_slice.network == private_net: |
| 50 | private_net_slice = net_slice |
| 51 | if not public_net_slice: |
| 52 | public_net_slice = NetworkSlice(slice=slice, network=public_nets[0]) |
| 53 | public_net_slice.save() |
| 54 | if not private_net_slice: |
| 55 | private_net_slice = NetworkSlice(slice=slice, network=private_net) |
| 56 | private_net_slice.save() |
Scott Baker | cb7649c | 2014-10-16 00:43:54 -0700 | [diff] [blame] | 57 | |
| 58 | |
| 59 | |
| 60 | |