blob: f03dd6f7b8c5bf6c644d77825c997e56a8933f5f [file] [log] [blame]
Tony Mack771f4b92013-04-12 01:49:57 -04001import re
Tony Mack5df6c552013-04-11 21:24:17 -04002from types import StringTypes
Tony Mackb9042702013-05-02 15:44:49 -04003from django.contrib.auth import authenticate
Siobhan Tully30fd4292013-05-10 08:59:56 -04004from openstack.client import OpenStackClient
5from openstack.driver import OpenStackDriver
6from core.api.auth import auth_check
7from core.models import Slice
8from core.api.sites import _get_sites
Tony Mack29c287f2013-04-11 21:07:16 -04009
Tony Mack29c287f2013-04-11 21:07:16 -040010def _get_slices(filter):
Tony Mack5df6c552013-04-11 21:24:17 -040011 if isinstance(filter, StringTypes) and filter.isdigit():
12 filter = int(filter)
Tony Mack29c287f2013-04-11 21:07:16 -040013 if isinstance(filter, int):
14 slices = Slice.objects.filter(id=filter)
15 elif isinstance(filter, StringTypes):
16 slices = Slice.objects.filter(name=filter)
Tony Mack5df6c552013-04-11 21:24:17 -040017 elif isinstance(filter, dict):
Tony Mack29c287f2013-04-11 21:07:16 -040018 slices = Slice.objects.filter(**filter)
19 else:
20 slices = []
21 return slices
22
Tony Mack3a1af412013-04-09 22:36:27 -040023
Tony Mack3a1af412013-04-09 22:36:27 -040024def add_slice(auth, fields):
25 driver = OpenStackDriver(client = auth_check(auth))
Tony Mack771f4b92013-04-12 01:49:57 -040026 login_base = fields['name'][:fields['name'].find('_')]
27 sites = _get_sites(login_base)
Tony Mack29c287f2013-04-11 21:07:16 -040028 if sites: fields['site'] = sites[0]
Tony Mack3a1af412013-04-09 22:36:27 -040029 slice = Slice(**fields)
Tony Mack771f4b92013-04-12 01:49:57 -040030
Tony Mack3a1af412013-04-09 22:36:27 -040031 # create tenant
32 nova_fields = {'tenant_name': slice.name,
33 'description': slice.description,
34 'enabled': slice.enabled}
35 tenant = driver.create_tenant(**nova_fields)
36 slice.tenant_id=tenant.id
37
38 # create network
Tony Mack53c63342013-04-10 08:11:45 -040039 network = driver.create_network(slice.name)
Tony Mackd95d1aa2013-04-10 08:02:02 -040040 slice.network_id = network['id']
Tony Mack3a1af412013-04-09 22:36:27 -040041
42 # create router
Tony Mack53c63342013-04-10 08:11:45 -040043 router = driver.create_router(slice.name)
Tony Mackd95d1aa2013-04-10 08:02:02 -040044 slice.router_id = router['id']
Tony Mack3a1af412013-04-09 22:36:27 -040045
46 slice.save()
47 return slice
48
49def update_slice(auth, id, **fields):
50 driver = OpenStackDriver(client = auth_check(auth))
Tony Mack29c287f2013-04-11 21:07:16 -040051 slices = _get_slices(id)
Tony Mack3a1af412013-04-09 22:36:27 -040052 if not slices:
53 return
54
55 # update tenant
56 slice = slices[0]
57 nova_fields = {}
58 if 'name' in fields:
59 nova_fields['tenant_name'] = fields['name']
60 if 'description' in fields:
61 nova_fields['description'] = fields['description']
62 if 'enabled' in fields:
63 nova_fields['enabled'] = fields['enabled']
64 driver.update_tenant(slice.tenant_id, **nova_fields)
65
66 # update db record
Tony Mack29c287f2013-04-11 21:07:16 -040067 sites = _get_sites(fields.get('site'))
68 if sites: fields['site'] = sites[0]
Tony Mack3a1af412013-04-09 22:36:27 -040069 slice.update(**fields)
70
71 return slice
72
73def delete_slice(auth, filter={}):
74 driver = OpenStackDriver(client = auth_check(auth))
Tony Mack29c287f2013-04-11 21:07:16 -040075 slices = _get_slices(id)
Tony Mack3a1af412013-04-09 22:36:27 -040076 for slice in slices:
Tony Mack62bc59a2013-04-14 23:27:12 -040077 driver.delete_network(slice.network_id)
78 driver.delete_router(slice.router_id)
Tony Mack3a1af412013-04-09 22:36:27 -040079 driver.delete_slice(id=slice.tenant_id)
80 slice.delete()
81 return 1
82
83def get_slices(auth, filter={}):
Tony Mackb9042702013-05-02 15:44:49 -040084 user = authenticate(username=auth.get('username'),
85 password=auth.get('password'))
Tony Mackd95d1aa2013-04-10 08:02:02 -040086 if 'site' in filter:
Tony Mack29c287f2013-04-11 21:07:16 -040087 sites = _get_sites(filter.get('site'))
88 if sites: filter['site'] = sites[0]
89 slices = _get_slices(filter)
Tony Mack3a1af412013-04-09 22:36:27 -040090 return slices
91
92
93