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 | 771f4b9 | 2013-04-12 01:49:57 -0400 | [diff] [blame^] | 9 | def validate_name(name): |
| 10 | # N.B.: Responsibility of the caller to ensure that login_base |
| 11 | # portion of the slice name corresponds to a valid site, if |
| 12 | # desired. |
| 13 | |
| 14 | # 1. Lowercase. |
| 15 | # 2. Begins with login_base (letters or numbers). |
| 16 | # 3. Then single underscore after login_base. |
| 17 | # 4. Then letters, numbers, or underscores. |
| 18 | good_name = r'^[a-z0-9]+_[a-zA-Z0-9_]+$' |
| 19 | if not name or \ |
| 20 | not re.match(good_name, name): |
| 21 | raise Exception, "Invalid slice name: %s" % name |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 22 | |
| 23 | def _get_slices(filter): |
Tony Mack | 5df6c55 | 2013-04-11 21:24:17 -0400 | [diff] [blame] | 24 | if isinstance(filter, StringTypes) and filter.isdigit(): |
| 25 | filter = int(filter) |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 26 | if isinstance(filter, int): |
| 27 | slices = Slice.objects.filter(id=filter) |
| 28 | elif isinstance(filter, StringTypes): |
| 29 | slices = Slice.objects.filter(name=filter) |
Tony Mack | 5df6c55 | 2013-04-11 21:24:17 -0400 | [diff] [blame] | 30 | elif isinstance(filter, dict): |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 31 | slices = Slice.objects.filter(**filter) |
| 32 | else: |
| 33 | slices = [] |
| 34 | return slices |
| 35 | |
Tony Mack | 3a1af41 | 2013-04-09 22:36:27 -0400 | [diff] [blame] | 36 | |
Tony Mack | 3a1af41 | 2013-04-09 22:36:27 -0400 | [diff] [blame] | 37 | def add_slice(auth, fields): |
| 38 | driver = OpenStackDriver(client = auth_check(auth)) |
Tony Mack | 771f4b9 | 2013-04-12 01:49:57 -0400 | [diff] [blame^] | 39 | validate_name(fields.get('name')) |
| 40 | login_base = fields['name'][:fields['name'].find('_')] |
| 41 | sites = _get_sites(login_base) |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 42 | if sites: fields['site'] = sites[0] |
Tony Mack | 3a1af41 | 2013-04-09 22:36:27 -0400 | [diff] [blame] | 43 | slice = Slice(**fields) |
Tony Mack | 771f4b9 | 2013-04-12 01:49:57 -0400 | [diff] [blame^] | 44 | |
Tony Mack | 3a1af41 | 2013-04-09 22:36:27 -0400 | [diff] [blame] | 45 | # create tenant |
| 46 | nova_fields = {'tenant_name': slice.name, |
| 47 | 'description': slice.description, |
| 48 | 'enabled': slice.enabled} |
| 49 | tenant = driver.create_tenant(**nova_fields) |
| 50 | slice.tenant_id=tenant.id |
| 51 | |
| 52 | # create network |
Tony Mack | 53c6334 | 2013-04-10 08:11:45 -0400 | [diff] [blame] | 53 | network = driver.create_network(slice.name) |
Tony Mack | d95d1aa | 2013-04-10 08:02:02 -0400 | [diff] [blame] | 54 | slice.network_id = network['id'] |
Tony Mack | 3a1af41 | 2013-04-09 22:36:27 -0400 | [diff] [blame] | 55 | |
| 56 | # create router |
Tony Mack | 53c6334 | 2013-04-10 08:11:45 -0400 | [diff] [blame] | 57 | router = driver.create_router(slice.name) |
Tony Mack | d95d1aa | 2013-04-10 08:02:02 -0400 | [diff] [blame] | 58 | slice.router_id = router['id'] |
Tony Mack | 3a1af41 | 2013-04-09 22:36:27 -0400 | [diff] [blame] | 59 | |
| 60 | slice.save() |
| 61 | return slice |
| 62 | |
| 63 | def update_slice(auth, id, **fields): |
| 64 | driver = OpenStackDriver(client = auth_check(auth)) |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 65 | slices = _get_slices(id) |
Tony Mack | 3a1af41 | 2013-04-09 22:36:27 -0400 | [diff] [blame] | 66 | if not slices: |
| 67 | return |
| 68 | |
| 69 | # update tenant |
| 70 | slice = slices[0] |
| 71 | nova_fields = {} |
| 72 | if 'name' in fields: |
| 73 | nova_fields['tenant_name'] = fields['name'] |
| 74 | if 'description' in fields: |
| 75 | nova_fields['description'] = fields['description'] |
| 76 | if 'enabled' in fields: |
| 77 | nova_fields['enabled'] = fields['enabled'] |
| 78 | driver.update_tenant(slice.tenant_id, **nova_fields) |
| 79 | |
| 80 | # update db record |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 81 | sites = _get_sites(fields.get('site')) |
| 82 | if sites: fields['site'] = sites[0] |
Tony Mack | 3a1af41 | 2013-04-09 22:36:27 -0400 | [diff] [blame] | 83 | slice.update(**fields) |
| 84 | |
| 85 | return slice |
| 86 | |
| 87 | def delete_slice(auth, filter={}): |
| 88 | driver = OpenStackDriver(client = auth_check(auth)) |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 89 | slices = _get_slices(id) |
Tony Mack | 3a1af41 | 2013-04-09 22:36:27 -0400 | [diff] [blame] | 90 | for slice in slices: |
| 91 | driver.delete_slice(id=slice.tenant_id) |
| 92 | slice.delete() |
| 93 | return 1 |
| 94 | |
| 95 | def get_slices(auth, filter={}): |
| 96 | client = auth_check(auth) |
Tony Mack | d95d1aa | 2013-04-10 08:02:02 -0400 | [diff] [blame] | 97 | if 'site' in filter: |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 98 | sites = _get_sites(filter.get('site')) |
| 99 | if sites: filter['site'] = sites[0] |
| 100 | slices = _get_slices(filter) |
Tony Mack | 3a1af41 | 2013-04-09 22:36:27 -0400 | [diff] [blame] | 101 | return slices |
| 102 | |
| 103 | |
| 104 | |