Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 1 | import os |
Tony Mack | 31d3413 | 2013-04-15 00:02:06 -0400 | [diff] [blame] | 2 | import commands |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 3 | from django.db import models |
| 4 | from plstackapi.core.models import PlCoreBase |
| 5 | from plstackapi.core.models import Slice |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 6 | |
| 7 | # Create your models here. |
| 8 | |
| 9 | class Subnet(PlCoreBase): |
Tony Mack | cdec090 | 2013-04-15 00:38:49 -0400 | [diff] [blame] | 10 | subnet_id = models.CharField(max_length=256, unique=True) |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 11 | cidr = models.CharField(max_length=20) |
| 12 | ip_version = models.IntegerField() |
| 13 | start = models.IPAddressField() |
| 14 | end = models.IPAddressField() |
Tony Mack | 771f4b9 | 2013-04-12 01:49:57 -0400 | [diff] [blame] | 15 | slice = models.ForeignKey(Slice, related_name='subnet') |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 16 | |
Tony Mack | f79ad8e | 2013-04-12 13:52:44 -0400 | [diff] [blame] | 17 | def __unicode__(self): return u'%s' % (self.slice.name) |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 18 | |
Tony Mack | 31d3413 | 2013-04-15 00:02:06 -0400 | [diff] [blame] | 19 | def save(self, *args, **kwds): |
Tony Mack | 31d3413 | 2013-04-15 00:02:06 -0400 | [diff] [blame] | 20 | if not self.subnet_id: |
Tony Mack | fdd4d80 | 2013-04-27 13:02:33 -0400 | [diff] [blame] | 21 | quantum_subnet = self.driver.create_subnet(name= self.slice.name, |
Tony Mack | 31d3413 | 2013-04-15 00:02:06 -0400 | [diff] [blame] | 22 | network_id=self.slice.network_id, |
| 23 | cidr_ip = self.cidr, |
| 24 | ip_version=self.ip_version, |
| 25 | start = self.start, |
| 26 | end = self.end) |
| 27 | self.subnet_id = quantum_subnet['id'] |
| 28 | # add subnet as interface to slice's router |
Tony Mack | fdd4d80 | 2013-04-27 13:02:33 -0400 | [diff] [blame] | 29 | self.driver.add_router_interface(self.slice.router_id, self.subnet_id) |
Tony Mack | aa6b750 | 2013-04-17 21:58:34 -0400 | [diff] [blame] | 30 | #add_route = 'route add -net %s dev br-ex gw 10.100.0.5' % self.cidr |
Tony Mack | 814922b | 2013-04-24 14:51:53 -0400 | [diff] [blame] | 31 | #commands.getstatusoutput(add_route) |
Tony Mack | 31d3413 | 2013-04-15 00:02:06 -0400 | [diff] [blame] | 32 | |
| 33 | super(Subnet, self).save(*args, **kwds) |
| 34 | |
| 35 | def delete(self, *args, **kwds): |
Tony Mack | 31d3413 | 2013-04-15 00:02:06 -0400 | [diff] [blame] | 36 | if self.subnet_id: |
Tony Mack | fdd4d80 | 2013-04-27 13:02:33 -0400 | [diff] [blame] | 37 | self.driver.delete_router_interface(self.slice.router_id, self.subnet_id) |
| 38 | self.driver.delete_subnet(self.subnet_id) |
Tony Mack | aa6b750 | 2013-04-17 21:58:34 -0400 | [diff] [blame] | 39 | #del_route = 'route del -net %s' % self.cidr |
Tony Mack | 814922b | 2013-04-24 14:51:53 -0400 | [diff] [blame] | 40 | #commands.getstatusoutput(del_route) |
Tony Mack | 31d3413 | 2013-04-15 00:02:06 -0400 | [diff] [blame] | 41 | super(Subnet, self).delete(*args, **kwds) |