blob: f832daee3926612081da09d949bfd9bb672596c5 [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
Tony Mack09080c62013-05-06 16:57:54 -04003from plstackapi.openstack.manager import OpenStackManager
Tony Mack29c287f2013-04-11 21:07:16 -04004from plstackapi.core.models import Sliver, Slice
Tony Mack29c287f2013-04-11 21:07:16 -04005from plstackapi.core.api.images import _get_images
6from plstackapi.core.api.keys import _get_keys
7from plstackapi.core.api.slices import _get_slices
8from plstackapi.core.api.deployment_networks import _get_deployment_networks
9from plstackapi.core.api.nodes import _get_nodes
10
11
12def _get_slivers(filter):
Tony Mack5df6c552013-04-11 21:24:17 -040013 if isinstance(filter, StringTypes) and filter.isdigit():
14 filter = int(filter)
Tony Mack29c287f2013-04-11 21:07:16 -040015 if isinstance(filter, int):
16 slivers = Sliver.objects.filter(id=filter)
17 elif isinstance(filter, StringTypes):
18 slivers = Sliver.objects.filter(name=filter)
Tony Mack5df6c552013-04-11 21:24:17 -040019 elif isinstance(filter, dict):
Tony Mack29c287f2013-04-11 21:07:16 -040020 slivers = Sliver.objects.filter(**filter)
21 else:
22 slivers = []
23 return slivers
24
25def add_sliver(auth, fields):
Tony Mack09080c62013-05-06 16:57:54 -040026 user = authenticate(username=auth.get('username'),
27 password=auth.get('password'))
28
Tony Mack29c287f2013-04-11 21:07:16 -040029 images = _get_images(fields.get('image'))
Tony Mack5d6598c2013-04-12 12:45:02 -040030 keys = _get_keys(fields.get('key'))
Tony Mack29c287f2013-04-11 21:07:16 -040031 slices = _get_slices(fields.get('slice'))
Tony Mack771f4b92013-04-12 01:49:57 -040032 deployment_networks = _get_deployment_networks(fields.get('deploymentNetwork'))
Tony Mack29c287f2013-04-11 21:07:16 -040033 nodes = _get_nodes(fields.get('node'))
Tony Mack09080c62013-05-06 16:57:54 -040034 if images: fields['image'] = images[0]
35 if keys: fields['key'] = keys[0]
36 if slices: fields['slice'] = slices[0]
37 if deployment_networks: fields['deploymentNetwork'] = deployment_networks[0]
Tony Mack5d6598c2013-04-12 12:45:02 -040038 if nodes: fields['node'] = nodes[0]
Tony Mack09080c62013-05-06 16:57:54 -040039
Tony Mack29c287f2013-04-11 21:07:16 -040040 sliver = Sliver(**fields)
Tony Mack09080c62013-05-06 16:57:54 -040041 auth['tenant'] = sliver.slice.name
42 sliver.os_manager = OpenStackManager(auth=auth, caller = user)
Tony Mack29c287f2013-04-11 21:07:16 -040043 sliver.save()
44 return sliver
45
46def update_sliver(auth, sliver, **fields):
47 return
48
49def delete_sliver(auth, filter={}):
Tony Mack09080c62013-05-06 16:57:54 -040050 user = authenticate(username=auth.get('username'),
51 password=auth.get('password'))
Tony Mack29c287f2013-04-11 21:07:16 -040052 slivers = _get_slivers(filter)
53 for sliver in slivers:
Tony Mack09080c62013-05-06 16:57:54 -040054 auth['tenant'] = sliver.slice.name
55 slice.os_manager = OpenStackManager(auth=auth, caller = user)
Tony Mack29c287f2013-04-11 21:07:16 -040056 sliver.delete()
57 return 1
58
59def get_slivers(auth, filter={}):
Tony Mack79748612013-05-01 14:52:03 -040060 user = authenticate(username=auth.get('username'),
61 password=auth.get('password'))
Tony Mack29c287f2013-04-11 21:07:16 -040062 if 'slice' in filter:
63 slices = _get_slices(filter.get('slice'))
64 if slices: filter['slice'] = slices[0]
65 slivers = _get_slivers(filter)
66 return slivers
67
68
69