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