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 | |
Tony Mack | 1fbdeca | 2013-04-06 00:10:36 -0400 | [diff] [blame] | 15 | def save(self): |
| 16 | if not self.id: |
| 17 | self.created = datetime.date.today() |
| 18 | self.updated = datetime.datetime.today() |
| 19 | super(PlCoreBase, self).save() |
| 20 | |
Tony Mack | 3d1f069 | 2013-04-04 23:55:54 -0400 | [diff] [blame] | 21 | class Role(PlCoreBase): |
| 22 | |
| 23 | ROLE_CHOICES = (('admin', 'Admin'), ('pi', 'Principle Investigator'), ('user','User')) |
Tony Mack | f2162d9 | 2013-04-07 19:32:20 -0400 | [diff] [blame] | 24 | role_id = models.CharField(max_length=256, unique=True) |
Tony Mack | 3d1f069 | 2013-04-04 23:55:54 -0400 | [diff] [blame] | 25 | role_type = models.CharField(max_length=80, unique=True, choices=ROLE_CHOICES) |
| 26 | |
| 27 | def __unicode__(self): return u'%s' % (self.role_type) |
| 28 | |
| 29 | def save(self): |
| 30 | if not self.id: |
| 31 | self.created = datetime.date.today() |
| 32 | self.updated = datetime.datetime.today() |
| 33 | super(Role, self).save() |
| 34 | |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 35 | class Site(PlCoreBase): |
Tony Mack | d7cb771 | 2013-03-26 13:21:36 -0400 | [diff] [blame] | 36 | tenant_id = models.CharField(max_length=200, help_text="Keystone tenant id") |
Tony Mack | 67f60ce | 2013-04-02 10:46:07 -0400 | [diff] [blame] | 37 | name = models.CharField(max_length=200, help_text="Name for this Site") |
Tony Mack | c689d50 | 2013-04-02 11:32:04 -0400 | [diff] [blame] | 38 | 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] | 39 | enabled = models.BooleanField(default=True, help_text="Status for this Site") |
| 40 | longitude = models.FloatField(null=True, blank=True) |
| 41 | latitude = models.FloatField(null=True, blank=True) |
| 42 | login_base = models.CharField(max_length=50, help_text="Prefix for Slices associated with this Site") |
| 43 | is_public = models.BooleanField(default=True, help_text="Indicates the visibility of this site to other members") |
| 44 | abbreviated_name = models.CharField(max_length=80) |
| 45 | |
| 46 | def __unicode__(self): return u'%s' % (self.name) |
| 47 | |
Tony Mack | d7cb771 | 2013-03-26 13:21:36 -0400 | [diff] [blame] | 48 | def save(self, *args, **kwargs): |
Tony Mack | 7823b89 | 2013-03-29 20:35:26 -0400 | [diff] [blame] | 49 | driver = OpenStackDriver() |
Tony Mack | d7cb771 | 2013-03-26 13:21:36 -0400 | [diff] [blame] | 50 | if not self.id: |
Tony Mack | 39444e4 | 2013-04-02 10:22:24 -0400 | [diff] [blame] | 51 | tenant = driver.create_tenant(tenant_name=self.login_base, |
Tony Mack | 7823b89 | 2013-03-29 20:35:26 -0400 | [diff] [blame] | 52 | description=self.name, |
| 53 | enabled=self.enabled) |
Tony Mack | d8c390f | 2013-04-02 10:36:19 -0400 | [diff] [blame] | 54 | self.tenant_id = tenant.id |
Tony Mack | d7cb771 | 2013-03-26 13:21:36 -0400 | [diff] [blame] | 55 | else: |
| 56 | # update record |
Tony Mack | 7823b89 | 2013-03-29 20:35:26 -0400 | [diff] [blame] | 57 | self.driver.update_tenant(self.tenant_id, name=self.login_base, |
| 58 | description=self.name, enabled=self.enabled) |
Tony Mack | d7cb771 | 2013-03-26 13:21:36 -0400 | [diff] [blame] | 59 | super(Site, self).save(*args, **kwargs) |
| 60 | |
Tony Mack | e1c8c38 | 2013-03-26 13:37:59 -0400 | [diff] [blame] | 61 | def delete(self, *args, **kwds): |
| 62 | # delete keystone tenant |
Tony Mack | 7823b89 | 2013-03-29 20:35:26 -0400 | [diff] [blame] | 63 | driver = OpenStackDriver() |
| 64 | driver.delete_tenant(self.tenant_id) |
Tony Mack | e1c8c38 | 2013-03-26 13:37:59 -0400 | [diff] [blame] | 65 | super(Site, self).delete(*args, **kwargs) |
| 66 | |
Tony Mack | 3d1f069 | 2013-04-04 23:55:54 -0400 | [diff] [blame] | 67 | class User(PlCoreBase): |
| 68 | user_id = models.CharField(max_length=256, unique=True) |
| 69 | firstname = models.CharField(help_text="person's given name", max_length=200) |
| 70 | lastname = models.CharField(help_text="person's surname", max_length=200) |
| 71 | email = models.EmailField(help_text="e-mail address") |
Tony Mack | 1fbdeca | 2013-04-06 00:10:36 -0400 | [diff] [blame] | 72 | phone = models.CharField(null=True, blank=True, help_text="phone number contact", max_length=100) |
| 73 | user_url = models.URLField(null=True, blank=True) |
| 74 | is_admin = models.BooleanField(default=False) |
Tony Mack | 3d1f069 | 2013-04-04 23:55:54 -0400 | [diff] [blame] | 75 | site = models.ForeignKey(Site, related_name='site_user', verbose_name="Site this user will be homed too") |
| 76 | |
| 77 | def __unicode__(self): return u'%s' % (self.email) |
| 78 | |
Tony Mack | 1fbdeca | 2013-04-06 00:10:36 -0400 | [diff] [blame] | 79 | def save(self, *args, **kwds): |
| 80 | driver = OpenStackDriver() |
| 81 | name = self.email[:self.email.find('@')] |
| 82 | fields = {'name': name, 'email': self.email, 'password': self.password, |
| 83 | 'enabled': self.enabled} |
Tony Mack | 3d1f069 | 2013-04-04 23:55:54 -0400 | [diff] [blame] | 84 | if not self.id: |
Tony Mack | 1fbdeca | 2013-04-06 00:10:36 -0400 | [diff] [blame] | 85 | user = driver.create_user(**fields) |
| 86 | else: |
| 87 | driver.update_user(self.user_id, **fields) |
| 88 | super(User, self).save(*args, **kwds) |
| 89 | |
| 90 | def delete(self, *args, **kwds): |
| 91 | driver = OpenStackDriver() |
| 92 | driver.delete_user(self.user_id) |
| 93 | super(User, self).delete(*args, **kwds) |
Tony Mack | 3d1f069 | 2013-04-04 23:55:54 -0400 | [diff] [blame] | 94 | |
| 95 | class SitePrivilege(PlCoreBase): |
| 96 | |
| 97 | user = models.ForeignKey('User') |
| 98 | site = models.ForeignKey('Site') |
| 99 | role = models.ForeignKey('Role') |
| 100 | |
| 101 | def __unicode__(self): return u'%s %s %s' % (self.site, self.user, self.role) |
| 102 | |
Tony Mack | 1fbdeca | 2013-04-06 00:10:36 -0400 | [diff] [blame] | 103 | def save(self, *args, **kwds): |
| 104 | driver = OpenStackDriver() |
| 105 | driver.add_user_role(user_id=user.user_id, |
| 106 | tenant_id=site.tenant_id, |
| 107 | role_name=role.name) |
| 108 | super(SitePrivilege, self).save(*args, **kwds) |
| 109 | |
| 110 | def delete(self, *args, **kwds): |
| 111 | driver = OpenStackDriver() |
| 112 | driver.delete_user_role(user_id=user.user_id, |
| 113 | tenant_id=site.tenant_id, |
| 114 | role_name=role.name) |
| 115 | super(SitePrivilege, self).delete(*args, **kwds) |
| 116 | |
| 117 | |
| 118 | class DeploymentNetwork(PlCoreBase): |
| 119 | name = models.CharField(max_length=200, unique=True, help_text="Name of the Deployment Network") |
| 120 | |
| 121 | def __unicode__(self): return u'%s' % (self.name) |
| 122 | |
| 123 | class SiteDeploymentNetwork(PlCoreBase): |
| 124 | class Meta: |
| 125 | unique_together = ['site', 'deploymentNetwork'] |
| 126 | |
| 127 | site = models.ForeignKey(Site, related_name='deploymentNetworks') |
| 128 | deploymentNetwork = models.ForeignKey(DeploymentNetwork, related_name='sites') |
| 129 | name = models.CharField(default="Blah", max_length=100) |
| 130 | |
| 131 | |
| 132 | def __unicode__(self): return u'%s::%s' % (self.site, self.deploymentNetwork) |
| 133 | |
Tony Mack | 3d1f069 | 2013-04-04 23:55:54 -0400 | [diff] [blame] | 134 | |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 135 | class Slice(PlCoreBase): |
Tony Mack | d7cb771 | 2013-03-26 13:21:36 -0400 | [diff] [blame] | 136 | tenant_id = models.CharField(max_length=200, help_text="Keystone tenant id") |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 137 | 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] | 138 | enabled = models.BooleanField(default=True, help_text="Status for this Slice") |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 139 | SLICE_CHOICES = (('plc', 'PLC'), ('delegated', 'Delegated'), ('controller','Controller'), ('none','None')) |
| 140 | instantiation = models.CharField(help_text="The instantiation type of the slice", max_length=80, choices=SLICE_CHOICES) |
| 141 | omf_friendly = models.BooleanField() |
| 142 | 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] | 143 | slice_url = models.URLField(blank=True, max_length=512) |
Tony Mack | 3d1f069 | 2013-04-04 23:55:54 -0400 | [diff] [blame] | 144 | site = models.ForeignKey(Site, related_name='site_slice', help_text="The Site this Node belongs too") |
Tony Mack | af905fc | 2013-04-06 01:30:40 -0400 | [diff] [blame] | 145 | network_id = models.CharField(max_length=256, help_text="Quantum network") |
| 146 | router_id = models.CharField(max_length=256, help_text="Quantum router id") |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 147 | |
| 148 | def __unicode__(self): return u'%s' % (self.name) |
| 149 | |
Tony Mack | d7cb771 | 2013-03-26 13:21:36 -0400 | [diff] [blame] | 150 | def save(self, *args, **kwds): |
Tony Mack | e1c8c38 | 2013-03-26 13:37:59 -0400 | [diff] [blame] | 151 | # sync keystone tenant |
Tony Mack | 7823b89 | 2013-03-29 20:35:26 -0400 | [diff] [blame] | 152 | driver = OpenStackDriver() |
| 153 | |
Tony Mack | d7cb771 | 2013-03-26 13:21:36 -0400 | [diff] [blame] | 154 | if not self.id: |
Tony Mack | 39444e4 | 2013-04-02 10:22:24 -0400 | [diff] [blame] | 155 | tenant = driver.create_tenant(tenant_name=self.name, |
Tony Mack | 7823b89 | 2013-03-29 20:35:26 -0400 | [diff] [blame] | 156 | description=self.description, |
| 157 | enabled=self.enabled) |
Tony Mack | d8c390f | 2013-04-02 10:36:19 -0400 | [diff] [blame] | 158 | self.tenant_id = tenant.id |
Tony Mack | 3d1f069 | 2013-04-04 23:55:54 -0400 | [diff] [blame] | 159 | |
Tony Mack | 3d1f069 | 2013-04-04 23:55:54 -0400 | [diff] [blame] | 160 | # create a network |
Tony Mack | af905fc | 2013-04-06 01:30:40 -0400 | [diff] [blame] | 161 | network = driver.create_network(name=self.name) |
| 162 | self.network_id = network['id'] |
| 163 | # create router |
| 164 | router = driver.create_router(name=self.name) |
| 165 | self.router_id = router['id'] |
Tony Mack | 3d1f069 | 2013-04-04 23:55:54 -0400 | [diff] [blame] | 166 | |
Tony Mack | d7cb771 | 2013-03-26 13:21:36 -0400 | [diff] [blame] | 167 | else: |
| 168 | # update record |
Tony Mack | 7823b89 | 2013-03-29 20:35:26 -0400 | [diff] [blame] | 169 | self.driver.update_tenant(self.tenant_id, name=self.name, |
| 170 | description=self.description, enabled=self.enabled) |
Tony Mack | 6299cb7 | 2013-04-02 11:23:26 -0400 | [diff] [blame] | 171 | super(Slice, self).save(*args, **kwds) |
Tony Mack | d7cb771 | 2013-03-26 13:21:36 -0400 | [diff] [blame] | 172 | |
Tony Mack | e1c8c38 | 2013-03-26 13:37:59 -0400 | [diff] [blame] | 173 | def delete(self, *args, **kwds): |
| 174 | # delete keystone tenant |
Tony Mack | 7823b89 | 2013-03-29 20:35:26 -0400 | [diff] [blame] | 175 | driver = OpenStackDriver() |
| 176 | driver.delete_tenant(self.tenant_id) |
Tony Mack | 6299cb7 | 2013-04-02 11:23:26 -0400 | [diff] [blame] | 177 | super(Slice, self).delete(*args, **kwds) |
Tony Mack | e1c8c38 | 2013-03-26 13:37:59 -0400 | [diff] [blame] | 178 | |
Tony Mack | 3d1f069 | 2013-04-04 23:55:54 -0400 | [diff] [blame] | 179 | class SliceMembership(PlCoreBase): |
| 180 | user = models.ForeignKey('User') |
| 181 | slice = models.ForeignKey('Slice') |
| 182 | role = models.ForeignKey('Role') |
| 183 | |
| 184 | def __unicode__(self): return u'%s %s %s' % (self.slice, self.user, self.role) |
| 185 | |
Tony Mack | 1fbdeca | 2013-04-06 00:10:36 -0400 | [diff] [blame] | 186 | def save(self, *args, **kwds): |
| 187 | driver = OpenStackDriver() |
| 188 | driver.add_user_role(user_id=user.user_id, |
| 189 | tenant_id=slice.tenant_id, |
| 190 | role_name=role.name) |
| 191 | super(SliceMembership, self).save(*args, **kwds) |
| 192 | |
| 193 | def delete(self, *args, **kwds): |
| 194 | driver = OpenStackDriver() |
| 195 | driver.delete_user_role(user_id=user.user_id, |
| 196 | tenant_id=slice.tenant_id, |
| 197 | role_name=role.name) |
| 198 | super(SliceMembership, self).delete(*args, **kwds) |
Tony Mack | 3d1f069 | 2013-04-04 23:55:54 -0400 | [diff] [blame] | 199 | |
| 200 | class SubNet(PlCoreBase): |
| 201 | subnet_id = models.CharField(max_length=256, unique=True) |
| 202 | cidr = models.CharField(max_length=20) |
| 203 | ip_version = models.IntegerField() |
| 204 | start = models.IPAddressField() |
| 205 | end = models.IPAddressField() |
| 206 | slice = models.ForeignKey(Slice, related_name='slice_subnet') |
| 207 | |
| 208 | def __unicode__(self): return u'%s' % (self.name) |
| 209 | |
| 210 | def save(self, *args, **kwargs): |
| 211 | driver = OpenStackDriver() |
| 212 | if not self.id: |
| 213 | subnet = driver.create_subnet(network_name=self.slice.name, |
| 214 | cidr_ip = self.cidr, |
| 215 | ip_version=self.ip_version, |
| 216 | start = self.start, |
| 217 | end = self.end) |
| 218 | |
| 219 | self.subnet_id = subnet.id |
| 220 | |
Tony Mack | af905fc | 2013-04-06 01:30:40 -0400 | [diff] [blame] | 221 | # add subnet as interface to slice router |
| 222 | driver.add_router_interface(self.slice.router_id, subnet.id) |
| 223 | |
Tony Mack | 3d1f069 | 2013-04-04 23:55:54 -0400 | [diff] [blame] | 224 | super(SubNet, self).save(*args, **kwargs) |
| 225 | |
| 226 | |
| 227 | def delete(self, *args, **kwargs): |
| 228 | # delete quantum network |
| 229 | driver = OpenStackDriver() |
| 230 | driver.delete_subnet(self.subnet_id) |
Tony Mack | af905fc | 2013-04-06 01:30:40 -0400 | [diff] [blame] | 231 | driver.delete_router_interface(self.slice.router_id, self.subnet.id) |
Tony Mack | 3d1f069 | 2013-04-04 23:55:54 -0400 | [diff] [blame] | 232 | super(SubNet, self).delete(*args, **kwargs) |
| 233 | |
Tony Mack | 3d1f069 | 2013-04-04 23:55:54 -0400 | [diff] [blame] | 234 | class Node(PlCoreBase): |
| 235 | name = models.CharField(max_length=200, unique=True, help_text="Name of the Node") |
| 236 | siteDeploymentNetwork = models.ForeignKey(SiteDeploymentNetwork, help_text="The Site and Deployment Network this Node belongs too.") |
| 237 | |
| 238 | def __unicode__(self): return u'%s' % (self.name) |
| 239 | |
| 240 | class Image(PlCoreBase): |
| 241 | image_id = models.CharField(max_length=256, unique=True) |
| 242 | name = models.CharField(max_length=256, unique=True) |
| 243 | disk_format = models.CharField(max_length=256) |
| 244 | container_format = models.CharField(max_length=256) |
| 245 | |
| 246 | def __unicode__(self): return u'%s' % (self.name) |
| 247 | |
| 248 | |
| 249 | class Flavor(PlCoreBase): |
| 250 | flavor_id = models.IntegerField(unique=True) |
| 251 | name = models.CharField(max_length=256, unique=True) |
| 252 | memory_mb = models.IntegerField() |
| 253 | disk_gb = models.IntegerField() |
| 254 | vcpus = models.IntegerField() |
| 255 | |
| 256 | def __unicode__(self): return u'%s' % (self.name) |
| 257 | |
| 258 | class Key(PlCoreBase): |
| 259 | name = models.CharField(max_length=256, unique=True) |
| 260 | key = models.CharField(max_length=512) |
| 261 | type = models.CharField(max_length=256) |
| 262 | blacklisted = models.BooleanField() |
| 263 | user = models.ForeignKey(User) |
| 264 | |
| 265 | def __unicode__(self): return u'%s' % (self.name) |
| 266 | |
| 267 | def save(self, *args, **kwds): |
| 268 | driver = OpenStackDriver() |
| 269 | if not self.id: |
| 270 | keypair = driver.create_keypair(name=self.name, key=self.key) |
| 271 | super(Key, self).save(*args, **kwds) |
| 272 | |
| 273 | def delete(self, *args, **kwds): |
| 274 | driver = OpenStackDriver() |
| 275 | driver.delete_keypair(self.name) |
| 276 | super(Key, self).delete(*args, **kwds) |
| 277 | |
| 278 | |
| 279 | |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 280 | class Sliver(PlCoreBase): |
Tony Mack | ebb2487 | 2013-04-02 08:20:59 -0400 | [diff] [blame] | 281 | instance_id = models.CharField(max_length=200, help_text="Nova instance id") |
Tony Mack | d9ae9da | 2013-04-02 08:58:22 -0400 | [diff] [blame] | 282 | name = models.CharField(max_length=200, help_text="Sliver name") |
Tony Mack | 3d1f069 | 2013-04-04 23:55:54 -0400 | [diff] [blame] | 283 | flavor = models.ForeignKey(Flavor, related_name='sliver_flavor') |
| 284 | image = models.ForeignKey(Image, related_name='sliver_image') |
| 285 | key = models.ForeignKey(Key, related_name='sliver_key') |
Tony Mack | 27527b9 | 2013-04-02 08:59:35 -0400 | [diff] [blame] | 286 | slice = models.ForeignKey(Slice, related_name='sliver_slice') |
Tony Mack | 3d1f069 | 2013-04-04 23:55:54 -0400 | [diff] [blame] | 287 | siteDeploymentNetwork = models.ForeignKey(SiteDeploymentNetwork, related_name='sliver_deployment') |
| 288 | node = models.ForeignKey(Node, related_name='sliver_node') |
Tony Mack | 7130ac3 | 2013-03-22 21:58:00 -0400 | [diff] [blame] | 289 | |
| 290 | def __unicode__(self): return u'%s::%s' % (self.slice, self.siteDeploymentNetwork) |
| 291 | |
Tony Mack | 7823b89 | 2013-03-29 20:35:26 -0400 | [diff] [blame] | 292 | def save(self, *args, **kwds): |
| 293 | driver = OpenStackDriver() |
| 294 | instance = driver.spawn_instances(name=self.name, |
| 295 | keyname=self.name, |
| 296 | hostnames=self.node.name, |
Tony Mack | 3d1f069 | 2013-04-04 23:55:54 -0400 | [diff] [blame] | 297 | flavor=self.flavor.name, |
| 298 | image=self.image.name) |
Tony Mack | 7823b89 | 2013-03-29 20:35:26 -0400 | [diff] [blame] | 299 | self.instance_id = instance.id |
| 300 | super(Sliver, self).save(*args, **kwds) |
| 301 | |
| 302 | def delete(self, *args, **kwds): |
| 303 | driver = OpenStackDriver() |
| 304 | driver.destroy_instance(name=self.name, id=self.instance_id) |
| 305 | super(Sliver, self).delete(*args, **kwds) |
Tony Mack | 7823b89 | 2013-03-29 20:35:26 -0400 | [diff] [blame] | 306 | |