Tony Mack | 5df6c55 | 2013-04-11 21:24:17 -0400 | [diff] [blame] | 1 | from types import StringTypes |
Tony Mack | e4cee7a | 2013-04-09 12:45:32 -0400 | [diff] [blame] | 2 | from plstackapi.openstack.client import OpenStackClient |
| 3 | from plstackapi.openstack.driver import OpenStackDriver |
Tony Mack | f1c1224 | 2013-04-09 16:08:43 -0400 | [diff] [blame] | 4 | from plstackapi.core.api.auth import auth_check |
| 5 | from plstackapi.core.models import Key, User |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 6 | from plstackapi.core.api.users import _get_users |
| 7 | |
| 8 | |
| 9 | def _get_keys(filter): |
Tony Mack | 5df6c55 | 2013-04-11 21:24:17 -0400 | [diff] [blame] | 10 | if isinstance(filter, StringTypes) and filter.isdigit(): |
| 11 | filter = int(filter) |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 12 | if isinstance(filter, int): |
| 13 | keys = Key.objects.filter(id=filter) |
| 14 | elif isinstance(filter, StringTypes): |
| 15 | keys = Key.objects.filter(name=filter) |
Tony Mack | 5df6c55 | 2013-04-11 21:24:17 -0400 | [diff] [blame] | 16 | elif isinstance(filter, dict): |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 17 | keys = Key.objects.filter(**filter) |
| 18 | else: |
| 19 | keys = [] |
| 20 | return keys |
Tony Mack | e4cee7a | 2013-04-09 12:45:32 -0400 | [diff] [blame] | 21 | |
| 22 | def add_key(auth, fields): |
| 23 | driver = OpenStackDriver(client = auth_check(auth)) |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 24 | users = _get_users(fields.get('user')) |
| 25 | if users: fields['user'] = users[0] |
Tony Mack | e4cee7a | 2013-04-09 12:45:32 -0400 | [diff] [blame] | 26 | key = Key(**fields) |
| 27 | nova_fields = {'name': key.name, |
| 28 | 'key': key.key} |
| 29 | nova_key = driver.create_keypair(**nova_fields) |
Tony Mack | 759b57a | 2013-04-14 21:03:31 -0400 | [diff] [blame] | 30 | key.key_id = nova_key.id |
Tony Mack | e4cee7a | 2013-04-09 12:45:32 -0400 | [diff] [blame] | 31 | key.save() |
| 32 | return key |
| 33 | |
| 34 | def update_key(auth, id, **fields): |
| 35 | return |
| 36 | |
Tony Mack | cc8a18d | 2013-04-09 12:48:19 -0400 | [diff] [blame] | 37 | def delete_key(auth, filter={}): |
Tony Mack | e4cee7a | 2013-04-09 12:45:32 -0400 | [diff] [blame] | 38 | driver = OpenStackDriver(client = auth_check(auth)) |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 39 | keys = _get_keys(filter) |
Tony Mack | e4cee7a | 2013-04-09 12:45:32 -0400 | [diff] [blame] | 40 | for key in keys: |
Tony Mack | 759b57a | 2013-04-14 21:03:31 -0400 | [diff] [blame] | 41 | driver.delete_keypair(id=key.key_id) |
Tony Mack | e4cee7a | 2013-04-09 12:45:32 -0400 | [diff] [blame] | 42 | key.delete() |
| 43 | return 1 |
| 44 | |
| 45 | def get_keys(auth, filter={}): |
| 46 | client = auth_check(auth) |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 47 | keys = _get_keys(filter) |
Tony Mack | e4cee7a | 2013-04-09 12:45:32 -0400 | [diff] [blame] | 48 | return keys |
| 49 | |
| 50 | |
| 51 | |