blob: 123f13937bd9c61b35606887aabc511e87015d01 [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
Siobhan Tully73291342013-05-10 10:50:08 -04003from openstack.manager import OpenStackManager
Siobhan Tully30fd4292013-05-10 08:59:56 -04004from core.models import Site
Tony Mack29c287f2013-04-11 21:07:16 -04005
Tony Mack29c287f2013-04-11 21:07:16 -04006def _get_sites(filter):
Tony Mack5df6c552013-04-11 21:24:17 -04007 if isinstance(filter, StringTypes) and filter.isdigit():
8 filter = int(filter)
Tony Mack29c287f2013-04-11 21:07:16 -04009 if isinstance(filter, int):
10 sites = Site.objects.filter(id=filter)
11 elif isinstance(filter, StringTypes):
Tony Mack771f4b92013-04-12 01:49:57 -040012 sites = Site.objects.filter(login_base=filter)
Tony Mack5df6c552013-04-11 21:24:17 -040013 elif isinstance(filter, dict):
Tony Mack29c287f2013-04-11 21:07:16 -040014 sites = Site.objects.filter(**filter)
15 else:
16 sites = []
17 return sites
Tony Mack7ab0db72013-04-08 23:19:32 -040018
Tony Mackb5478262013-04-08 23:51:47 -040019def add_site(auth, fields):
Tony Mack09080c62013-05-06 16:57:54 -040020 user = authenticate(username=auth.get('username'),
21 password=auth.get('password'))
22 auth['tenant'] = user.site.login_base
23
Tony Mack7ab0db72013-04-08 23:19:32 -040024 site = Site(**fields)
Tony Mack09080c62013-05-06 16:57:54 -040025 site.os_manager = OpenStackManager(auth=auth, caller = user)
Tony Mack7ab0db72013-04-08 23:19:32 -040026 site.save()
Tony Macka3d5ee62013-04-08 23:34:31 -040027 return site
Tony Mack7ab0db72013-04-08 23:19:32 -040028
Tony Mack1ae3cd12013-04-09 11:12:23 -040029def update_site(auth, id, **fields):
Tony Mack09080c62013-05-06 16:57:54 -040030 user = authenticate(username=auth.get('username'),
31 password=auth.get('password'))
32 auth['tenant'] = user.site.login_base
33
Tony Mack29c287f2013-04-11 21:07:16 -040034 sites = _get_sites(id)
Tony Mack7ab0db72013-04-08 23:19:32 -040035 if not sites:
36 return
37
38 site = Site[0]
Tony Mack09080c62013-05-06 16:57:54 -040039 site.os_manager = OpenStackManager(auth=auth, caller = user)
Tony Macka3d5ee62013-04-08 23:34:31 -040040 site.update(**fields)
Tony Mack7ab0db72013-04-08 23:19:32 -040041 return site
42
43def delete_site(auth, filter={}):
Tony Mack09080c62013-05-06 16:57:54 -040044 user = authenticate(username=auth.get('username'),
45 password=auth.get('password'))
46 auth['tenant'] = user.site.login_base
Tony Mack29c287f2013-04-11 21:07:16 -040047 sites = _get_sites(id)
Tony Mack7ab0db72013-04-08 23:19:32 -040048 for site in sites:
Tony Mack09080c62013-05-06 16:57:54 -040049 site.os_manager = OpenStackManager(auth=auth, caller = user)
Tony Mack7ab0db72013-04-08 23:19:32 -040050 site.delete()
51 return 1
52
53def get_sites(auth, filter={}):
Tony Mack09080c62013-05-06 16:57:54 -040054 user = authenticate(username=auth.get('username'),
55 password=auth.get('password'))
Tony Mack771f4b92013-04-12 01:49:57 -040056 sites = _get_sites(filter)
Tony Mack7ab0db72013-04-08 23:19:32 -040057 return sites
58
59
60