blob: a8fea634644cacf213a64787bb7850e6afad2f42 [file] [log] [blame]
Tony Mack5cbec5d2013-04-02 07:55:11 -04001import os
Tony Mack7130ac32013-03-22 21:58:00 -04002from django.db import models
Tony Mack7823b892013-03-29 20:35:26 -04003from plstackapi.openstack.driver import OpenStackDriver
Tony Mack7130ac32013-03-22 21:58:00 -04004
5# Create your models here.
6
7class 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 Mack1fbdeca2013-04-06 00:10:36 -040015 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 Mack3d1f0692013-04-04 23:55:54 -040021class Role(PlCoreBase):
22
23 ROLE_CHOICES = (('admin', 'Admin'), ('pi', 'Principle Investigator'), ('user','User'))
Tony Mackf2162d92013-04-07 19:32:20 -040024 role_id = models.CharField(max_length=256, unique=True)
Tony Mack3d1f0692013-04-04 23:55:54 -040025 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 Mack7130ac32013-03-22 21:58:00 -040035class Site(PlCoreBase):
Tony Mackd7cb7712013-03-26 13:21:36 -040036 tenant_id = models.CharField(max_length=200, help_text="Keystone tenant id")
Tony Mack67f60ce2013-04-02 10:46:07 -040037 name = models.CharField(max_length=200, help_text="Name for this Site")
Tony Mackc689d502013-04-02 11:32:04 -040038 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 -040039 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 Mackd7cb7712013-03-26 13:21:36 -040048 def save(self, *args, **kwargs):
Tony Mack7823b892013-03-29 20:35:26 -040049 driver = OpenStackDriver()
Tony Mackd7cb7712013-03-26 13:21:36 -040050 if not self.id:
Tony Mack39444e42013-04-02 10:22:24 -040051 tenant = driver.create_tenant(tenant_name=self.login_base,
Tony Mack7823b892013-03-29 20:35:26 -040052 description=self.name,
53 enabled=self.enabled)
Tony Mackd8c390f2013-04-02 10:36:19 -040054 self.tenant_id = tenant.id
Tony Mackd7cb7712013-03-26 13:21:36 -040055 else:
56 # update record
Tony Mack7823b892013-03-29 20:35:26 -040057 self.driver.update_tenant(self.tenant_id, name=self.login_base,
58 description=self.name, enabled=self.enabled)
Tony Mackd7cb7712013-03-26 13:21:36 -040059 super(Site, self).save(*args, **kwargs)
60
Tony Macke1c8c382013-03-26 13:37:59 -040061 def delete(self, *args, **kwds):
62 # delete keystone tenant
Tony Mack7823b892013-03-29 20:35:26 -040063 driver = OpenStackDriver()
64 driver.delete_tenant(self.tenant_id)
Tony Macke1c8c382013-03-26 13:37:59 -040065 super(Site, self).delete(*args, **kwargs)
66
Tony Mack3d1f0692013-04-04 23:55:54 -040067class 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 Mack1fbdeca2013-04-06 00:10:36 -040072 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 Mack3d1f0692013-04-04 23:55:54 -040075 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 Mack1fbdeca2013-04-06 00:10:36 -040079 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 Mack3d1f0692013-04-04 23:55:54 -040084 if not self.id:
Tony Mack1fbdeca2013-04-06 00:10:36 -040085 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 Mack3d1f0692013-04-04 23:55:54 -040094
95class 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 Mack1fbdeca2013-04-06 00:10:36 -0400103 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
118class 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
123class 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 Mack3d1f0692013-04-04 23:55:54 -0400134
Tony Mack7130ac32013-03-22 21:58:00 -0400135class Slice(PlCoreBase):
Tony Mackd7cb7712013-03-26 13:21:36 -0400136 tenant_id = models.CharField(max_length=200, help_text="Keystone tenant id")
Tony Mack7130ac32013-03-22 21:58:00 -0400137 name = models.CharField(help_text="The Name of the Slice", max_length=80)
Tony Macka3a40cf2013-04-02 11:21:21 -0400138 enabled = models.BooleanField(default=True, help_text="Status for this Slice")
Tony Mack7130ac32013-03-22 21:58:00 -0400139 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 Mackc689d502013-04-02 11:32:04 -0400143 slice_url = models.URLField(blank=True, max_length=512)
Tony Mack3d1f0692013-04-04 23:55:54 -0400144 site = models.ForeignKey(Site, related_name='site_slice', help_text="The Site this Node belongs too")
Tony Mackaf905fc2013-04-06 01:30:40 -0400145 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 Mack7130ac32013-03-22 21:58:00 -0400147
148 def __unicode__(self): return u'%s' % (self.name)
149
Tony Mackd7cb7712013-03-26 13:21:36 -0400150 def save(self, *args, **kwds):
Tony Macke1c8c382013-03-26 13:37:59 -0400151 # sync keystone tenant
Tony Mack7823b892013-03-29 20:35:26 -0400152 driver = OpenStackDriver()
153
Tony Mackd7cb7712013-03-26 13:21:36 -0400154 if not self.id:
Tony Mack39444e42013-04-02 10:22:24 -0400155 tenant = driver.create_tenant(tenant_name=self.name,
Tony Mack7823b892013-03-29 20:35:26 -0400156 description=self.description,
157 enabled=self.enabled)
Tony Mackd8c390f2013-04-02 10:36:19 -0400158 self.tenant_id = tenant.id
Tony Mack3d1f0692013-04-04 23:55:54 -0400159
Tony Mack3d1f0692013-04-04 23:55:54 -0400160 # create a network
Tony Mackaf905fc2013-04-06 01:30:40 -0400161 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 Mack3d1f0692013-04-04 23:55:54 -0400166
Tony Mackd7cb7712013-03-26 13:21:36 -0400167 else:
168 # update record
Tony Mack7823b892013-03-29 20:35:26 -0400169 self.driver.update_tenant(self.tenant_id, name=self.name,
170 description=self.description, enabled=self.enabled)
Tony Mack6299cb72013-04-02 11:23:26 -0400171 super(Slice, self).save(*args, **kwds)
Tony Mackd7cb7712013-03-26 13:21:36 -0400172
Tony Macke1c8c382013-03-26 13:37:59 -0400173 def delete(self, *args, **kwds):
174 # delete keystone tenant
Tony Mack7823b892013-03-29 20:35:26 -0400175 driver = OpenStackDriver()
176 driver.delete_tenant(self.tenant_id)
Tony Mack6299cb72013-04-02 11:23:26 -0400177 super(Slice, self).delete(*args, **kwds)
Tony Macke1c8c382013-03-26 13:37:59 -0400178
Tony Mack3d1f0692013-04-04 23:55:54 -0400179class 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 Mack1fbdeca2013-04-06 00:10:36 -0400186 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 Mack3d1f0692013-04-04 23:55:54 -0400199
200class 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 Mackaf905fc2013-04-06 01:30:40 -0400221 # add subnet as interface to slice router
222 driver.add_router_interface(self.slice.router_id, subnet.id)
223
Tony Mack3d1f0692013-04-04 23:55:54 -0400224 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 Mackaf905fc2013-04-06 01:30:40 -0400231 driver.delete_router_interface(self.slice.router_id, self.subnet.id)
Tony Mack3d1f0692013-04-04 23:55:54 -0400232 super(SubNet, self).delete(*args, **kwargs)
233
Tony Mack3d1f0692013-04-04 23:55:54 -0400234class 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
240class 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
249class 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
258class 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 Mack7130ac32013-03-22 21:58:00 -0400280class Sliver(PlCoreBase):
Tony Mackebb24872013-04-02 08:20:59 -0400281 instance_id = models.CharField(max_length=200, help_text="Nova instance id")
Tony Mackd9ae9da2013-04-02 08:58:22 -0400282 name = models.CharField(max_length=200, help_text="Sliver name")
Tony Mack3d1f0692013-04-04 23:55:54 -0400283 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 Mack27527b92013-04-02 08:59:35 -0400286 slice = models.ForeignKey(Slice, related_name='sliver_slice')
Tony Mack3d1f0692013-04-04 23:55:54 -0400287 siteDeploymentNetwork = models.ForeignKey(SiteDeploymentNetwork, related_name='sliver_deployment')
288 node = models.ForeignKey(Node, related_name='sliver_node')
Tony Mack7130ac32013-03-22 21:58:00 -0400289
290 def __unicode__(self): return u'%s::%s' % (self.slice, self.siteDeploymentNetwork)
291
Tony Mack7823b892013-03-29 20:35:26 -0400292 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 Mack3d1f0692013-04-04 23:55:54 -0400297 flavor=self.flavor.name,
298 image=self.image.name)
Tony Mack7823b892013-03-29 20:35:26 -0400299 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 Mack7823b892013-03-29 20:35:26 -0400306