blob: 3a24f4d6504ae61d8cf5b9e0bb2892b62cf64b1b [file] [log] [blame]
Tony Mack5cbec5d2013-04-02 07:55:11 -04001import os
Tony Mack9aa28af2013-04-08 16:41:31 -04002import datetime
Tony Mack7130ac32013-03-22 21:58:00 -04003from django.db import models
Tony Mack7823b892013-03-29 20:35:26 -04004from plstackapi.openstack.driver import OpenStackDriver
Tony Mack7130ac32013-03-22 21:58:00 -04005
6# Create your models here.
7
8class 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 Mack1fbdeca2013-04-06 00:10:36 -040016 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 Mack3d1f0692013-04-04 23:55:54 -040022class Role(PlCoreBase):
23
24 ROLE_CHOICES = (('admin', 'Admin'), ('pi', 'Principle Investigator'), ('user','User'))
Tony Mackf2162d92013-04-07 19:32:20 -040025 role_id = models.CharField(max_length=256, unique=True)
Tony Mack3d1f0692013-04-04 23:55:54 -040026 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 Mack7130ac32013-03-22 21:58:00 -040036class Site(PlCoreBase):
Tony Mackd7cb7712013-03-26 13:21:36 -040037 tenant_id = models.CharField(max_length=200, help_text="Keystone tenant id")
Tony Mack67f60ce2013-04-02 10:46:07 -040038 name = models.CharField(max_length=200, help_text="Name for this Site")
Tony Mackc689d502013-04-02 11:32:04 -040039 site_url = models.URLField(null=True, blank=True, max_length=512, help_text="Site's Home URL Page")
Tony Mack7130ac32013-03-22 21:58:00 -040040 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)
43 login_base = models.CharField(max_length=50, help_text="Prefix for Slices associated with this Site")
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 Mackd7cb7712013-03-26 13:21:36 -040049 def save(self, *args, **kwargs):
Tony Mack7823b892013-03-29 20:35:26 -040050 driver = OpenStackDriver()
Tony Mackd7cb7712013-03-26 13:21:36 -040051 if not self.id:
Tony Mack39444e42013-04-02 10:22:24 -040052 tenant = driver.create_tenant(tenant_name=self.login_base,
Tony Mack7823b892013-03-29 20:35:26 -040053 description=self.name,
54 enabled=self.enabled)
Tony Mackd8c390f2013-04-02 10:36:19 -040055 self.tenant_id = tenant.id
Tony Mackd7cb7712013-03-26 13:21:36 -040056 else:
57 # update record
Tony Mack7823b892013-03-29 20:35:26 -040058 self.driver.update_tenant(self.tenant_id, name=self.login_base,
59 description=self.name, enabled=self.enabled)
Tony Mackd7cb7712013-03-26 13:21:36 -040060 super(Site, self).save(*args, **kwargs)
61
Tony Macke1c8c382013-03-26 13:37:59 -040062 def delete(self, *args, **kwds):
63 # delete keystone tenant
Tony Mack7823b892013-03-29 20:35:26 -040064 driver = OpenStackDriver()
65 driver.delete_tenant(self.tenant_id)
Tony Macke1c8c382013-03-26 13:37:59 -040066 super(Site, self).delete(*args, **kwargs)
67
Tony Mack3d1f0692013-04-04 23:55:54 -040068class User(PlCoreBase):
69 user_id = models.CharField(max_length=256, unique=True)
70 firstname = models.CharField(help_text="person's given name", max_length=200)
71 lastname = models.CharField(help_text="person's surname", max_length=200)
72 email = models.EmailField(help_text="e-mail address")
Tony Mack1fbdeca2013-04-06 00:10:36 -040073 phone = models.CharField(null=True, blank=True, help_text="phone number contact", max_length=100)
74 user_url = models.URLField(null=True, blank=True)
75 is_admin = models.BooleanField(default=False)
Tony Mack3d1f0692013-04-04 23:55:54 -040076 site = models.ForeignKey(Site, related_name='site_user', verbose_name="Site this user will be homed too")
77
78 def __unicode__(self): return u'%s' % (self.email)
79
Tony Mack1fbdeca2013-04-06 00:10:36 -040080 def save(self, *args, **kwds):
81 driver = OpenStackDriver()
82 name = self.email[:self.email.find('@')]
83 fields = {'name': name, 'email': self.email, 'password': self.password,
84 'enabled': self.enabled}
Tony Mack3d1f0692013-04-04 23:55:54 -040085 if not self.id:
Tony Mack1fbdeca2013-04-06 00:10:36 -040086 user = driver.create_user(**fields)
87 else:
88 driver.update_user(self.user_id, **fields)
89 super(User, self).save(*args, **kwds)
90
91 def delete(self, *args, **kwds):
92 driver = OpenStackDriver()
93 driver.delete_user(self.user_id)
94 super(User, self).delete(*args, **kwds)
Tony Mack3d1f0692013-04-04 23:55:54 -040095
96class SitePrivilege(PlCoreBase):
97
98 user = models.ForeignKey('User')
99 site = models.ForeignKey('Site')
100 role = models.ForeignKey('Role')
101
102 def __unicode__(self): return u'%s %s %s' % (self.site, self.user, self.role)
103
Tony Mack1fbdeca2013-04-06 00:10:36 -0400104 def save(self, *args, **kwds):
105 driver = OpenStackDriver()
106 driver.add_user_role(user_id=user.user_id,
107 tenant_id=site.tenant_id,
108 role_name=role.name)
109 super(SitePrivilege, self).save(*args, **kwds)
110
111 def delete(self, *args, **kwds):
112 driver = OpenStackDriver()
113 driver.delete_user_role(user_id=user.user_id,
114 tenant_id=site.tenant_id,
115 role_name=role.name)
116 super(SitePrivilege, self).delete(*args, **kwds)
117
118
119class DeploymentNetwork(PlCoreBase):
120 name = models.CharField(max_length=200, unique=True, help_text="Name of the Deployment Network")
121
122 def __unicode__(self): return u'%s' % (self.name)
123
124class SiteDeploymentNetwork(PlCoreBase):
125 class Meta:
126 unique_together = ['site', 'deploymentNetwork']
127
128 site = models.ForeignKey(Site, related_name='deploymentNetworks')
129 deploymentNetwork = models.ForeignKey(DeploymentNetwork, related_name='sites')
130 name = models.CharField(default="Blah", max_length=100)
131
132
133 def __unicode__(self): return u'%s::%s' % (self.site, self.deploymentNetwork)
134
Tony Mack3d1f0692013-04-04 23:55:54 -0400135
Tony Mack7130ac32013-03-22 21:58:00 -0400136class Slice(PlCoreBase):
Tony Mackd7cb7712013-03-26 13:21:36 -0400137 tenant_id = models.CharField(max_length=200, help_text="Keystone tenant id")
Tony Mack7130ac32013-03-22 21:58:00 -0400138 name = models.CharField(help_text="The Name of the Slice", max_length=80)
Tony Macka3a40cf2013-04-02 11:21:21 -0400139 enabled = models.BooleanField(default=True, help_text="Status for this Slice")
Tony Mack7130ac32013-03-22 21:58:00 -0400140 SLICE_CHOICES = (('plc', 'PLC'), ('delegated', 'Delegated'), ('controller','Controller'), ('none','None'))
141 instantiation = models.CharField(help_text="The instantiation type of the slice", max_length=80, choices=SLICE_CHOICES)
142 omf_friendly = models.BooleanField()
143 description=models.TextField(blank=True,help_text="High level description of the slice and expected activities", max_length=1024)
Tony Mackc689d502013-04-02 11:32:04 -0400144 slice_url = models.URLField(blank=True, max_length=512)
Tony Mack3d1f0692013-04-04 23:55:54 -0400145 site = models.ForeignKey(Site, related_name='site_slice', help_text="The Site this Node belongs too")
Tony Mackaf905fc2013-04-06 01:30:40 -0400146 network_id = models.CharField(max_length=256, help_text="Quantum network")
147 router_id = models.CharField(max_length=256, help_text="Quantum router id")
Tony Mack7130ac32013-03-22 21:58:00 -0400148
149 def __unicode__(self): return u'%s' % (self.name)
150
Tony Mackd7cb7712013-03-26 13:21:36 -0400151 def save(self, *args, **kwds):
Tony Macke1c8c382013-03-26 13:37:59 -0400152 # sync keystone tenant
Tony Mack7823b892013-03-29 20:35:26 -0400153 driver = OpenStackDriver()
154
Tony Mackd7cb7712013-03-26 13:21:36 -0400155 if not self.id:
Tony Mack39444e42013-04-02 10:22:24 -0400156 tenant = driver.create_tenant(tenant_name=self.name,
Tony Mack7823b892013-03-29 20:35:26 -0400157 description=self.description,
158 enabled=self.enabled)
Tony Mackd8c390f2013-04-02 10:36:19 -0400159 self.tenant_id = tenant.id
Tony Mack3d1f0692013-04-04 23:55:54 -0400160
Tony Mack3d1f0692013-04-04 23:55:54 -0400161 # create a network
Tony Mackaf905fc2013-04-06 01:30:40 -0400162 network = driver.create_network(name=self.name)
163 self.network_id = network['id']
164 # create router
165 router = driver.create_router(name=self.name)
166 self.router_id = router['id']
Tony Mack3d1f0692013-04-04 23:55:54 -0400167
Tony Mackd7cb7712013-03-26 13:21:36 -0400168 else:
169 # update record
Tony Mack7823b892013-03-29 20:35:26 -0400170 self.driver.update_tenant(self.tenant_id, name=self.name,
171 description=self.description, enabled=self.enabled)
Tony Mack6299cb72013-04-02 11:23:26 -0400172 super(Slice, self).save(*args, **kwds)
Tony Mackd7cb7712013-03-26 13:21:36 -0400173
Tony Macke1c8c382013-03-26 13:37:59 -0400174 def delete(self, *args, **kwds):
175 # delete keystone tenant
Tony Mack7823b892013-03-29 20:35:26 -0400176 driver = OpenStackDriver()
177 driver.delete_tenant(self.tenant_id)
Tony Mack6299cb72013-04-02 11:23:26 -0400178 super(Slice, self).delete(*args, **kwds)
Tony Macke1c8c382013-03-26 13:37:59 -0400179
Tony Mack3d1f0692013-04-04 23:55:54 -0400180class SliceMembership(PlCoreBase):
181 user = models.ForeignKey('User')
182 slice = models.ForeignKey('Slice')
183 role = models.ForeignKey('Role')
184
185 def __unicode__(self): return u'%s %s %s' % (self.slice, self.user, self.role)
186
Tony Mack1fbdeca2013-04-06 00:10:36 -0400187 def save(self, *args, **kwds):
188 driver = OpenStackDriver()
189 driver.add_user_role(user_id=user.user_id,
190 tenant_id=slice.tenant_id,
191 role_name=role.name)
192 super(SliceMembership, self).save(*args, **kwds)
193
194 def delete(self, *args, **kwds):
195 driver = OpenStackDriver()
196 driver.delete_user_role(user_id=user.user_id,
197 tenant_id=slice.tenant_id,
198 role_name=role.name)
199 super(SliceMembership, self).delete(*args, **kwds)
Tony Mack3d1f0692013-04-04 23:55:54 -0400200
201class SubNet(PlCoreBase):
202 subnet_id = models.CharField(max_length=256, unique=True)
203 cidr = models.CharField(max_length=20)
204 ip_version = models.IntegerField()
205 start = models.IPAddressField()
206 end = models.IPAddressField()
207 slice = models.ForeignKey(Slice, related_name='slice_subnet')
208
209 def __unicode__(self): return u'%s' % (self.name)
210
211 def save(self, *args, **kwargs):
212 driver = OpenStackDriver()
213 if not self.id:
214 subnet = driver.create_subnet(network_name=self.slice.name,
215 cidr_ip = self.cidr,
216 ip_version=self.ip_version,
217 start = self.start,
218 end = self.end)
219
220 self.subnet_id = subnet.id
221
Tony Mackaf905fc2013-04-06 01:30:40 -0400222 # add subnet as interface to slice router
223 driver.add_router_interface(self.slice.router_id, subnet.id)
224
Tony Mack3d1f0692013-04-04 23:55:54 -0400225 super(SubNet, self).save(*args, **kwargs)
226
227
228 def delete(self, *args, **kwargs):
229 # delete quantum network
230 driver = OpenStackDriver()
231 driver.delete_subnet(self.subnet_id)
Tony Mackaf905fc2013-04-06 01:30:40 -0400232 driver.delete_router_interface(self.slice.router_id, self.subnet.id)
Tony Mack3d1f0692013-04-04 23:55:54 -0400233 super(SubNet, self).delete(*args, **kwargs)
234
Tony Mack3d1f0692013-04-04 23:55:54 -0400235class Node(PlCoreBase):
236 name = models.CharField(max_length=200, unique=True, help_text="Name of the Node")
237 siteDeploymentNetwork = models.ForeignKey(SiteDeploymentNetwork, help_text="The Site and Deployment Network this Node belongs too.")
238
239 def __unicode__(self): return u'%s' % (self.name)
240
241class Image(PlCoreBase):
242 image_id = models.CharField(max_length=256, unique=True)
243 name = models.CharField(max_length=256, unique=True)
244 disk_format = models.CharField(max_length=256)
245 container_format = models.CharField(max_length=256)
246
247 def __unicode__(self): return u'%s' % (self.name)
248
249
250class Flavor(PlCoreBase):
251 flavor_id = models.IntegerField(unique=True)
252 name = models.CharField(max_length=256, unique=True)
253 memory_mb = models.IntegerField()
254 disk_gb = models.IntegerField()
255 vcpus = models.IntegerField()
256
257 def __unicode__(self): return u'%s' % (self.name)
258
259class Key(PlCoreBase):
260 name = models.CharField(max_length=256, unique=True)
261 key = models.CharField(max_length=512)
262 type = models.CharField(max_length=256)
263 blacklisted = models.BooleanField()
264 user = models.ForeignKey(User)
265
266 def __unicode__(self): return u'%s' % (self.name)
267
268 def save(self, *args, **kwds):
269 driver = OpenStackDriver()
270 if not self.id:
271 keypair = driver.create_keypair(name=self.name, key=self.key)
272 super(Key, self).save(*args, **kwds)
273
274 def delete(self, *args, **kwds):
275 driver = OpenStackDriver()
276 driver.delete_keypair(self.name)
277 super(Key, self).delete(*args, **kwds)
278
279
280
Tony Mack7130ac32013-03-22 21:58:00 -0400281class Sliver(PlCoreBase):
Tony Mackebb24872013-04-02 08:20:59 -0400282 instance_id = models.CharField(max_length=200, help_text="Nova instance id")
Tony Mackd9ae9da2013-04-02 08:58:22 -0400283 name = models.CharField(max_length=200, help_text="Sliver name")
Tony Mack3d1f0692013-04-04 23:55:54 -0400284 flavor = models.ForeignKey(Flavor, related_name='sliver_flavor')
285 image = models.ForeignKey(Image, related_name='sliver_image')
286 key = models.ForeignKey(Key, related_name='sliver_key')
Tony Mack27527b92013-04-02 08:59:35 -0400287 slice = models.ForeignKey(Slice, related_name='sliver_slice')
Tony Mack3d1f0692013-04-04 23:55:54 -0400288 siteDeploymentNetwork = models.ForeignKey(SiteDeploymentNetwork, related_name='sliver_deployment')
289 node = models.ForeignKey(Node, related_name='sliver_node')
Tony Mack7130ac32013-03-22 21:58:00 -0400290
291 def __unicode__(self): return u'%s::%s' % (self.slice, self.siteDeploymentNetwork)
292
Tony Mack7823b892013-03-29 20:35:26 -0400293 def save(self, *args, **kwds):
294 driver = OpenStackDriver()
295 instance = driver.spawn_instances(name=self.name,
296 keyname=self.name,
297 hostnames=self.node.name,
Tony Mack3d1f0692013-04-04 23:55:54 -0400298 flavor=self.flavor.name,
299 image=self.image.name)
Tony Mack7823b892013-03-29 20:35:26 -0400300 self.instance_id = instance.id
301 super(Sliver, self).save(*args, **kwds)
302
303 def delete(self, *args, **kwds):
304 driver = OpenStackDriver()
305 driver.destroy_instance(name=self.name, id=self.instance_id)
306 super(Sliver, self).delete(*args, **kwds)
Tony Mack7823b892013-03-29 20:35:26 -0400307