Tony Mack | 5df6c55 | 2013-04-11 21:24:17 -0400 | [diff] [blame] | 1 | from types import StringTypes |
Tony Mack | 09080c6 | 2013-05-06 16:57:54 -0400 | [diff] [blame] | 2 | from django.contrib.auth import authenticate |
Siobhan Tully | 7329134 | 2013-05-10 10:50:08 -0400 | [diff] [blame^] | 3 | from openstack.manager import OpenStackManager |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 4 | from core.models import Key |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 5 | from core.api.users import _get_users |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 6 | |
| 7 | |
| 8 | def _get_keys(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 | keys = Key.objects.filter(id=filter) |
| 13 | elif isinstance(filter, StringTypes): |
| 14 | keys = Key.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 | keys = Key.objects.filter(**filter) |
| 17 | else: |
| 18 | keys = [] |
| 19 | return keys |
Tony Mack | e4cee7a | 2013-04-09 12:45:32 -0400 | [diff] [blame] | 20 | |
| 21 | def add_key(auth, fields): |
Tony Mack | 09080c6 | 2013-05-06 16:57:54 -0400 | [diff] [blame] | 22 | user = authenticate(username=auth.get('username'), |
| 23 | password=auth.get('password')) |
| 24 | auth['tenant'] = user.site.login_base |
| 25 | manager = OpenStackManager(auth=auth, caller = user) |
| 26 | |
| 27 | # look up user object |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 28 | users = _get_users(fields.get('user')) |
| 29 | if users: fields['user'] = users[0] |
Tony Mack | 09080c6 | 2013-05-06 16:57:54 -0400 | [diff] [blame] | 30 | # save |
Tony Mack | e4cee7a | 2013-04-09 12:45:32 -0400 | [diff] [blame] | 31 | key = Key(**fields) |
Tony Mack | 09080c6 | 2013-05-06 16:57:54 -0400 | [diff] [blame] | 32 | key.os_manager = manager |
Tony Mack | e4cee7a | 2013-04-09 12:45:32 -0400 | [diff] [blame] | 33 | key.save() |
| 34 | return key |
| 35 | |
| 36 | def update_key(auth, id, **fields): |
| 37 | return |
| 38 | |
Tony Mack | cc8a18d | 2013-04-09 12:48:19 -0400 | [diff] [blame] | 39 | def delete_key(auth, filter={}): |
Tony Mack | 09080c6 | 2013-05-06 16:57:54 -0400 | [diff] [blame] | 40 | user = authenticate(username=auth.get('username'), |
| 41 | password=auth.get('password')) |
| 42 | auth['tenant'] = user.site.login_base |
| 43 | manager = OpenStackManager(auth=auth, caller = user) |
| 44 | |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 45 | keys = _get_keys(filter) |
Tony Mack | e4cee7a | 2013-04-09 12:45:32 -0400 | [diff] [blame] | 46 | for key in keys: |
Tony Mack | 09080c6 | 2013-05-06 16:57:54 -0400 | [diff] [blame] | 47 | key.os_manager = manager |
Tony Mack | e4cee7a | 2013-04-09 12:45:32 -0400 | [diff] [blame] | 48 | key.delete() |
| 49 | return 1 |
| 50 | |
| 51 | def get_keys(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 | keys = _get_keys(filter) |
Tony Mack | e4cee7a | 2013-04-09 12:45:32 -0400 | [diff] [blame] | 55 | return keys |
| 56 | |
| 57 | |
| 58 | |