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