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