blob: b255e981a97d74ba83e10af178ec1e4de5ce0a98 [file] [log] [blame]
Tony Macke4cee7a2013-04-09 12:45:32 -04001from plstackapi.openstack.client import OpenStackClient
2from plstackapi.openstack.driver import OpenStackDriver
Tony Mackf1c12242013-04-09 16:08:43 -04003from plstackapi.core.api.auth import auth_check
4from plstackapi.core.models import Key, User
Tony Macke4cee7a2013-04-09 12:45:32 -04005
6def lookup_user(fields):
7 user = None
8 if 'user' in fields:
9 if isinstance(fields['user'], int):
10 users = User.objects.filter(id=fields['user'])
11 else:
Tony Mack509b3182013-04-09 12:54:55 -040012 users = User.objects.filter(email=fields['user'])
Tony Macke4cee7a2013-04-09 12:45:32 -040013 if users:
14 user = users[0]
Tony Mack45fea9b2013-04-09 20:26:39 -040015 if not user:
16 raise Exception, "No such user: %s" % fields['user']
Tony Macke4cee7a2013-04-09 12:45:32 -040017 return user
18
19def add_key(auth, fields):
20 driver = OpenStackDriver(client = auth_check(auth))
21 user = lookup_user(fields)
22 if user: fields['user'] = user
23 key = Key(**fields)
24 nova_fields = {'name': key.name,
25 'key': key.key}
26 nova_key = driver.create_keypair(**nova_fields)
27 key.save()
28 return key
29
30def update_key(auth, id, **fields):
31 return
32
Tony Mackcc8a18d2013-04-09 12:48:19 -040033def delete_key(auth, filter={}):
Tony Macke4cee7a2013-04-09 12:45:32 -040034 driver = OpenStackDriver(client = auth_check(auth))
35 keys = Key.objects.filter(**filter)
36 for key in keys:
Tony Mackdec8f4a2013-04-09 13:08:12 -040037 driver.delete_keypair(name=key.name)
Tony Macke4cee7a2013-04-09 12:45:32 -040038 key.delete()
39 return 1
40
41def get_keys(auth, filter={}):
42 client = auth_check(auth)
43 keys = Key.objects.filter(**filter)
44 return keys
45
46
47