blob: 16a7e0bb698236898d0fdfb7ee40827d7c5e9cb5 [file] [log] [blame]
Tony Mack5df6c552013-04-11 21:24:17 -04001from types import StringTypes
Tony Mack79748612013-05-01 14:52:03 -04002from django.contrib.auth import authenticate
Siobhan Tully73291342013-05-10 10:50:08 -04003from openstack.manager import OpenStackManager
Siobhan Tully30fd4292013-05-10 08:59:56 -04004from core.models import Sliver, Slice
5from core.api.images import _get_images
6from core.api.keys import _get_keys
7from core.api.slices import _get_slices
8from core.api.deployment_networks import _get_deployment_networks
9from core.api.nodes import _get_nodes
Tony Mack29c287f2013-04-11 21:07:16 -040010
11def _get_slivers(filter):
Tony Mack5df6c552013-04-11 21:24:17 -040012 if isinstance(filter, StringTypes) and filter.isdigit():
13 filter = int(filter)
Tony Mack29c287f2013-04-11 21:07:16 -040014 if isinstance(filter, int):
15 slivers = Sliver.objects.filter(id=filter)
16 elif isinstance(filter, StringTypes):
17 slivers = Sliver.objects.filter(name=filter)
Tony Mack5df6c552013-04-11 21:24:17 -040018 elif isinstance(filter, dict):
Tony Mack29c287f2013-04-11 21:07:16 -040019 slivers = Sliver.objects.filter(**filter)
20 else:
21 slivers = []
22 return slivers
23
24def add_sliver(auth, fields):
Tony Mack09080c62013-05-06 16:57:54 -040025 user = authenticate(username=auth.get('username'),
26 password=auth.get('password'))
27
Tony Mack29c287f2013-04-11 21:07:16 -040028 images = _get_images(fields.get('image'))
Tony Mack5d6598c2013-04-12 12:45:02 -040029 keys = _get_keys(fields.get('key'))
Tony Mack29c287f2013-04-11 21:07:16 -040030 slices = _get_slices(fields.get('slice'))
Tony Mack771f4b92013-04-12 01:49:57 -040031 deployment_networks = _get_deployment_networks(fields.get('deploymentNetwork'))
Tony Mack29c287f2013-04-11 21:07:16 -040032 nodes = _get_nodes(fields.get('node'))
Tony Mack09080c62013-05-06 16:57:54 -040033 if images: fields['image'] = images[0]
34 if keys: fields['key'] = keys[0]
35 if slices: fields['slice'] = slices[0]
36 if deployment_networks: fields['deploymentNetwork'] = deployment_networks[0]
Tony Mack5d6598c2013-04-12 12:45:02 -040037 if nodes: fields['node'] = nodes[0]
Tony Mack09080c62013-05-06 16:57:54 -040038
Tony Mack29c287f2013-04-11 21:07:16 -040039 sliver = Sliver(**fields)
Tony Mack09080c62013-05-06 16:57:54 -040040 auth['tenant'] = sliver.slice.name
41 sliver.os_manager = OpenStackManager(auth=auth, caller = user)
Tony Mack29c287f2013-04-11 21:07:16 -040042 sliver.save()
43 return sliver
44
45def update_sliver(auth, sliver, **fields):
46 return
47
48def delete_sliver(auth, filter={}):
Tony Mack09080c62013-05-06 16:57:54 -040049 user = authenticate(username=auth.get('username'),
50 password=auth.get('password'))
Tony Mack29c287f2013-04-11 21:07:16 -040051 slivers = _get_slivers(filter)
52 for sliver in slivers:
Tony Mack09080c62013-05-06 16:57:54 -040053 auth['tenant'] = sliver.slice.name
54 slice.os_manager = OpenStackManager(auth=auth, caller = user)
Tony Mack29c287f2013-04-11 21:07:16 -040055 sliver.delete()
56 return 1
57
58def get_slivers(auth, filter={}):
Tony Mack79748612013-05-01 14:52:03 -040059 user = authenticate(username=auth.get('username'),
60 password=auth.get('password'))
Tony Mack29c287f2013-04-11 21:07:16 -040061 if 'slice' in filter:
62 slices = _get_slices(filter.get('slice'))
63 if slices: filter['slice'] = slices[0]
64 slivers = _get_slivers(filter)
65 return slivers
66
67
68