Tony Mack | 5df6c55 | 2013-04-11 21:24:17 -0400 | [diff] [blame] | 1 | from types import StringTypes |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 2 | from plstackapi.openstack.client import OpenStackClient |
| 3 | from plstackapi.openstack.driver import OpenStackDriver |
| 4 | from plstackapi.core.api.auth import auth_check |
| 5 | from plstackapi.core.models import Sliver, Slice |
| 6 | from plstackapi.core.api.flavors import _get_flavors |
| 7 | from plstackapi.core.api.images import _get_images |
| 8 | from plstackapi.core.api.keys import _get_keys |
| 9 | from plstackapi.core.api.slices import _get_slices |
| 10 | from plstackapi.core.api.deployment_networks import _get_deployment_networks |
| 11 | from plstackapi.core.api.nodes import _get_nodes |
| 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)) |
| 29 | |
Tony Mack | 771f4b9 | 2013-04-12 01:49:57 -0400 | [diff] [blame] | 30 | flavors = _get_flavors(fields.get('flavor')) |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 31 | if flavors: fields['flavor'] = flavors[0] |
| 32 | images = _get_images(fields.get('image')) |
| 33 | if images: fields['image'] = images[0] |
Tony Mack | 5d6598c | 2013-04-12 12:45:02 -0400 | [diff] [blame] | 34 | keys = _get_keys(fields.get('key')) |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 35 | if keys: fields['key'] = keys[0] |
| 36 | slices = _get_slices(fields.get('slice')) |
Tony Mack | 5d6598c | 2013-04-12 12:45:02 -0400 | [diff] [blame] | 37 | if slices: |
| 38 | fields['slice'] = slices[0] |
Tony Mack | 771f4b9 | 2013-04-12 01:49:57 -0400 | [diff] [blame] | 39 | deployment_networks = _get_deployment_networks(fields.get('deploymentNetwork')) |
| 40 | if deployment_networks: fields['deploymentNetwork'] = deployment_networks[0] |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 41 | nodes = _get_nodes(fields.get('node')) |
Tony Mack | 5d6598c | 2013-04-12 12:45:02 -0400 | [diff] [blame] | 42 | if nodes: fields['node'] = nodes[0] |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 43 | sliver = Sliver(**fields) |
| 44 | # create quantum sliver |
Tony Mack | 221957d | 2013-04-13 19:08:34 -0400 | [diff] [blame] | 45 | instance = driver.spawn_instance(name=sliver.name, |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 46 | key_name = sliver.key.name, |
Tony Mack | 221957d | 2013-04-13 19:08:34 -0400 | [diff] [blame] | 47 | flavor_id = sliver.flavor.flavor_id, |
| 48 | image_id = sliver.image.image_id, |
| 49 | hostname = sliver.node.name ) |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 50 | |
Tony Mack | 221957d | 2013-04-13 19:08:34 -0400 | [diff] [blame] | 51 | sliver.instance_id=instance.id |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 52 | |
| 53 | sliver.save() |
| 54 | return sliver |
| 55 | |
| 56 | def update_sliver(auth, sliver, **fields): |
| 57 | return |
| 58 | |
| 59 | def delete_sliver(auth, filter={}): |
| 60 | driver = OpenStackDriver(client = auth_check(auth)) |
| 61 | slivers = _get_slivers(filter) |
| 62 | for sliver in slivers: |
| 63 | driver.destroy_instance(sliver.sliver_id) |
| 64 | sliver.delete() |
| 65 | return 1 |
| 66 | |
| 67 | def get_slivers(auth, filter={}): |
| 68 | client = auth_check(auth) |
| 69 | if 'slice' in filter: |
| 70 | slices = _get_slices(filter.get('slice')) |
| 71 | if slices: filter['slice'] = slices[0] |
| 72 | slivers = _get_slivers(filter) |
| 73 | return slivers |
| 74 | |
| 75 | |
| 76 | |