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