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