blob: d9d2028b34619d3197221cfc3b99dc71e28acc04 [file] [log] [blame]
Siobhan Tully4bc09f22013-04-10 21:15:21 -04001import os
2from django.db import models
Sapan Bhatia14356b72014-11-05 10:32:41 -05003from django.db.models import Q
Tony Mack2b241cf2013-04-16 21:57:55 -04004from django.core import exceptions
Sapan Bhatia14356b72014-11-05 10:32:41 -05005from core.models import PlCoreBase,PlCoreBaseManager,PlCoreBaseDeletionManager
Siobhan Tully30fd4292013-05-10 08:59:56 -04006from core.models import Image
Siobhan Tully30fd4292013-05-10 08:59:56 -04007from core.models import Slice
8from core.models import Node
9from core.models import Site
Siobhan Tullybf1153a2013-05-27 20:53:48 -040010from core.models import Deployment
Tony Mackb0d97422013-06-10 09:57:45 -040011from core.models import User
Siobhan Tullyde5450d2013-06-21 11:35:33 -040012from core.models import Tag
Scott Baker7a61dc42014-09-02 17:08:20 -070013from core.models import Flavor
Siobhan Tullyde5450d2013-06-21 11:35:33 -040014from django.contrib.contenttypes import generic
Sapan Bhatia14356b72014-11-05 10:32:41 -050015from planetstack.config import Config
16
17config = Config()
Siobhan Tully4bc09f22013-04-10 21:15:21 -040018
Scott Baker7a61dc42014-09-02 17:08:20 -070019def get_default_flavor(deployment = None):
20 # Find a default flavor that can be used for a sliver. This is particularly
21 # useful in evolution. It's also intended this helper function can be used
22 # for admin.py when users
23
24 if deployment:
25 flavors = deployment.flavors.all()
26 else:
27 flavors = Flavor.objects.all()
28
29 if not flavors:
Scott Baker7a61dc42014-09-02 17:08:20 -070030 return None
31
32 for flavor in flavors:
33 if flavor.default:
Scott Baker7a61dc42014-09-02 17:08:20 -070034 return flavor
35
Scott Baker7a61dc42014-09-02 17:08:20 -070036 return flavors[0]
37
Sapan Bhatia14356b72014-11-05 10:32:41 -050038class SliverDeletionManager(PlCoreBaseDeletionManager):
39 def get_queryset(self):
40 parent=super(SliverDeletionManager, self)
41 try:
42 backend_type = config.observer_backend_type
43 except AttributeError:
44 backend_type = None
45
46 parent_queryset = parent.get_queryset() if hasattr(parent, "get_queryset") else parent.get_query_set()
47 if (backend_type):
48 return parent_queryset.filter(Q(node__deployment__backend_type=backend_type))
49 else:
50 return parent_queryset
51
52 # deprecated in django 1.7 in favor of get_queryset().
53 def get_query_set(self):
54 return self.get_queryset()
55
56
57class SliverManager(PlCoreBaseManager):
58 def get_queryset(self):
59 parent=super(SliverManager, self)
60
61 try:
62 backend_type = config.observer_backend_type
63 except AttributeError:
64 backend_type = None
65
66 parent_queryset = parent.get_queryset() if hasattr(parent, "get_queryset") else parent.get_query_set()
67
68 if backend_type:
69 return parent_queryset.filter(Q(node__deployment__backend_type=backend_type))
70 else:
71 return parent_queryset
72
73 # deprecated in django 1.7 in favor of get_queryset().
74 def get_query_set(self):
75 return self.get_queryset()
76
Siobhan Tully4bc09f22013-04-10 21:15:21 -040077# Create your models here.
78class Sliver(PlCoreBase):
Sapan Bhatia14356b72014-11-05 10:32:41 -050079 objects = SliverManager()
80 deleted_objects = SliverDeletionManager()
Scott Baker7a61dc42014-09-02 17:08:20 -070081 instance_id = models.CharField(null=True, blank=True, max_length=200, help_text="Nova instance id")
Siobhan Tully4bc09f22013-04-10 21:15:21 -040082 name = models.CharField(max_length=200, help_text="Sliver name")
Tony Mack38119372013-04-30 18:17:38 -040083 instance_name = models.CharField(blank=True, null=True, max_length=200, help_text="OpenStack generated name")
Tony Mack1eeeae92013-04-16 23:04:44 -040084 ip = models.GenericIPAddressField(help_text="Sliver ip address", blank=True, null=True)
Tony Mack771f4b92013-04-12 01:49:57 -040085 image = models.ForeignKey(Image, related_name='slivers')
Tony Mackb0d97422013-06-10 09:57:45 -040086 #key = models.ForeignKey(Key, related_name='slivers')
87 creator = models.ForeignKey(User, related_name='slivers', blank=True, null=True)
Tony Mack771f4b92013-04-12 01:49:57 -040088 slice = models.ForeignKey(Slice, related_name='slivers')
89 node = models.ForeignKey(Node, related_name='slivers')
Siobhan Tullybf1153a2013-05-27 20:53:48 -040090 deploymentNetwork = models.ForeignKey(Deployment, verbose_name='deployment', related_name='sliver_deploymentNetwork')
smbakerddc21c72013-05-05 13:58:52 -070091 numberCores = models.IntegerField(verbose_name="Number of Cores", help_text="Number of cores for sliver", default=0)
Scott Baker7a61dc42014-09-02 17:08:20 -070092 flavor = models.ForeignKey(Flavor, help_text="Flavor of this instance", default=get_default_flavor)
Siobhan Tullyde5450d2013-06-21 11:35:33 -040093 tags = generic.GenericRelation(Tag)
Scott Baker9c7a1e12014-05-30 14:42:42 -070094 userData = models.TextField(blank=True, null=True, help_text="user_data passed to instance during creation")
Siobhan Tully4bc09f22013-04-10 21:15:21 -040095
Scott Baker3015c762013-08-23 10:28:07 -070096 def __unicode__(self):
97 if self.instance_name:
98 return u'%s' % (self.instance_name)
99 elif self.id:
100 return u'uninstantiated-%s' % str(self.id)
101 elif self.slice:
102 return u'unsaved-sliver on %s' % self.slice.name
103 else:
104 return u'unsaved-sliver'
105
Tony Mackcdec0902013-04-15 00:38:49 -0400106 def save(self, *args, **kwds):
Tony Mack38e7ae52014-09-24 10:08:51 -0400107 self.name = self.slice.slicename
Tony Mack2bd5b412013-06-11 21:05:06 -0400108 if not self.creator and hasattr(self, 'caller'):
109 self.creator = self.caller
Tony Mack8ac1de72014-04-03 10:30:43 -0400110 self.deploymentNetwork = self.node.deployment
Scott Baker5380c522014-06-06 14:49:43 -0700111
Scott Bakerec06eba2014-06-20 18:03:04 -0700112# XXX smbaker - disabled for now, was causing fault in tenant view create slice
113# if not self.deploymentNetwork.test_acl(slice=self.slice):
114# 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 -0700115
Tony Mackcdec0902013-04-15 00:38:49 -0400116 super(Sliver, self).save(*args, **kwds)
Tony Mack5b061472014-02-04 07:57:10 -0500117
118 def can_update(self, user):
119 return self.slice.can_update(user)
120
Scott Baker434ca7e2014-08-15 12:29:20 -0700121 def all_ips(self):
122 ips={}
123 for ns in self.networksliver_set.all():
124 ips[ns.network.name] = ns.ip
125 return ips
126
127 def all_ips_string(self):
128 result = []
129 ips = self.all_ips()
130 for key in sorted(ips.keys()):
131 #result.append("%s = %s" % (key, ips[key]))
132 result.append(ips[key])
133 return ", ".join(result)
134 all_ips_string.short_description = "addresses"
135
Tony Mack5b061472014-02-04 07:57:10 -0500136 @staticmethod
137 def select_by_user(user):
138 if user.is_admin:
139 qs = Sliver.objects.all()
140 else:
Tony Macke4be32f2014-03-11 20:45:25 -0400141 slices = Slice.select_by_user(user)
142 qs = Sliver.objects.filter(slice__in=slices)
Tony Mack5b061472014-02-04 07:57:10 -0500143 return qs