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