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