Tony Mack | 5df6c55 | 2013-04-11 21:24:17 -0400 | [diff] [blame] | 1 | from types import StringTypes |
Tony Mack | 7ab0db7 | 2013-04-08 23:19:32 -0400 | [diff] [blame] | 2 | from plstackapi.openstack.client import OpenStackClient |
| 3 | from plstackapi.openstack.driver import OpenStackDriver |
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 | 7ab0db7 | 2013-04-08 23:19:32 -0400 | [diff] [blame] | 22 | driver = OpenStackDriver(client = auth_check(auth)) |
| 23 | site = Site(**fields) |
Tony Mack | 3b113dc | 2013-04-08 23:54:26 -0400 | [diff] [blame] | 24 | nova_fields = {'tenant_name': site.login_base, |
| 25 | 'description': site.name, |
| 26 | 'enabled': site.enabled} |
Tony Mack | 7ab0db7 | 2013-04-08 23:19:32 -0400 | [diff] [blame] | 27 | tenant = driver.create_tenant(**nova_fields) |
| 28 | site.tenant_id=tenant.id |
| 29 | site.save() |
Tony Mack | a3d5ee6 | 2013-04-08 23:34:31 -0400 | [diff] [blame] | 30 | return site |
Tony Mack | 7ab0db7 | 2013-04-08 23:19:32 -0400 | [diff] [blame] | 31 | |
Tony Mack | 1ae3cd1 | 2013-04-09 11:12:23 -0400 | [diff] [blame] | 32 | def update_site(auth, id, **fields): |
Tony Mack | 7ab0db7 | 2013-04-08 23:19:32 -0400 | [diff] [blame] | 33 | driver = OpenStackDriver(client = auth_check(auth)) |
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] |
| 39 | nova_fields = {} |
| 40 | if 'description' in fields: |
| 41 | nova_fields['description'] = fields['name'] |
| 42 | if 'enabled' in fields: |
| 43 | nova_fields['enabled'] = fields['enabled'] |
Tony Mack | a3d5ee6 | 2013-04-08 23:34:31 -0400 | [diff] [blame] | 44 | driver.update_tenant(site.tenant_id, **nova_fields) |
| 45 | site.update(**fields) |
Tony Mack | 7ab0db7 | 2013-04-08 23:19:32 -0400 | [diff] [blame] | 46 | return site |
| 47 | |
| 48 | def delete_site(auth, filter={}): |
| 49 | driver = OpenStackDriver(client = auth_check(auth)) |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 50 | sites = _get_sites(id) |
Tony Mack | 7ab0db7 | 2013-04-08 23:19:32 -0400 | [diff] [blame] | 51 | for site in sites: |
Tony Mack | c96afa8 | 2013-04-09 01:18:54 -0400 | [diff] [blame] | 52 | driver.delete_tenant(id=site.tenant_id) |
Tony Mack | 7ab0db7 | 2013-04-08 23:19:32 -0400 | [diff] [blame] | 53 | site.delete() |
| 54 | return 1 |
| 55 | |
| 56 | def get_sites(auth, filter={}): |
| 57 | client = auth_check(auth) |
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 | |