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 | 19fedd8 | 2013-04-09 23:20:59 -0400 | [diff] [blame^] | 5 | from plstackapi.core.models import Slice |
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']) |
| 25 | login_base = fields['name'][:fields['name'].find('@')] |
| 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 |
| 51 | network = driver.create_network(name=self.name) |
| 52 | self.network_id = network['id'] |
| 53 | |
| 54 | # create router |
| 55 | router = driver.create_router(name=self.name) |
| 56 | self.router_id = router['id'] |
| 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 | fbf287c | 2013-04-09 23:16:28 -0400 | [diff] [blame] | 95 | site = lookup_site(filter) |
| 96 | if site: filter['site'] = site |
Tony Mack | 3a1af41 | 2013-04-09 22:36:27 -0400 | [diff] [blame] | 97 | slices = Slice.objects.filter(**filter) |
| 98 | return slices |
| 99 | |
| 100 | |
| 101 | |