blob: a5b3084a1f7793f9a9d5fa0d8557b32806513018 [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
6from plstackapi.core.api.flavors import _get_flavors
7from plstackapi.core.api.images import _get_images
8from plstackapi.core.api.keys import _get_keys
9from plstackapi.core.api.slices import _get_slices
10from plstackapi.core.api.deployment_networks import _get_deployment_networks
11from plstackapi.core.api.nodes import _get_nodes
12
13
14def _get_slivers(filter):
Tony Mack5df6c552013-04-11 21:24:17 -040015 if isinstance(filter, StringTypes) and filter.isdigit():
16 filter = int(filter)
Tony Mack29c287f2013-04-11 21:07:16 -040017 if isinstance(filter, int):
18 slivers = Sliver.objects.filter(id=filter)
19 elif isinstance(filter, StringTypes):
20 slivers = Sliver.objects.filter(name=filter)
Tony Mack5df6c552013-04-11 21:24:17 -040021 elif isinstance(filter, dict):
Tony Mack29c287f2013-04-11 21:07:16 -040022 slivers = Sliver.objects.filter(**filter)
23 else:
24 slivers = []
25 return slivers
26
27def add_sliver(auth, fields):
28 driver = OpenStackDriver(client = auth_check(auth))
29
Tony Mack771f4b92013-04-12 01:49:57 -040030 flavors = _get_flavors(fields.get('flavor'))
Tony Mack29c287f2013-04-11 21:07:16 -040031 if flavors: fields['flavor'] = flavors[0]
32 images = _get_images(fields.get('image'))
33 if images: fields['image'] = images[0]
Tony Mack5d6598c2013-04-12 12:45:02 -040034 keys = _get_keys(fields.get('key'))
Tony Mack29c287f2013-04-11 21:07:16 -040035 if keys: fields['key'] = keys[0]
36 slices = _get_slices(fields.get('slice'))
Tony Mack5d6598c2013-04-12 12:45:02 -040037 if slices:
38 fields['slice'] = slices[0]
Tony Mack771f4b92013-04-12 01:49:57 -040039 deployment_networks = _get_deployment_networks(fields.get('deploymentNetwork'))
40 if deployment_networks: fields['deploymentNetwork'] = deployment_networks[0]
Tony Mack29c287f2013-04-11 21:07:16 -040041 nodes = _get_nodes(fields.get('node'))
Tony Mack5d6598c2013-04-12 12:45:02 -040042 if nodes: fields['node'] = nodes[0]
Tony Mack29c287f2013-04-11 21:07:16 -040043 sliver = Sliver(**fields)
44 # create quantum sliver
Tony Mack221957d2013-04-13 19:08:34 -040045 instance = driver.spawn_instance(name=sliver.name,
Tony Mack29c287f2013-04-11 21:07:16 -040046 key_name = sliver.key.name,
Tony Mack221957d2013-04-13 19:08:34 -040047 flavor_id = sliver.flavor.flavor_id,
48 image_id = sliver.image.image_id,
49 hostname = sliver.node.name )
Tony Mack29c287f2013-04-11 21:07:16 -040050
Tony Mack221957d2013-04-13 19:08:34 -040051 sliver.instance_id=instance.id
Tony Mack29c287f2013-04-11 21:07:16 -040052
53 sliver.save()
54 return sliver
55
56def update_sliver(auth, sliver, **fields):
57 return
58
59def 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
67def 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