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