blob: cc7dcce47eab41dff469d2e500453519640e8ddf [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)
Siobhan Tully231f4c82013-05-02 05:47:24 -040025
Siobhan Tully4bc09f22013-04-10 21:15:21 -040026 def __unicode__(self): return u'%s' % (self.name)
27
Tony Mack62bc59a2013-04-14 23:27:12 -040028 def save(self, *args, **kwds):
Scott Bakere8d596f2013-05-13 23:17:13 -070029 if self.serviceClass is None:
30 # We allowed None=True for serviceClass because Django evolution
31 # will fail unless it is allowed. But, we we really don't want it to
32 # ever save None, so fix it up here.
33 self.serviceClass = ServiceClass.get_default()
Siobhan Tully73291342013-05-10 10:50:08 -040034 if not hasattr(self, 'os_manager'):
Tony Mack89f70f12013-05-10 20:20:03 -040035 from openstack.manager import OpenStackManager
Siobhan Tully73291342013-05-10 10:50:08 -040036 setattr(self, 'os_manager', OpenStackManager())
Tony Mack89f70f12013-05-10 20:20:03 -040037 self.os_manager.save_slice(self)
Tony Mack62bc59a2013-04-14 23:27:12 -040038 super(Slice, self).save(*args, **kwds)
39
40 def delete(self, *args, **kwds):
Siobhan Tully73291342013-05-10 10:50:08 -040041 if not hasattr(self, 'os_manager'):
Tony Mack89f70f12013-05-10 20:20:03 -040042 from openstack.manager import OpenStackManager
Siobhan Tully73291342013-05-10 10:50:08 -040043 setattr(self, 'os_manager', OpenStackManager())
Tony Mack89f70f12013-05-10 20:20:03 -040044 self.os_manager.delete_slice(self)
Tony Mack62bc59a2013-04-14 23:27:12 -040045 super(Slice, self).delete(*args, **kwds)
46
Siobhan Tully4bc09f22013-04-10 21:15:21 -040047class SliceMembership(PlCoreBase):
Siobhan Tully30fd4292013-05-10 08:59:56 -040048 user = models.ForeignKey('User', related_name='slice_memberships')
Siobhan Tully4bc09f22013-04-10 21:15:21 -040049 slice = models.ForeignKey('Slice', related_name='slice_memberships')
50 role = models.ForeignKey('Role')
51
52 def __unicode__(self): return u'%s %s %s' % (self.slice, self.user, self.role)
53
Tony Mack00d361f2013-04-28 10:28:42 -040054 def save(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.add_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).save(*args, **kwds)
61
62 def delete(self, *args, **kwds):
Siobhan Tully73291342013-05-10 10:50:08 -040063 if not hasattr(self, 'os_manager'):
Tony Mack89f70f12013-05-10 20:20:03 -040064 from openstack.manager import OpenStackManager
Siobhan Tully73291342013-05-10 10:50:08 -040065 setattr(self, 'os_manager', OpenStackManager())
Siobhan Tullyaa1bcd52013-05-10 12:43:09 -040066 if self.os_manager.driver:
67 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 -040068 super(SliceMembership, self).delete(*args, **kwds)