Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 1 | import os |
| 2 | import base64 |
Tony Mack | ae7f30c | 2013-09-25 12:46:50 -0400 | [diff] [blame] | 3 | from django.db.models import F, Q |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 4 | from planetstack.config import Config |
Sapan Bhatia | 04c94ad | 2013-09-02 18:00:28 -0400 | [diff] [blame] | 5 | from observer.openstacksyncstep import OpenStackSyncStep |
| 6 | from core.models.slice import Slice |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 7 | |
| 8 | class SyncSlices(OpenStackSyncStep): |
Tony Mack | dacfb98 | 2013-09-24 21:57:16 -0400 | [diff] [blame] | 9 | provides=[Slice] |
| 10 | requested_interval=0 |
Tony Mack | 66646d5 | 2013-09-24 21:47:12 -0400 | [diff] [blame] | 11 | |
| 12 | def fetch_pending(self): |
| 13 | return Slice.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None)) |
| 14 | |
Tony Mack | dacfb98 | 2013-09-24 21:57:16 -0400 | [diff] [blame] | 15 | def sync_record(self, slice): |
| 16 | if not slice.tenant_id: |
| 17 | nova_fields = {'tenant_name': slice.name, |
| 18 | 'description': slice.description, |
| 19 | 'enabled': slice.enabled} |
| 20 | tenant = self.driver.create_tenant(**nova_fields) |
| 21 | slice.tenant_id = tenant.id |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 22 | |
Tony Mack | dacfb98 | 2013-09-24 21:57:16 -0400 | [diff] [blame] | 23 | # XXX give caller an admin role at the tenant they've created |
| 24 | self.driver.add_user_role(self.caller.kuser_id, tenant.id, 'admin') |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 25 | |
Tony Mack | dacfb98 | 2013-09-24 21:57:16 -0400 | [diff] [blame] | 26 | # refresh credentials using this tenant |
| 27 | self.driver.shell.connect(username=self.driver.shell.keystone.username, |
| 28 | password=self.driver.shell.keystone.password, |
| 29 | tenant=tenant.name) |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 30 | |
Tony Mack | dacfb98 | 2013-09-24 21:57:16 -0400 | [diff] [blame] | 31 | # create network |
| 32 | network = self.driver.create_network(slice.name) |
| 33 | slice.network_id = network['id'] |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 34 | |
Tony Mack | dacfb98 | 2013-09-24 21:57:16 -0400 | [diff] [blame] | 35 | # create router |
| 36 | router = self.driver.create_router(slice.name) |
| 37 | slice.router_id = router['id'] |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 38 | |
Tony Mack | dacfb98 | 2013-09-24 21:57:16 -0400 | [diff] [blame] | 39 | # create subnet |
| 40 | next_subnet = self.get_next_subnet() |
| 41 | cidr = str(next_subnet.cidr) |
| 42 | ip_version = next_subnet.version |
| 43 | start = str(next_subnet[2]) |
| 44 | end = str(next_subnet[-2]) |
| 45 | subnet = self.driver.create_subnet(name=slice.name, |
| 46 | network_id = network['id'], |
| 47 | cidr_ip = cidr, |
| 48 | ip_version = ip_version, |
| 49 | start = start, |
| 50 | end = end) |
| 51 | slice.subnet_id = subnet['id'] |
| 52 | # add subnet as interface to slice's router |
| 53 | self.driver.add_router_interface(router['id'], subnet['id']) |
| 54 | # add external route |
| 55 | self.driver.add_external_route(subnet) |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 56 | |
| 57 | |
Tony Mack | dacfb98 | 2013-09-24 21:57:16 -0400 | [diff] [blame] | 58 | if slice.id and slice.tenant_id: |
| 59 | self.driver.update_tenant(slice.tenant_id, |
| 60 | description=slice.description, |
| 61 | enabled=slice.enabled) |
Sapan Bhatia | 24836f1 | 2013-08-27 10:16:05 -0400 | [diff] [blame] | 62 | |
Tony Mack | dacfb98 | 2013-09-24 21:57:16 -0400 | [diff] [blame] | 63 | slice.save() |