blob: 9ba5ab7d77be2a4dfc0909331e9c028f4eaca501 [file] [log] [blame]
Sapan Bhatia24836f12013-08-27 10:16:05 -04001import os
2import base64
Tony Mackae7f30c2013-09-25 12:46:50 -04003from django.db.models import F, Q
Sapan Bhatia24836f12013-08-27 10:16:05 -04004from planetstack.config import Config
Sapan Bhatia04c94ad2013-09-02 18:00:28 -04005from observer.openstacksyncstep import OpenStackSyncStep
6from core.models.slice import Slice
Sapan Bhatia24836f12013-08-27 10:16:05 -04007
8class SyncSlices(OpenStackSyncStep):
Tony Mackdacfb982013-09-24 21:57:16 -04009 provides=[Slice]
10 requested_interval=0
Tony Mack66646d52013-09-24 21:47:12 -040011
12 def fetch_pending(self):
13 return Slice.objects.filter(Q(enacted__lt=F('updated')) | Q(enacted=None))
14
Tony Mackdacfb982013-09-24 21:57:16 -040015 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 Bhatia24836f12013-08-27 10:16:05 -040022
Tony Mackdacfb982013-09-24 21:57:16 -040023 # 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 Bhatia24836f12013-08-27 10:16:05 -040025
Tony Mackdacfb982013-09-24 21:57:16 -040026 # 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 Bhatia24836f12013-08-27 10:16:05 -040030
Tony Mackdacfb982013-09-24 21:57:16 -040031 # create network
32 network = self.driver.create_network(slice.name)
33 slice.network_id = network['id']
Sapan Bhatia24836f12013-08-27 10:16:05 -040034
Tony Mackdacfb982013-09-24 21:57:16 -040035 # create router
36 router = self.driver.create_router(slice.name)
37 slice.router_id = router['id']
Sapan Bhatia24836f12013-08-27 10:16:05 -040038
Tony Mackdacfb982013-09-24 21:57:16 -040039 # 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 Bhatia24836f12013-08-27 10:16:05 -040056
57
Tony Mackdacfb982013-09-24 21:57:16 -040058 if slice.id and slice.tenant_id:
59 self.driver.update_tenant(slice.tenant_id,
60 description=slice.description,
61 enabled=slice.enabled)
Sapan Bhatia24836f12013-08-27 10:16:05 -040062
Tony Mackdacfb982013-09-24 21:57:16 -040063 slice.save()