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