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] |
| 34 | keys = _get_keys(fields.get('get')) |
| 35 | if keys: fields['key'] = keys[0] |
| 36 | slices = _get_slices(fields.get('slice')) |
| 37 | if slices: fields['slice'] = slices[0] |
Tony Mack | 771f4b9 | 2013-04-12 01:49:57 -0400 | [diff] [blame^] | 38 | deployment_networks = _get_deployment_networks(fields.get('deploymentNetwork')) |
| 39 | if deployment_networks: fields['deploymentNetwork'] = deployment_networks[0] |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 40 | nodes = _get_nodes(fields.get('node')) |
| 41 | if nodes: fields['node'] = nodess[0] |
| 42 | |
| 43 | sliver = Sliver(**fields) |
| 44 | # create quantum sliver |
| 45 | sliver = driver.spawn_instance(name=sliver.name, |
| 46 | key_name = sliver.key.name, |
| 47 | flavor=sliver.flavor.name, |
| 48 | image = sliver.image.name, |
| 49 | hostname = sliver.node.hostname ) |
| 50 | |
| 51 | sliver.instance_id=sliver.id |
| 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 | |