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