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] |
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 | if not site: |
Tony Mack | 4b68fad | 2013-04-09 23:17:25 -0400 | [diff] [blame^] | 37 | raise Exception, "No such site: %s" % fields |
Tony Mack | 3a1af41 | 2013-04-09 22:36:27 -0400 | [diff] [blame] | 38 | return site |
| 39 | |
| 40 | def add_slice(auth, fields): |
| 41 | driver = OpenStackDriver(client = auth_check(auth)) |
| 42 | site = lookup_site(fields) |
| 43 | if site: fields['site'] = site |
| 44 | slice = Slice(**fields) |
| 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 |
| 53 | network = driver.create_network(name=self.name) |
| 54 | self.network_id = network['id'] |
| 55 | |
| 56 | # create router |
| 57 | router = driver.create_router(name=self.name) |
| 58 | self.router_id = router['id'] |
| 59 | |
| 60 | slice.save() |
| 61 | return slice |
| 62 | |
| 63 | def update_slice(auth, id, **fields): |
| 64 | driver = OpenStackDriver(client = auth_check(auth)) |
| 65 | slices = Slice.objects.filter(id=id) |
| 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 |
| 81 | site = lookup_site(fields) |
| 82 | if site: fields['site'] = site |
| 83 | slice.update(**fields) |
| 84 | |
| 85 | return slice |
| 86 | |
| 87 | def delete_slice(auth, filter={}): |
| 88 | driver = OpenStackDriver(client = auth_check(auth)) |
| 89 | slices = Slice.objects.filter(**filter) |
| 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 | fbf287c | 2013-04-09 23:16:28 -0400 | [diff] [blame] | 97 | site = lookup_site(filter) |
| 98 | if site: filter['site'] = site |
Tony Mack | 3a1af41 | 2013-04-09 22:36:27 -0400 | [diff] [blame] | 99 | slices = Slice.objects.filter(**filter) |
| 100 | return slices |
| 101 | |
| 102 | |
| 103 | |