Tony Mack | e4cee7a | 2013-04-09 12:45:32 -0400 | [diff] [blame] | 1 | from plstackapi.openstack.client import OpenStackClient |
| 2 | from plstackapi.openstack.driver import OpenStackDriver |
Tony Mack | f1c1224 | 2013-04-09 16:08:43 -0400 | [diff] [blame] | 3 | from plstackapi.core.api.auth import auth_check |
| 4 | from plstackapi.core.models import Key, User |
Tony Mack | e4cee7a | 2013-04-09 12:45:32 -0400 | [diff] [blame] | 5 | |
| 6 | def 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 Mack | 509b318 | 2013-04-09 12:54:55 -0400 | [diff] [blame] | 12 | users = User.objects.filter(email=fields['user']) |
Tony Mack | e4cee7a | 2013-04-09 12:45:32 -0400 | [diff] [blame] | 13 | if users: |
| 14 | user = users[0] |
| 15 | return user |
| 16 | |
| 17 | def add_key(auth, fields): |
| 18 | driver = OpenStackDriver(client = auth_check(auth)) |
| 19 | user = lookup_user(fields) |
| 20 | if user: fields['user'] = user |
| 21 | key = Key(**fields) |
| 22 | nova_fields = {'name': key.name, |
| 23 | 'key': key.key} |
| 24 | nova_key = driver.create_keypair(**nova_fields) |
| 25 | key.save() |
| 26 | return key |
| 27 | |
| 28 | def update_key(auth, id, **fields): |
| 29 | return |
| 30 | |
Tony Mack | cc8a18d | 2013-04-09 12:48:19 -0400 | [diff] [blame] | 31 | def delete_key(auth, filter={}): |
Tony Mack | e4cee7a | 2013-04-09 12:45:32 -0400 | [diff] [blame] | 32 | driver = OpenStackDriver(client = auth_check(auth)) |
| 33 | keys = Key.objects.filter(**filter) |
| 34 | for key in keys: |
Tony Mack | dec8f4a | 2013-04-09 13:08:12 -0400 | [diff] [blame] | 35 | driver.delete_keypair(name=key.name) |
Tony Mack | e4cee7a | 2013-04-09 12:45:32 -0400 | [diff] [blame] | 36 | key.delete() |
| 37 | return 1 |
| 38 | |
| 39 | def get_keys(auth, filter={}): |
| 40 | client = auth_check(auth) |
| 41 | keys = Key.objects.filter(**filter) |
| 42 | return keys |
| 43 | |
| 44 | |
| 45 | |