blob: a0cc8f891362ba3703b5842d4c344d1d237179db [file] [log] [blame]
Tony Mack771f4b92013-04-12 01:49:57 -04001import re
Tony Mack5df6c552013-04-11 21:24:17 -04002from types import StringTypes
Tony Mack3a1af412013-04-09 22:36:27 -04003from plstackapi.openstack.client import OpenStackClient
4from plstackapi.openstack.driver import OpenStackDriver
5from plstackapi.core.api.auth import auth_check
Tony Mack29c287f2013-04-11 21:07:16 -04006from plstackapi.core.models import Slice
7from plstackapi.core.api.sites import _get_sites
8
Tony Mack29c287f2013-04-11 21:07:16 -04009def _get_slices(filter):
Tony Mack5df6c552013-04-11 21:24:17 -040010 if isinstance(filter, StringTypes) and filter.isdigit():
11 filter = int(filter)
Tony Mack29c287f2013-04-11 21:07:16 -040012 if isinstance(filter, int):
13 slices = Slice.objects.filter(id=filter)
14 elif isinstance(filter, StringTypes):
15 slices = Slice.objects.filter(name=filter)
Tony Mack5df6c552013-04-11 21:24:17 -040016 elif isinstance(filter, dict):
Tony Mack29c287f2013-04-11 21:07:16 -040017 slices = Slice.objects.filter(**filter)
18 else:
19 slices = []
20 return slices
21
Tony Mack3a1af412013-04-09 22:36:27 -040022
Tony Mack3a1af412013-04-09 22:36:27 -040023def add_slice(auth, fields):
24 driver = OpenStackDriver(client = auth_check(auth))
Tony Mack771f4b92013-04-12 01:49:57 -040025 login_base = fields['name'][:fields['name'].find('_')]
26 sites = _get_sites(login_base)
Tony Mack29c287f2013-04-11 21:07:16 -040027 if sites: fields['site'] = sites[0]
Tony Mack3a1af412013-04-09 22:36:27 -040028 slice = Slice(**fields)
Tony Mack771f4b92013-04-12 01:49:57 -040029
Tony Mack3a1af412013-04-09 22:36:27 -040030 # create tenant
31 nova_fields = {'tenant_name': slice.name,
32 'description': slice.description,
33 'enabled': slice.enabled}
34 tenant = driver.create_tenant(**nova_fields)
35 slice.tenant_id=tenant.id
36
37 # create network
Tony Mack53c63342013-04-10 08:11:45 -040038 network = driver.create_network(slice.name)
Tony Mackd95d1aa2013-04-10 08:02:02 -040039 slice.network_id = network['id']
Tony Mack3a1af412013-04-09 22:36:27 -040040
41 # create router
Tony Mack53c63342013-04-10 08:11:45 -040042 router = driver.create_router(slice.name)
Tony Mackd95d1aa2013-04-10 08:02:02 -040043 slice.router_id = router['id']
Tony Mack3a1af412013-04-09 22:36:27 -040044
45 slice.save()
46 return slice
47
48def update_slice(auth, id, **fields):
49 driver = OpenStackDriver(client = auth_check(auth))
Tony Mack29c287f2013-04-11 21:07:16 -040050 slices = _get_slices(id)
Tony Mack3a1af412013-04-09 22:36:27 -040051 if not slices:
52 return
53
54 # update tenant
55 slice = slices[0]
56 nova_fields = {}
57 if 'name' in fields:
58 nova_fields['tenant_name'] = fields['name']
59 if 'description' in fields:
60 nova_fields['description'] = fields['description']
61 if 'enabled' in fields:
62 nova_fields['enabled'] = fields['enabled']
63 driver.update_tenant(slice.tenant_id, **nova_fields)
64
65 # update db record
Tony Mack29c287f2013-04-11 21:07:16 -040066 sites = _get_sites(fields.get('site'))
67 if sites: fields['site'] = sites[0]
Tony Mack3a1af412013-04-09 22:36:27 -040068 slice.update(**fields)
69
70 return slice
71
72def delete_slice(auth, filter={}):
73 driver = OpenStackDriver(client = auth_check(auth))
Tony Mack29c287f2013-04-11 21:07:16 -040074 slices = _get_slices(id)
Tony Mack3a1af412013-04-09 22:36:27 -040075 for slice in slices:
Tony Mack62bc59a2013-04-14 23:27:12 -040076 driver.delete_network(slice.network_id)
77 driver.delete_router(slice.router_id)
Tony Mack3a1af412013-04-09 22:36:27 -040078 driver.delete_slice(id=slice.tenant_id)
79 slice.delete()
80 return 1
81
82def get_slices(auth, filter={}):
83 client = auth_check(auth)
Tony Mackd95d1aa2013-04-10 08:02:02 -040084 if 'site' in filter:
Tony Mack29c287f2013-04-11 21:07:16 -040085 sites = _get_sites(filter.get('site'))
86 if sites: filter['site'] = sites[0]
87 slices = _get_slices(filter)
Tony Mack3a1af412013-04-09 22:36:27 -040088 return slices
89
90
91