blob: 42ffe4622b7ae3c8a4de81019e7b9fa708d434a4 [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
5from plstackapi.core.models import Key, User
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)
30 key.save()
31 return key
32
33def update_key(auth, id, **fields):
34 return
35
Tony Mackcc8a18d2013-04-09 12:48:19 -040036def delete_key(auth, filter={}):
Tony Macke4cee7a2013-04-09 12:45:32 -040037 driver = OpenStackDriver(client = auth_check(auth))
Tony Mack29c287f2013-04-11 21:07:16 -040038 keys = _get_keys(filter)
Tony Macke4cee7a2013-04-09 12:45:32 -040039 for key in keys:
Tony Mackdec8f4a2013-04-09 13:08:12 -040040 driver.delete_keypair(name=key.name)
Tony Macke4cee7a2013-04-09 12:45:32 -040041 key.delete()
42 return 1
43
44def get_keys(auth, filter={}):
45 client = auth_check(auth)
Tony Mack29c287f2013-04-11 21:07:16 -040046 keys = _get_keys(filter)
Tony Macke4cee7a2013-04-09 12:45:32 -040047 return keys
48
49
50