blob: 3b70cac9a2aa4a732ee9cde4f46819442dc7c81a [file] [log] [blame]
Tony Mack5df6c552013-04-11 21:24:17 -04001from types import StringTypes
Tony Macke4cee7a2013-04-09 12:45:32 -04002from plstackapi.openstack.client import OpenStackClient
3from plstackapi.openstack.driver import OpenStackDriver
Tony Mackf1c12242013-04-09 16:08:43 -04004from plstackapi.core.api.auth import auth_check
Tony Mack0e723b92013-04-27 11:08:19 -04005from plstackapi.core.models import Key
Tony Mack29c287f2013-04-11 21:07:16 -04006from plstackapi.core.api.users import _get_users
7
8
9def _get_keys(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 keys = Key.objects.filter(id=filter)
14 elif isinstance(filter, StringTypes):
15 keys = Key.objects.filter(name=filter)
Tony Mack5df6c552013-04-11 21:24:17 -040016 elif isinstance(filter, dict):
Tony Mack29c287f2013-04-11 21:07:16 -040017 keys = Key.objects.filter(**filter)
18 else:
19 keys = []
20 return keys
Tony Macke4cee7a2013-04-09 12:45:32 -040021
22def add_key(auth, fields):
23 driver = OpenStackDriver(client = auth_check(auth))
Tony Mack29c287f2013-04-11 21:07:16 -040024 users = _get_users(fields.get('user'))
25 if users: fields['user'] = users[0]
Tony Macke4cee7a2013-04-09 12:45:32 -040026 key = Key(**fields)
27 nova_fields = {'name': key.name,
28 'key': key.key}
29 nova_key = driver.create_keypair(**nova_fields)
Tony Mack759b57a2013-04-14 21:03:31 -040030 key.key_id = nova_key.id
Tony Macke4cee7a2013-04-09 12:45:32 -040031 key.save()
32 return key
33
34def update_key(auth, id, **fields):
35 return
36
Tony Mackcc8a18d2013-04-09 12:48:19 -040037def delete_key(auth, filter={}):
Tony Macke4cee7a2013-04-09 12:45:32 -040038 driver = OpenStackDriver(client = auth_check(auth))
Tony Mack29c287f2013-04-11 21:07:16 -040039 keys = _get_keys(filter)
Tony Macke4cee7a2013-04-09 12:45:32 -040040 for key in keys:
Tony Mack759b57a2013-04-14 21:03:31 -040041 driver.delete_keypair(id=key.key_id)
Tony Macke4cee7a2013-04-09 12:45:32 -040042 key.delete()
43 return 1
44
45def get_keys(auth, filter={}):
46 client = auth_check(auth)
Tony Mack29c287f2013-04-11 21:07:16 -040047 keys = _get_keys(filter)
Tony Macke4cee7a2013-04-09 12:45:32 -040048 return keys
49
50
51