Tony Mack | 67c6b1f | 2013-04-09 23:04:42 -0400 | [diff] [blame] | 1 | import re |
Tony Mack | 3a1af41 | 2013-04-09 22:36:27 -0400 | [diff] [blame] | 2 | from plstackapi.openstack.client import OpenStackClient |
| 3 | from plstackapi.openstack.driver import OpenStackDriver |
| 4 | from plstackapi.core.api.auth import auth_check |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame^] | 5 | from plstackapi.core.models import Slice |
| 6 | from plstackapi.core.api.sites import _get_sites |
| 7 | |
| 8 | |
| 9 | def _get_slices(filter): |
| 10 | if isinstance(filter, int): |
| 11 | slices = Slice.objects.filter(id=filter) |
| 12 | elif isinstance(filter, StringTypes): |
| 13 | slices = Slice.objects.filter(name=filter) |
| 14 | elif isinstance(filer, dict): |
| 15 | slices = Slice.objects.filter(**filter) |
| 16 | else: |
| 17 | slices = [] |
| 18 | return slices |
| 19 | |
Tony Mack | 3a1af41 | 2013-04-09 22:36:27 -0400 | [diff] [blame] | 20 | |
Tony Mack | 3a1af41 | 2013-04-09 22:36:27 -0400 | [diff] [blame] | 21 | def add_slice(auth, fields): |
| 22 | driver = OpenStackDriver(client = auth_check(auth)) |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame^] | 23 | sites = _get_sites(fields.get('site')) |
| 24 | if sites: fields['site'] = sites[0] |
Tony Mack | 3a1af41 | 2013-04-09 22:36:27 -0400 | [diff] [blame] | 25 | slice = Slice(**fields) |
| 26 | # create tenant |
| 27 | nova_fields = {'tenant_name': slice.name, |
| 28 | 'description': slice.description, |
| 29 | 'enabled': slice.enabled} |
| 30 | tenant = driver.create_tenant(**nova_fields) |
| 31 | slice.tenant_id=tenant.id |
| 32 | |
| 33 | # create network |
Tony Mack | 53c6334 | 2013-04-10 08:11:45 -0400 | [diff] [blame] | 34 | network = driver.create_network(slice.name) |
Tony Mack | d95d1aa | 2013-04-10 08:02:02 -0400 | [diff] [blame] | 35 | slice.network_id = network['id'] |
Tony Mack | 3a1af41 | 2013-04-09 22:36:27 -0400 | [diff] [blame] | 36 | |
| 37 | # create router |
Tony Mack | 53c6334 | 2013-04-10 08:11:45 -0400 | [diff] [blame] | 38 | router = driver.create_router(slice.name) |
Tony Mack | d95d1aa | 2013-04-10 08:02:02 -0400 | [diff] [blame] | 39 | slice.router_id = router['id'] |
Tony Mack | 3a1af41 | 2013-04-09 22:36:27 -0400 | [diff] [blame] | 40 | |
| 41 | slice.save() |
| 42 | return slice |
| 43 | |
| 44 | def update_slice(auth, id, **fields): |
| 45 | driver = OpenStackDriver(client = auth_check(auth)) |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame^] | 46 | slices = _get_slices(id) |
Tony Mack | 3a1af41 | 2013-04-09 22:36:27 -0400 | [diff] [blame] | 47 | if not slices: |
| 48 | return |
| 49 | |
| 50 | # update tenant |
| 51 | slice = slices[0] |
| 52 | nova_fields = {} |
| 53 | if 'name' in fields: |
| 54 | nova_fields['tenant_name'] = fields['name'] |
| 55 | if 'description' in fields: |
| 56 | nova_fields['description'] = fields['description'] |
| 57 | if 'enabled' in fields: |
| 58 | nova_fields['enabled'] = fields['enabled'] |
| 59 | driver.update_tenant(slice.tenant_id, **nova_fields) |
| 60 | |
| 61 | # update db record |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame^] | 62 | sites = _get_sites(fields.get('site')) |
| 63 | if sites: fields['site'] = sites[0] |
Tony Mack | 3a1af41 | 2013-04-09 22:36:27 -0400 | [diff] [blame] | 64 | slice.update(**fields) |
| 65 | |
| 66 | return slice |
| 67 | |
| 68 | def delete_slice(auth, filter={}): |
| 69 | driver = OpenStackDriver(client = auth_check(auth)) |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame^] | 70 | slices = _get_slices(id) |
Tony Mack | 3a1af41 | 2013-04-09 22:36:27 -0400 | [diff] [blame] | 71 | for slice in slices: |
| 72 | driver.delete_slice(id=slice.tenant_id) |
| 73 | slice.delete() |
| 74 | return 1 |
| 75 | |
| 76 | def get_slices(auth, filter={}): |
| 77 | client = auth_check(auth) |
Tony Mack | d95d1aa | 2013-04-10 08:02:02 -0400 | [diff] [blame] | 78 | if 'site' in filter: |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame^] | 79 | sites = _get_sites(filter.get('site')) |
| 80 | if sites: filter['site'] = sites[0] |
| 81 | slices = _get_slices(filter) |
Tony Mack | 3a1af41 | 2013-04-09 22:36:27 -0400 | [diff] [blame] | 82 | return slices |
| 83 | |
| 84 | |
| 85 | |