blob: b8c463daf227e0dbd89945f5fd3b2cedf2687163 [file] [log] [blame]
Tony Mack5df6c552013-04-11 21:24:17 -04001from types import StringTypes
Tony Mack7ab0db72013-04-08 23:19:32 -04002from plstackapi.openstack.client import OpenStackClient
3from plstackapi.openstack.driver import OpenStackDriver
Tony Mackf1c12242013-04-09 16:08:43 -04004from plstackapi.core.api.auth import auth_check
5from plstackapi.core.models import Site
Tony Mack29c287f2013-04-11 21:07:16 -04006
7
8def _get_sites(filter):
Tony Mack5df6c552013-04-11 21:24:17 -04009 if isinstance(filter, StringTypes) and filter.isdigit():
10 filter = int(filter)
Tony Mack29c287f2013-04-11 21:07:16 -040011 if isinstance(filter, int):
12 sites = Site.objects.filter(id=filter)
13 elif isinstance(filter, StringTypes):
Tony Mack771f4b92013-04-12 01:49:57 -040014 sites = Site.objects.filter(login_base=filter)
Tony Mack5df6c552013-04-11 21:24:17 -040015 elif isinstance(filter, dict):
Tony Mack29c287f2013-04-11 21:07:16 -040016 sites = Site.objects.filter(**filter)
17 else:
18 sites = []
19 return sites
Tony Mack7ab0db72013-04-08 23:19:32 -040020
Tony Mackb5478262013-04-08 23:51:47 -040021def add_site(auth, fields):
Tony Mack7ab0db72013-04-08 23:19:32 -040022 driver = OpenStackDriver(client = auth_check(auth))
23 site = Site(**fields)
Tony Mack3b113dc2013-04-08 23:54:26 -040024 nova_fields = {'tenant_name': site.login_base,
25 'description': site.name,
26 'enabled': site.enabled}
Tony Mack7ab0db72013-04-08 23:19:32 -040027 tenant = driver.create_tenant(**nova_fields)
28 site.tenant_id=tenant.id
29 site.save()
Tony Macka3d5ee62013-04-08 23:34:31 -040030 return site
Tony Mack7ab0db72013-04-08 23:19:32 -040031
Tony Mack1ae3cd12013-04-09 11:12:23 -040032def update_site(auth, id, **fields):
Tony Mack7ab0db72013-04-08 23:19:32 -040033 driver = OpenStackDriver(client = auth_check(auth))
Tony Mack29c287f2013-04-11 21:07:16 -040034 sites = _get_sites(id)
Tony Mack7ab0db72013-04-08 23:19:32 -040035 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 Macka3d5ee62013-04-08 23:34:31 -040044 driver.update_tenant(site.tenant_id, **nova_fields)
45 site.update(**fields)
Tony Mack7ab0db72013-04-08 23:19:32 -040046 return site
47
48def delete_site(auth, filter={}):
49 driver = OpenStackDriver(client = auth_check(auth))
Tony Mack29c287f2013-04-11 21:07:16 -040050 sites = _get_sites(id)
Tony Mack7ab0db72013-04-08 23:19:32 -040051 for site in sites:
Tony Mackc96afa82013-04-09 01:18:54 -040052 driver.delete_tenant(id=site.tenant_id)
Tony Mack7ab0db72013-04-08 23:19:32 -040053 site.delete()
54 return 1
55
56def get_sites(auth, filter={}):
57 client = auth_check(auth)
Tony Mack771f4b92013-04-12 01:49:57 -040058 sites = _get_sites(filter)
Tony Mack7ab0db72013-04-08 23:19:32 -040059 return sites
60
61
62