blob: fd679df390d502e66da82d3f6223df2b330cddb1 [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 Mack3d1f0692013-04-04 23:55:54 -040015class Role(PlCoreBase):
16
17 ROLE_CHOICES = (('admin', 'Admin'), ('pi', 'Principle Investigator'), ('user','User'))
18 role_type = models.CharField(max_length=80, unique=True, choices=ROLE_CHOICES)
19
20 def __unicode__(self): return u'%s' % (self.role_type)
21
22 def save(self):
23 if not self.id:
24 self.created = datetime.date.today()
25 self.updated = datetime.datetime.today()
26 super(Role, self).save()
27
Tony Mack7130ac32013-03-22 21:58:00 -040028class Site(PlCoreBase):
Tony Mackd7cb7712013-03-26 13:21:36 -040029 tenant_id = models.CharField(max_length=200, help_text="Keystone tenant id")
Tony Mack67f60ce2013-04-02 10:46:07 -040030 name = models.CharField(max_length=200, help_text="Name for this Site")
Tony Mackc689d502013-04-02 11:32:04 -040031 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 -040032 enabled = models.BooleanField(default=True, help_text="Status for this Site")
33 longitude = models.FloatField(null=True, blank=True)
34 latitude = models.FloatField(null=True, blank=True)
35 login_base = models.CharField(max_length=50, help_text="Prefix for Slices associated with this Site")
36 is_public = models.BooleanField(default=True, help_text="Indicates the visibility of this site to other members")
37 abbreviated_name = models.CharField(max_length=80)
38
39 def __unicode__(self): return u'%s' % (self.name)
40
Tony Mackd7cb7712013-03-26 13:21:36 -040041 def save(self, *args, **kwargs):
Tony Mack7823b892013-03-29 20:35:26 -040042 driver = OpenStackDriver()
Tony Mackd7cb7712013-03-26 13:21:36 -040043 if not self.id:
Tony Mack39444e42013-04-02 10:22:24 -040044 tenant = driver.create_tenant(tenant_name=self.login_base,
Tony Mack7823b892013-03-29 20:35:26 -040045 description=self.name,
46 enabled=self.enabled)
Tony Mackd8c390f2013-04-02 10:36:19 -040047 self.tenant_id = tenant.id
Tony Mackd7cb7712013-03-26 13:21:36 -040048 else:
49 # update record
Tony Mack7823b892013-03-29 20:35:26 -040050 self.driver.update_tenant(self.tenant_id, name=self.login_base,
51 description=self.name, enabled=self.enabled)
Tony Mackd7cb7712013-03-26 13:21:36 -040052 super(Site, self).save(*args, **kwargs)
53
Tony Macke1c8c382013-03-26 13:37:59 -040054 def delete(self, *args, **kwds):
55 # delete keystone tenant
Tony Mack7823b892013-03-29 20:35:26 -040056 driver = OpenStackDriver()
57 driver.delete_tenant(self.tenant_id)
Tony Macke1c8c382013-03-26 13:37:59 -040058 super(Site, self).delete(*args, **kwargs)
59
Tony Mack3d1f0692013-04-04 23:55:54 -040060class User(PlCoreBase):
61 user_id = models.CharField(max_length=256, unique=True)
62 firstname = models.CharField(help_text="person's given name", max_length=200)
63 lastname = models.CharField(help_text="person's surname", max_length=200)
64 email = models.EmailField(help_text="e-mail address")
65 phone = models.CharField(help_text="phone number contact", max_length=100)
66 user_url = models.URLField()
67 site = models.ForeignKey(Site, related_name='site_user', verbose_name="Site this user will be homed too")
68
69 def __unicode__(self): return u'%s' % (self.email)
70
71 def save(self):
72 if not self.id:
73 self.created = datetime.date.today()
74 self.updated = datetime.datetime.today()
75 super(User, self).save()
76
77class SitePrivilege(PlCoreBase):
78
79 user = models.ForeignKey('User')
80 site = models.ForeignKey('Site')
81 role = models.ForeignKey('Role')
82
83 def __unicode__(self): return u'%s %s %s' % (self.site, self.user, self.role)
84
85 def save(self):
86 if not self.id:
87 self.created = datetime.date.today()
88 self.updated = datetime.datetime.today()
89 super(SitePrivilege, self).save()
90
Tony Mack7130ac32013-03-22 21:58:00 -040091class Slice(PlCoreBase):
Tony Mackd7cb7712013-03-26 13:21:36 -040092 tenant_id = models.CharField(max_length=200, help_text="Keystone tenant id")
Tony Mack7130ac32013-03-22 21:58:00 -040093 name = models.CharField(help_text="The Name of the Slice", max_length=80)
Tony Macka3a40cf2013-04-02 11:21:21 -040094 enabled = models.BooleanField(default=True, help_text="Status for this Slice")
Tony Mack7130ac32013-03-22 21:58:00 -040095 SLICE_CHOICES = (('plc', 'PLC'), ('delegated', 'Delegated'), ('controller','Controller'), ('none','None'))
96 instantiation = models.CharField(help_text="The instantiation type of the slice", max_length=80, choices=SLICE_CHOICES)
97 omf_friendly = models.BooleanField()
98 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 -040099 slice_url = models.URLField(blank=True, max_length=512)
Tony Mack3d1f0692013-04-04 23:55:54 -0400100 site = models.ForeignKey(Site, related_name='site_slice', help_text="The Site this Node belongs too")
Tony Mack7130ac32013-03-22 21:58:00 -0400101
102 def __unicode__(self): return u'%s' % (self.name)
103
Tony Mackd7cb7712013-03-26 13:21:36 -0400104 def save(self, *args, **kwds):
Tony Macke1c8c382013-03-26 13:37:59 -0400105 # sync keystone tenant
Tony Mack7823b892013-03-29 20:35:26 -0400106 driver = OpenStackDriver()
107
Tony Mackd7cb7712013-03-26 13:21:36 -0400108 if not self.id:
Tony Mack39444e42013-04-02 10:22:24 -0400109 tenant = driver.create_tenant(tenant_name=self.name,
Tony Mack7823b892013-03-29 20:35:26 -0400110 description=self.description,
111 enabled=self.enabled)
Tony Mackd8c390f2013-04-02 10:36:19 -0400112 self.tenant_id = tenant.id
Tony Mack3d1f0692013-04-04 23:55:54 -0400113
114 # create router
115 driver.create_router(name=self.name)
116
117 # create a network
118 driver.create_network(name=self.name)
119
Tony Mackd7cb7712013-03-26 13:21:36 -0400120 else:
121 # update record
Tony Mack7823b892013-03-29 20:35:26 -0400122 self.driver.update_tenant(self.tenant_id, name=self.name,
123 description=self.description, enabled=self.enabled)
Tony Mack6299cb72013-04-02 11:23:26 -0400124 super(Slice, self).save(*args, **kwds)
Tony Mackd7cb7712013-03-26 13:21:36 -0400125
Tony Macke1c8c382013-03-26 13:37:59 -0400126 def delete(self, *args, **kwds):
127 # delete keystone tenant
Tony Mack7823b892013-03-29 20:35:26 -0400128 driver = OpenStackDriver()
129 driver.delete_tenant(self.tenant_id)
Tony Mack6299cb72013-04-02 11:23:26 -0400130 super(Slice, self).delete(*args, **kwds)
Tony Macke1c8c382013-03-26 13:37:59 -0400131
Tony Mack3d1f0692013-04-04 23:55:54 -0400132class SliceMembership(PlCoreBase):
133 user = models.ForeignKey('User')
134 slice = models.ForeignKey('Slice')
135 role = models.ForeignKey('Role')
136
137 def __unicode__(self): return u'%s %s %s' % (self.slice, self.user, self.role)
138
139 def save(self):
140 if not self.id:
141 self.created = datetime.date.today()
142 self.updated = datetime.datetime.today()
143 super(SliceMembership, self).save()
144
145class SubNet(PlCoreBase):
146 subnet_id = models.CharField(max_length=256, unique=True)
147 cidr = models.CharField(max_length=20)
148 ip_version = models.IntegerField()
149 start = models.IPAddressField()
150 end = models.IPAddressField()
151 slice = models.ForeignKey(Slice, related_name='slice_subnet')
152
153 def __unicode__(self): return u'%s' % (self.name)
154
155 def save(self, *args, **kwargs):
156 driver = OpenStackDriver()
157 if not self.id:
158 subnet = driver.create_subnet(network_name=self.slice.name,
159 cidr_ip = self.cidr,
160 ip_version=self.ip_version,
161 start = self.start,
162 end = self.end)
163
164 self.subnet_id = subnet.id
165
166 super(SubNet, self).save(*args, **kwargs)
167
168
169 def delete(self, *args, **kwargs):
170 # delete quantum network
171 driver = OpenStackDriver()
172 driver.delete_subnet(self.subnet_id)
173 super(SubNet, self).delete(*args, **kwargs)
174
175
176
Tony Mack7130ac32013-03-22 21:58:00 -0400177class DeploymentNetwork(PlCoreBase):
178 name = models.CharField(max_length=200, unique=True, help_text="Name of the Deployment Network")
179
180 def __unicode__(self): return u'%s' % (self.name)
181
182class SiteDeploymentNetwork(PlCoreBase):
183 class Meta:
184 unique_together = ['site', 'deploymentNetwork']
185
186 site = models.ForeignKey(Site, related_name='deploymentNetworks')
187 deploymentNetwork = models.ForeignKey(DeploymentNetwork, related_name='sites')
188 name = models.CharField(default="Blah", max_length=100)
189
190
191 def __unicode__(self): return u'%s::%s' % (self.site, self.deploymentNetwork)
192
193
Tony Mack3d1f0692013-04-04 23:55:54 -0400194class Node(PlCoreBase):
195 name = models.CharField(max_length=200, unique=True, help_text="Name of the Node")
196 siteDeploymentNetwork = models.ForeignKey(SiteDeploymentNetwork, help_text="The Site and Deployment Network this Node belongs too.")
197
198 def __unicode__(self): return u'%s' % (self.name)
199
200class Image(PlCoreBase):
201 image_id = models.CharField(max_length=256, unique=True)
202 name = models.CharField(max_length=256, unique=True)
203 disk_format = models.CharField(max_length=256)
204 container_format = models.CharField(max_length=256)
205
206 def __unicode__(self): return u'%s' % (self.name)
207
208
209class Flavor(PlCoreBase):
210 flavor_id = models.IntegerField(unique=True)
211 name = models.CharField(max_length=256, unique=True)
212 memory_mb = models.IntegerField()
213 disk_gb = models.IntegerField()
214 vcpus = models.IntegerField()
215
216 def __unicode__(self): return u'%s' % (self.name)
217
218class Key(PlCoreBase):
219 name = models.CharField(max_length=256, unique=True)
220 key = models.CharField(max_length=512)
221 type = models.CharField(max_length=256)
222 blacklisted = models.BooleanField()
223 user = models.ForeignKey(User)
224
225 def __unicode__(self): return u'%s' % (self.name)
226
227 def save(self, *args, **kwds):
228 driver = OpenStackDriver()
229 if not self.id:
230 keypair = driver.create_keypair(name=self.name, key=self.key)
231 super(Key, self).save(*args, **kwds)
232
233 def delete(self, *args, **kwds):
234 driver = OpenStackDriver()
235 driver.delete_keypair(self.name)
236 super(Key, self).delete(*args, **kwds)
237
238
239
Tony Mack7130ac32013-03-22 21:58:00 -0400240class Sliver(PlCoreBase):
Tony Mackebb24872013-04-02 08:20:59 -0400241 instance_id = models.CharField(max_length=200, help_text="Nova instance id")
Tony Mackd9ae9da2013-04-02 08:58:22 -0400242 name = models.CharField(max_length=200, help_text="Sliver name")
Tony Mack3d1f0692013-04-04 23:55:54 -0400243 flavor = models.ForeignKey(Flavor, related_name='sliver_flavor')
244 image = models.ForeignKey(Image, related_name='sliver_image')
245 key = models.ForeignKey(Key, related_name='sliver_key')
Tony Mack27527b92013-04-02 08:59:35 -0400246 slice = models.ForeignKey(Slice, related_name='sliver_slice')
Tony Mack3d1f0692013-04-04 23:55:54 -0400247 siteDeploymentNetwork = models.ForeignKey(SiteDeploymentNetwork, related_name='sliver_deployment')
248 node = models.ForeignKey(Node, related_name='sliver_node')
Tony Mack7130ac32013-03-22 21:58:00 -0400249
250 def __unicode__(self): return u'%s::%s' % (self.slice, self.siteDeploymentNetwork)
251
Tony Mack7823b892013-03-29 20:35:26 -0400252 def save(self, *args, **kwds):
253 driver = OpenStackDriver()
254 instance = driver.spawn_instances(name=self.name,
255 keyname=self.name,
256 hostnames=self.node.name,
Tony Mack3d1f0692013-04-04 23:55:54 -0400257 flavor=self.flavor.name,
258 image=self.image.name)
Tony Mack7823b892013-03-29 20:35:26 -0400259 self.instance_id = instance.id
260 super(Sliver, self).save(*args, **kwds)
261
262 def delete(self, *args, **kwds):
263 driver = OpenStackDriver()
264 driver.destroy_instance(name=self.name, id=self.instance_id)
265 super(Sliver, self).delete(*args, **kwds)
Tony Mack7823b892013-03-29 20:35:26 -0400266