blob: dd5bc86fe5791728eb4334ad5fefb3fec13c6c77 [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)
Tony Mack22ea05a2013-04-09 01:13:04 -040043 login_base = models.CharField(max_length=50, unique=True, help_text="Prefix for Slices associated with this Site")
Tony Mack7130ac32013-03-22 21:58:00 -040044 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 Macke1c8c382013-03-26 13:37:59 -040049
Tony Mack3d1f0692013-04-04 23:55:54 -040050class User(PlCoreBase):
51 user_id = models.CharField(max_length=256, unique=True)
52 firstname = models.CharField(help_text="person's given name", max_length=200)
53 lastname = models.CharField(help_text="person's surname", max_length=200)
54 email = models.EmailField(help_text="e-mail address")
Tony Mack1fbdeca2013-04-06 00:10:36 -040055 phone = models.CharField(null=True, blank=True, help_text="phone number contact", max_length=100)
56 user_url = models.URLField(null=True, blank=True)
57 is_admin = models.BooleanField(default=False)
Tony Mack22ea05a2013-04-09 01:13:04 -040058 site = models.ForeignKey(Site, related_name='users', verbose_name="Site this user will be homed too")
Tony Mack3d1f0692013-04-04 23:55:54 -040059
60 def __unicode__(self): return u'%s' % (self.email)
61
Tony Mack1fbdeca2013-04-06 00:10:36 -040062 def save(self, *args, **kwds):
63 driver = OpenStackDriver()
64 name = self.email[:self.email.find('@')]
65 fields = {'name': name, 'email': self.email, 'password': self.password,
66 'enabled': self.enabled}
Tony Mack3d1f0692013-04-04 23:55:54 -040067 if not self.id:
Tony Mack1fbdeca2013-04-06 00:10:36 -040068 user = driver.create_user(**fields)
69 else:
70 driver.update_user(self.user_id, **fields)
71 super(User, self).save(*args, **kwds)
72
73 def delete(self, *args, **kwds):
74 driver = OpenStackDriver()
75 driver.delete_user(self.user_id)
76 super(User, self).delete(*args, **kwds)
Tony Mack3d1f0692013-04-04 23:55:54 -040077
78class SitePrivilege(PlCoreBase):
79
80 user = models.ForeignKey('User')
Tony Mack22ea05a2013-04-09 01:13:04 -040081 site = models.ForeignKey('Site', related_name='site_privileges')
Tony Mack3d1f0692013-04-04 23:55:54 -040082 role = models.ForeignKey('Role')
83
84 def __unicode__(self): return u'%s %s %s' % (self.site, self.user, self.role)
85
Tony Mack1fbdeca2013-04-06 00:10:36 -040086 def save(self, *args, **kwds):
87 driver = OpenStackDriver()
88 driver.add_user_role(user_id=user.user_id,
89 tenant_id=site.tenant_id,
90 role_name=role.name)
91 super(SitePrivilege, self).save(*args, **kwds)
92
93 def delete(self, *args, **kwds):
94 driver = OpenStackDriver()
95 driver.delete_user_role(user_id=user.user_id,
96 tenant_id=site.tenant_id,
97 role_name=role.name)
98 super(SitePrivilege, self).delete(*args, **kwds)
99
100
101class DeploymentNetwork(PlCoreBase):
102 name = models.CharField(max_length=200, unique=True, help_text="Name of the Deployment Network")
103
104 def __unicode__(self): return u'%s' % (self.name)
105
106class SiteDeploymentNetwork(PlCoreBase):
107 class Meta:
108 unique_together = ['site', 'deploymentNetwork']
109
Tony Mack22ea05a2013-04-09 01:13:04 -0400110 site = models.ForeignKey(Site, related_name='deployment_networks')
Tony Mackca9a73b2013-04-09 00:45:59 -0400111 deploymentNetwork = models.ForeignKey(DeploymentNetwork)
Tony Mack1fbdeca2013-04-06 00:10:36 -0400112 name = models.CharField(default="Blah", max_length=100)
113
114
115 def __unicode__(self): return u'%s::%s' % (self.site, self.deploymentNetwork)
116
Tony Mack3d1f0692013-04-04 23:55:54 -0400117
Tony Mack7130ac32013-03-22 21:58:00 -0400118class Slice(PlCoreBase):
Tony Mackd7cb7712013-03-26 13:21:36 -0400119 tenant_id = models.CharField(max_length=200, help_text="Keystone tenant id")
Tony Mack7130ac32013-03-22 21:58:00 -0400120 name = models.CharField(help_text="The Name of the Slice", max_length=80)
Tony Macka3a40cf2013-04-02 11:21:21 -0400121 enabled = models.BooleanField(default=True, help_text="Status for this Slice")
Tony Mack7130ac32013-03-22 21:58:00 -0400122 SLICE_CHOICES = (('plc', 'PLC'), ('delegated', 'Delegated'), ('controller','Controller'), ('none','None'))
123 instantiation = models.CharField(help_text="The instantiation type of the slice", max_length=80, choices=SLICE_CHOICES)
124 omf_friendly = models.BooleanField()
125 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 -0400126 slice_url = models.URLField(blank=True, max_length=512)
Tony Mack22ea05a2013-04-09 01:13:04 -0400127 site = models.ForeignKey(Site, related_name='slices', help_text="The Site this Node belongs too")
Tony Mackaf905fc2013-04-06 01:30:40 -0400128 network_id = models.CharField(max_length=256, help_text="Quantum network")
129 router_id = models.CharField(max_length=256, help_text="Quantum router id")
Tony Mack7130ac32013-03-22 21:58:00 -0400130
131 def __unicode__(self): return u'%s' % (self.name)
132
Tony Mackd7cb7712013-03-26 13:21:36 -0400133 def save(self, *args, **kwds):
Tony Macke1c8c382013-03-26 13:37:59 -0400134 # sync keystone tenant
Tony Mack7823b892013-03-29 20:35:26 -0400135 driver = OpenStackDriver()
136
Tony Mackd7cb7712013-03-26 13:21:36 -0400137 if not self.id:
Tony Mack39444e42013-04-02 10:22:24 -0400138 tenant = driver.create_tenant(tenant_name=self.name,
Tony Mack7823b892013-03-29 20:35:26 -0400139 description=self.description,
140 enabled=self.enabled)
Tony Mackd8c390f2013-04-02 10:36:19 -0400141 self.tenant_id = tenant.id
Tony Mack3d1f0692013-04-04 23:55:54 -0400142
Tony Mack3d1f0692013-04-04 23:55:54 -0400143 # create a network
Tony Mackaf905fc2013-04-06 01:30:40 -0400144 network = driver.create_network(name=self.name)
145 self.network_id = network['id']
146 # create router
147 router = driver.create_router(name=self.name)
148 self.router_id = router['id']
Tony Mack3d1f0692013-04-04 23:55:54 -0400149
Tony Mackd7cb7712013-03-26 13:21:36 -0400150 else:
151 # update record
Tony Mack7823b892013-03-29 20:35:26 -0400152 self.driver.update_tenant(self.tenant_id, name=self.name,
153 description=self.description, enabled=self.enabled)
Tony Mack6299cb72013-04-02 11:23:26 -0400154 super(Slice, self).save(*args, **kwds)
Tony Mackd7cb7712013-03-26 13:21:36 -0400155
Tony Macke1c8c382013-03-26 13:37:59 -0400156 def delete(self, *args, **kwds):
157 # delete keystone tenant
Tony Mack7823b892013-03-29 20:35:26 -0400158 driver = OpenStackDriver()
159 driver.delete_tenant(self.tenant_id)
Tony Mack6299cb72013-04-02 11:23:26 -0400160 super(Slice, self).delete(*args, **kwds)
Tony Macke1c8c382013-03-26 13:37:59 -0400161
Tony Mack3d1f0692013-04-04 23:55:54 -0400162class SliceMembership(PlCoreBase):
163 user = models.ForeignKey('User')
164 slice = models.ForeignKey('Slice')
165 role = models.ForeignKey('Role')
166
167 def __unicode__(self): return u'%s %s %s' % (self.slice, self.user, self.role)
168
Tony Mack1fbdeca2013-04-06 00:10:36 -0400169 def save(self, *args, **kwds):
170 driver = OpenStackDriver()
171 driver.add_user_role(user_id=user.user_id,
172 tenant_id=slice.tenant_id,
173 role_name=role.name)
174 super(SliceMembership, self).save(*args, **kwds)
175
176 def delete(self, *args, **kwds):
177 driver = OpenStackDriver()
178 driver.delete_user_role(user_id=user.user_id,
179 tenant_id=slice.tenant_id,
180 role_name=role.name)
181 super(SliceMembership, self).delete(*args, **kwds)
Tony Mack3d1f0692013-04-04 23:55:54 -0400182
183class SubNet(PlCoreBase):
184 subnet_id = models.CharField(max_length=256, unique=True)
185 cidr = models.CharField(max_length=20)
186 ip_version = models.IntegerField()
187 start = models.IPAddressField()
188 end = models.IPAddressField()
Tony Mackca9a73b2013-04-09 00:45:59 -0400189 slice = models.ForeignKey(Slice )
Tony Mack3d1f0692013-04-04 23:55:54 -0400190
191 def __unicode__(self): return u'%s' % (self.name)
192
193 def save(self, *args, **kwargs):
194 driver = OpenStackDriver()
195 if not self.id:
196 subnet = driver.create_subnet(network_name=self.slice.name,
197 cidr_ip = self.cidr,
198 ip_version=self.ip_version,
199 start = self.start,
200 end = self.end)
201
202 self.subnet_id = subnet.id
203
Tony Mackaf905fc2013-04-06 01:30:40 -0400204 # add subnet as interface to slice router
205 driver.add_router_interface(self.slice.router_id, subnet.id)
206
Tony Mack3d1f0692013-04-04 23:55:54 -0400207 super(SubNet, self).save(*args, **kwargs)
208
209
210 def delete(self, *args, **kwargs):
211 # delete quantum network
212 driver = OpenStackDriver()
213 driver.delete_subnet(self.subnet_id)
Tony Mackaf905fc2013-04-06 01:30:40 -0400214 driver.delete_router_interface(self.slice.router_id, self.subnet.id)
Tony Mack3d1f0692013-04-04 23:55:54 -0400215 super(SubNet, self).delete(*args, **kwargs)
216
Tony Mack3d1f0692013-04-04 23:55:54 -0400217class Node(PlCoreBase):
218 name = models.CharField(max_length=200, unique=True, help_text="Name of the Node")
219 siteDeploymentNetwork = models.ForeignKey(SiteDeploymentNetwork, help_text="The Site and Deployment Network this Node belongs too.")
220
221 def __unicode__(self): return u'%s' % (self.name)
222
223class Image(PlCoreBase):
224 image_id = models.CharField(max_length=256, unique=True)
225 name = models.CharField(max_length=256, unique=True)
226 disk_format = models.CharField(max_length=256)
227 container_format = models.CharField(max_length=256)
228
229 def __unicode__(self): return u'%s' % (self.name)
230
231
232class Flavor(PlCoreBase):
233 flavor_id = models.IntegerField(unique=True)
234 name = models.CharField(max_length=256, unique=True)
235 memory_mb = models.IntegerField()
236 disk_gb = models.IntegerField()
237 vcpus = models.IntegerField()
238
239 def __unicode__(self): return u'%s' % (self.name)
240
241class Key(PlCoreBase):
242 name = models.CharField(max_length=256, unique=True)
243 key = models.CharField(max_length=512)
244 type = models.CharField(max_length=256)
245 blacklisted = models.BooleanField()
246 user = models.ForeignKey(User)
247
248 def __unicode__(self): return u'%s' % (self.name)
249
250 def save(self, *args, **kwds):
251 driver = OpenStackDriver()
252 if not self.id:
253 keypair = driver.create_keypair(name=self.name, key=self.key)
254 super(Key, self).save(*args, **kwds)
255
256 def delete(self, *args, **kwds):
257 driver = OpenStackDriver()
258 driver.delete_keypair(self.name)
259 super(Key, self).delete(*args, **kwds)
260
261
262
Tony Mack7130ac32013-03-22 21:58:00 -0400263class Sliver(PlCoreBase):
Tony Mackebb24872013-04-02 08:20:59 -0400264 instance_id = models.CharField(max_length=200, help_text="Nova instance id")
Tony Mackd9ae9da2013-04-02 08:58:22 -0400265 name = models.CharField(max_length=200, help_text="Sliver name")
Tony Mackca9a73b2013-04-09 00:45:59 -0400266 flavor = models.ForeignKey(Flavor)
267 image = models.ForeignKey(Image)
268 key = models.ForeignKey(Key)
269 slice = models.ForeignKey(Slice)
270 siteDeploymentNetwork = models.ForeignKey(SiteDeploymentNetwork)
271 node = models.ForeignKey(Node)
Tony Mack7130ac32013-03-22 21:58:00 -0400272
273 def __unicode__(self): return u'%s::%s' % (self.slice, self.siteDeploymentNetwork)
274
Tony Mack7823b892013-03-29 20:35:26 -0400275 def save(self, *args, **kwds):
276 driver = OpenStackDriver()
277 instance = driver.spawn_instances(name=self.name,
278 keyname=self.name,
279 hostnames=self.node.name,
Tony Mack3d1f0692013-04-04 23:55:54 -0400280 flavor=self.flavor.name,
281 image=self.image.name)
Tony Mack7823b892013-03-29 20:35:26 -0400282 self.instance_id = instance.id
283 super(Sliver, self).save(*args, **kwds)
284
285 def delete(self, *args, **kwds):
286 driver = OpenStackDriver()
287 driver.destroy_instance(name=self.name, id=self.instance_id)
288 super(Sliver, self).delete(*args, **kwds)
Tony Mack7823b892013-03-29 20:35:26 -0400289