blob: 8904bbd5ccd669a52bb10e0b5af0b7e6534cd2c2 [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'))
24 role_type = models.CharField(max_length=80, unique=True, choices=ROLE_CHOICES)
25
26 def __unicode__(self): return u'%s' % (self.role_type)
27
28 def save(self):
29 if not self.id:
30 self.created = datetime.date.today()
31 self.updated = datetime.datetime.today()
32 super(Role, self).save()
33
Tony Mack7130ac32013-03-22 21:58:00 -040034class Site(PlCoreBase):
Tony Mackd7cb7712013-03-26 13:21:36 -040035 tenant_id = models.CharField(max_length=200, help_text="Keystone tenant id")
Tony Mack67f60ce2013-04-02 10:46:07 -040036 name = models.CharField(max_length=200, help_text="Name for this Site")
Tony Mackc689d502013-04-02 11:32:04 -040037 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 -040038 enabled = models.BooleanField(default=True, help_text="Status for this Site")
39 longitude = models.FloatField(null=True, blank=True)
40 latitude = models.FloatField(null=True, blank=True)
41 login_base = models.CharField(max_length=50, help_text="Prefix for Slices associated with this Site")
42 is_public = models.BooleanField(default=True, help_text="Indicates the visibility of this site to other members")
43 abbreviated_name = models.CharField(max_length=80)
44
45 def __unicode__(self): return u'%s' % (self.name)
46
Tony Mackd7cb7712013-03-26 13:21:36 -040047 def save(self, *args, **kwargs):
Tony Mack7823b892013-03-29 20:35:26 -040048 driver = OpenStackDriver()
Tony Mackd7cb7712013-03-26 13:21:36 -040049 if not self.id:
Tony Mack39444e42013-04-02 10:22:24 -040050 tenant = driver.create_tenant(tenant_name=self.login_base,
Tony Mack7823b892013-03-29 20:35:26 -040051 description=self.name,
52 enabled=self.enabled)
Tony Mackd8c390f2013-04-02 10:36:19 -040053 self.tenant_id = tenant.id
Tony Mackd7cb7712013-03-26 13:21:36 -040054 else:
55 # update record
Tony Mack7823b892013-03-29 20:35:26 -040056 self.driver.update_tenant(self.tenant_id, name=self.login_base,
57 description=self.name, enabled=self.enabled)
Tony Mackd7cb7712013-03-26 13:21:36 -040058 super(Site, self).save(*args, **kwargs)
59
Tony Macke1c8c382013-03-26 13:37:59 -040060 def delete(self, *args, **kwds):
61 # delete keystone tenant
Tony Mack7823b892013-03-29 20:35:26 -040062 driver = OpenStackDriver()
63 driver.delete_tenant(self.tenant_id)
Tony Macke1c8c382013-03-26 13:37:59 -040064 super(Site, self).delete(*args, **kwargs)
65
Tony Mack3d1f0692013-04-04 23:55:54 -040066class User(PlCoreBase):
67 user_id = models.CharField(max_length=256, unique=True)
68 firstname = models.CharField(help_text="person's given name", max_length=200)
69 lastname = models.CharField(help_text="person's surname", max_length=200)
70 email = models.EmailField(help_text="e-mail address")
Tony Mack1fbdeca2013-04-06 00:10:36 -040071 phone = models.CharField(null=True, blank=True, help_text="phone number contact", max_length=100)
72 user_url = models.URLField(null=True, blank=True)
73 is_admin = models.BooleanField(default=False)
Tony Mack3d1f0692013-04-04 23:55:54 -040074 site = models.ForeignKey(Site, related_name='site_user', verbose_name="Site this user will be homed too")
75
76 def __unicode__(self): return u'%s' % (self.email)
77
Tony Mack1fbdeca2013-04-06 00:10:36 -040078 def save(self, *args, **kwds):
79 driver = OpenStackDriver()
80 name = self.email[:self.email.find('@')]
81 fields = {'name': name, 'email': self.email, 'password': self.password,
82 'enabled': self.enabled}
Tony Mack3d1f0692013-04-04 23:55:54 -040083 if not self.id:
Tony Mack1fbdeca2013-04-06 00:10:36 -040084 user = driver.create_user(**fields)
85 else:
86 driver.update_user(self.user_id, **fields)
87 super(User, self).save(*args, **kwds)
88
89 def delete(self, *args, **kwds):
90 driver = OpenStackDriver()
91 driver.delete_user(self.user_id)
92 super(User, self).delete(*args, **kwds)
Tony Mack3d1f0692013-04-04 23:55:54 -040093
94class SitePrivilege(PlCoreBase):
95
96 user = models.ForeignKey('User')
97 site = models.ForeignKey('Site')
98 role = models.ForeignKey('Role')
99
100 def __unicode__(self): return u'%s %s %s' % (self.site, self.user, self.role)
101
Tony Mack1fbdeca2013-04-06 00:10:36 -0400102 def save(self, *args, **kwds):
103 driver = OpenStackDriver()
104 driver.add_user_role(user_id=user.user_id,
105 tenant_id=site.tenant_id,
106 role_name=role.name)
107 super(SitePrivilege, self).save(*args, **kwds)
108
109 def delete(self, *args, **kwds):
110 driver = OpenStackDriver()
111 driver.delete_user_role(user_id=user.user_id,
112 tenant_id=site.tenant_id,
113 role_name=role.name)
114 super(SitePrivilege, self).delete(*args, **kwds)
115
116
117class DeploymentNetwork(PlCoreBase):
118 name = models.CharField(max_length=200, unique=True, help_text="Name of the Deployment Network")
119
120 def __unicode__(self): return u'%s' % (self.name)
121
122class SiteDeploymentNetwork(PlCoreBase):
123 class Meta:
124 unique_together = ['site', 'deploymentNetwork']
125
126 site = models.ForeignKey(Site, related_name='deploymentNetworks')
127 deploymentNetwork = models.ForeignKey(DeploymentNetwork, related_name='sites')
128 name = models.CharField(default="Blah", max_length=100)
129
130
131 def __unicode__(self): return u'%s::%s' % (self.site, self.deploymentNetwork)
132
Tony Mack3d1f0692013-04-04 23:55:54 -0400133
Tony Mack7130ac32013-03-22 21:58:00 -0400134class Slice(PlCoreBase):
Tony Mackd7cb7712013-03-26 13:21:36 -0400135 tenant_id = models.CharField(max_length=200, help_text="Keystone tenant id")
Tony Mack7130ac32013-03-22 21:58:00 -0400136 name = models.CharField(help_text="The Name of the Slice", max_length=80)
Tony Macka3a40cf2013-04-02 11:21:21 -0400137 enabled = models.BooleanField(default=True, help_text="Status for this Slice")
Tony Mack7130ac32013-03-22 21:58:00 -0400138 SLICE_CHOICES = (('plc', 'PLC'), ('delegated', 'Delegated'), ('controller','Controller'), ('none','None'))
139 instantiation = models.CharField(help_text="The instantiation type of the slice", max_length=80, choices=SLICE_CHOICES)
140 omf_friendly = models.BooleanField()
141 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 -0400142 slice_url = models.URLField(blank=True, max_length=512)
Tony Mack3d1f0692013-04-04 23:55:54 -0400143 site = models.ForeignKey(Site, related_name='site_slice', help_text="The Site this Node belongs too")
Tony Mackaf905fc2013-04-06 01:30:40 -0400144 network_id = models.CharField(max_length=256, help_text="Quantum network")
145 router_id = models.CharField(max_length=256, help_text="Quantum router id")
Tony Mack7130ac32013-03-22 21:58:00 -0400146
147 def __unicode__(self): return u'%s' % (self.name)
148
Tony Mackd7cb7712013-03-26 13:21:36 -0400149 def save(self, *args, **kwds):
Tony Macke1c8c382013-03-26 13:37:59 -0400150 # sync keystone tenant
Tony Mack7823b892013-03-29 20:35:26 -0400151 driver = OpenStackDriver()
152
Tony Mackd7cb7712013-03-26 13:21:36 -0400153 if not self.id:
Tony Mack39444e42013-04-02 10:22:24 -0400154 tenant = driver.create_tenant(tenant_name=self.name,
Tony Mack7823b892013-03-29 20:35:26 -0400155 description=self.description,
156 enabled=self.enabled)
Tony Mackd8c390f2013-04-02 10:36:19 -0400157 self.tenant_id = tenant.id
Tony Mack3d1f0692013-04-04 23:55:54 -0400158
Tony Mack3d1f0692013-04-04 23:55:54 -0400159 # create a network
Tony Mackaf905fc2013-04-06 01:30:40 -0400160 network = driver.create_network(name=self.name)
161 self.network_id = network['id']
162 # create router
163 router = driver.create_router(name=self.name)
164 self.router_id = router['id']
Tony Mack3d1f0692013-04-04 23:55:54 -0400165
Tony Mackd7cb7712013-03-26 13:21:36 -0400166 else:
167 # update record
Tony Mack7823b892013-03-29 20:35:26 -0400168 self.driver.update_tenant(self.tenant_id, name=self.name,
169 description=self.description, enabled=self.enabled)
Tony Mack6299cb72013-04-02 11:23:26 -0400170 super(Slice, self).save(*args, **kwds)
Tony Mackd7cb7712013-03-26 13:21:36 -0400171
Tony Macke1c8c382013-03-26 13:37:59 -0400172 def delete(self, *args, **kwds):
173 # delete keystone tenant
Tony Mack7823b892013-03-29 20:35:26 -0400174 driver = OpenStackDriver()
175 driver.delete_tenant(self.tenant_id)
Tony Mack6299cb72013-04-02 11:23:26 -0400176 super(Slice, self).delete(*args, **kwds)
Tony Macke1c8c382013-03-26 13:37:59 -0400177
Tony Mack3d1f0692013-04-04 23:55:54 -0400178class SliceMembership(PlCoreBase):
179 user = models.ForeignKey('User')
180 slice = models.ForeignKey('Slice')
181 role = models.ForeignKey('Role')
182
183 def __unicode__(self): return u'%s %s %s' % (self.slice, self.user, self.role)
184
Tony Mack1fbdeca2013-04-06 00:10:36 -0400185 def save(self, *args, **kwds):
186 driver = OpenStackDriver()
187 driver.add_user_role(user_id=user.user_id,
188 tenant_id=slice.tenant_id,
189 role_name=role.name)
190 super(SliceMembership, self).save(*args, **kwds)
191
192 def delete(self, *args, **kwds):
193 driver = OpenStackDriver()
194 driver.delete_user_role(user_id=user.user_id,
195 tenant_id=slice.tenant_id,
196 role_name=role.name)
197 super(SliceMembership, self).delete(*args, **kwds)
Tony Mack3d1f0692013-04-04 23:55:54 -0400198
199class SubNet(PlCoreBase):
200 subnet_id = models.CharField(max_length=256, unique=True)
201 cidr = models.CharField(max_length=20)
202 ip_version = models.IntegerField()
203 start = models.IPAddressField()
204 end = models.IPAddressField()
205 slice = models.ForeignKey(Slice, related_name='slice_subnet')
206
207 def __unicode__(self): return u'%s' % (self.name)
208
209 def save(self, *args, **kwargs):
210 driver = OpenStackDriver()
211 if not self.id:
212 subnet = driver.create_subnet(network_name=self.slice.name,
213 cidr_ip = self.cidr,
214 ip_version=self.ip_version,
215 start = self.start,
216 end = self.end)
217
218 self.subnet_id = subnet.id
219
Tony Mackaf905fc2013-04-06 01:30:40 -0400220 # add subnet as interface to slice router
221 driver.add_router_interface(self.slice.router_id, subnet.id)
222
Tony Mack3d1f0692013-04-04 23:55:54 -0400223 super(SubNet, self).save(*args, **kwargs)
224
225
226 def delete(self, *args, **kwargs):
227 # delete quantum network
228 driver = OpenStackDriver()
229 driver.delete_subnet(self.subnet_id)
Tony Mackaf905fc2013-04-06 01:30:40 -0400230 driver.delete_router_interface(self.slice.router_id, self.subnet.id)
Tony Mack3d1f0692013-04-04 23:55:54 -0400231 super(SubNet, self).delete(*args, **kwargs)
232
Tony Mack3d1f0692013-04-04 23:55:54 -0400233class Node(PlCoreBase):
234 name = models.CharField(max_length=200, unique=True, help_text="Name of the Node")
235 siteDeploymentNetwork = models.ForeignKey(SiteDeploymentNetwork, help_text="The Site and Deployment Network this Node belongs too.")
236
237 def __unicode__(self): return u'%s' % (self.name)
238
239class Image(PlCoreBase):
240 image_id = models.CharField(max_length=256, unique=True)
241 name = models.CharField(max_length=256, unique=True)
242 disk_format = models.CharField(max_length=256)
243 container_format = models.CharField(max_length=256)
244
245 def __unicode__(self): return u'%s' % (self.name)
246
247
248class Flavor(PlCoreBase):
249 flavor_id = models.IntegerField(unique=True)
250 name = models.CharField(max_length=256, unique=True)
251 memory_mb = models.IntegerField()
252 disk_gb = models.IntegerField()
253 vcpus = models.IntegerField()
254
255 def __unicode__(self): return u'%s' % (self.name)
256
257class Key(PlCoreBase):
258 name = models.CharField(max_length=256, unique=True)
259 key = models.CharField(max_length=512)
260 type = models.CharField(max_length=256)
261 blacklisted = models.BooleanField()
262 user = models.ForeignKey(User)
263
264 def __unicode__(self): return u'%s' % (self.name)
265
266 def save(self, *args, **kwds):
267 driver = OpenStackDriver()
268 if not self.id:
269 keypair = driver.create_keypair(name=self.name, key=self.key)
270 super(Key, self).save(*args, **kwds)
271
272 def delete(self, *args, **kwds):
273 driver = OpenStackDriver()
274 driver.delete_keypair(self.name)
275 super(Key, self).delete(*args, **kwds)
276
277
278
Tony Mack7130ac32013-03-22 21:58:00 -0400279class Sliver(PlCoreBase):
Tony Mackebb24872013-04-02 08:20:59 -0400280 instance_id = models.CharField(max_length=200, help_text="Nova instance id")
Tony Mackd9ae9da2013-04-02 08:58:22 -0400281 name = models.CharField(max_length=200, help_text="Sliver name")
Tony Mack3d1f0692013-04-04 23:55:54 -0400282 flavor = models.ForeignKey(Flavor, related_name='sliver_flavor')
283 image = models.ForeignKey(Image, related_name='sliver_image')
284 key = models.ForeignKey(Key, related_name='sliver_key')
Tony Mack27527b92013-04-02 08:59:35 -0400285 slice = models.ForeignKey(Slice, related_name='sliver_slice')
Tony Mack3d1f0692013-04-04 23:55:54 -0400286 siteDeploymentNetwork = models.ForeignKey(SiteDeploymentNetwork, related_name='sliver_deployment')
287 node = models.ForeignKey(Node, related_name='sliver_node')
Tony Mack7130ac32013-03-22 21:58:00 -0400288
289 def __unicode__(self): return u'%s::%s' % (self.slice, self.siteDeploymentNetwork)
290
Tony Mack7823b892013-03-29 20:35:26 -0400291 def save(self, *args, **kwds):
292 driver = OpenStackDriver()
293 instance = driver.spawn_instances(name=self.name,
294 keyname=self.name,
295 hostnames=self.node.name,
Tony Mack3d1f0692013-04-04 23:55:54 -0400296 flavor=self.flavor.name,
297 image=self.image.name)
Tony Mack7823b892013-03-29 20:35:26 -0400298 self.instance_id = instance.id
299 super(Sliver, self).save(*args, **kwds)
300
301 def delete(self, *args, **kwds):
302 driver = OpenStackDriver()
303 driver.destroy_instance(name=self.name, id=self.instance_id)
304 super(Sliver, self).delete(*args, **kwds)
Tony Mack7823b892013-03-29 20:35:26 -0400305