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