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 |
| 5 | from plstackapi.core.models import Site |
| 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] |
| 29 | if not site: |
Tony Mack | 67c6b1f | 2013-04-09 23:04:42 -0400 | [diff] [blame^] | 30 | raise Exception, "No such site: %s" % login_base |
Tony Mack | 3a1af41 | 2013-04-09 22:36:27 -0400 | [diff] [blame] | 31 | return site |
| 32 | |
| 33 | def add_slice(auth, fields): |
| 34 | driver = OpenStackDriver(client = auth_check(auth)) |
| 35 | site = lookup_site(fields) |
| 36 | if site: fields['site'] = site |
| 37 | slice = Slice(**fields) |
| 38 | # create tenant |
| 39 | nova_fields = {'tenant_name': slice.name, |
| 40 | 'description': slice.description, |
| 41 | 'enabled': slice.enabled} |
| 42 | tenant = driver.create_tenant(**nova_fields) |
| 43 | slice.tenant_id=tenant.id |
| 44 | |
| 45 | # create network |
| 46 | network = driver.create_network(name=self.name) |
| 47 | self.network_id = network['id'] |
| 48 | |
| 49 | # create router |
| 50 | router = driver.create_router(name=self.name) |
| 51 | self.router_id = router['id'] |
| 52 | |
| 53 | slice.save() |
| 54 | return slice |
| 55 | |
| 56 | def update_slice(auth, id, **fields): |
| 57 | driver = OpenStackDriver(client = auth_check(auth)) |
| 58 | slices = Slice.objects.filter(id=id) |
| 59 | if not slices: |
| 60 | return |
| 61 | |
| 62 | # update tenant |
| 63 | slice = slices[0] |
| 64 | nova_fields = {} |
| 65 | if 'name' in fields: |
| 66 | nova_fields['tenant_name'] = fields['name'] |
| 67 | if 'description' in fields: |
| 68 | nova_fields['description'] = fields['description'] |
| 69 | if 'enabled' in fields: |
| 70 | nova_fields['enabled'] = fields['enabled'] |
| 71 | driver.update_tenant(slice.tenant_id, **nova_fields) |
| 72 | |
| 73 | # update db record |
| 74 | site = lookup_site(fields) |
| 75 | if site: fields['site'] = site |
| 76 | slice.update(**fields) |
| 77 | |
| 78 | return slice |
| 79 | |
| 80 | def delete_slice(auth, filter={}): |
| 81 | driver = OpenStackDriver(client = auth_check(auth)) |
| 82 | slices = Slice.objects.filter(**filter) |
| 83 | for slice in slices: |
| 84 | driver.delete_slice(id=slice.tenant_id) |
| 85 | slice.delete() |
| 86 | return 1 |
| 87 | |
| 88 | def get_slices(auth, filter={}): |
| 89 | client = auth_check(auth) |
| 90 | site = lookup_site(fields) |
| 91 | if site: fields['site'] = site |
| 92 | slices = Slice.objects.filter(**filter) |
| 93 | return slices |
| 94 | |
| 95 | |
| 96 | |