Tony Mack | 7ab0db7 | 2013-04-08 23:19:32 -0400 | [diff] [blame] | 1 | from plstackapi.openstack.client import OpenStackClient |
| 2 | from plstackapi.openstack.driver import OpenStackDriver |
| 3 | from plstackapi.planetstack.api.auth import auth_check |
| 4 | from plstackapi.planetstack.models import Site |
| 5 | |
| 6 | |
Tony Mack | b547826 | 2013-04-08 23:51:47 -0400 | [diff] [blame] | 7 | def add_site(auth, fields): |
Tony Mack | 7ab0db7 | 2013-04-08 23:19:32 -0400 | [diff] [blame] | 8 | driver = OpenStackDriver(client = auth_check(auth)) |
| 9 | site = Site(**fields) |
Tony Mack | 3b113dc | 2013-04-08 23:54:26 -0400 | [diff] [blame] | 10 | nova_fields = {'tenant_name': site.login_base, |
| 11 | 'description': site.name, |
| 12 | 'enabled': site.enabled} |
Tony Mack | 7ab0db7 | 2013-04-08 23:19:32 -0400 | [diff] [blame] | 13 | tenant = driver.create_tenant(**nova_fields) |
| 14 | site.tenant_id=tenant.id |
| 15 | site.save() |
Tony Mack | a3d5ee6 | 2013-04-08 23:34:31 -0400 | [diff] [blame] | 16 | return site |
Tony Mack | 7ab0db7 | 2013-04-08 23:19:32 -0400 | [diff] [blame] | 17 | |
Tony Mack | 1ae3cd1 | 2013-04-09 11:12:23 -0400 | [diff] [blame] | 18 | def update_site(auth, id, **fields): |
Tony Mack | 7ab0db7 | 2013-04-08 23:19:32 -0400 | [diff] [blame] | 19 | driver = OpenStackDriver(client = auth_check(auth)) |
Tony Mack | 1ae3cd1 | 2013-04-09 11:12:23 -0400 | [diff] [blame] | 20 | sites = Site.objects.filter(id=id) |
Tony Mack | 7ab0db7 | 2013-04-08 23:19:32 -0400 | [diff] [blame] | 21 | if not sites: |
| 22 | return |
| 23 | |
| 24 | site = Site[0] |
| 25 | nova_fields = {} |
| 26 | if 'description' in fields: |
| 27 | nova_fields['description'] = fields['name'] |
| 28 | if 'enabled' in fields: |
| 29 | nova_fields['enabled'] = fields['enabled'] |
Tony Mack | a3d5ee6 | 2013-04-08 23:34:31 -0400 | [diff] [blame] | 30 | driver.update_tenant(site.tenant_id, **nova_fields) |
| 31 | site.update(**fields) |
Tony Mack | 7ab0db7 | 2013-04-08 23:19:32 -0400 | [diff] [blame] | 32 | return site |
| 33 | |
| 34 | def delete_site(auth, filter={}): |
| 35 | driver = OpenStackDriver(client = auth_check(auth)) |
| 36 | sites = Site.objects.filter(**filter) |
| 37 | for site in sites: |
Tony Mack | c96afa8 | 2013-04-09 01:18:54 -0400 | [diff] [blame] | 38 | driver.delete_tenant(id=site.tenant_id) |
Tony Mack | 7ab0db7 | 2013-04-08 23:19:32 -0400 | [diff] [blame] | 39 | site.delete() |
| 40 | return 1 |
| 41 | |
| 42 | def get_sites(auth, filter={}): |
| 43 | client = auth_check(auth) |
| 44 | sites = Site.objects.filter(**filter) |
| 45 | return sites |
| 46 | |
| 47 | |
| 48 | |