Tony Mack | 5cbec5d | 2013-04-02 07:55:11 -0400 | [diff] [blame] | 1 | import os |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 2 | from django.db import models |
Tony Mack | 7823b89 | 2013-03-29 20:35:26 -0400 | [diff] [blame] | 3 | from plstackapi.openstack.driver import OpenStackDriver |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 4 | |
| 5 | # Create your models here. |
| 6 | |
| 7 | class PlCoreBase(models.Model): |
| 8 | |
| 9 | created = models.DateTimeField(auto_now_add=True) |
| 10 | updated = models.DateTimeField(auto_now=True) |
| 11 | |
| 12 | class Meta: |
| 13 | abstract = True |
| 14 | |
| 15 | class Site(PlCoreBase): |
Tony Mack | d7cb771 | 2013-03-26 13:21:36 -0400 | [diff] [blame] | 16 | tenant_id = models.CharField(max_length=200, help_text="Keystone tenant id") |
Tony Mack | 67f60ce | 2013-04-02 10:46:07 -0400 | [diff] [blame] | 17 | name = models.CharField(max_length=200, help_text="Name for this Site") |
Tony Mack | c689d50 | 2013-04-02 11:32:04 -0400 | [diff] [blame] | 18 | site_url = models.URLField(null=True, blank=True, max_length=512, help_text="Site's Home URL Page") |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 19 | enabled = models.BooleanField(default=True, help_text="Status for this Site") |
| 20 | longitude = models.FloatField(null=True, blank=True) |
| 21 | latitude = models.FloatField(null=True, blank=True) |
| 22 | login_base = models.CharField(max_length=50, help_text="Prefix for Slices associated with this Site") |
| 23 | is_public = models.BooleanField(default=True, help_text="Indicates the visibility of this site to other members") |
| 24 | abbreviated_name = models.CharField(max_length=80) |
| 25 | |
| 26 | def __unicode__(self): return u'%s' % (self.name) |
| 27 | |
Tony Mack | d7cb771 | 2013-03-26 13:21:36 -0400 | [diff] [blame] | 28 | def save(self, *args, **kwargs): |
Tony Mack | 7823b89 | 2013-03-29 20:35:26 -0400 | [diff] [blame] | 29 | driver = OpenStackDriver() |
Tony Mack | d7cb771 | 2013-03-26 13:21:36 -0400 | [diff] [blame] | 30 | if not self.id: |
Tony Mack | 39444e4 | 2013-04-02 10:22:24 -0400 | [diff] [blame] | 31 | tenant = driver.create_tenant(tenant_name=self.login_base, |
Tony Mack | 7823b89 | 2013-03-29 20:35:26 -0400 | [diff] [blame] | 32 | description=self.name, |
| 33 | enabled=self.enabled) |
Tony Mack | d8c390f | 2013-04-02 10:36:19 -0400 | [diff] [blame] | 34 | self.tenant_id = tenant.id |
Tony Mack | d7cb771 | 2013-03-26 13:21:36 -0400 | [diff] [blame] | 35 | else: |
| 36 | # update record |
Tony Mack | 7823b89 | 2013-03-29 20:35:26 -0400 | [diff] [blame] | 37 | self.driver.update_tenant(self.tenant_id, name=self.login_base, |
| 38 | description=self.name, enabled=self.enabled) |
Tony Mack | d7cb771 | 2013-03-26 13:21:36 -0400 | [diff] [blame] | 39 | super(Site, self).save(*args, **kwargs) |
| 40 | |
Tony Mack | e1c8c38 | 2013-03-26 13:37:59 -0400 | [diff] [blame] | 41 | def delete(self, *args, **kwds): |
| 42 | # delete keystone tenant |
Tony Mack | 7823b89 | 2013-03-29 20:35:26 -0400 | [diff] [blame] | 43 | driver = OpenStackDriver() |
| 44 | driver.delete_tenant(self.tenant_id) |
Tony Mack | e1c8c38 | 2013-03-26 13:37:59 -0400 | [diff] [blame] | 45 | super(Site, self).delete(*args, **kwargs) |
| 46 | |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 47 | class Slice(PlCoreBase): |
Tony Mack | d7cb771 | 2013-03-26 13:21:36 -0400 | [diff] [blame] | 48 | tenant_id = models.CharField(max_length=200, help_text="Keystone tenant id") |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 49 | name = models.CharField(help_text="The Name of the Slice", max_length=80) |
Tony Mack | a3a40cf | 2013-04-02 11:21:21 -0400 | [diff] [blame] | 50 | enabled = models.BooleanField(default=True, help_text="Status for this Slice") |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 51 | SLICE_CHOICES = (('plc', 'PLC'), ('delegated', 'Delegated'), ('controller','Controller'), ('none','None')) |
| 52 | instantiation = models.CharField(help_text="The instantiation type of the slice", max_length=80, choices=SLICE_CHOICES) |
| 53 | omf_friendly = models.BooleanField() |
| 54 | description=models.TextField(blank=True,help_text="High level description of the slice and expected activities", max_length=1024) |
Tony Mack | c689d50 | 2013-04-02 11:32:04 -0400 | [diff] [blame] | 55 | slice_url = models.URLField(blank=True, max_length=512) |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 56 | site = models.ForeignKey(Site, related_name='slices', help_text="The Site this Node belongs too") |
| 57 | |
| 58 | def __unicode__(self): return u'%s' % (self.name) |
| 59 | |
Tony Mack | d7cb771 | 2013-03-26 13:21:36 -0400 | [diff] [blame] | 60 | def save(self, *args, **kwds): |
Tony Mack | e1c8c38 | 2013-03-26 13:37:59 -0400 | [diff] [blame] | 61 | # sync keystone tenant |
Tony Mack | 7823b89 | 2013-03-29 20:35:26 -0400 | [diff] [blame] | 62 | driver = OpenStackDriver() |
| 63 | |
Tony Mack | d7cb771 | 2013-03-26 13:21:36 -0400 | [diff] [blame] | 64 | if not self.id: |
Tony Mack | 39444e4 | 2013-04-02 10:22:24 -0400 | [diff] [blame] | 65 | tenant = driver.create_tenant(tenant_name=self.name, |
Tony Mack | 7823b89 | 2013-03-29 20:35:26 -0400 | [diff] [blame] | 66 | description=self.description, |
| 67 | enabled=self.enabled) |
Tony Mack | d8c390f | 2013-04-02 10:36:19 -0400 | [diff] [blame] | 68 | self.tenant_id = tenant.id |
Tony Mack | d7cb771 | 2013-03-26 13:21:36 -0400 | [diff] [blame] | 69 | else: |
| 70 | # update record |
Tony Mack | 7823b89 | 2013-03-29 20:35:26 -0400 | [diff] [blame] | 71 | self.driver.update_tenant(self.tenant_id, name=self.name, |
| 72 | description=self.description, enabled=self.enabled) |
Tony Mack | 6299cb7 | 2013-04-02 11:23:26 -0400 | [diff] [blame] | 73 | super(Slice, self).save(*args, **kwds) |
Tony Mack | d7cb771 | 2013-03-26 13:21:36 -0400 | [diff] [blame] | 74 | |
Tony Mack | e1c8c38 | 2013-03-26 13:37:59 -0400 | [diff] [blame] | 75 | def delete(self, *args, **kwds): |
| 76 | # delete keystone tenant |
Tony Mack | 7823b89 | 2013-03-29 20:35:26 -0400 | [diff] [blame] | 77 | driver = OpenStackDriver() |
| 78 | driver.delete_tenant(self.tenant_id) |
Tony Mack | 6299cb7 | 2013-04-02 11:23:26 -0400 | [diff] [blame] | 79 | super(Slice, self).delete(*args, **kwds) |
Tony Mack | e1c8c38 | 2013-03-26 13:37:59 -0400 | [diff] [blame] | 80 | |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 81 | class DeploymentNetwork(PlCoreBase): |
| 82 | name = models.CharField(max_length=200, unique=True, help_text="Name of the Deployment Network") |
| 83 | |
| 84 | def __unicode__(self): return u'%s' % (self.name) |
| 85 | |
| 86 | class SiteDeploymentNetwork(PlCoreBase): |
| 87 | class Meta: |
| 88 | unique_together = ['site', 'deploymentNetwork'] |
| 89 | |
| 90 | site = models.ForeignKey(Site, related_name='deploymentNetworks') |
| 91 | deploymentNetwork = models.ForeignKey(DeploymentNetwork, related_name='sites') |
| 92 | name = models.CharField(default="Blah", max_length=100) |
| 93 | |
| 94 | |
| 95 | def __unicode__(self): return u'%s::%s' % (self.site, self.deploymentNetwork) |
| 96 | |
| 97 | |
| 98 | class Sliver(PlCoreBase): |
Tony Mack | ebb2487 | 2013-04-02 08:20:59 -0400 | [diff] [blame] | 99 | tenant_id = models.CharField(max_length=200, help_text="Keystone tenant id") |
| 100 | instance_id = models.CharField(max_length=200, help_text="Nova instance id") |
Tony Mack | d9ae9da | 2013-04-02 08:58:22 -0400 | [diff] [blame] | 101 | name = models.CharField(max_length=200, help_text="Sliver name") |
| 102 | flavor = models.CharField(max_length=200, help_text="OS Flavor") |
| 103 | image = models.CharField(max_length=200, help_text="Image Name") |
Tony Mack | 27527b9 | 2013-04-02 08:59:35 -0400 | [diff] [blame] | 104 | slice = models.ForeignKey(Slice, related_name='sliver_slice') |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 105 | siteDeploymentNetwork = models.ForeignKey(SiteDeploymentNetwork) |
Tony Mack | 4ec6535 | 2013-04-02 08:09:29 -0400 | [diff] [blame] | 106 | #node = models.ForeignKey(Node, related_name='node') |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 107 | |
| 108 | def __unicode__(self): return u'%s::%s' % (self.slice, self.siteDeploymentNetwork) |
| 109 | |
Tony Mack | 7823b89 | 2013-03-29 20:35:26 -0400 | [diff] [blame] | 110 | def save(self, *args, **kwds): |
| 111 | driver = OpenStackDriver() |
| 112 | instance = driver.spawn_instances(name=self.name, |
| 113 | keyname=self.name, |
| 114 | hostnames=self.node.name, |
| 115 | flavor=self.flavor, |
| 116 | image=self.image) |
| 117 | self.instance_id = instance.id |
| 118 | super(Sliver, self).save(*args, **kwds) |
| 119 | |
| 120 | def delete(self, *args, **kwds): |
| 121 | driver = OpenStackDriver() |
| 122 | driver.destroy_instance(name=self.name, id=self.instance_id) |
| 123 | super(Sliver, self).delete(*args, **kwds) |
| 124 | |
| 125 | |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 126 | class Node(PlCoreBase): |
| 127 | name = models.CharField(max_length=200, unique=True, help_text="Name of the Node") |
| 128 | siteDeploymentNetwork = models.ForeignKey(SiteDeploymentNetwork, help_text="The Site and Deployment Network this Node belongs too.") |
| 129 | |
| 130 | def __unicode__(self): return u'%s' % (self.name) |
| 131 | |
Tony Mack | d7cb771 | 2013-03-26 13:21:36 -0400 | [diff] [blame] | 132 | class Network(PlCoreBase): |
Tony Mack | 27527b9 | 2013-04-02 08:59:35 -0400 | [diff] [blame] | 133 | slice = models.ForeignKey(Slice, related_name='network_slice') |
Tony Mack | d7cb771 | 2013-03-26 13:21:36 -0400 | [diff] [blame] | 134 | name = models.CharField(max_length=200, unique=True) |
| 135 | quantum_id = models.CharField(max_length=200, unique=True) |
| 136 | |
| 137 | def __unicode__(self): return u'%s' % (self.name) |
| 138 | |
| 139 | def save(self, *args, **kwargs): |
| 140 | os_shell = OpenStackShell() |
| 141 | network_fields = {'name': self.name} |
| 142 | |
| 143 | if not self.id: |
| 144 | # check if quantum network record exists |
| 145 | networks = os_shell.quantum.list_networks(name=self.name) |
| 146 | if not networks: |
| 147 | network = os_shell.quantum.create_network(name=self.name, |
| 148 | admin_state_up=False) |
| 149 | else: |
| 150 | network = networks[0] |
| 151 | self.quantum_id = network.id |
| 152 | super(Network, self).save(*args, **kwargs) |
| 153 | |
Tony Mack | e1c8c38 | 2013-03-26 13:37:59 -0400 | [diff] [blame] | 154 | def delete(self, *args, **kwargs): |
| 155 | # delete quantum network |
| 156 | os_shell = OpenStackShell() |
| 157 | os_shell.quantum.delete_network(self.quantum_id) |
| 158 | |
| 159 | super(Network, self).delete(*args, **kwargs) |
| 160 | |
Tony Mack | d7cb771 | 2013-03-26 13:21:36 -0400 | [diff] [blame] | 161 | class SubNet(PlCoreBase): |
| 162 | network = models.ForeignKey(Network, related_name='network') |
| 163 | name = models.CharField(max_length=200, unique=True) |
| 164 | quantum_id = models.CharField(max_length=200, unique=True) |
| 165 | cidr = models.CharField(max_length=20) |
| 166 | ip_version = models.IntegerField() |
| 167 | start = models.IPAddressField() |
| 168 | end = models.IPAddressField() |
| 169 | |
| 170 | def __unicode__(self): return u'%s' % (self.name) |
| 171 | |
| 172 | def save(self, *args, **kwargs): |
| 173 | os_shell = OpenStackShell() |
| 174 | subnet_fields = {'network_id': self.network.quantum_id, |
| 175 | 'name' : self.name, |
| 176 | 'ip_version': self.ip_version, |
| 177 | 'cidr': self.cidr, |
Tony Mack | 560788f | 2013-03-28 20:46:58 -0400 | [diff] [blame] | 178 | 'allocation_pools': {'start': self.start, |
| 179 | 'end': self.end} |
Tony Mack | d7cb771 | 2013-03-26 13:21:36 -0400 | [diff] [blame] | 180 | } |
| 181 | if not self.id: |
| 182 | subnets = os_shell.quantum.list_subnets(name=self.name) |
| 183 | if not subnets: |
| 184 | subnet = os_shell.quantum.create_subnet(**subnet_fields) |
| 185 | else: |
| 186 | subnet = subnets[0] |
| 187 | self.quantum_id = subnet.id |
| 188 | super(SubNet, self).save(*args, **kwargs) |
Tony Mack | e1c8c38 | 2013-03-26 13:37:59 -0400 | [diff] [blame] | 189 | |
| 190 | |
| 191 | def delete(self, *args, **kwargs): |
| 192 | # delete quantum network |
| 193 | os_shell = OpenStackShell() |
| 194 | os_shell.quantum.delete_subnet(self.quantum_id) |
| 195 | |
| 196 | super(SubNet, self).delete(*args, **kwargs) |
Tony Mack | d7cb771 | 2013-03-26 13:21:36 -0400 | [diff] [blame] | 197 | |
| 198 | |
| 199 | |