blob: 7561b5ebac48f330bce755ae62ffa8c31e991b2e [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 Mackf423f552013-04-09 11:06:48 -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 Mack7c03fad2013-04-09 11:45:28 -040058 enabled = models.BooleanField(default=True, help_text="Status for this User")
Tony Mack22ea05a2013-04-09 01:13:04 -040059 site = models.ForeignKey(Site, related_name='users', verbose_name="Site this user will be homed too")
Tony Mack3d1f0692013-04-04 23:55:54 -040060
61 def __unicode__(self): return u'%s' % (self.email)
62
Tony Mack3d1f0692013-04-04 23:55:54 -040063class SitePrivilege(PlCoreBase):
64
Tony Mack5cc16ba2013-04-09 10:30:45 -040065 user = models.ForeignKey('User', related_name='site_privileges')
Tony Mack22ea05a2013-04-09 01:13:04 -040066 site = models.ForeignKey('Site', related_name='site_privileges')
Tony Mack3d1f0692013-04-04 23:55:54 -040067 role = models.ForeignKey('Role')
68
69 def __unicode__(self): return u'%s %s %s' % (self.site, self.user, self.role)
70
Tony Mack1fbdeca2013-04-06 00:10:36 -040071 def save(self, *args, **kwds):
72 driver = OpenStackDriver()
73 driver.add_user_role(user_id=user.user_id,
74 tenant_id=site.tenant_id,
75 role_name=role.name)
76 super(SitePrivilege, self).save(*args, **kwds)
77
78 def delete(self, *args, **kwds):
79 driver = OpenStackDriver()
80 driver.delete_user_role(user_id=user.user_id,
81 tenant_id=site.tenant_id,
82 role_name=role.name)
83 super(SitePrivilege, self).delete(*args, **kwds)
84
85
86class DeploymentNetwork(PlCoreBase):
87 name = models.CharField(max_length=200, unique=True, help_text="Name of the Deployment Network")
88
89 def __unicode__(self): return u'%s' % (self.name)
90
91class SiteDeploymentNetwork(PlCoreBase):
92 class Meta:
93 unique_together = ['site', 'deploymentNetwork']
94
Tony Mack22ea05a2013-04-09 01:13:04 -040095 site = models.ForeignKey(Site, related_name='deployment_networks')
Tony Mackfcb9ee52013-04-09 07:56:26 -040096 deploymentNetwork = models.ForeignKey(DeploymentNetwork, related_name='sites')
Tony Mack1fbdeca2013-04-06 00:10:36 -040097 name = models.CharField(default="Blah", max_length=100)
98
99
100 def __unicode__(self): return u'%s::%s' % (self.site, self.deploymentNetwork)
101
Tony Mack3d1f0692013-04-04 23:55:54 -0400102
Tony Mack7130ac32013-03-22 21:58:00 -0400103class Slice(PlCoreBase):
Tony Mackd7cb7712013-03-26 13:21:36 -0400104 tenant_id = models.CharField(max_length=200, help_text="Keystone tenant id")
Tony Mack7130ac32013-03-22 21:58:00 -0400105 name = models.CharField(help_text="The Name of the Slice", max_length=80)
Tony Macka3a40cf2013-04-02 11:21:21 -0400106 enabled = models.BooleanField(default=True, help_text="Status for this Slice")
Tony Mack7130ac32013-03-22 21:58:00 -0400107 SLICE_CHOICES = (('plc', 'PLC'), ('delegated', 'Delegated'), ('controller','Controller'), ('none','None'))
108 instantiation = models.CharField(help_text="The instantiation type of the slice", max_length=80, choices=SLICE_CHOICES)
109 omf_friendly = models.BooleanField()
110 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 -0400111 slice_url = models.URLField(blank=True, max_length=512)
Tony Mack22ea05a2013-04-09 01:13:04 -0400112 site = models.ForeignKey(Site, related_name='slices', help_text="The Site this Node belongs too")
Tony Mackaf905fc2013-04-06 01:30:40 -0400113 network_id = models.CharField(max_length=256, help_text="Quantum network")
114 router_id = models.CharField(max_length=256, help_text="Quantum router id")
Tony Mack7130ac32013-03-22 21:58:00 -0400115
116 def __unicode__(self): return u'%s' % (self.name)
117
Tony Mackd7cb7712013-03-26 13:21:36 -0400118 def save(self, *args, **kwds):
Tony Macke1c8c382013-03-26 13:37:59 -0400119 # sync keystone tenant
Tony Mack7823b892013-03-29 20:35:26 -0400120 driver = OpenStackDriver()
121
Tony Mackd7cb7712013-03-26 13:21:36 -0400122 if not self.id:
Tony Mack39444e42013-04-02 10:22:24 -0400123 tenant = driver.create_tenant(tenant_name=self.name,
Tony Mack7823b892013-03-29 20:35:26 -0400124 description=self.description,
125 enabled=self.enabled)
Tony Mackd8c390f2013-04-02 10:36:19 -0400126 self.tenant_id = tenant.id
Tony Mack3d1f0692013-04-04 23:55:54 -0400127
Tony Mack3d1f0692013-04-04 23:55:54 -0400128 # create a network
Tony Mackaf905fc2013-04-06 01:30:40 -0400129 network = driver.create_network(name=self.name)
130 self.network_id = network['id']
131 # create router
132 router = driver.create_router(name=self.name)
133 self.router_id = router['id']
Tony Mack3d1f0692013-04-04 23:55:54 -0400134
Tony Mackd7cb7712013-03-26 13:21:36 -0400135 else:
136 # update record
Tony Mack7823b892013-03-29 20:35:26 -0400137 self.driver.update_tenant(self.tenant_id, name=self.name,
138 description=self.description, enabled=self.enabled)
Tony Mack6299cb72013-04-02 11:23:26 -0400139 super(Slice, self).save(*args, **kwds)
Tony Mackd7cb7712013-03-26 13:21:36 -0400140
Tony Macke1c8c382013-03-26 13:37:59 -0400141 def delete(self, *args, **kwds):
142 # delete keystone tenant
Tony Mack7823b892013-03-29 20:35:26 -0400143 driver = OpenStackDriver()
144 driver.delete_tenant(self.tenant_id)
Tony Mack6299cb72013-04-02 11:23:26 -0400145 super(Slice, self).delete(*args, **kwds)
Tony Macke1c8c382013-03-26 13:37:59 -0400146
Tony Mack3d1f0692013-04-04 23:55:54 -0400147class SliceMembership(PlCoreBase):
Tony Mack5cc16ba2013-04-09 10:30:45 -0400148 user = models.ForeignKey('User', related_name='slice_memberships')
149 slice = models.ForeignKey('Slice', related_name='slice_memberships')
Tony Mack3d1f0692013-04-04 23:55:54 -0400150 role = models.ForeignKey('Role')
151
152 def __unicode__(self): return u'%s %s %s' % (self.slice, self.user, self.role)
153
Tony Mack1fbdeca2013-04-06 00:10:36 -0400154 def save(self, *args, **kwds):
155 driver = OpenStackDriver()
156 driver.add_user_role(user_id=user.user_id,
157 tenant_id=slice.tenant_id,
158 role_name=role.name)
159 super(SliceMembership, self).save(*args, **kwds)
160
161 def delete(self, *args, **kwds):
162 driver = OpenStackDriver()
163 driver.delete_user_role(user_id=user.user_id,
164 tenant_id=slice.tenant_id,
165 role_name=role.name)
166 super(SliceMembership, self).delete(*args, **kwds)
Tony Mack3d1f0692013-04-04 23:55:54 -0400167
168class SubNet(PlCoreBase):
169 subnet_id = models.CharField(max_length=256, unique=True)
170 cidr = models.CharField(max_length=20)
171 ip_version = models.IntegerField()
172 start = models.IPAddressField()
173 end = models.IPAddressField()
Tony Mackca9a73b2013-04-09 00:45:59 -0400174 slice = models.ForeignKey(Slice )
Tony Mack3d1f0692013-04-04 23:55:54 -0400175
176 def __unicode__(self): return u'%s' % (self.name)
177
178 def save(self, *args, **kwargs):
179 driver = OpenStackDriver()
180 if not self.id:
181 subnet = driver.create_subnet(network_name=self.slice.name,
182 cidr_ip = self.cidr,
183 ip_version=self.ip_version,
184 start = self.start,
185 end = self.end)
186
187 self.subnet_id = subnet.id
188
Tony Mackaf905fc2013-04-06 01:30:40 -0400189 # add subnet as interface to slice router
190 driver.add_router_interface(self.slice.router_id, subnet.id)
191
Tony Mack3d1f0692013-04-04 23:55:54 -0400192 super(SubNet, self).save(*args, **kwargs)
193
194
195 def delete(self, *args, **kwargs):
196 # delete quantum network
197 driver = OpenStackDriver()
198 driver.delete_subnet(self.subnet_id)
Tony Mackaf905fc2013-04-06 01:30:40 -0400199 driver.delete_router_interface(self.slice.router_id, self.subnet.id)
Tony Mack3d1f0692013-04-04 23:55:54 -0400200 super(SubNet, self).delete(*args, **kwargs)
201
Tony Mack3d1f0692013-04-04 23:55:54 -0400202class Node(PlCoreBase):
203 name = models.CharField(max_length=200, unique=True, help_text="Name of the Node")
204 siteDeploymentNetwork = models.ForeignKey(SiteDeploymentNetwork, help_text="The Site and Deployment Network this Node belongs too.")
205
206 def __unicode__(self): return u'%s' % (self.name)
207
208class Image(PlCoreBase):
209 image_id = models.CharField(max_length=256, unique=True)
210 name = models.CharField(max_length=256, unique=True)
211 disk_format = models.CharField(max_length=256)
212 container_format = models.CharField(max_length=256)
213
214 def __unicode__(self): return u'%s' % (self.name)
215
216
217class Flavor(PlCoreBase):
218 flavor_id = models.IntegerField(unique=True)
219 name = models.CharField(max_length=256, unique=True)
220 memory_mb = models.IntegerField()
221 disk_gb = models.IntegerField()
222 vcpus = models.IntegerField()
223
224 def __unicode__(self): return u'%s' % (self.name)
225
226class Key(PlCoreBase):
227 name = models.CharField(max_length=256, unique=True)
228 key = models.CharField(max_length=512)
229 type = models.CharField(max_length=256)
Tony Macke4cee7a2013-04-09 12:45:32 -0400230 blacklisted = models.BooleanField(default=False)
Tony Mack5cc16ba2013-04-09 10:30:45 -0400231 user = models.ForeignKey(User, related_name='keys')
Tony Mack3d1f0692013-04-04 23:55:54 -0400232
233 def __unicode__(self): return u'%s' % (self.name)
234
235 def save(self, *args, **kwds):
236 driver = OpenStackDriver()
237 if not self.id:
238 keypair = driver.create_keypair(name=self.name, key=self.key)
239 super(Key, self).save(*args, **kwds)
240
241 def delete(self, *args, **kwds):
242 driver = OpenStackDriver()
243 driver.delete_keypair(self.name)
244 super(Key, self).delete(*args, **kwds)
245
246
Tony Mack7130ac32013-03-22 21:58:00 -0400247class Sliver(PlCoreBase):
Tony Mackebb24872013-04-02 08:20:59 -0400248 instance_id = models.CharField(max_length=200, help_text="Nova instance id")
Tony Mackd9ae9da2013-04-02 08:58:22 -0400249 name = models.CharField(max_length=200, help_text="Sliver name")
Tony Mackca9a73b2013-04-09 00:45:59 -0400250 flavor = models.ForeignKey(Flavor)
251 image = models.ForeignKey(Image)
252 key = models.ForeignKey(Key)
253 slice = models.ForeignKey(Slice)
254 siteDeploymentNetwork = models.ForeignKey(SiteDeploymentNetwork)
255 node = models.ForeignKey(Node)
Tony Mack7130ac32013-03-22 21:58:00 -0400256
257 def __unicode__(self): return u'%s::%s' % (self.slice, self.siteDeploymentNetwork)
258
Tony Mack7823b892013-03-29 20:35:26 -0400259 def save(self, *args, **kwds):
260 driver = OpenStackDriver()
261 instance = driver.spawn_instances(name=self.name,
262 keyname=self.name,
263 hostnames=self.node.name,
Tony Mack3d1f0692013-04-04 23:55:54 -0400264 flavor=self.flavor.name,
265 image=self.image.name)
Tony Mack7823b892013-03-29 20:35:26 -0400266 self.instance_id = instance.id
267 super(Sliver, self).save(*args, **kwds)
268
269 def delete(self, *args, **kwds):
270 driver = OpenStackDriver()
271 driver.destroy_instance(name=self.name, id=self.instance_id)
272 super(Sliver, self).delete(*args, **kwds)
Tony Mack7823b892013-03-29 20:35:26 -0400273