blob: c694a1e98cb258cbccc36ec17090e6c01baf3bf8 [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
Scott Baker7a61dc42014-09-02 17:08:20 -070012from core.models import Flavor
Siobhan Tullyde5450d2013-06-21 11:35:33 -040013from django.contrib.contenttypes import generic
Siobhan Tully4bc09f22013-04-10 21:15:21 -040014
Scott Baker7a61dc42014-09-02 17:08:20 -070015def get_default_flavor(deployment = None):
16 # Find a default flavor that can be used for a sliver. This is particularly
17 # useful in evolution. It's also intended this helper function can be used
18 # for admin.py when users
19
20 if deployment:
21 flavors = deployment.flavors.all()
22 else:
23 flavors = Flavor.objects.all()
24
25 if not flavors:
Scott Baker7a61dc42014-09-02 17:08:20 -070026 return None
27
28 for flavor in flavors:
29 if flavor.default:
Scott Baker7a61dc42014-09-02 17:08:20 -070030 return flavor
31
Scott Baker7a61dc42014-09-02 17:08:20 -070032 return flavors[0]
33
Siobhan Tully4bc09f22013-04-10 21:15:21 -040034# Create your models here.
35class Sliver(PlCoreBase):
Scott Baker7a61dc42014-09-02 17:08:20 -070036 instance_id = models.CharField(null=True, blank=True, max_length=200, help_text="Nova instance id")
Siobhan Tully4bc09f22013-04-10 21:15:21 -040037 name = models.CharField(max_length=200, help_text="Sliver name")
Tony Mack38119372013-04-30 18:17:38 -040038 instance_name = models.CharField(blank=True, null=True, max_length=200, help_text="OpenStack generated name")
Tony Mack1eeeae92013-04-16 23:04:44 -040039 ip = models.GenericIPAddressField(help_text="Sliver ip address", blank=True, null=True)
Tony Mack771f4b92013-04-12 01:49:57 -040040 image = models.ForeignKey(Image, related_name='slivers')
Tony Mackb0d97422013-06-10 09:57:45 -040041 #key = models.ForeignKey(Key, related_name='slivers')
42 creator = models.ForeignKey(User, related_name='slivers', blank=True, null=True)
Tony Mack771f4b92013-04-12 01:49:57 -040043 slice = models.ForeignKey(Slice, related_name='slivers')
44 node = models.ForeignKey(Node, related_name='slivers')
Siobhan Tullybf1153a2013-05-27 20:53:48 -040045 deploymentNetwork = models.ForeignKey(Deployment, verbose_name='deployment', related_name='sliver_deploymentNetwork')
smbakerddc21c72013-05-05 13:58:52 -070046 numberCores = models.IntegerField(verbose_name="Number of Cores", help_text="Number of cores for sliver", default=0)
Scott Baker7a61dc42014-09-02 17:08:20 -070047 flavor = models.ForeignKey(Flavor, help_text="Flavor of this instance", default=get_default_flavor)
Siobhan Tullyde5450d2013-06-21 11:35:33 -040048 tags = generic.GenericRelation(Tag)
Scott Baker9c7a1e12014-05-30 14:42:42 -070049 userData = models.TextField(blank=True, null=True, help_text="user_data passed to instance during creation")
Siobhan Tully4bc09f22013-04-10 21:15:21 -040050
Scott Baker3015c762013-08-23 10:28:07 -070051 def __unicode__(self):
52 if self.instance_name:
53 return u'%s' % (self.instance_name)
54 elif self.id:
55 return u'uninstantiated-%s' % str(self.id)
56 elif self.slice:
57 return u'unsaved-sliver on %s' % self.slice.name
58 else:
59 return u'unsaved-sliver'
60
Tony Mackcdec0902013-04-15 00:38:49 -040061 def save(self, *args, **kwds):
Tony Mack38e7ae52014-09-24 10:08:51 -040062 self.name = self.slice.slicename
Tony Mack2bd5b412013-06-11 21:05:06 -040063 if not self.creator and hasattr(self, 'caller'):
64 self.creator = self.caller
Tony Mack8ac1de72014-04-03 10:30:43 -040065 self.deploymentNetwork = self.node.deployment
Scott Baker5380c522014-06-06 14:49:43 -070066
Scott Bakerec06eba2014-06-20 18:03:04 -070067# XXX smbaker - disabled for now, was causing fault in tenant view create slice
68# if not self.deploymentNetwork.test_acl(slice=self.slice):
69# 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 -070070
Tony Mackcdec0902013-04-15 00:38:49 -040071 super(Sliver, self).save(*args, **kwds)
Tony Mack5b061472014-02-04 07:57:10 -050072
73 def can_update(self, user):
74 return self.slice.can_update(user)
75
Scott Baker434ca7e2014-08-15 12:29:20 -070076 def all_ips(self):
77 ips={}
78 for ns in self.networksliver_set.all():
79 ips[ns.network.name] = ns.ip
80 return ips
81
82 def all_ips_string(self):
83 result = []
84 ips = self.all_ips()
85 for key in sorted(ips.keys()):
86 #result.append("%s = %s" % (key, ips[key]))
87 result.append(ips[key])
88 return ", ".join(result)
89 all_ips_string.short_description = "addresses"
90
Tony Mack5b061472014-02-04 07:57:10 -050091 @staticmethod
92 def select_by_user(user):
93 if user.is_admin:
94 qs = Sliver.objects.all()
95 else:
Tony Macke4be32f2014-03-11 20:45:25 -040096 slices = Slice.select_by_user(user)
97 qs = Sliver.objects.filter(slice__in=slices)
Tony Mack5b061472014-02-04 07:57:10 -050098 return qs