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 | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 4 | from plstackapi.core.models import PlCoreBase |
| 5 | from plstackapi.core.models import Flavor |
| 6 | from plstackapi.core.models import Image |
| 7 | from plstackapi.core.models import Key |
| 8 | from plstackapi.core.models import Slice |
| 9 | from plstackapi.core.models import Node |
| 10 | from plstackapi.core.models import Site |
| 11 | from plstackapi.core.models import DeploymentNetwork |
| 12 | from plstackapi.openstack.driver import OpenStackDriver |
| 13 | |
| 14 | # Create your models here. |
| 15 | class Sliver(PlCoreBase): |
| 16 | instance_id = models.CharField(max_length=200, help_text="Nova instance id") |
| 17 | name = models.CharField(max_length=200, help_text="Sliver name") |
Tony Mack | 1eeeae9 | 2013-04-16 23:04:44 -0400 | [diff] [blame] | 18 | ip = models.GenericIPAddressField(help_text="Sliver ip address", blank=True, null=True) |
Tony Mack | 771f4b9 | 2013-04-12 01:49:57 -0400 | [diff] [blame] | 19 | flavor = models.ForeignKey(Flavor, related_name='slivers') |
| 20 | image = models.ForeignKey(Image, related_name='slivers') |
| 21 | key = models.ForeignKey(Key, related_name='slivers') |
| 22 | slice = models.ForeignKey(Slice, related_name='slivers') |
| 23 | node = models.ForeignKey(Node, related_name='slivers') |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 24 | deploymentNetwork = models.ForeignKey(DeploymentNetwork, related_name='sliver_deploymentNetwork') |
| 25 | |
Tony Mack | 29c287f | 2013-04-11 21:07:16 -0400 | [diff] [blame] | 26 | def __unicode__(self): return u'%s::%s' % (self.slice, self.deploymentNetwork) |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 27 | |
Tony Mack | cdec090 | 2013-04-15 00:38:49 -0400 | [diff] [blame] | 28 | def save(self, *args, **kwds): |
Tony Mack | 8e92dc5 | 2013-04-16 21:58:39 -0400 | [diff] [blame] | 29 | if not self.slice.subnet.exists(): |
Tony Mack | 2b241cf | 2013-04-16 21:57:55 -0400 | [diff] [blame] | 30 | raise exceptions.ValidationError, "Slice %s has no subnet" % self.slice.name |
| 31 | |
Tony Mack | cdec090 | 2013-04-15 00:38:49 -0400 | [diff] [blame] | 32 | if not self.instance_id: |
Tony Mack | 2b241cf | 2013-04-16 21:57:55 -0400 | [diff] [blame] | 33 | driver = OpenStackDriver() |
Tony Mack | cdec090 | 2013-04-15 00:38:49 -0400 | [diff] [blame] | 34 | instance = driver.spawn_instance(name=self.name, |
| 35 | key_name = self.key.name, |
| 36 | flavor_id = self.flavor.flavor_id, |
| 37 | image_id = self.image.image_id, |
| 38 | hostname = self.node.name ) |
| 39 | self.instance_id = instance.id |
| 40 | |
| 41 | super(Sliver, self).save(*args, **kwds) |
| 42 | |
| 43 | def delete(self, *args, **kwds): |
| 44 | driver = OpenStackDriver() |
| 45 | if self.instance_id: |
| 46 | driver.destroy_instance(self.instance_id) |
| 47 | |
| 48 | super(Sliver, self).delete(*args, **kwds) |