Tony Mack | 31d3413 | 2013-04-15 00:02:06 -0400 | [diff] [blame] | 1 | import commands |
Tony Mack | 5df6c55 | 2013-04-11 21:24:17 -0400 | [diff] [blame] | 2 | from types import StringTypes |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 3 | from openstack.client import OpenStackClient |
| 4 | from openstack.driver import OpenStackDriver |
| 5 | from core.api.auth import auth_check |
| 6 | from core.models import Subnet |
| 7 | from core.api.slices import _get_slices |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 8 | |
Tony Mack | 31d3413 | 2013-04-15 00:02:06 -0400 | [diff] [blame] | 9 | |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 10 | def _get_subnets(filter): |
Tony Mack | 5df6c55 | 2013-04-11 21:24:17 -0400 | [diff] [blame] | 11 | if isinstance(filter, StringTypes) and filter.isdigit(): |
| 12 | filter = int(filter) |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 13 | if isinstance(filter, int): |
| 14 | subnets = Subnet.objects.filter(id=filter) |
| 15 | elif isinstance(filter, StringTypes): |
Tony Mack | 771f4b9 | 2013-04-12 01:49:57 -0400 | [diff] [blame] | 16 | # the name is the subnet's slice's name |
| 17 | slices = _get_slices(filter) |
| 18 | slice = None |
| 19 | if slices: slice=slices[0] |
| 20 | subnets = Subnet.objects.filter(slice=slice) |
Tony Mack | 5df6c55 | 2013-04-11 21:24:17 -0400 | [diff] [blame] | 21 | elif isinstance(filter, dict): |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 22 | subnets = Subnet.objects.filter(**filter) |
| 23 | else: |
| 24 | subnets = [] |
| 25 | return subnets |
| 26 | |
| 27 | def add_subnet(auth, fields): |
| 28 | driver = OpenStackDriver(client = auth_check(auth)) |
Tony Mack | 771f4b9 | 2013-04-12 01:49:57 -0400 | [diff] [blame] | 29 | slices = _get_slices(fields.get('slice')) |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 30 | if slices: fields['slice'] = slices[0] |
| 31 | subnet = Subnet(**fields) |
| 32 | # create quantum subnet |
Tony Mack | 4895203 | 2013-04-12 11:49:34 -0400 | [diff] [blame] | 33 | quantum_subnet = driver.create_subnet(name= subnet.slice.name, |
| 34 | network_id=subnet.slice.network_id, |
| 35 | cidr_ip = subnet.cidr, |
| 36 | ip_version=subnet.ip_version, |
| 37 | start = subnet.start, |
| 38 | end = subnet.end) |
| 39 | subnet.subnet_id=quantum_subnet['id'] |
| 40 | ## set dns servers |
| 41 | #driver.update_subnet(subnet.id, {'dns_nameservers': ['8.8.8.8', '8.8.4.4']}) |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 42 | |
| 43 | # add subnet as interface to slice's router |
Tony Mack | b09cedb | 2013-04-15 07:19:25 -0400 | [diff] [blame] | 44 | try: driver.add_router_interface(subnet.slice.router_id, subnet.subnet_id) |
| 45 | except: pass |
Tony Mack | aa6b750 | 2013-04-17 21:58:34 -0400 | [diff] [blame] | 46 | #add_route = 'route add -net %s dev br-ex gw 10.100.0.5' % self.cidr |
Tony Mack | 4ecbb2a | 2013-04-15 00:03:42 -0400 | [diff] [blame] | 47 | commands.getstatusoutput(add_route) |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 48 | subnet.save() |
| 49 | return subnet |
| 50 | |
| 51 | def update_subnet(auth, subnet, **fields): |
| 52 | return |
| 53 | |
| 54 | def delete_subnet(auth, filter={}): |
| 55 | driver = OpenStackDriver(client = auth_check(auth)) |
| 56 | subnets = Subnet.objects.filter(**filter) |
| 57 | for subnet in subnets: |
Tony Mack | d8a837a | 2013-04-16 22:07:22 -0400 | [diff] [blame] | 58 | driver.delete_router_interface(subnet.slice.router_id, subnet.subnet_id) |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 59 | driver.delete_subnet(subnet.subnet_id) |
| 60 | subnet.delete() |
Tony Mack | aa6b750 | 2013-04-17 21:58:34 -0400 | [diff] [blame] | 61 | #del_route = 'route del -net %s' % subnet.cidr |
Tony Mack | 4ecbb2a | 2013-04-15 00:03:42 -0400 | [diff] [blame] | 62 | commands.getstatusoutput(del_route) |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 63 | return 1 |
| 64 | |
| 65 | def get_subnets(auth, filter={}): |
| 66 | client = auth_check(auth) |
| 67 | if 'slice' in filter: |
| 68 | slice = _get_slice(filter.get('slice')) |
| 69 | if slice: filter['slice'] = slice |
| 70 | subnets = Subnet.objects.filter(**filter) |
| 71 | return subnets |
| 72 | |
| 73 | |
| 74 | |