fix pk lookups
[plstackapi.git] / plstackapi / core / api / keys.py
1 from types import StringTypes
2 from plstackapi.openstack.client import OpenStackClient
3 from plstackapi.openstack.driver import OpenStackDriver
4 from plstackapi.core.api.auth import auth_check
5 from plstackapi.core.models import Key, User
6 from plstackapi.core.api.users import _get_users
7
8
9 def _get_keys(filter):
10     if isinstance(filter, StringTypes) and filter.isdigit():
11         filter = int(filter)
12     if isinstance(filter, int):
13         keys = Key.objects.filter(id=filter)
14     elif isinstance(filter, StringTypes):
15         keys = Key.objects.filter(name=filter)
16     elif isinstance(filter, dict):
17         keys = Key.objects.filter(**filter)
18     else:
19         keys = []
20     return keys 
21
22 def add_key(auth, fields):
23     driver = OpenStackDriver(client = auth_check(auth))
24     users = _get_users(fields.get('user')) 
25     if users: fields['user'] = users[0]    
26     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
33 def update_key(auth, id, **fields):
34     return  
35
36 def delete_key(auth, filter={}):
37     driver = OpenStackDriver(client = auth_check(auth))   
38     keys = _get_keys(filter)
39     for key in keys:
40         driver.delete_keypair(name=key.name) 
41         key.delete()
42     return 1
43
44 def get_keys(auth, filter={}):
45     client = auth_check(auth)
46     keys = _get_keys(filter)
47     return keys             
48         
49
50