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) |
| 30 | key.save() |
| 31 | return key |
| 32 | |
| 33 | def update_key(auth, id, **fields): |
| 34 | return |
| 35 | |
Tony Mack | cc8a18d | 2013-04-09 12:48:19 -0400 | [diff] [blame] | 36 | def delete_key(auth, filter={}): |
Tony Mack | e4cee7a | 2013-04-09 12:45:32 -0400 | [diff] [blame] | 37 | driver = OpenStackDriver(client = auth_check(auth)) |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 38 | keys = _get_keys(filter) |
Tony Mack | e4cee7a | 2013-04-09 12:45:32 -0400 | [diff] [blame] | 39 | for key in keys: |
Tony Mack | dec8f4a | 2013-04-09 13:08:12 -0400 | [diff] [blame] | 40 | driver.delete_keypair(name=key.name) |
Tony Mack | e4cee7a | 2013-04-09 12:45:32 -0400 | [diff] [blame] | 41 | key.delete() |
| 42 | return 1 |
| 43 | |
| 44 | def get_keys(auth, filter={}): |
| 45 | client = auth_check(auth) |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 46 | keys = _get_keys(filter) |
Tony Mack | e4cee7a | 2013-04-09 12:45:32 -0400 | [diff] [blame] | 47 | return keys |
| 48 | |
| 49 | |
| 50 | |