Tony Mack | 5cbec5d | 2013-04-02 07:55:11 -0400 | [diff] [blame] | 1 | import os |
Tony Mack | 9aa28af | 2013-04-08 16:41:31 -0400 | [diff] [blame] | 2 | import datetime |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 3 | from django.db import models |
Tony Mack | 7823b89 | 2013-03-29 20:35:26 -0400 | [diff] [blame] | 4 | from plstackapi.openstack.driver import OpenStackDriver |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 5 | |
| 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 | |
Tony Mack | 1fbdeca | 2013-04-06 00:10:36 -0400 | [diff] [blame] | 16 | def save(self): |
| 17 | if not self.id: |
| 18 | self.created = datetime.date.today() |
| 19 | self.updated = datetime.datetime.today() |
| 20 | super(PlCoreBase, self).save() |
| 21 | |
Tony Mack | 3d1f069 | 2013-04-04 23:55:54 -0400 | [diff] [blame] | 22 | class Role(PlCoreBase): |
| 23 | |
| 24 | ROLE_CHOICES = (('admin', 'Admin'), ('pi', 'Principle Investigator'), ('user','User')) |
Tony Mack | f2162d9 | 2013-04-07 19:32:20 -0400 | [diff] [blame] | 25 | role_id = models.CharField(max_length=256, unique=True) |
Tony Mack | 3d1f069 | 2013-04-04 23:55:54 -0400 | [diff] [blame] | 26 | role_type = models.CharField(max_length=80, unique=True, choices=ROLE_CHOICES) |
| 27 | |
| 28 | def __unicode__(self): return u'%s' % (self.role_type) |
| 29 | |
| 30 | def save(self): |
| 31 | if not self.id: |
| 32 | self.created = datetime.date.today() |
| 33 | self.updated = datetime.datetime.today() |
| 34 | super(Role, self).save() |
| 35 | |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 36 | class Site(PlCoreBase): |
Tony Mack | f423f55 | 2013-04-09 11:06:48 -0400 | [diff] [blame] | 37 | tenant_id = models.CharField(max_length=200, help_text="Keystone tenant id") |
Tony Mack | 67f60ce | 2013-04-02 10:46:07 -0400 | [diff] [blame] | 38 | name = models.CharField(max_length=200, help_text="Name for this Site") |
Tony Mack | c689d50 | 2013-04-02 11:32:04 -0400 | [diff] [blame] | 39 | 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] | 40 | enabled = models.BooleanField(default=True, help_text="Status for this Site") |
| 41 | longitude = models.FloatField(null=True, blank=True) |
| 42 | latitude = models.FloatField(null=True, blank=True) |
Tony Mack | 22ea05a | 2013-04-09 01:13:04 -0400 | [diff] [blame] | 43 | login_base = models.CharField(max_length=50, unique=True, help_text="Prefix for Slices associated with this Site") |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 44 | is_public = models.BooleanField(default=True, help_text="Indicates the visibility of this site to other members") |
| 45 | abbreviated_name = models.CharField(max_length=80) |
| 46 | |
| 47 | def __unicode__(self): return u'%s' % (self.name) |
| 48 | |
Tony Mack | e1c8c38 | 2013-03-26 13:37:59 -0400 | [diff] [blame] | 49 | |
Tony Mack | 3d1f069 | 2013-04-04 23:55:54 -0400 | [diff] [blame] | 50 | class User(PlCoreBase): |
| 51 | user_id = models.CharField(max_length=256, unique=True) |
| 52 | firstname = models.CharField(help_text="person's given name", max_length=200) |
| 53 | lastname = models.CharField(help_text="person's surname", max_length=200) |
| 54 | email = models.EmailField(help_text="e-mail address") |
Tony Mack | 1fbdeca | 2013-04-06 00:10:36 -0400 | [diff] [blame] | 55 | phone = models.CharField(null=True, blank=True, help_text="phone number contact", max_length=100) |
| 56 | user_url = models.URLField(null=True, blank=True) |
| 57 | is_admin = models.BooleanField(default=False) |
Tony Mack | 7c03fad | 2013-04-09 11:45:28 -0400 | [diff] [blame] | 58 | enabled = models.BooleanField(default=True, help_text="Status for this User") |
Tony Mack | 22ea05a | 2013-04-09 01:13:04 -0400 | [diff] [blame] | 59 | site = models.ForeignKey(Site, related_name='users', verbose_name="Site this user will be homed too") |
Tony Mack | 3d1f069 | 2013-04-04 23:55:54 -0400 | [diff] [blame] | 60 | |
| 61 | def __unicode__(self): return u'%s' % (self.email) |
| 62 | |
Tony Mack | 3d1f069 | 2013-04-04 23:55:54 -0400 | [diff] [blame] | 63 | class SitePrivilege(PlCoreBase): |
| 64 | |
Tony Mack | 5cc16ba | 2013-04-09 10:30:45 -0400 | [diff] [blame] | 65 | user = models.ForeignKey('User', related_name='site_privileges') |
Tony Mack | 22ea05a | 2013-04-09 01:13:04 -0400 | [diff] [blame] | 66 | site = models.ForeignKey('Site', related_name='site_privileges') |
Tony Mack | 3d1f069 | 2013-04-04 23:55:54 -0400 | [diff] [blame] | 67 | role = models.ForeignKey('Role') |
| 68 | |
| 69 | def __unicode__(self): return u'%s %s %s' % (self.site, self.user, self.role) |
| 70 | |
Tony Mack | 1fbdeca | 2013-04-06 00:10:36 -0400 | [diff] [blame] | 71 | def save(self, *args, **kwds): |
| 72 | driver = OpenStackDriver() |
| 73 | driver.add_user_role(user_id=user.user_id, |
| 74 | tenant_id=site.tenant_id, |
| 75 | role_name=role.name) |
| 76 | super(SitePrivilege, self).save(*args, **kwds) |
| 77 | |
| 78 | def delete(self, *args, **kwds): |
| 79 | driver = OpenStackDriver() |
| 80 | driver.delete_user_role(user_id=user.user_id, |
| 81 | tenant_id=site.tenant_id, |
| 82 | role_name=role.name) |
| 83 | super(SitePrivilege, self).delete(*args, **kwds) |
| 84 | |
| 85 | |
| 86 | class DeploymentNetwork(PlCoreBase): |
| 87 | name = models.CharField(max_length=200, unique=True, help_text="Name of the Deployment Network") |
| 88 | |
| 89 | def __unicode__(self): return u'%s' % (self.name) |
| 90 | |
| 91 | class SiteDeploymentNetwork(PlCoreBase): |
| 92 | class Meta: |
| 93 | unique_together = ['site', 'deploymentNetwork'] |
| 94 | |
Tony Mack | 22ea05a | 2013-04-09 01:13:04 -0400 | [diff] [blame] | 95 | site = models.ForeignKey(Site, related_name='deployment_networks') |
Tony Mack | fcb9ee5 | 2013-04-09 07:56:26 -0400 | [diff] [blame] | 96 | deploymentNetwork = models.ForeignKey(DeploymentNetwork, related_name='sites') |
Tony Mack | 1fbdeca | 2013-04-06 00:10:36 -0400 | [diff] [blame] | 97 | name = models.CharField(default="Blah", max_length=100) |
| 98 | |
| 99 | |
| 100 | def __unicode__(self): return u'%s::%s' % (self.site, self.deploymentNetwork) |
| 101 | |
Tony Mack | 3d1f069 | 2013-04-04 23:55:54 -0400 | [diff] [blame] | 102 | |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 103 | class Slice(PlCoreBase): |
Tony Mack | d7cb771 | 2013-03-26 13:21:36 -0400 | [diff] [blame] | 104 | tenant_id = models.CharField(max_length=200, help_text="Keystone tenant id") |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 105 | 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] | 106 | enabled = models.BooleanField(default=True, help_text="Status for this Slice") |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 107 | SLICE_CHOICES = (('plc', 'PLC'), ('delegated', 'Delegated'), ('controller','Controller'), ('none','None')) |
| 108 | instantiation = models.CharField(help_text="The instantiation type of the slice", max_length=80, choices=SLICE_CHOICES) |
Tony Mack | a54ad2d | 2013-04-09 23:05:07 -0400 | [diff] [blame^] | 109 | omf_friendly = models.BooleanField(default=False) |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 110 | 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] | 111 | slice_url = models.URLField(blank=True, max_length=512) |
Tony Mack | 22ea05a | 2013-04-09 01:13:04 -0400 | [diff] [blame] | 112 | site = models.ForeignKey(Site, related_name='slices', help_text="The Site this Node belongs too") |
Tony Mack | af905fc | 2013-04-06 01:30:40 -0400 | [diff] [blame] | 113 | network_id = models.CharField(max_length=256, help_text="Quantum network") |
| 114 | router_id = models.CharField(max_length=256, help_text="Quantum router id") |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 115 | |
| 116 | def __unicode__(self): return u'%s' % (self.name) |
| 117 | |
Tony Mack | e1c8c38 | 2013-03-26 13:37:59 -0400 | [diff] [blame] | 118 | |
Tony Mack | 3d1f069 | 2013-04-04 23:55:54 -0400 | [diff] [blame] | 119 | class SliceMembership(PlCoreBase): |
Tony Mack | 5cc16ba | 2013-04-09 10:30:45 -0400 | [diff] [blame] | 120 | user = models.ForeignKey('User', related_name='slice_memberships') |
| 121 | slice = models.ForeignKey('Slice', related_name='slice_memberships') |
Tony Mack | 3d1f069 | 2013-04-04 23:55:54 -0400 | [diff] [blame] | 122 | role = models.ForeignKey('Role') |
| 123 | |
| 124 | def __unicode__(self): return u'%s %s %s' % (self.slice, self.user, self.role) |
| 125 | |
Tony Mack | 1fbdeca | 2013-04-06 00:10:36 -0400 | [diff] [blame] | 126 | def save(self, *args, **kwds): |
| 127 | driver = OpenStackDriver() |
| 128 | driver.add_user_role(user_id=user.user_id, |
| 129 | tenant_id=slice.tenant_id, |
| 130 | role_name=role.name) |
| 131 | super(SliceMembership, self).save(*args, **kwds) |
| 132 | |
| 133 | def delete(self, *args, **kwds): |
| 134 | driver = OpenStackDriver() |
| 135 | driver.delete_user_role(user_id=user.user_id, |
| 136 | tenant_id=slice.tenant_id, |
| 137 | role_name=role.name) |
| 138 | super(SliceMembership, self).delete(*args, **kwds) |
Tony Mack | 3d1f069 | 2013-04-04 23:55:54 -0400 | [diff] [blame] | 139 | |
| 140 | class SubNet(PlCoreBase): |
| 141 | subnet_id = models.CharField(max_length=256, unique=True) |
| 142 | cidr = models.CharField(max_length=20) |
| 143 | ip_version = models.IntegerField() |
| 144 | start = models.IPAddressField() |
| 145 | end = models.IPAddressField() |
Tony Mack | f1c1224 | 2013-04-09 16:08:43 -0400 | [diff] [blame] | 146 | slice = models.ForeignKey(Slice, related_name='subnets') |
Tony Mack | 3d1f069 | 2013-04-04 23:55:54 -0400 | [diff] [blame] | 147 | |
| 148 | def __unicode__(self): return u'%s' % (self.name) |
| 149 | |
| 150 | def save(self, *args, **kwargs): |
| 151 | driver = OpenStackDriver() |
| 152 | if not self.id: |
| 153 | subnet = driver.create_subnet(network_name=self.slice.name, |
| 154 | cidr_ip = self.cidr, |
| 155 | ip_version=self.ip_version, |
| 156 | start = self.start, |
| 157 | end = self.end) |
| 158 | |
| 159 | self.subnet_id = subnet.id |
| 160 | |
Tony Mack | af905fc | 2013-04-06 01:30:40 -0400 | [diff] [blame] | 161 | # add subnet as interface to slice router |
| 162 | driver.add_router_interface(self.slice.router_id, subnet.id) |
| 163 | |
Tony Mack | 3d1f069 | 2013-04-04 23:55:54 -0400 | [diff] [blame] | 164 | super(SubNet, self).save(*args, **kwargs) |
| 165 | |
| 166 | |
| 167 | def delete(self, *args, **kwargs): |
| 168 | # delete quantum network |
| 169 | driver = OpenStackDriver() |
| 170 | driver.delete_subnet(self.subnet_id) |
Tony Mack | af905fc | 2013-04-06 01:30:40 -0400 | [diff] [blame] | 171 | driver.delete_router_interface(self.slice.router_id, self.subnet.id) |
Tony Mack | 3d1f069 | 2013-04-04 23:55:54 -0400 | [diff] [blame] | 172 | super(SubNet, self).delete(*args, **kwargs) |
| 173 | |
Tony Mack | 3d1f069 | 2013-04-04 23:55:54 -0400 | [diff] [blame] | 174 | class Node(PlCoreBase): |
| 175 | name = models.CharField(max_length=200, unique=True, help_text="Name of the Node") |
| 176 | siteDeploymentNetwork = models.ForeignKey(SiteDeploymentNetwork, help_text="The Site and Deployment Network this Node belongs too.") |
| 177 | |
| 178 | def __unicode__(self): return u'%s' % (self.name) |
| 179 | |
| 180 | class Image(PlCoreBase): |
| 181 | image_id = models.CharField(max_length=256, unique=True) |
| 182 | name = models.CharField(max_length=256, unique=True) |
| 183 | disk_format = models.CharField(max_length=256) |
| 184 | container_format = models.CharField(max_length=256) |
| 185 | |
| 186 | def __unicode__(self): return u'%s' % (self.name) |
| 187 | |
| 188 | |
| 189 | class Flavor(PlCoreBase): |
| 190 | flavor_id = models.IntegerField(unique=True) |
| 191 | name = models.CharField(max_length=256, unique=True) |
| 192 | memory_mb = models.IntegerField() |
| 193 | disk_gb = models.IntegerField() |
| 194 | vcpus = models.IntegerField() |
| 195 | |
| 196 | def __unicode__(self): return u'%s' % (self.name) |
| 197 | |
| 198 | class Key(PlCoreBase): |
| 199 | name = models.CharField(max_length=256, unique=True) |
| 200 | key = models.CharField(max_length=512) |
| 201 | type = models.CharField(max_length=256) |
Tony Mack | e4cee7a | 2013-04-09 12:45:32 -0400 | [diff] [blame] | 202 | blacklisted = models.BooleanField(default=False) |
Tony Mack | 5cc16ba | 2013-04-09 10:30:45 -0400 | [diff] [blame] | 203 | user = models.ForeignKey(User, related_name='keys') |
Tony Mack | 3d1f069 | 2013-04-04 23:55:54 -0400 | [diff] [blame] | 204 | |
| 205 | def __unicode__(self): return u'%s' % (self.name) |
| 206 | |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 207 | class Sliver(PlCoreBase): |
Tony Mack | ebb2487 | 2013-04-02 08:20:59 -0400 | [diff] [blame] | 208 | instance_id = models.CharField(max_length=200, help_text="Nova instance id") |
Tony Mack | d9ae9da | 2013-04-02 08:58:22 -0400 | [diff] [blame] | 209 | name = models.CharField(max_length=200, help_text="Sliver name") |
Tony Mack | ca9a73b | 2013-04-09 00:45:59 -0400 | [diff] [blame] | 210 | flavor = models.ForeignKey(Flavor) |
| 211 | image = models.ForeignKey(Image) |
| 212 | key = models.ForeignKey(Key) |
Tony Mack | 3a1af41 | 2013-04-09 22:36:27 -0400 | [diff] [blame] | 213 | slice = models.ForeignKey(Slice, related_name='slivers') |
Tony Mack | ca9a73b | 2013-04-09 00:45:59 -0400 | [diff] [blame] | 214 | siteDeploymentNetwork = models.ForeignKey(SiteDeploymentNetwork) |
| 215 | node = models.ForeignKey(Node) |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 216 | |
| 217 | def __unicode__(self): return u'%s::%s' % (self.slice, self.siteDeploymentNetwork) |
| 218 | |
Tony Mack | 7823b89 | 2013-03-29 20:35:26 -0400 | [diff] [blame] | 219 | def save(self, *args, **kwds): |
| 220 | driver = OpenStackDriver() |
| 221 | instance = driver.spawn_instances(name=self.name, |
| 222 | keyname=self.name, |
| 223 | hostnames=self.node.name, |
Tony Mack | 3d1f069 | 2013-04-04 23:55:54 -0400 | [diff] [blame] | 224 | flavor=self.flavor.name, |
| 225 | image=self.image.name) |
Tony Mack | 7823b89 | 2013-03-29 20:35:26 -0400 | [diff] [blame] | 226 | self.instance_id = instance.id |
| 227 | super(Sliver, self).save(*args, **kwds) |
| 228 | |
| 229 | def delete(self, *args, **kwds): |
| 230 | driver = OpenStackDriver() |
| 231 | driver.destroy_instance(name=self.name, id=self.instance_id) |
| 232 | super(Sliver, self).delete(*args, **kwds) |
Tony Mack | 7823b89 | 2013-03-29 20:35:26 -0400 | [diff] [blame] | 233 | |