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 |
Tony Mack | 09080c6 | 2013-05-06 16:57:54 -0400 | [diff] [blame] | 3 | from django.contrib.auth import authenticate |
| 4 | from plstackapi.openstack.manager import OpenStackManager |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 5 | from plstackapi.core.models import Subnet |
| 6 | from plstackapi.core.api.slices import _get_slices |
| 7 | |
Tony Mack | 31d3413 | 2013-04-15 00:02:06 -0400 | [diff] [blame] | 8 | |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 9 | def _get_subnets(filter): |
Tony Mack | 5df6c55 | 2013-04-11 21:24:17 -0400 | [diff] [blame] | 10 | if isinstance(filter, StringTypes) and filter.isdigit(): |
| 11 | filter = int(filter) |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 12 | if isinstance(filter, int): |
| 13 | subnets = Subnet.objects.filter(id=filter) |
| 14 | elif isinstance(filter, StringTypes): |
Tony Mack | 771f4b9 | 2013-04-12 01:49:57 -0400 | [diff] [blame] | 15 | # 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 Mack | 5df6c55 | 2013-04-11 21:24:17 -0400 | [diff] [blame] | 20 | elif isinstance(filter, dict): |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 21 | subnets = Subnet.objects.filter(**filter) |
| 22 | else: |
| 23 | subnets = [] |
| 24 | return subnets |
| 25 | |
| 26 | def add_subnet(auth, fields): |
Tony Mack | 09080c6 | 2013-05-06 16:57:54 -0400 | [diff] [blame] | 27 | user = authenticate(username=auth.get('username'), |
| 28 | password=auth.get('password')) |
| 29 | |
Tony Mack | 771f4b9 | 2013-04-12 01:49:57 -0400 | [diff] [blame] | 30 | slices = _get_slices(fields.get('slice')) |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 31 | if slices: fields['slice'] = slices[0] |
| 32 | subnet = Subnet(**fields) |
Tony Mack | 09080c6 | 2013-05-06 16:57:54 -0400 | [diff] [blame] | 33 | auth['tenant'] = subnet.slice.name |
| 34 | subnet.os_manager = OpenStackManager(auth=auth, caller = user) |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 35 | subnet.save() |
| 36 | return subnet |
| 37 | |
| 38 | def update_subnet(auth, subnet, **fields): |
| 39 | return |
| 40 | |
| 41 | def delete_subnet(auth, filter={}): |
Tony Mack | 09080c6 | 2013-05-06 16:57:54 -0400 | [diff] [blame] | 42 | user = authenticate(username=auth.get('username'), |
| 43 | password=auth.get('password')) |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 44 | subnets = Subnet.objects.filter(**filter) |
| 45 | for subnet in subnets: |
Tony Mack | 09080c6 | 2013-05-06 16:57:54 -0400 | [diff] [blame] | 46 | auth['tenant'] = subnet.slice.name |
| 47 | subnet.os_manager = OpenStackManager(auth=auth, caller = user) |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 48 | subnet.delete() |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 49 | return 1 |
| 50 | |
| 51 | def get_subnets(auth, filter={}): |
Tony Mack | 09080c6 | 2013-05-06 16:57:54 -0400 | [diff] [blame] | 52 | user = authenticate(username=auth.get('username'), |
| 53 | password=auth.get('password')) |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 54 | 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 | |