blob: e7e85dd8365687997990b682eeacc72bcd4c855b [file] [log] [blame]
Tony Mack31d34132013-04-15 00:02:06 -04001import commands
Tony Mack5df6c552013-04-11 21:24:17 -04002from types import StringTypes
Tony Mack09080c62013-05-06 16:57:54 -04003from django.contrib.auth import authenticate
4from plstackapi.openstack.manager import OpenStackManager
Tony Mack29c287f2013-04-11 21:07:16 -04005from plstackapi.core.models import Subnet
6from plstackapi.core.api.slices import _get_slices
7
Tony Mack31d34132013-04-15 00:02:06 -04008
Tony Mack29c287f2013-04-11 21:07:16 -04009def _get_subnets(filter):
Tony Mack5df6c552013-04-11 21:24:17 -040010 if isinstance(filter, StringTypes) and filter.isdigit():
11 filter = int(filter)
Tony Mack29c287f2013-04-11 21:07:16 -040012 if isinstance(filter, int):
13 subnets = Subnet.objects.filter(id=filter)
14 elif isinstance(filter, StringTypes):
Tony Mack771f4b92013-04-12 01:49:57 -040015 # the name is the subnet's slice's name
16 slices = _get_slices(filter)
17 slice = None
18 if slices: slice=slices[0]
19 subnets = Subnet.objects.filter(slice=slice)
Tony Mack5df6c552013-04-11 21:24:17 -040020 elif isinstance(filter, dict):
Tony Mack29c287f2013-04-11 21:07:16 -040021 subnets = Subnet.objects.filter(**filter)
22 else:
23 subnets = []
24 return subnets
25
26def add_subnet(auth, fields):
Tony Mack09080c62013-05-06 16:57:54 -040027 user = authenticate(username=auth.get('username'),
28 password=auth.get('password'))
29
Tony Mack771f4b92013-04-12 01:49:57 -040030 slices = _get_slices(fields.get('slice'))
Tony Mack29c287f2013-04-11 21:07:16 -040031 if slices: fields['slice'] = slices[0]
32 subnet = Subnet(**fields)
Tony Mack09080c62013-05-06 16:57:54 -040033 auth['tenant'] = subnet.slice.name
34 subnet.os_manager = OpenStackManager(auth=auth, caller = user)
Tony Mack29c287f2013-04-11 21:07:16 -040035 subnet.save()
36 return subnet
37
38def update_subnet(auth, subnet, **fields):
39 return
40
41def delete_subnet(auth, filter={}):
Tony Mack09080c62013-05-06 16:57:54 -040042 user = authenticate(username=auth.get('username'),
43 password=auth.get('password'))
Tony Mack29c287f2013-04-11 21:07:16 -040044 subnets = Subnet.objects.filter(**filter)
45 for subnet in subnets:
Tony Mack09080c62013-05-06 16:57:54 -040046 auth['tenant'] = subnet.slice.name
47 subnet.os_manager = OpenStackManager(auth=auth, caller = user)
Tony Mack29c287f2013-04-11 21:07:16 -040048 subnet.delete()
Tony Mack29c287f2013-04-11 21:07:16 -040049 return 1
50
51def get_subnets(auth, filter={}):
Tony Mack09080c62013-05-06 16:57:54 -040052 user = authenticate(username=auth.get('username'),
53 password=auth.get('password'))
Tony Mack29c287f2013-04-11 21:07:16 -040054 if 'slice' in filter:
55 slice = _get_slice(filter.get('slice'))
56 if slice: filter['slice'] = slice
57 subnets = Subnet.objects.filter(**filter)
58 return subnets
59
60
61