blob: c8bd9624da22d11c0a7e49a56b99aeff1d2c035b [file] [log] [blame]
Tony Mack5df6c552013-04-11 21:24:17 -04001from types import StringTypes
Tony Mack09080c62013-05-06 16:57:54 -04002from django.contrib.auth import authenticate
3from plstackapi.openstack.manager import OpenStackManager
Tony Mackf1c12242013-04-09 16:08:43 -04004from plstackapi.core.api.auth import auth_check
5from plstackapi.core.models import Site
Tony Mack29c287f2013-04-11 21:07:16 -04006
7
8def _get_sites(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 sites = Site.objects.filter(id=filter)
13 elif isinstance(filter, StringTypes):
Tony Mack771f4b92013-04-12 01:49:57 -040014 sites = Site.objects.filter(login_base=filter)
Tony Mack5df6c552013-04-11 21:24:17 -040015 elif isinstance(filter, dict):
Tony Mack29c287f2013-04-11 21:07:16 -040016 sites = Site.objects.filter(**filter)
17 else:
18 sites = []
19 return sites
Tony Mack7ab0db72013-04-08 23:19:32 -040020
Tony Mackb5478262013-04-08 23:51:47 -040021def add_site(auth, fields):
Tony Mack09080c62013-05-06 16:57:54 -040022 user = authenticate(username=auth.get('username'),
23 password=auth.get('password'))
24 auth['tenant'] = user.site.login_base
25
Tony Mack7ab0db72013-04-08 23:19:32 -040026 site = Site(**fields)
Tony Mack09080c62013-05-06 16:57:54 -040027 site.os_manager = OpenStackManager(auth=auth, caller = user)
Tony Mack7ab0db72013-04-08 23:19:32 -040028 site.save()
Tony Macka3d5ee62013-04-08 23:34:31 -040029 return site
Tony Mack7ab0db72013-04-08 23:19:32 -040030
Tony Mack1ae3cd12013-04-09 11:12:23 -040031def update_site(auth, id, **fields):
Tony Mack09080c62013-05-06 16:57:54 -040032 user = authenticate(username=auth.get('username'),
33 password=auth.get('password'))
34 auth['tenant'] = user.site.login_base
35
Tony Mack29c287f2013-04-11 21:07:16 -040036 sites = _get_sites(id)
Tony Mack7ab0db72013-04-08 23:19:32 -040037 if not sites:
38 return
39
40 site = Site[0]
Tony Mack09080c62013-05-06 16:57:54 -040041 site.os_manager = OpenStackManager(auth=auth, caller = user)
Tony Macka3d5ee62013-04-08 23:34:31 -040042 site.update(**fields)
Tony Mack7ab0db72013-04-08 23:19:32 -040043 return site
44
45def delete_site(auth, filter={}):
Tony Mack09080c62013-05-06 16:57:54 -040046 user = authenticate(username=auth.get('username'),
47 password=auth.get('password'))
48 auth['tenant'] = user.site.login_base
Tony Mack29c287f2013-04-11 21:07:16 -040049 sites = _get_sites(id)
Tony Mack7ab0db72013-04-08 23:19:32 -040050 for site in sites:
Tony Mack09080c62013-05-06 16:57:54 -040051 site.os_manager = OpenStackManager(auth=auth, caller = user)
Tony Mack7ab0db72013-04-08 23:19:32 -040052 site.delete()
53 return 1
54
55def get_sites(auth, filter={}):
Tony Mack09080c62013-05-06 16:57:54 -040056 user = authenticate(username=auth.get('username'),
57 password=auth.get('password'))
Tony Mack771f4b92013-04-12 01:49:57 -040058 sites = _get_sites(filter)
Tony Mack7ab0db72013-04-08 23:19:32 -040059 return sites
60
61
62