blob: da9379d33ca8c184a1ee85049260bc55bfdf4cd5 [file] [log] [blame]
Siobhan Tully4bc09f22013-04-10 21:15:21 -04001import os
2from django.db import models
Siobhan Tully30fd4292013-05-10 08:59:56 -04003from core.models import PlCoreBase
4from core.models import Site
5from core.models import User
6from core.models import Role
Siobhan Tullybf1153a2013-05-27 20:53:48 -04007from core.models import Deployment
Scott Bakere8d596f2013-05-13 23:17:13 -07008from core.models import ServiceClass
Siobhan Tully4bc09f22013-04-10 21:15:21 -04009
10# Create your models here.
11
12class Slice(PlCoreBase):
Tony Mackcdec0902013-04-15 00:38:49 -040013 tenant_id = models.CharField(max_length=200, help_text="Keystone tenant id")
Tony Mack48952032013-04-12 11:49:34 -040014 name = models.CharField(unique=True, help_text="The Name of the Slice", max_length=80)
Siobhan Tully4bc09f22013-04-10 21:15:21 -040015 enabled = models.BooleanField(default=True, help_text="Status for this Slice")
Siobhan Tully4bc09f22013-04-10 21:15:21 -040016 omf_friendly = models.BooleanField()
17 description=models.TextField(blank=True,help_text="High level description of the slice and expected activities", max_length=1024)
18 slice_url = models.URLField(blank=True, max_length=512)
19 site = models.ForeignKey(Site, related_name='slices', help_text="The Site this Node belongs too")
Tony Mack85d18832013-05-09 17:02:31 -040020 network_id = models.CharField(null=True, blank=True, max_length=256, help_text="Quantum network")
21 router_id = models.CharField(null=True, blank=True, max_length=256, help_text="Quantum router id")
22 subnet_id = models.CharField(null=True, blank=True, max_length=256, help_text="Quantum subnet id")
Siobhan Tully4bc09f22013-04-10 21:15:21 -040023
Scott Bakere8d596f2013-05-13 23:17:13 -070024 serviceClass = models.ForeignKey(ServiceClass, related_name = "slices", null=True, default=ServiceClass.get_default)
Tony Mack2bd5b412013-06-11 21:05:06 -040025 creator = models.ForeignKey(User, related_name='slices', blank=True, null=True)
Siobhan Tully231f4c82013-05-02 05:47:24 -040026
Siobhan Tully4bc09f22013-04-10 21:15:21 -040027 def __unicode__(self): return u'%s' % (self.name)
28
Tony Mack62bc59a2013-04-14 23:27:12 -040029 def save(self, *args, **kwds):
Scott Bakere8d596f2013-05-13 23:17:13 -070030 if self.serviceClass is None:
31 # We allowed None=True for serviceClass because Django evolution
32 # will fail unless it is allowed. But, we we really don't want it to
33 # ever save None, so fix it up here.
34 self.serviceClass = ServiceClass.get_default()
Tony Mack2bd5b412013-06-11 21:05:06 -040035 if not self.creator and hasattr(self, 'caller'):
36 self.creator = self.caller
Tony Mack62bc59a2013-04-14 23:27:12 -040037 super(Slice, self).save(*args, **kwds)
38
Siobhan Tully4bc09f22013-04-10 21:15:21 -040039class SliceMembership(PlCoreBase):
Siobhan Tully30fd4292013-05-10 08:59:56 -040040 user = models.ForeignKey('User', related_name='slice_memberships')
Siobhan Tully4bc09f22013-04-10 21:15:21 -040041 slice = models.ForeignKey('Slice', related_name='slice_memberships')
42 role = models.ForeignKey('Role')
43
44 def __unicode__(self): return u'%s %s %s' % (self.slice, self.user, self.role)
45
Tony Mack00d361f2013-04-28 10:28:42 -040046 def save(self, *args, **kwds):
Siobhan Tully73291342013-05-10 10:50:08 -040047 if not hasattr(self, 'os_manager'):
Tony Mack89f70f12013-05-10 20:20:03 -040048 from openstack.manager import OpenStackManager
Siobhan Tully73291342013-05-10 10:50:08 -040049 setattr(self, 'os_manager', OpenStackManager())
Siobhan Tullyaa1bcd52013-05-10 12:43:09 -040050 if self.os_manager.driver:
51 self.os_manager.driver.add_user_role(self.user.kuser_id, self.slice.tenant_id, self.role.role_type)
Tony Mack00d361f2013-04-28 10:28:42 -040052 super(SliceMembership, self).save(*args, **kwds)
53
54 def delete(self, *args, **kwds):
Siobhan Tully73291342013-05-10 10:50:08 -040055 if not hasattr(self, 'os_manager'):
Tony Mack89f70f12013-05-10 20:20:03 -040056 from openstack.manager import OpenStackManager
Siobhan Tully73291342013-05-10 10:50:08 -040057 setattr(self, 'os_manager', OpenStackManager())
Siobhan Tullyaa1bcd52013-05-10 12:43:09 -040058 if self.os_manager.driver:
59 self.os_manager.driver.delete_user_role(self.user.kuser_id, self.slice.tenant_id, self.role.role_type)
Tony Mack00d361f2013-04-28 10:28:42 -040060 super(SliceMembership, self).delete(*args, **kwds)