Tony Mack | 5df6c55 | 2013-04-11 21:24:17 -0400 | [diff] [blame] | 1 | from types import StringTypes |
Tony Mack | 09080c6 | 2013-05-06 16:57:54 -0400 | [diff] [blame] | 2 | from django.contrib.auth import authenticate |
Siobhan Tully | 7329134 | 2013-05-10 10:50:08 -0400 | [diff] [blame] | 3 | from openstack.manager import OpenStackManager |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 4 | from core.models import Site |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 5 | |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 6 | def _get_sites(filter): |
Tony Mack | 5df6c55 | 2013-04-11 21:24:17 -0400 | [diff] [blame] | 7 | if isinstance(filter, StringTypes) and filter.isdigit(): |
| 8 | filter = int(filter) |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 9 | if isinstance(filter, int): |
| 10 | sites = Site.objects.filter(id=filter) |
| 11 | elif isinstance(filter, StringTypes): |
Tony Mack | 771f4b9 | 2013-04-12 01:49:57 -0400 | [diff] [blame] | 12 | sites = Site.objects.filter(login_base=filter) |
Tony Mack | 5df6c55 | 2013-04-11 21:24:17 -0400 | [diff] [blame] | 13 | elif isinstance(filter, dict): |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 14 | sites = Site.objects.filter(**filter) |
| 15 | else: |
| 16 | sites = [] |
| 17 | return sites |
Tony Mack | 7ab0db7 | 2013-04-08 23:19:32 -0400 | [diff] [blame] | 18 | |
Tony Mack | b547826 | 2013-04-08 23:51:47 -0400 | [diff] [blame] | 19 | def add_site(auth, fields): |
Tony Mack | 09080c6 | 2013-05-06 16:57:54 -0400 | [diff] [blame] | 20 | user = authenticate(username=auth.get('username'), |
| 21 | password=auth.get('password')) |
| 22 | auth['tenant'] = user.site.login_base |
| 23 | |
Tony Mack | 7ab0db7 | 2013-04-08 23:19:32 -0400 | [diff] [blame] | 24 | site = Site(**fields) |
Tony Mack | 09080c6 | 2013-05-06 16:57:54 -0400 | [diff] [blame] | 25 | site.os_manager = OpenStackManager(auth=auth, caller = user) |
Tony Mack | 7ab0db7 | 2013-04-08 23:19:32 -0400 | [diff] [blame] | 26 | site.save() |
Tony Mack | a3d5ee6 | 2013-04-08 23:34:31 -0400 | [diff] [blame] | 27 | return site |
Tony Mack | 7ab0db7 | 2013-04-08 23:19:32 -0400 | [diff] [blame] | 28 | |
Tony Mack | 1ae3cd1 | 2013-04-09 11:12:23 -0400 | [diff] [blame] | 29 | def update_site(auth, id, **fields): |
Tony Mack | 09080c6 | 2013-05-06 16:57:54 -0400 | [diff] [blame] | 30 | user = authenticate(username=auth.get('username'), |
| 31 | password=auth.get('password')) |
| 32 | auth['tenant'] = user.site.login_base |
| 33 | |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 34 | sites = _get_sites(id) |
Tony Mack | 7ab0db7 | 2013-04-08 23:19:32 -0400 | [diff] [blame] | 35 | if not sites: |
| 36 | return |
| 37 | |
| 38 | site = Site[0] |
Tony Mack | 09080c6 | 2013-05-06 16:57:54 -0400 | [diff] [blame] | 39 | site.os_manager = OpenStackManager(auth=auth, caller = user) |
Tony Mack | a3d5ee6 | 2013-04-08 23:34:31 -0400 | [diff] [blame] | 40 | site.update(**fields) |
Tony Mack | 7ab0db7 | 2013-04-08 23:19:32 -0400 | [diff] [blame] | 41 | return site |
| 42 | |
| 43 | def delete_site(auth, filter={}): |
Tony Mack | 09080c6 | 2013-05-06 16:57:54 -0400 | [diff] [blame] | 44 | user = authenticate(username=auth.get('username'), |
| 45 | password=auth.get('password')) |
| 46 | auth['tenant'] = user.site.login_base |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 47 | sites = _get_sites(id) |
Tony Mack | 7ab0db7 | 2013-04-08 23:19:32 -0400 | [diff] [blame] | 48 | for site in sites: |
Tony Mack | 09080c6 | 2013-05-06 16:57:54 -0400 | [diff] [blame] | 49 | site.os_manager = OpenStackManager(auth=auth, caller = user) |
Tony Mack | 7ab0db7 | 2013-04-08 23:19:32 -0400 | [diff] [blame] | 50 | site.delete() |
| 51 | return 1 |
| 52 | |
| 53 | def get_sites(auth, filter={}): |
Tony Mack | 09080c6 | 2013-05-06 16:57:54 -0400 | [diff] [blame] | 54 | user = authenticate(username=auth.get('username'), |
| 55 | password=auth.get('password')) |
Tony Mack | 771f4b9 | 2013-04-12 01:49:57 -0400 | [diff] [blame] | 56 | sites = _get_sites(filter) |
Tony Mack | 7ab0db7 | 2013-04-08 23:19:32 -0400 | [diff] [blame] | 57 | return sites |
| 58 | |
| 59 | |
| 60 | |