Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 1 | import os |
| 2 | from django.db import models |
Tony Mack | 2b241cf | 2013-04-16 21:57:55 -0400 | [diff] [blame] | 3 | from django.core import exceptions |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 4 | from core.models import PlCoreBase |
| 5 | from core.models import Image |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 6 | from core.models import Slice |
| 7 | from core.models import Node |
| 8 | from core.models import Site |
Siobhan Tully | bf1153a | 2013-05-27 20:53:48 -0400 | [diff] [blame] | 9 | from core.models import Deployment |
Tony Mack | b0d9742 | 2013-06-10 09:57:45 -0400 | [diff] [blame] | 10 | from core.models import User |
Siobhan Tully | de5450d | 2013-06-21 11:35:33 -0400 | [diff] [blame] | 11 | from core.models import Tag |
Scott Baker | 7a61dc4 | 2014-09-02 17:08:20 -0700 | [diff] [blame] | 12 | from core.models import Flavor |
Siobhan Tully | de5450d | 2013-06-21 11:35:33 -0400 | [diff] [blame] | 13 | from django.contrib.contenttypes import generic |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 14 | |
Scott Baker | 7a61dc4 | 2014-09-02 17:08:20 -0700 | [diff] [blame] | 15 | def 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 Baker | 7a61dc4 | 2014-09-02 17:08:20 -0700 | [diff] [blame] | 26 | return None |
| 27 | |
| 28 | for flavor in flavors: |
| 29 | if flavor.default: |
Scott Baker | 7a61dc4 | 2014-09-02 17:08:20 -0700 | [diff] [blame] | 30 | return flavor |
| 31 | |
Scott Baker | 7a61dc4 | 2014-09-02 17:08:20 -0700 | [diff] [blame] | 32 | return flavors[0] |
| 33 | |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 34 | # Create your models here. |
| 35 | class Sliver(PlCoreBase): |
Scott Baker | 7a61dc4 | 2014-09-02 17:08:20 -0700 | [diff] [blame] | 36 | instance_id = models.CharField(null=True, blank=True, max_length=200, help_text="Nova instance id") |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 37 | name = models.CharField(max_length=200, help_text="Sliver name") |
Tony Mack | 3811937 | 2013-04-30 18:17:38 -0400 | [diff] [blame] | 38 | instance_name = models.CharField(blank=True, null=True, max_length=200, help_text="OpenStack generated name") |
Tony Mack | 1eeeae9 | 2013-04-16 23:04:44 -0400 | [diff] [blame] | 39 | ip = models.GenericIPAddressField(help_text="Sliver ip address", blank=True, null=True) |
Tony Mack | 771f4b9 | 2013-04-12 01:49:57 -0400 | [diff] [blame] | 40 | image = models.ForeignKey(Image, related_name='slivers') |
Tony Mack | b0d9742 | 2013-06-10 09:57:45 -0400 | [diff] [blame] | 41 | #key = models.ForeignKey(Key, related_name='slivers') |
| 42 | creator = models.ForeignKey(User, related_name='slivers', blank=True, null=True) |
Tony Mack | 771f4b9 | 2013-04-12 01:49:57 -0400 | [diff] [blame] | 43 | slice = models.ForeignKey(Slice, related_name='slivers') |
| 44 | node = models.ForeignKey(Node, related_name='slivers') |
Siobhan Tully | bf1153a | 2013-05-27 20:53:48 -0400 | [diff] [blame] | 45 | deploymentNetwork = models.ForeignKey(Deployment, verbose_name='deployment', related_name='sliver_deploymentNetwork') |
smbaker | ddc21c7 | 2013-05-05 13:58:52 -0700 | [diff] [blame] | 46 | numberCores = models.IntegerField(verbose_name="Number of Cores", help_text="Number of cores for sliver", default=0) |
Scott Baker | 7a61dc4 | 2014-09-02 17:08:20 -0700 | [diff] [blame] | 47 | flavor = models.ForeignKey(Flavor, help_text="Flavor of this instance", default=get_default_flavor) |
Siobhan Tully | de5450d | 2013-06-21 11:35:33 -0400 | [diff] [blame] | 48 | tags = generic.GenericRelation(Tag) |
Scott Baker | 9c7a1e1 | 2014-05-30 14:42:42 -0700 | [diff] [blame] | 49 | userData = models.TextField(blank=True, null=True, help_text="user_data passed to instance during creation") |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 50 | |
Scott Baker | 3015c76 | 2013-08-23 10:28:07 -0700 | [diff] [blame] | 51 | 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 Mack | cdec090 | 2013-04-15 00:38:49 -0400 | [diff] [blame] | 61 | def save(self, *args, **kwds): |
Tony Mack | 38e7ae5 | 2014-09-24 10:08:51 -0400 | [diff] [blame] | 62 | self.name = self.slice.slicename |
Tony Mack | 2bd5b41 | 2013-06-11 21:05:06 -0400 | [diff] [blame] | 63 | if not self.creator and hasattr(self, 'caller'): |
| 64 | self.creator = self.caller |
Tony Mack | 8ac1de7 | 2014-04-03 10:30:43 -0400 | [diff] [blame] | 65 | self.deploymentNetwork = self.node.deployment |
Scott Baker | 5380c52 | 2014-06-06 14:49:43 -0700 | [diff] [blame] | 66 | |
Scott Baker | ec06eba | 2014-06-20 18:03:04 -0700 | [diff] [blame] | 67 | # 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 Baker | 5380c52 | 2014-06-06 14:49:43 -0700 | [diff] [blame] | 70 | |
Tony Mack | cdec090 | 2013-04-15 00:38:49 -0400 | [diff] [blame] | 71 | super(Sliver, self).save(*args, **kwds) |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 72 | |
| 73 | def can_update(self, user): |
| 74 | return self.slice.can_update(user) |
| 75 | |
Scott Baker | 434ca7e | 2014-08-15 12:29:20 -0700 | [diff] [blame] | 76 | 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 Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 91 | @staticmethod |
| 92 | def select_by_user(user): |
| 93 | if user.is_admin: |
| 94 | qs = Sliver.objects.all() |
| 95 | else: |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 96 | slices = Slice.select_by_user(user) |
| 97 | qs = Sliver.objects.filter(slice__in=slices) |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 98 | return qs |