blob: 16a2c27183d6d2c1906d0586afbf1048324ba464 [file] [log] [blame]
Siobhan Tully4bc09f22013-04-10 21:15:21 -04001import os
2from django.db import models
3from plstackapi.core.models import PlCoreBase
4from plstackapi.core.models import Site
Siobhan Tully53437282013-04-26 19:30:27 -04005from plstackapi.core.models import PLUser
Siobhan Tully4bc09f22013-04-10 21:15:21 -04006from plstackapi.core.models import Role
7from plstackapi.core.models import DeploymentNetwork
Siobhan Tully4bc09f22013-04-10 21:15:21 -04008
9# Create your models here.
10
11class Slice(PlCoreBase):
Tony Mackcdec0902013-04-15 00:38:49 -040012 tenant_id = models.CharField(max_length=200, help_text="Keystone tenant id")
Tony Mack48952032013-04-12 11:49:34 -040013 name = models.CharField(unique=True, help_text="The Name of the Slice", max_length=80)
Siobhan Tully4bc09f22013-04-10 21:15:21 -040014 enabled = models.BooleanField(default=True, help_text="Status for this Slice")
15 SLICE_CHOICES = (('plc', 'PLC'), ('delegated', 'Delegated'), ('controller','Controller'), ('none','None'))
16 instantiation = models.CharField(help_text="The instantiation type of the slice", max_length=80, choices=SLICE_CHOICES)
17 omf_friendly = models.BooleanField()
18 description=models.TextField(blank=True,help_text="High level description of the slice and expected activities", max_length=1024)
19 slice_url = models.URLField(blank=True, max_length=512)
20 site = models.ForeignKey(Site, related_name='slices', help_text="The Site this Node belongs too")
21 network_id = models.CharField(max_length=256, help_text="Quantum network")
22 router_id = models.CharField(max_length=256, help_text="Quantum router id")
23
Siobhan Tully231f4c82013-05-02 05:47:24 -040024 SVC_CLASS_CHOICES = (('besteffort', 'Best Effort'), ('silver', 'Silver'), ('gold','Gold'))
25 serviceClass = models.CharField(verbose_name="Service Class",default="besteffort",help_text="The Service Class of this slice", max_length=30, choices=SVC_CLASS_CHOICES)
26
27
Siobhan Tully4bc09f22013-04-10 21:15:21 -040028 def __unicode__(self): return u'%s' % (self.name)
29
Tony Mack62bc59a2013-04-14 23:27:12 -040030 def save(self, *args, **kwds):
Tony Mack62bc59a2013-04-14 23:27:12 -040031 if not self.tenant_id:
32 nova_fields = {'tenant_name': self.name,
33 'description': self.description,
34 'enabled': self.enabled}
Tony Mackfdd4d802013-04-27 13:02:33 -040035 tenant = self.driver.create_tenant(**nova_fields)
Tony Mack62bc59a2013-04-14 23:27:12 -040036 self.tenant_id = tenant.id
37
Tony Mack9bcbe4f2013-04-29 08:13:27 -040038 # give caller an admin role at the tenant they've created
39 self.driver.add_user_role(self.caller.user_id, tenant.id, 'admin')
40
Tony Mack5d0ce612013-04-29 17:53:07 -040041 # refresh credentials using this tenant
42 self.driver.shell.connect(username=self.driver.shell.keystone.username,
43 password=self.driver.shell.keystone.password,
44 tenant=tenant.name)
45
Tony Mack62bc59a2013-04-14 23:27:12 -040046 # create network
Tony Mackfdd4d802013-04-27 13:02:33 -040047 network = self.driver.create_network(self.name)
Tony Mack62bc59a2013-04-14 23:27:12 -040048 self.network_id = network['id']
49
50 # create router
Tony Mackfdd4d802013-04-27 13:02:33 -040051 router = self.driver.create_router(self.name)
Tony Mack62bc59a2013-04-14 23:27:12 -040052 self.router_id = router['id']
53
Tony Mack54304c62013-04-28 11:27:07 -040054 if self.id:
55 self.driver.update_tenant(self.tenant_id,
56 description=self.description,
57 enabled=self.enabled)
58
Tony Mack62bc59a2013-04-14 23:27:12 -040059 super(Slice, self).save(*args, **kwds)
60
61 def delete(self, *args, **kwds):
Tony Mack62bc59a2013-04-14 23:27:12 -040062 if self.tenant_id:
Tony Mackfdd4d802013-04-27 13:02:33 -040063 self.driver.delete_router(self.router_id)
64 self.driver.delete_network(self.network_id)
65 self.driver.delete_tenant(self.tenant_id)
Tony Mack62bc59a2013-04-14 23:27:12 -040066
67 super(Slice, self).delete(*args, **kwds)
68
Siobhan Tully4bc09f22013-04-10 21:15:21 -040069class SliceMembership(PlCoreBase):
Siobhan Tully53437282013-04-26 19:30:27 -040070 user = models.ForeignKey('PLUser', related_name='slice_memberships')
Siobhan Tully4bc09f22013-04-10 21:15:21 -040071 slice = models.ForeignKey('Slice', related_name='slice_memberships')
72 role = models.ForeignKey('Role')
73
74 def __unicode__(self): return u'%s %s %s' % (self.slice, self.user, self.role)
75
Tony Mack00d361f2013-04-28 10:28:42 -040076 def save(self, *args, **kwds):
77 self.driver.add_user_role(self.user.user_id, self.slice.tenant_id, self.role.role_type)
78 super(SliceMembership, self).save(*args, **kwds)
79
80 def delete(self, *args, **kwds):
81 self.driver.delete_user_role(self.user.user_id, self.slice.tenant_id, self.role.role_type)
82 super(SliceMembership, self).delete(*args, **kwds)