Tony Mack | 5df6c55 | 2013-04-11 21:24:17 -0400 | [diff] [blame] | 1 | from types import StringTypes |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 2 | from plstackapi.openstack.client import OpenStackClient |
| 3 | from plstackapi.openstack.driver import OpenStackDriver |
| 4 | from plstackapi.core.api.auth import auth_check |
| 5 | from plstackapi.core.models import Subnet |
| 6 | from plstackapi.core.api.slices import _get_slices |
| 7 | |
| 8 | def _get_subnets(filter): |
Tony Mack | 5df6c55 | 2013-04-11 21:24:17 -0400 | [diff] [blame] | 9 | if isinstance(filter, StringTypes) and filter.isdigit(): |
| 10 | filter = int(filter) |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 11 | if isinstance(filter, int): |
| 12 | subnets = Subnet.objects.filter(id=filter) |
| 13 | elif isinstance(filter, StringTypes): |
Tony Mack | 771f4b9 | 2013-04-12 01:49:57 -0400 | [diff] [blame^] | 14 | # the name is the subnet's slice's name |
| 15 | slices = _get_slices(filter) |
| 16 | slice = None |
| 17 | if slices: slice=slices[0] |
| 18 | subnets = Subnet.objects.filter(slice=slice) |
Tony Mack | 5df6c55 | 2013-04-11 21:24:17 -0400 | [diff] [blame] | 19 | elif isinstance(filter, dict): |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 20 | subnets = Subnet.objects.filter(**filter) |
| 21 | else: |
| 22 | subnets = [] |
| 23 | return subnets |
| 24 | |
| 25 | def add_subnet(auth, fields): |
| 26 | driver = OpenStackDriver(client = auth_check(auth)) |
Tony Mack | 771f4b9 | 2013-04-12 01:49:57 -0400 | [diff] [blame^] | 27 | slices = _get_slices(fields.get('slice')) |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 28 | if slices: fields['slice'] = slices[0] |
| 29 | subnet = Subnet(**fields) |
| 30 | # create quantum subnet |
Tony Mack | 771f4b9 | 2013-04-12 01:49:57 -0400 | [diff] [blame^] | 31 | subnet = driver.create_subnet(network_name=subnet.slice.name, |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 32 | cidr_ip = subnet.cidr, |
| 33 | ip_version=subnet.ip_version, |
| 34 | start = subnet.start, |
| 35 | end = subnet.end, |
| 36 | dns_nameservers = ['8.8.8.8', '8.8.4.4']) |
| 37 | |
| 38 | subnet.subnet_id=subnet.id |
| 39 | |
| 40 | # add subnet as interface to slice's router |
| 41 | driver.add_router_interface(subnet.slice.router_id, subnet.subnet_id) |
| 42 | |
| 43 | subnet.save() |
| 44 | return subnet |
| 45 | |
| 46 | def update_subnet(auth, subnet, **fields): |
| 47 | return |
| 48 | |
| 49 | def delete_subnet(auth, filter={}): |
| 50 | driver = OpenStackDriver(client = auth_check(auth)) |
| 51 | subnets = Subnet.objects.filter(**filter) |
| 52 | for subnet in subnets: |
| 53 | driver.delete_router_interface(subnet.slice.router_id, subnet.subnet_id) |
| 54 | driver.delete_subnet(subnet.subnet_id) |
| 55 | subnet.delete() |
| 56 | return 1 |
| 57 | |
| 58 | def get_subnets(auth, filter={}): |
| 59 | client = auth_check(auth) |
| 60 | if 'slice' in filter: |
| 61 | slice = _get_slice(filter.get('slice')) |
| 62 | if slice: filter['slice'] = slice |
| 63 | subnets = Subnet.objects.filter(**filter) |
| 64 | return subnets |
| 65 | |
| 66 | |
| 67 | |