Tony Mack | 771f4b9 | 2013-04-12 01:49:57 -0400 | [diff] [blame] | 1 | import re |
Tony Mack | 5df6c55 | 2013-04-11 21:24:17 -0400 | [diff] [blame] | 2 | from types import StringTypes |
Tony Mack | b904270 | 2013-05-02 15:44:49 -0400 | [diff] [blame] | 3 | from django.contrib.auth import authenticate |
Siobhan Tully | 7329134 | 2013-05-10 10:50:08 -0400 | [diff] [blame^] | 4 | from openstack.manager import OpenStackManager |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 5 | from core.models import Slice |
| 6 | from core.api.sites import _get_sites |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 7 | |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 8 | def _get_slices(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 | slices = Slice.objects.filter(id=filter) |
| 13 | elif isinstance(filter, StringTypes): |
| 14 | slices = Slice.objects.filter(name=filter) |
Tony Mack | 5df6c55 | 2013-04-11 21:24:17 -0400 | [diff] [blame] | 15 | elif isinstance(filter, dict): |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 16 | slices = Slice.objects.filter(**filter) |
| 17 | else: |
| 18 | slices = [] |
| 19 | return slices |
| 20 | |
Tony Mack | 3a1af41 | 2013-04-09 22:36:27 -0400 | [diff] [blame] | 21 | |
Tony Mack | 3a1af41 | 2013-04-09 22:36:27 -0400 | [diff] [blame] | 22 | def add_slice(auth, fields): |
Tony Mack | 09080c6 | 2013-05-06 16:57:54 -0400 | [diff] [blame] | 23 | user = authenticate(username=auth.get('username'), |
| 24 | password=auth.get('password')) |
| 25 | auth['tenant'] = user.site.login_base |
| 26 | |
Tony Mack | 771f4b9 | 2013-04-12 01:49:57 -0400 | [diff] [blame] | 27 | login_base = fields['name'][:fields['name'].find('_')] |
| 28 | sites = _get_sites(login_base) |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 29 | if sites: fields['site'] = sites[0] |
Tony Mack | 3a1af41 | 2013-04-09 22:36:27 -0400 | [diff] [blame] | 30 | slice = Slice(**fields) |
Tony Mack | 09080c6 | 2013-05-06 16:57:54 -0400 | [diff] [blame] | 31 | slice.os_manager = OpenStackManager(auth=auth, caller = user) |
Tony Mack | 3a1af41 | 2013-04-09 22:36:27 -0400 | [diff] [blame] | 32 | slice.save() |
| 33 | return slice |
| 34 | |
| 35 | def update_slice(auth, id, **fields): |
Tony Mack | 09080c6 | 2013-05-06 16:57:54 -0400 | [diff] [blame] | 36 | user = authenticate(username=auth.get('username'), |
| 37 | password=auth.get('password')) |
| 38 | auth['tenant'] = user.site.login_base |
| 39 | |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 40 | slices = _get_slices(id) |
Tony Mack | 3a1af41 | 2013-04-09 22:36:27 -0400 | [diff] [blame] | 41 | if not slices: |
| 42 | return |
Tony Mack | 3a1af41 | 2013-04-09 22:36:27 -0400 | [diff] [blame] | 43 | slice = slices[0] |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 44 | sites = _get_sites(fields.get('site')) |
| 45 | if sites: fields['site'] = sites[0] |
Tony Mack | 09080c6 | 2013-05-06 16:57:54 -0400 | [diff] [blame] | 46 | |
| 47 | slice.os_manager = OpenStackManager(auth=auth, caller = user) |
| 48 | for (k,v) in fields.items(): |
| 49 | setattr(slice, k, v) |
| 50 | slice.save() |
Tony Mack | 3a1af41 | 2013-04-09 22:36:27 -0400 | [diff] [blame] | 51 | |
| 52 | return slice |
| 53 | |
| 54 | def delete_slice(auth, filter={}): |
Tony Mack | 09080c6 | 2013-05-06 16:57:54 -0400 | [diff] [blame] | 55 | user = authenticate(username=auth.get('username'), |
| 56 | password=auth.get('password')) |
| 57 | auth['tenant'] = user.site.login_base |
| 58 | slices = _get_slices(filter) |
Tony Mack | 3a1af41 | 2013-04-09 22:36:27 -0400 | [diff] [blame] | 59 | for slice in slices: |
Tony Mack | 09080c6 | 2013-05-06 16:57:54 -0400 | [diff] [blame] | 60 | slice.os_manager = OpenStackManager(auth=auth, caller = user) |
Tony Mack | 3a1af41 | 2013-04-09 22:36:27 -0400 | [diff] [blame] | 61 | slice.delete() |
| 62 | return 1 |
| 63 | |
| 64 | def get_slices(auth, filter={}): |
Tony Mack | b904270 | 2013-05-02 15:44:49 -0400 | [diff] [blame] | 65 | user = authenticate(username=auth.get('username'), |
| 66 | password=auth.get('password')) |
Tony Mack | d95d1aa | 2013-04-10 08:02:02 -0400 | [diff] [blame] | 67 | if 'site' in filter: |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 68 | sites = _get_sites(filter.get('site')) |
| 69 | if sites: filter['site'] = sites[0] |
| 70 | slices = _get_slices(filter) |
Tony Mack | 3a1af41 | 2013-04-09 22:36:27 -0400 | [diff] [blame] | 71 | return slices |
| 72 | |
| 73 | |
| 74 | |