Tony Mack | 5df6c55 | 2013-04-11 21:24:17 -0400 | [diff] [blame] | 1 | from types import StringTypes |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame^] | 2 | from openstack.client import OpenStackClient |
| 3 | from openstack.driver import OpenStackDriver |
| 4 | from core.models import Key |
| 5 | from core.api.auth import auth_check |
| 6 | from core.api.users import _get_users |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 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) |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame^] | 30 | key.nkey_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: |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame^] | 41 | driver.delete_keypair(id=key.nkey_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 | |