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