Tony Mack | 771f4b9 | 2013-04-12 01:49:57 -0400 | [diff] [blame] | 1 | import re |
Tony Mack | 5df6c55 | 2013-04-11 21:24:17 -0400 | [diff] [blame] | 2 | from types import StringTypes |
Tony Mack | b904270 | 2013-05-02 15:44:49 -0400 | [diff] [blame] | 3 | from django.contrib.auth import authenticate |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame^] | 4 | from openstack.client import OpenStackClient |
| 5 | from openstack.driver import OpenStackDriver |
| 6 | from core.api.auth import auth_check |
| 7 | from core.models import Slice |
| 8 | from core.api.sites import _get_sites |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 9 | |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 10 | def _get_slices(filter): |
Tony Mack | 5df6c55 | 2013-04-11 21:24:17 -0400 | [diff] [blame] | 11 | if isinstance(filter, StringTypes) and filter.isdigit(): |
| 12 | filter = int(filter) |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 13 | if isinstance(filter, int): |
| 14 | slices = Slice.objects.filter(id=filter) |
| 15 | elif isinstance(filter, StringTypes): |
| 16 | slices = Slice.objects.filter(name=filter) |
Tony Mack | 5df6c55 | 2013-04-11 21:24:17 -0400 | [diff] [blame] | 17 | elif isinstance(filter, dict): |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 18 | slices = Slice.objects.filter(**filter) |
| 19 | else: |
| 20 | slices = [] |
| 21 | return slices |
| 22 | |
Tony Mack | 3a1af41 | 2013-04-09 22:36:27 -0400 | [diff] [blame] | 23 | |
Tony Mack | 3a1af41 | 2013-04-09 22:36:27 -0400 | [diff] [blame] | 24 | def add_slice(auth, fields): |
| 25 | driver = OpenStackDriver(client = auth_check(auth)) |
Tony Mack | 771f4b9 | 2013-04-12 01:49:57 -0400 | [diff] [blame] | 26 | login_base = fields['name'][:fields['name'].find('_')] |
| 27 | sites = _get_sites(login_base) |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 28 | if sites: fields['site'] = sites[0] |
Tony Mack | 3a1af41 | 2013-04-09 22:36:27 -0400 | [diff] [blame] | 29 | slice = Slice(**fields) |
Tony Mack | 771f4b9 | 2013-04-12 01:49:57 -0400 | [diff] [blame] | 30 | |
Tony Mack | 3a1af41 | 2013-04-09 22:36:27 -0400 | [diff] [blame] | 31 | # 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 Mack | 53c6334 | 2013-04-10 08:11:45 -0400 | [diff] [blame] | 39 | network = driver.create_network(slice.name) |
Tony Mack | d95d1aa | 2013-04-10 08:02:02 -0400 | [diff] [blame] | 40 | slice.network_id = network['id'] |
Tony Mack | 3a1af41 | 2013-04-09 22:36:27 -0400 | [diff] [blame] | 41 | |
| 42 | # create router |
Tony Mack | 53c6334 | 2013-04-10 08:11:45 -0400 | [diff] [blame] | 43 | router = driver.create_router(slice.name) |
Tony Mack | d95d1aa | 2013-04-10 08:02:02 -0400 | [diff] [blame] | 44 | slice.router_id = router['id'] |
Tony Mack | 3a1af41 | 2013-04-09 22:36:27 -0400 | [diff] [blame] | 45 | |
| 46 | slice.save() |
| 47 | return slice |
| 48 | |
| 49 | def update_slice(auth, id, **fields): |
| 50 | driver = OpenStackDriver(client = auth_check(auth)) |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 51 | slices = _get_slices(id) |
Tony Mack | 3a1af41 | 2013-04-09 22:36:27 -0400 | [diff] [blame] | 52 | 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 Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 67 | sites = _get_sites(fields.get('site')) |
| 68 | if sites: fields['site'] = sites[0] |
Tony Mack | 3a1af41 | 2013-04-09 22:36:27 -0400 | [diff] [blame] | 69 | slice.update(**fields) |
| 70 | |
| 71 | return slice |
| 72 | |
| 73 | def delete_slice(auth, filter={}): |
| 74 | driver = OpenStackDriver(client = auth_check(auth)) |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 75 | slices = _get_slices(id) |
Tony Mack | 3a1af41 | 2013-04-09 22:36:27 -0400 | [diff] [blame] | 76 | for slice in slices: |
Tony Mack | 62bc59a | 2013-04-14 23:27:12 -0400 | [diff] [blame] | 77 | driver.delete_network(slice.network_id) |
| 78 | driver.delete_router(slice.router_id) |
Tony Mack | 3a1af41 | 2013-04-09 22:36:27 -0400 | [diff] [blame] | 79 | driver.delete_slice(id=slice.tenant_id) |
| 80 | slice.delete() |
| 81 | return 1 |
| 82 | |
| 83 | def get_slices(auth, filter={}): |
Tony Mack | b904270 | 2013-05-02 15:44:49 -0400 | [diff] [blame] | 84 | user = authenticate(username=auth.get('username'), |
| 85 | password=auth.get('password')) |
Tony Mack | d95d1aa | 2013-04-10 08:02:02 -0400 | [diff] [blame] | 86 | if 'site' in filter: |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 87 | sites = _get_sites(filter.get('site')) |
| 88 | if sites: filter['site'] = sites[0] |
| 89 | slices = _get_slices(filter) |
Tony Mack | 3a1af41 | 2013-04-09 22:36:27 -0400 | [diff] [blame] | 90 | return slices |
| 91 | |
| 92 | |
| 93 | |