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