blob: da5d17f4f87801973d16d9f02ddc03d5ef4d6e96 [file] [log] [blame]
Tony Mack5df6c552013-04-11 21:24:17 -04001from types import StringTypes
Tony Mack29c287f2013-04-11 21:07:16 -04002from plstackapi.openstack.client import OpenStackClient
3from plstackapi.openstack.driver import OpenStackDriver
4from plstackapi.core.api.auth import auth_check
5from plstackapi.core.models import Sliver, Slice
Tony Mack29c287f2013-04-11 21:07:16 -04006from plstackapi.core.api.images import _get_images
7from plstackapi.core.api.keys import _get_keys
8from plstackapi.core.api.slices import _get_slices
9from plstackapi.core.api.deployment_networks import _get_deployment_networks
10from plstackapi.core.api.nodes import _get_nodes
11
12
13def _get_slivers(filter):
Tony Mack5df6c552013-04-11 21:24:17 -040014 if isinstance(filter, StringTypes) and filter.isdigit():
15 filter = int(filter)
Tony Mack29c287f2013-04-11 21:07:16 -040016 if isinstance(filter, int):
17 slivers = Sliver.objects.filter(id=filter)
18 elif isinstance(filter, StringTypes):
19 slivers = Sliver.objects.filter(name=filter)
Tony Mack5df6c552013-04-11 21:24:17 -040020 elif isinstance(filter, dict):
Tony Mack29c287f2013-04-11 21:07:16 -040021 slivers = Sliver.objects.filter(**filter)
22 else:
23 slivers = []
24 return slivers
25
26def add_sliver(auth, fields):
27 driver = OpenStackDriver(client = auth_check(auth))
28
Tony Mack29c287f2013-04-11 21:07:16 -040029 images = _get_images(fields.get('image'))
30 if images: fields['image'] = images[0]
Tony Mack5d6598c2013-04-12 12:45:02 -040031 keys = _get_keys(fields.get('key'))
Tony Mack29c287f2013-04-11 21:07:16 -040032 if keys: fields['key'] = keys[0]
33 slices = _get_slices(fields.get('slice'))
Tony Mack5d6598c2013-04-12 12:45:02 -040034 if slices:
35 fields['slice'] = slices[0]
Tony Mack771f4b92013-04-12 01:49:57 -040036 deployment_networks = _get_deployment_networks(fields.get('deploymentNetwork'))
37 if deployment_networks: fields['deploymentNetwork'] = deployment_networks[0]
Tony Mack29c287f2013-04-11 21:07:16 -040038 nodes = _get_nodes(fields.get('node'))
Tony Mack5d6598c2013-04-12 12:45:02 -040039 if nodes: fields['node'] = nodes[0]
Tony Mack29c287f2013-04-11 21:07:16 -040040 sliver = Sliver(**fields)
41 # create quantum sliver
Tony Mack221957d2013-04-13 19:08:34 -040042 instance = driver.spawn_instance(name=sliver.name,
Tony Mack29c287f2013-04-11 21:07:16 -040043 key_name = sliver.key.name,
Tony Mack221957d2013-04-13 19:08:34 -040044 image_id = sliver.image.image_id,
45 hostname = sliver.node.name )
Tony Mack29c287f2013-04-11 21:07:16 -040046
Tony Mack221957d2013-04-13 19:08:34 -040047 sliver.instance_id=instance.id
Tony Mack29c287f2013-04-11 21:07:16 -040048
49 sliver.save()
50 return sliver
51
52def update_sliver(auth, sliver, **fields):
53 return
54
55def delete_sliver(auth, filter={}):
56 driver = OpenStackDriver(client = auth_check(auth))
57 slivers = _get_slivers(filter)
58 for sliver in slivers:
59 driver.destroy_instance(sliver.sliver_id)
60 sliver.delete()
61 return 1
62
63def get_slivers(auth, filter={}):
64 client = auth_check(auth)
65 if 'slice' in filter:
66 slices = _get_slices(filter.get('slice'))
67 if slices: filter['slice'] = slices[0]
68 slivers = _get_slivers(filter)
69 return slivers
70
71
72