blob: e0ce5e229612e6cf5de49e0087db5a65e0ea944f [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
Tony Mackcdec0902013-04-15 00:38:49 -040040 def save(self, *args, **kwds):
Tony Mack10082022013-05-06 17:15:00 -040041 if not self.name:
42 self.name = self.slice.name
Tony Mack2bd5b412013-06-11 21:05:06 -040043 if not self.creator and hasattr(self, 'caller'):
44 self.creator = self.caller
Tony Mack8ac1de72014-04-03 10:30:43 -040045 self.deploymentNetwork = self.node.deployment
Scott Baker5380c522014-06-06 14:49:43 -070046
Scott Bakerec06eba2014-06-20 18:03:04 -070047# XXX smbaker - disabled for now, was causing fault in tenant view create slice
48# if not self.deploymentNetwork.test_acl(slice=self.slice):
49# raise exceptions.ValidationError("Deployment %s's ACL does not allow any of this slice %s's users" % (self.deploymentNetwork.name, self.slice.name))
Scott Baker5380c522014-06-06 14:49:43 -070050
Tony Mackcdec0902013-04-15 00:38:49 -040051 super(Sliver, self).save(*args, **kwds)
Tony Mack5b061472014-02-04 07:57:10 -050052
53 def can_update(self, user):
54 return self.slice.can_update(user)
55
Scott Baker434ca7e2014-08-15 12:29:20 -070056 def all_ips(self):
57 ips={}
58 for ns in self.networksliver_set.all():
59 ips[ns.network.name] = ns.ip
60 return ips
61
62 def all_ips_string(self):
63 result = []
64 ips = self.all_ips()
65 for key in sorted(ips.keys()):
66 #result.append("%s = %s" % (key, ips[key]))
67 result.append(ips[key])
68 return ", ".join(result)
69 all_ips_string.short_description = "addresses"
70
Tony Mack5b061472014-02-04 07:57:10 -050071 @staticmethod
72 def select_by_user(user):
73 if user.is_admin:
74 qs = Sliver.objects.all()
75 else:
Tony Macke4be32f2014-03-11 20:45:25 -040076 slices = Slice.select_by_user(user)
77 qs = Sliver.objects.filter(slice__in=slices)
Tony Mack5b061472014-02-04 07:57:10 -050078 return qs