Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 1 | from django.db import models |
Tony Mack | d7cb771 | 2013-03-26 13:21:36 -0400 | [diff] [blame] | 2 | from plstackapi.openstack.shell import OpenStackShell |
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): |
| 28 | os_shell = OpenStackShell() |
| 29 | tenant_fields = {'name': self.login_base, |
| 30 | 'enabled': self.enabled, |
| 31 | 'description': self.name} |
| 32 | |
| 33 | if not self.id: |
| 34 | # check if keystone tenant record exists |
| 35 | tenants = os_shell.keystone.tenants.findall(name=self.login_base) |
| 36 | if not tenants: |
| 37 | tenant = os_shell.keystone.tenants.create(**tenant_fields) |
| 38 | else: |
| 39 | tenant = tenants[0] |
| 40 | self.tenant_id = tenants.id |
| 41 | else: |
| 42 | # update record |
| 43 | os_shell.keystone.tenants.update(self.tenant_id, **tenant_fields) |
| 44 | super(Site, self).save(*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): |
| 59 | os_shell = OpenStackShell() |
| 60 | tenant_fields = {'name': self.name, |
| 61 | 'enabled': self.enabled, |
| 62 | 'description': self.description} |
| 63 | if not self.id: |
| 64 | # check if keystone tenant record exists |
| 65 | tenants = os_shell.keystone.tenants.findall(name=self.name) |
| 66 | if not tenants: |
| 67 | tenant = os_shell.keystone.tenants.create(**tenant_fields) |
| 68 | else: |
| 69 | tenant = tenants[0] |
| 70 | self.tenant_id = tenants.id |
| 71 | else: |
| 72 | # update record |
| 73 | os_shell.keystone.tenants.update(self.tenant_id, **tenant_fields) |
| 74 | super(Site, self).save(*args, **kwargs) |
| 75 | |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 76 | class DeploymentNetwork(PlCoreBase): |
| 77 | name = models.CharField(max_length=200, unique=True, help_text="Name of the Deployment Network") |
| 78 | |
| 79 | def __unicode__(self): return u'%s' % (self.name) |
| 80 | |
| 81 | class SiteDeploymentNetwork(PlCoreBase): |
| 82 | class Meta: |
| 83 | unique_together = ['site', 'deploymentNetwork'] |
| 84 | |
| 85 | site = models.ForeignKey(Site, related_name='deploymentNetworks') |
| 86 | deploymentNetwork = models.ForeignKey(DeploymentNetwork, related_name='sites') |
| 87 | name = models.CharField(default="Blah", max_length=100) |
| 88 | |
| 89 | |
| 90 | def __unicode__(self): return u'%s::%s' % (self.site, self.deploymentNetwork) |
| 91 | |
| 92 | |
| 93 | class Sliver(PlCoreBase): |
Tony Mack | d7cb771 | 2013-03-26 13:21:36 -0400 | [diff] [blame] | 94 | tenant_id = models.CharField(max_length=200, help_text="Keystone tenant id") |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 95 | name = models.CharField(max_length=200, unique=True) |
| 96 | slice = models.ForeignKey(Slice) |
| 97 | siteDeploymentNetwork = models.ForeignKey(SiteDeploymentNetwork) |
| 98 | |
| 99 | def __unicode__(self): return u'%s::%s' % (self.slice, self.siteDeploymentNetwork) |
| 100 | |
| 101 | class Node(PlCoreBase): |
| 102 | name = models.CharField(max_length=200, unique=True, help_text="Name of the Node") |
| 103 | siteDeploymentNetwork = models.ForeignKey(SiteDeploymentNetwork, help_text="The Site and Deployment Network this Node belongs too.") |
| 104 | |
| 105 | def __unicode__(self): return u'%s' % (self.name) |
| 106 | |
Tony Mack | d7cb771 | 2013-03-26 13:21:36 -0400 | [diff] [blame] | 107 | class Network(PlCoreBase): |
| 108 | slice = models.ForeignKey(Slice, related_name='slice') |
| 109 | name = models.CharField(max_length=200, unique=True) |
| 110 | quantum_id = models.CharField(max_length=200, unique=True) |
| 111 | |
| 112 | def __unicode__(self): return u'%s' % (self.name) |
| 113 | |
| 114 | def save(self, *args, **kwargs): |
| 115 | os_shell = OpenStackShell() |
| 116 | network_fields = {'name': self.name} |
| 117 | |
| 118 | if not self.id: |
| 119 | # check if quantum network record exists |
| 120 | networks = os_shell.quantum.list_networks(name=self.name) |
| 121 | if not networks: |
| 122 | network = os_shell.quantum.create_network(name=self.name, |
| 123 | admin_state_up=False) |
| 124 | else: |
| 125 | network = networks[0] |
| 126 | self.quantum_id = network.id |
| 127 | super(Network, self).save(*args, **kwargs) |
| 128 | |
| 129 | class SubNet(PlCoreBase): |
| 130 | network = models.ForeignKey(Network, related_name='network') |
| 131 | name = models.CharField(max_length=200, unique=True) |
| 132 | quantum_id = models.CharField(max_length=200, unique=True) |
| 133 | cidr = models.CharField(max_length=20) |
| 134 | ip_version = models.IntegerField() |
| 135 | start = models.IPAddressField() |
| 136 | end = models.IPAddressField() |
| 137 | |
| 138 | def __unicode__(self): return u'%s' % (self.name) |
| 139 | |
| 140 | def save(self, *args, **kwargs): |
| 141 | os_shell = OpenStackShell() |
| 142 | subnet_fields = {'network_id': self.network.quantum_id, |
| 143 | 'name' : self.name, |
| 144 | 'ip_version': self.ip_version, |
| 145 | 'cidr': self.cidr, |
| 146 | 'allocation_pools': ['start': self.start, |
| 147 | 'end': self.end] |
| 148 | } |
| 149 | if not self.id: |
| 150 | subnets = os_shell.quantum.list_subnets(name=self.name) |
| 151 | if not subnets: |
| 152 | subnet = os_shell.quantum.create_subnet(**subnet_fields) |
| 153 | else: |
| 154 | subnet = subnets[0] |
| 155 | self.quantum_id = subnet.id |
| 156 | super(SubNet, self).save(*args, **kwargs) |
| 157 | |
| 158 | |
| 159 | |