blob: 6351bd14d231b6881cb32580ec5d957d1a84d341 [file] [log] [blame]
Siobhan Tully4bc09f22013-04-10 21:15:21 -04001import os
2from django.db import models
Tony Mack2b241cf2013-04-16 21:57:55 -04003from django.core import exceptions
Siobhan Tully30fd4292013-05-10 08:59:56 -04004from core.models import PlCoreBase
5from core.models import Image
Siobhan Tully30fd4292013-05-10 08:59:56 -04006from core.models import Slice
7from core.models import Node
8from core.models import Site
Siobhan Tullybf1153a2013-05-27 20:53:48 -04009from core.models import Deployment
Tony Mackb0d97422013-06-10 09:57:45 -040010from core.models import User
Siobhan Tullyde5450d2013-06-21 11:35:33 -040011from core.models import Tag
12from django.contrib.contenttypes import generic
Siobhan Tully4bc09f22013-04-10 21:15:21 -040013
14# Create your models here.
15class Sliver(PlCoreBase):
Tony Mack85d18832013-05-09 17:02:31 -040016 instance_id = models.CharField(null=True, blank=True, max_length=200, help_text="Nova instance id")
Siobhan Tully4bc09f22013-04-10 21:15:21 -040017 name = models.CharField(max_length=200, help_text="Sliver name")
Tony Mack38119372013-04-30 18:17:38 -040018 instance_name = models.CharField(blank=True, null=True, max_length=200, help_text="OpenStack generated name")
Tony Mack1eeeae92013-04-16 23:04:44 -040019 ip = models.GenericIPAddressField(help_text="Sliver ip address", blank=True, null=True)
Tony Mack771f4b92013-04-12 01:49:57 -040020 image = models.ForeignKey(Image, related_name='slivers')
Tony Mackb0d97422013-06-10 09:57:45 -040021 #key = models.ForeignKey(Key, related_name='slivers')
22 creator = models.ForeignKey(User, related_name='slivers', blank=True, null=True)
Tony Mack771f4b92013-04-12 01:49:57 -040023 slice = models.ForeignKey(Slice, related_name='slivers')
24 node = models.ForeignKey(Node, related_name='slivers')
Siobhan Tullybf1153a2013-05-27 20:53:48 -040025 deploymentNetwork = models.ForeignKey(Deployment, verbose_name='deployment', related_name='sliver_deploymentNetwork')
smbakerddc21c72013-05-05 13:58:52 -070026 numberCores = models.IntegerField(verbose_name="Number of Cores", help_text="Number of cores for sliver", default=0)
Siobhan Tullyde5450d2013-06-21 11:35:33 -040027 tags = generic.GenericRelation(Tag)
Scott Baker9c7a1e12014-05-30 14:42:42 -070028 userData = models.TextField(blank=True, null=True, help_text="user_data passed to instance during creation")
Siobhan Tully4bc09f22013-04-10 21:15:21 -040029
Scott Baker3015c762013-08-23 10:28:07 -070030 def __unicode__(self):
31 if self.instance_name:
32 return u'%s' % (self.instance_name)
33 elif self.id:
34 return u'uninstantiated-%s' % str(self.id)
35 elif self.slice:
36 return u'unsaved-sliver on %s' % self.slice.name
37 else:
38 return u'unsaved-sliver'
39
Siobhan Tully4bc09f22013-04-10 21:15:21 -040040
Tony Mackcdec0902013-04-15 00:38:49 -040041 def save(self, *args, **kwds):
Tony Mack10082022013-05-06 17:15:00 -040042 if not self.name:
43 self.name = self.slice.name
Tony Mack2bd5b412013-06-11 21:05:06 -040044 if not self.creator and hasattr(self, 'caller'):
45 self.creator = self.caller
Tony Mack8ac1de72014-04-03 10:30:43 -040046 self.deploymentNetwork = self.node.deployment
Tony Mackcdec0902013-04-15 00:38:49 -040047 super(Sliver, self).save(*args, **kwds)
Tony Mack5b061472014-02-04 07:57:10 -050048
49 def can_update(self, user):
50 return self.slice.can_update(user)
51
Tony Mack5b061472014-02-04 07:57:10 -050052 @staticmethod
53 def select_by_user(user):
54 if user.is_admin:
55 qs = Sliver.objects.all()
56 else:
Tony Macke4be32f2014-03-11 20:45:25 -040057 slices = Slice.select_by_user(user)
58 qs = Sliver.objects.filter(slice__in=slices)
Tony Mack5b061472014-02-04 07:57:10 -050059 return qs