blob: 66af59e5540ed5c02047fda9d71cae960c68642b [file] [log] [blame]
Tony Mack5df6c552013-04-11 21:24:17 -04001from types import StringTypes
Tony Mack09080c62013-05-06 16:57:54 -04002from django.contrib.auth import authenticate
3from plstackapi.openstack.manager import OpenStackManager
Tony Mack0e723b92013-04-27 11:08:19 -04004from plstackapi.core.models import Key
Tony Mack29c287f2013-04-11 21:07:16 -04005from plstackapi.core.api.users import _get_users
6
7
8def _get_keys(filter):
Tony Mack5df6c552013-04-11 21:24:17 -04009 if isinstance(filter, StringTypes) and filter.isdigit():
10 filter = int(filter)
Tony Mack29c287f2013-04-11 21:07:16 -040011 if isinstance(filter, int):
12 keys = Key.objects.filter(id=filter)
13 elif isinstance(filter, StringTypes):
14 keys = Key.objects.filter(name=filter)
Tony Mack5df6c552013-04-11 21:24:17 -040015 elif isinstance(filter, dict):
Tony Mack29c287f2013-04-11 21:07:16 -040016 keys = Key.objects.filter(**filter)
17 else:
18 keys = []
19 return keys
Tony Macke4cee7a2013-04-09 12:45:32 -040020
21def add_key(auth, fields):
Tony Mack09080c62013-05-06 16:57:54 -040022 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 Mack29c287f2013-04-11 21:07:16 -040028 users = _get_users(fields.get('user'))
29 if users: fields['user'] = users[0]
Tony Mack09080c62013-05-06 16:57:54 -040030 # save
Tony Macke4cee7a2013-04-09 12:45:32 -040031 key = Key(**fields)
Tony Mack09080c62013-05-06 16:57:54 -040032 key.os_manager = manager
Tony Macke4cee7a2013-04-09 12:45:32 -040033 key.save()
34 return key
35
36def update_key(auth, id, **fields):
37 return
38
Tony Mackcc8a18d2013-04-09 12:48:19 -040039def delete_key(auth, filter={}):
Tony Mack09080c62013-05-06 16:57:54 -040040 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 Mack29c287f2013-04-11 21:07:16 -040045 keys = _get_keys(filter)
Tony Macke4cee7a2013-04-09 12:45:32 -040046 for key in keys:
Tony Mack09080c62013-05-06 16:57:54 -040047 key.os_manager = manager
Tony Macke4cee7a2013-04-09 12:45:32 -040048 key.delete()
49 return 1
50
51def get_keys(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 keys = _get_keys(filter)
Tony Macke4cee7a2013-04-09 12:45:32 -040055 return keys
56
57
58