blob: f55249a60aa2ef85d77ff968f32aeafffa09fb31 [file] [log] [blame]
Tony Mack771f4b92013-04-12 01:49:57 -04001import re
Tony Mack5df6c552013-04-11 21:24:17 -04002from types import StringTypes
Tony Mackb9042702013-05-02 15:44:49 -04003from django.contrib.auth import authenticate
Tony Mack09080c62013-05-06 16:57:54 -04004from plstackapi.openstack.manager import OpenStackManager
Tony Mack29c287f2013-04-11 21:07:16 -04005from plstackapi.core.models import Slice
6from plstackapi.core.api.sites import _get_sites
7
Tony Mack29c287f2013-04-11 21:07:16 -04008def _get_slices(filter):
Tony Mack5df6c552013-04-11 21:24:17 -04009 if isinstance(filter, StringTypes) and filter.isdigit():
10 filter = int(filter)
Tony Mack29c287f2013-04-11 21:07:16 -040011 if isinstance(filter, int):
12 slices = Slice.objects.filter(id=filter)
13 elif isinstance(filter, StringTypes):
14 slices = Slice.objects.filter(name=filter)
Tony Mack5df6c552013-04-11 21:24:17 -040015 elif isinstance(filter, dict):
Tony Mack29c287f2013-04-11 21:07:16 -040016 slices = Slice.objects.filter(**filter)
17 else:
18 slices = []
19 return slices
20
Tony Mack3a1af412013-04-09 22:36:27 -040021
Tony Mack3a1af412013-04-09 22:36:27 -040022def add_slice(auth, fields):
Tony Mack09080c62013-05-06 16:57:54 -040023 user = authenticate(username=auth.get('username'),
24 password=auth.get('password'))
25 auth['tenant'] = user.site.login_base
26
Tony Mack771f4b92013-04-12 01:49:57 -040027 login_base = fields['name'][:fields['name'].find('_')]
28 sites = _get_sites(login_base)
Tony Mack29c287f2013-04-11 21:07:16 -040029 if sites: fields['site'] = sites[0]
Tony Mack3a1af412013-04-09 22:36:27 -040030 slice = Slice(**fields)
Tony Mack09080c62013-05-06 16:57:54 -040031 slice.os_manager = OpenStackManager(auth=auth, caller = user)
Tony Mack3a1af412013-04-09 22:36:27 -040032 slice.save()
33 return slice
34
35def update_slice(auth, id, **fields):
Tony Mack09080c62013-05-06 16:57:54 -040036 user = authenticate(username=auth.get('username'),
37 password=auth.get('password'))
38 auth['tenant'] = user.site.login_base
39
Tony Mack29c287f2013-04-11 21:07:16 -040040 slices = _get_slices(id)
Tony Mack3a1af412013-04-09 22:36:27 -040041 if not slices:
42 return
Tony Mack3a1af412013-04-09 22:36:27 -040043 slice = slices[0]
Tony Mack29c287f2013-04-11 21:07:16 -040044 sites = _get_sites(fields.get('site'))
45 if sites: fields['site'] = sites[0]
Tony Mack09080c62013-05-06 16:57:54 -040046
47 slice.os_manager = OpenStackManager(auth=auth, caller = user)
48 for (k,v) in fields.items():
49 setattr(slice, k, v)
50 slice.save()
Tony Mack3a1af412013-04-09 22:36:27 -040051
52 return slice
53
54def delete_slice(auth, filter={}):
Tony Mack09080c62013-05-06 16:57:54 -040055 user = authenticate(username=auth.get('username'),
56 password=auth.get('password'))
57 auth['tenant'] = user.site.login_base
58 slices = _get_slices(filter)
Tony Mack3a1af412013-04-09 22:36:27 -040059 for slice in slices:
Tony Mack09080c62013-05-06 16:57:54 -040060 slice.os_manager = OpenStackManager(auth=auth, caller = user)
Tony Mack3a1af412013-04-09 22:36:27 -040061 slice.delete()
62 return 1
63
64def get_slices(auth, filter={}):
Tony Mackb9042702013-05-02 15:44:49 -040065 user = authenticate(username=auth.get('username'),
66 password=auth.get('password'))
Tony Mackd95d1aa2013-04-10 08:02:02 -040067 if 'site' in filter:
Tony Mack29c287f2013-04-11 21:07:16 -040068 sites = _get_sites(filter.get('site'))
69 if sites: filter['site'] = sites[0]
70 slices = _get_slices(filter)
Tony Mack3a1af412013-04-09 22:36:27 -040071 return slices
72
73
74