Tony Mack | 5df6c55 | 2013-04-11 21:24:17 -0400 | [diff] [blame] | 1 | from types import StringTypes |
Tony Mack | 7974861 | 2013-05-01 14:52:03 -0400 | [diff] [blame] | 2 | from django.contrib.auth import authenticate |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame^] | 3 | from openstack.client import OpenStackClient |
| 4 | from openstack.driver import OpenStackDriver |
| 5 | from core.api.auth import auth_check |
| 6 | from core.models import Sliver, Slice |
| 7 | from core.api.images import _get_images |
| 8 | from core.api.keys import _get_keys |
| 9 | from core.api.slices import _get_slices |
| 10 | from core.api.deployment_networks import _get_deployment_networks |
| 11 | from core.api.nodes import _get_nodes |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 12 | |
| 13 | |
| 14 | def _get_slivers(filter): |
Tony Mack | 5df6c55 | 2013-04-11 21:24:17 -0400 | [diff] [blame] | 15 | if isinstance(filter, StringTypes) and filter.isdigit(): |
| 16 | filter = int(filter) |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 17 | if isinstance(filter, int): |
| 18 | slivers = Sliver.objects.filter(id=filter) |
| 19 | elif isinstance(filter, StringTypes): |
| 20 | slivers = Sliver.objects.filter(name=filter) |
Tony Mack | 5df6c55 | 2013-04-11 21:24:17 -0400 | [diff] [blame] | 21 | elif isinstance(filter, dict): |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 22 | slivers = Sliver.objects.filter(**filter) |
| 23 | else: |
| 24 | slivers = [] |
| 25 | return slivers |
| 26 | |
| 27 | def add_sliver(auth, fields): |
| 28 | driver = OpenStackDriver(client = auth_check(auth)) |
Tony Mack | 7974861 | 2013-05-01 14:52:03 -0400 | [diff] [blame] | 29 | |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 30 | images = _get_images(fields.get('image')) |
| 31 | if images: fields['image'] = images[0] |
Tony Mack | 5d6598c | 2013-04-12 12:45:02 -0400 | [diff] [blame] | 32 | keys = _get_keys(fields.get('key')) |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 33 | if keys: fields['key'] = keys[0] |
| 34 | slices = _get_slices(fields.get('slice')) |
Tony Mack | 5d6598c | 2013-04-12 12:45:02 -0400 | [diff] [blame] | 35 | if slices: |
| 36 | fields['slice'] = slices[0] |
Tony Mack | 771f4b9 | 2013-04-12 01:49:57 -0400 | [diff] [blame] | 37 | deployment_networks = _get_deployment_networks(fields.get('deploymentNetwork')) |
| 38 | if deployment_networks: fields['deploymentNetwork'] = deployment_networks[0] |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 39 | nodes = _get_nodes(fields.get('node')) |
Tony Mack | 5d6598c | 2013-04-12 12:45:02 -0400 | [diff] [blame] | 40 | if nodes: fields['node'] = nodes[0] |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 41 | sliver = Sliver(**fields) |
Tony Mack | 7974861 | 2013-05-01 14:52:03 -0400 | [diff] [blame] | 42 | sliver.driver = driver |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 43 | sliver.save() |
| 44 | return sliver |
| 45 | |
| 46 | def update_sliver(auth, sliver, **fields): |
| 47 | return |
| 48 | |
| 49 | def delete_sliver(auth, filter={}): |
| 50 | driver = OpenStackDriver(client = auth_check(auth)) |
| 51 | slivers = _get_slivers(filter) |
| 52 | for sliver in slivers: |
Tony Mack | 7974861 | 2013-05-01 14:52:03 -0400 | [diff] [blame] | 53 | sliver.driver = driver |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 54 | sliver.delete() |
| 55 | return 1 |
| 56 | |
| 57 | def get_slivers(auth, filter={}): |
Tony Mack | 7974861 | 2013-05-01 14:52:03 -0400 | [diff] [blame] | 58 | user = authenticate(username=auth.get('username'), |
| 59 | password=auth.get('password')) |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 60 | if 'slice' in filter: |
| 61 | slices = _get_slices(filter.get('slice')) |
| 62 | if slices: filter['slice'] = slices[0] |
| 63 | slivers = _get_slivers(filter) |
| 64 | return slivers |
| 65 | |
| 66 | |
| 67 | |