blob: 44cd56fd07d85a7c14320ce3d9c2f9eea536d6ea [file] [log] [blame]
Tony Mack31d34132013-04-15 00:02:06 -04001import commands
Tony Mack5df6c552013-04-11 21:24:17 -04002from types import StringTypes
Tony Mack29c287f2013-04-11 21:07:16 -04003from plstackapi.openstack.client import OpenStackClient
4from plstackapi.openstack.driver import OpenStackDriver
5from plstackapi.core.api.auth import auth_check
6from plstackapi.core.models import Subnet
7from plstackapi.core.api.slices import _get_slices
8
Tony Mack31d34132013-04-15 00:02:06 -04009
Tony Mack29c287f2013-04-11 21:07:16 -040010def _get_subnets(filter):
Tony Mack5df6c552013-04-11 21:24:17 -040011 if isinstance(filter, StringTypes) and filter.isdigit():
12 filter = int(filter)
Tony Mack29c287f2013-04-11 21:07:16 -040013 if isinstance(filter, int):
14 subnets = Subnet.objects.filter(id=filter)
15 elif isinstance(filter, StringTypes):
Tony Mack771f4b92013-04-12 01:49:57 -040016 # 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 Mack5df6c552013-04-11 21:24:17 -040021 elif isinstance(filter, dict):
Tony Mack29c287f2013-04-11 21:07:16 -040022 subnets = Subnet.objects.filter(**filter)
23 else:
24 subnets = []
25 return subnets
26
27def add_subnet(auth, fields):
28 driver = OpenStackDriver(client = auth_check(auth))
Tony Mack771f4b92013-04-12 01:49:57 -040029 slices = _get_slices(fields.get('slice'))
Tony Mack29c287f2013-04-11 21:07:16 -040030 if slices: fields['slice'] = slices[0]
31 subnet = Subnet(**fields)
32 # create quantum subnet
Tony Mack48952032013-04-12 11:49:34 -040033 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 Mack29c287f2013-04-11 21:07:16 -040042
43 # add subnet as interface to slice's router
44 driver.add_router_interface(subnet.slice.router_id, subnet.subnet_id)
Tony Mack4ecbb2a2013-04-15 00:03:42 -040045 add_route = 'route add -net %s dev br-ex gw 10.100.0.5' % self.cidr
46 commands.getstatusoutput(add_route)
Tony Mack29c287f2013-04-11 21:07:16 -040047 subnet.save()
48 return subnet
49
50def update_subnet(auth, subnet, **fields):
51 return
52
53def delete_subnet(auth, filter={}):
54 driver = OpenStackDriver(client = auth_check(auth))
55 subnets = Subnet.objects.filter(**filter)
56 for subnet in subnets:
57 driver.delete_router_interface(subnet.slice.router_id, subnet.subnet_id)
58 driver.delete_subnet(subnet.subnet_id)
59 subnet.delete()
Tony Mack4ecbb2a2013-04-15 00:03:42 -040060 del_route = 'route del -net %s' % self.cidr
61 commands.getstatusoutput(del_route)
Tony Mack29c287f2013-04-11 21:07:16 -040062 return 1
63
64def get_subnets(auth, filter={}):
65 client = auth_check(auth)
66 if 'slice' in filter:
67 slice = _get_slice(filter.get('slice'))
68 if slice: filter['slice'] = slice
69 subnets = Subnet.objects.filter(**filter)
70 return subnets
71
72
73