blob: 2ce24c7034224a95672341f016ed355f58d01351 [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
Siobhan Tully30fd4292013-05-10 08:59:56 -04006from core.api.slices import _get_slices
7from core.api.deployment_networks import _get_deployment_networks
8from core.api.nodes import _get_nodes
Tony Mack29c287f2013-04-11 21:07:16 -04009
10def _get_slivers(filter):
Tony Mack5df6c552013-04-11 21:24:17 -040011 if isinstance(filter, StringTypes) and filter.isdigit():
12 filter = int(filter)
Tony Mack29c287f2013-04-11 21:07:16 -040013 if isinstance(filter, int):
14 slivers = Sliver.objects.filter(id=filter)
15 elif isinstance(filter, StringTypes):
16 slivers = Sliver.objects.filter(name=filter)
Tony Mack5df6c552013-04-11 21:24:17 -040017 elif isinstance(filter, dict):
Tony Mack29c287f2013-04-11 21:07:16 -040018 slivers = Sliver.objects.filter(**filter)
19 else:
20 slivers = []
21 return slivers
22
23def add_sliver(auth, fields):
Tony Mack09080c62013-05-06 16:57:54 -040024 user = authenticate(username=auth.get('username'),
25 password=auth.get('password'))
26
Tony Mack29c287f2013-04-11 21:07:16 -040027 images = _get_images(fields.get('image'))
Tony Mack29c287f2013-04-11 21:07:16 -040028 slices = _get_slices(fields.get('slice'))
Tony Mack771f4b92013-04-12 01:49:57 -040029 deployment_networks = _get_deployment_networks(fields.get('deploymentNetwork'))
Tony Mack29c287f2013-04-11 21:07:16 -040030 nodes = _get_nodes(fields.get('node'))
Tony Mack09080c62013-05-06 16:57:54 -040031 if images: fields['image'] = images[0]
Tony Mack09080c62013-05-06 16:57:54 -040032 if slices: fields['slice'] = slices[0]
33 if deployment_networks: fields['deploymentNetwork'] = deployment_networks[0]
Tony Mack5d6598c2013-04-12 12:45:02 -040034 if nodes: fields['node'] = nodes[0]
Tony Mack09080c62013-05-06 16:57:54 -040035
Tony Mack29c287f2013-04-11 21:07:16 -040036 sliver = Sliver(**fields)
Tony Mack09080c62013-05-06 16:57:54 -040037 auth['tenant'] = sliver.slice.name
38 sliver.os_manager = OpenStackManager(auth=auth, caller = user)
Tony Mack29c287f2013-04-11 21:07:16 -040039 sliver.save()
40 return sliver
41
42def update_sliver(auth, sliver, **fields):
43 return
44
45def delete_sliver(auth, filter={}):
Tony Mack09080c62013-05-06 16:57:54 -040046 user = authenticate(username=auth.get('username'),
47 password=auth.get('password'))
Tony Mack29c287f2013-04-11 21:07:16 -040048 slivers = _get_slivers(filter)
49 for sliver in slivers:
Tony Mack09080c62013-05-06 16:57:54 -040050 auth['tenant'] = sliver.slice.name
51 slice.os_manager = OpenStackManager(auth=auth, caller = user)
Tony Mack29c287f2013-04-11 21:07:16 -040052 sliver.delete()
53 return 1
54
55def get_slivers(auth, filter={}):
Tony Mack79748612013-05-01 14:52:03 -040056 user = authenticate(username=auth.get('username'),
57 password=auth.get('password'))
Tony Mack29c287f2013-04-11 21:07:16 -040058 if 'slice' in filter:
59 slices = _get_slices(filter.get('slice'))
60 if slices: filter['slice'] = slices[0]
61 slivers = _get_slivers(filter)
62 return slivers
63
64
65