blob: 8f8dedce690412b552d0e686d3df135b7f17e34f [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 Tully4bc09f22013-04-10 21:15:21 -04004from plstackapi.core.models import PlCoreBase
5from plstackapi.core.models import Flavor
6from plstackapi.core.models import Image
7from plstackapi.core.models import Key
8from plstackapi.core.models import Slice
9from plstackapi.core.models import Node
10from plstackapi.core.models import Site
11from plstackapi.core.models import DeploymentNetwork
12from plstackapi.openstack.driver import OpenStackDriver
13
14# Create your models here.
15class 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 Mack771f4b92013-04-12 01:49:57 -040018 flavor = models.ForeignKey(Flavor, related_name='slivers')
19 image = models.ForeignKey(Image, related_name='slivers')
20 key = models.ForeignKey(Key, related_name='slivers')
21 slice = models.ForeignKey(Slice, related_name='slivers')
22 node = models.ForeignKey(Node, related_name='slivers')
Siobhan Tully4bc09f22013-04-10 21:15:21 -040023 deploymentNetwork = models.ForeignKey(DeploymentNetwork, related_name='sliver_deploymentNetwork')
24
Tony Mack29c287f2013-04-11 21:07:16 -040025 def __unicode__(self): return u'%s::%s' % (self.slice, self.deploymentNetwork)
Siobhan Tully4bc09f22013-04-10 21:15:21 -040026
Tony Mackcdec0902013-04-15 00:38:49 -040027 def save(self, *args, **kwds):
Tony Mack8e92dc52013-04-16 21:58:39 -040028 if not self.slice.subnet.exists():
Tony Mack2b241cf2013-04-16 21:57:55 -040029 raise exceptions.ValidationError, "Slice %s has no subnet" % self.slice.name
30
Tony Mackcdec0902013-04-15 00:38:49 -040031 if not self.instance_id:
Tony Mack2b241cf2013-04-16 21:57:55 -040032 driver = OpenStackDriver()
Tony Mackcdec0902013-04-15 00:38:49 -040033 instance = driver.spawn_instance(name=self.name,
34 key_name = self.key.name,
35 flavor_id = self.flavor.flavor_id,
36 image_id = self.image.image_id,
37 hostname = self.node.name )
38 self.instance_id = instance.id
39
40 super(Sliver, self).save(*args, **kwds)
41
42 def delete(self, *args, **kwds):
43 driver = OpenStackDriver()
44 if self.instance_id:
45 driver.destroy_instance(self.instance_id)
46
47 super(Sliver, self).delete(*args, **kwds)