blob: d93df0e41ac2d318ec199a4ce2fadf15b1661012 [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.models import Site
Tony Mack29c287f2013-04-11 21:07:16 -04005
6
7def _get_sites(filter):
Tony Mack5df6c552013-04-11 21:24:17 -04008 if isinstance(filter, StringTypes) and filter.isdigit():
9 filter = int(filter)
Tony Mack29c287f2013-04-11 21:07:16 -040010 if isinstance(filter, int):
11 sites = Site.objects.filter(id=filter)
12 elif isinstance(filter, StringTypes):
Tony Mack771f4b92013-04-12 01:49:57 -040013 sites = Site.objects.filter(login_base=filter)
Tony Mack5df6c552013-04-11 21:24:17 -040014 elif isinstance(filter, dict):
Tony Mack29c287f2013-04-11 21:07:16 -040015 sites = Site.objects.filter(**filter)
16 else:
17 sites = []
18 return sites
Tony Mack7ab0db72013-04-08 23:19:32 -040019
Tony Mackb5478262013-04-08 23:51:47 -040020def add_site(auth, fields):
Tony Mack09080c62013-05-06 16:57:54 -040021 user = authenticate(username=auth.get('username'),
22 password=auth.get('password'))
23 auth['tenant'] = user.site.login_base
24
Tony Mack7ab0db72013-04-08 23:19:32 -040025 site = Site(**fields)
Tony Mack09080c62013-05-06 16:57:54 -040026 site.os_manager = OpenStackManager(auth=auth, caller = user)
Tony Mack7ab0db72013-04-08 23:19:32 -040027 site.save()
Tony Macka3d5ee62013-04-08 23:34:31 -040028 return site
Tony Mack7ab0db72013-04-08 23:19:32 -040029
Tony Mack1ae3cd12013-04-09 11:12:23 -040030def update_site(auth, id, **fields):
Tony Mack09080c62013-05-06 16:57:54 -040031 user = authenticate(username=auth.get('username'),
32 password=auth.get('password'))
33 auth['tenant'] = user.site.login_base
34
Tony Mack29c287f2013-04-11 21:07:16 -040035 sites = _get_sites(id)
Tony Mack7ab0db72013-04-08 23:19:32 -040036 if not sites:
37 return
38
39 site = Site[0]
Tony Mack09080c62013-05-06 16:57:54 -040040 site.os_manager = OpenStackManager(auth=auth, caller = user)
Tony Macka3d5ee62013-04-08 23:34:31 -040041 site.update(**fields)
Tony Mack7ab0db72013-04-08 23:19:32 -040042 return site
43
44def delete_site(auth, filter={}):
Tony Mack09080c62013-05-06 16:57:54 -040045 user = authenticate(username=auth.get('username'),
46 password=auth.get('password'))
47 auth['tenant'] = user.site.login_base
Tony Mack29c287f2013-04-11 21:07:16 -040048 sites = _get_sites(id)
Tony Mack7ab0db72013-04-08 23:19:32 -040049 for site in sites:
Tony Mack09080c62013-05-06 16:57:54 -040050 site.os_manager = OpenStackManager(auth=auth, caller = user)
Tony Mack7ab0db72013-04-08 23:19:32 -040051 site.delete()
52 return 1
53
54def get_sites(auth, filter={}):
Tony Mack09080c62013-05-06 16:57:54 -040055 user = authenticate(username=auth.get('username'),
56 password=auth.get('password'))
Tony Mack771f4b92013-04-12 01:49:57 -040057 sites = _get_sites(filter)
Tony Mack7ab0db72013-04-08 23:19:32 -040058 return sites
59
60
61