blob: 79edaca53375527e94e1cb78f170085b772f2d2e [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
15class Site(PlCoreBase):
Tony Mackd7cb7712013-03-26 13:21:36 -040016 tenant_id = models.CharField(max_length=200, help_text="Keystone tenant id")
Tony Mack7130ac32013-03-22 21:58:00 -040017 name = models.CharField(max_length=200, unique=True, help_text="Name for this Site")
18 site_url = models.URLField(help_text="Site's Home URL Page")
19 enabled = models.BooleanField(default=True, help_text="Status for this Site")
20 longitude = models.FloatField(null=True, blank=True)
21 latitude = models.FloatField(null=True, blank=True)
22 login_base = models.CharField(max_length=50, help_text="Prefix for Slices associated with this Site")
23 is_public = models.BooleanField(default=True, help_text="Indicates the visibility of this site to other members")
24 abbreviated_name = models.CharField(max_length=80)
25
26 def __unicode__(self): return u'%s' % (self.name)
27
Tony Mackd7cb7712013-03-26 13:21:36 -040028 def save(self, *args, **kwargs):
Tony Mack7823b892013-03-29 20:35:26 -040029 driver = OpenStackDriver()
Tony Mackd7cb7712013-03-26 13:21:36 -040030 if not self.id:
Tony Mack7823b892013-03-29 20:35:26 -040031 tenant = driver.create_tenant(name=self.login_base,
32 description=self.name,
33 enabled=self.enabled)
Tony Mackd7cb7712013-03-26 13:21:36 -040034 self.tenant_id = tenants.id
35 else:
36 # update record
Tony Mack7823b892013-03-29 20:35:26 -040037 self.driver.update_tenant(self.tenant_id, name=self.login_base,
38 description=self.name, enabled=self.enabled)
Tony Mackd7cb7712013-03-26 13:21:36 -040039 super(Site, self).save(*args, **kwargs)
40
Tony Macke1c8c382013-03-26 13:37:59 -040041 def delete(self, *args, **kwds):
42 # delete keystone tenant
Tony Mack7823b892013-03-29 20:35:26 -040043 driver = OpenStackDriver()
44 driver.delete_tenant(self.tenant_id)
Tony Macke1c8c382013-03-26 13:37:59 -040045 super(Site, self).delete(*args, **kwargs)
46
Tony Mack7130ac32013-03-22 21:58:00 -040047class Slice(PlCoreBase):
Tony Mackd7cb7712013-03-26 13:21:36 -040048 tenant_id = models.CharField(max_length=200, help_text="Keystone tenant id")
Tony Mack7130ac32013-03-22 21:58:00 -040049 name = models.CharField(help_text="The Name of the Slice", max_length=80)
50 SLICE_CHOICES = (('plc', 'PLC'), ('delegated', 'Delegated'), ('controller','Controller'), ('none','None'))
51 instantiation = models.CharField(help_text="The instantiation type of the slice", max_length=80, choices=SLICE_CHOICES)
52 omf_friendly = models.BooleanField()
53 description=models.TextField(blank=True,help_text="High level description of the slice and expected activities", max_length=1024)
54 slice_url = models.URLField(blank=True)
55 site = models.ForeignKey(Site, related_name='slices', help_text="The Site this Node belongs too")
56
57 def __unicode__(self): return u'%s' % (self.name)
58
Tony Mackd7cb7712013-03-26 13:21:36 -040059 def save(self, *args, **kwds):
Tony Macke1c8c382013-03-26 13:37:59 -040060 # sync keystone tenant
Tony Mack7823b892013-03-29 20:35:26 -040061 driver = OpenStackDriver()
62
Tony Mackd7cb7712013-03-26 13:21:36 -040063 tenant_fields = {'name': self.name,
64 'enabled': self.enabled,
65 'description': self.description}
66 if not self.id:
Tony Mack7823b892013-03-29 20:35:26 -040067 tenant = driver.create_tenant(name=self.name,
68 description=self.description,
69 enabled=self.enabled)
Tony Mackd7cb7712013-03-26 13:21:36 -040070 self.tenant_id = tenants.id
71 else:
72 # update record
Tony Mack7823b892013-03-29 20:35:26 -040073 self.driver.update_tenant(self.tenant_id, name=self.name,
74 description=self.description, enabled=self.enabled)
75 super(Slice, self).save(*args, **kwargs)
Tony Mackd7cb7712013-03-26 13:21:36 -040076
Tony Macke1c8c382013-03-26 13:37:59 -040077 def delete(self, *args, **kwds):
78 # delete keystone tenant
Tony Mack7823b892013-03-29 20:35:26 -040079 driver = OpenStackDriver()
80 driver.delete_tenant(self.tenant_id)
Tony Macke1c8c382013-03-26 13:37:59 -040081 super(Slice, self).delete(*args, **kwargs)
82
Tony Mack7130ac32013-03-22 21:58:00 -040083class DeploymentNetwork(PlCoreBase):
84 name = models.CharField(max_length=200, unique=True, help_text="Name of the Deployment Network")
85
86 def __unicode__(self): return u'%s' % (self.name)
87
88class SiteDeploymentNetwork(PlCoreBase):
89 class Meta:
90 unique_together = ['site', 'deploymentNetwork']
91
92 site = models.ForeignKey(Site, related_name='deploymentNetworks')
93 deploymentNetwork = models.ForeignKey(DeploymentNetwork, related_name='sites')
94 name = models.CharField(default="Blah", max_length=100)
95
96
97 def __unicode__(self): return u'%s::%s' % (self.site, self.deploymentNetwork)
98
99
100class Sliver(PlCoreBase):
Tony Mackebb24872013-04-02 08:20:59 -0400101 tenant_id = models.CharField(max_length=200, help_text="Keystone tenant id")
102 instance_id = models.CharField(max_length=200, help_text="Nova instance id")
Tony Mackd9ae9da2013-04-02 08:58:22 -0400103 name = models.CharField(max_length=200, help_text="Sliver name")
104 flavor = models.CharField(max_length=200, help_text="OS Flavor")
105 image = models.CharField(max_length=200, help_text="Image Name")
Tony Mack27527b92013-04-02 08:59:35 -0400106 slice = models.ForeignKey(Slice, related_name='sliver_slice')
Tony Mack7130ac32013-03-22 21:58:00 -0400107 siteDeploymentNetwork = models.ForeignKey(SiteDeploymentNetwork)
Tony Mack4ec65352013-04-02 08:09:29 -0400108 #node = models.ForeignKey(Node, related_name='node')
Tony Mack7130ac32013-03-22 21:58:00 -0400109
110 def __unicode__(self): return u'%s::%s' % (self.slice, self.siteDeploymentNetwork)
111
Tony Mack7823b892013-03-29 20:35:26 -0400112 def save(self, *args, **kwds):
113 driver = OpenStackDriver()
114 instance = driver.spawn_instances(name=self.name,
115 keyname=self.name,
116 hostnames=self.node.name,
117 flavor=self.flavor,
118 image=self.image)
119 self.instance_id = instance.id
120 super(Sliver, self).save(*args, **kwds)
121
122 def delete(self, *args, **kwds):
123 driver = OpenStackDriver()
124 driver.destroy_instance(name=self.name, id=self.instance_id)
125 super(Sliver, self).delete(*args, **kwds)
126
127
Tony Mack7130ac32013-03-22 21:58:00 -0400128class Node(PlCoreBase):
129 name = models.CharField(max_length=200, unique=True, help_text="Name of the Node")
130 siteDeploymentNetwork = models.ForeignKey(SiteDeploymentNetwork, help_text="The Site and Deployment Network this Node belongs too.")
131
132 def __unicode__(self): return u'%s' % (self.name)
133
Tony Mackd7cb7712013-03-26 13:21:36 -0400134class Network(PlCoreBase):
Tony Mack27527b92013-04-02 08:59:35 -0400135 slice = models.ForeignKey(Slice, related_name='network_slice')
Tony Mackd7cb7712013-03-26 13:21:36 -0400136 name = models.CharField(max_length=200, unique=True)
137 quantum_id = models.CharField(max_length=200, unique=True)
138
139 def __unicode__(self): return u'%s' % (self.name)
140
141 def save(self, *args, **kwargs):
142 os_shell = OpenStackShell()
143 network_fields = {'name': self.name}
144
145 if not self.id:
146 # check if quantum network record exists
147 networks = os_shell.quantum.list_networks(name=self.name)
148 if not networks:
149 network = os_shell.quantum.create_network(name=self.name,
150 admin_state_up=False)
151 else:
152 network = networks[0]
153 self.quantum_id = network.id
154 super(Network, self).save(*args, **kwargs)
155
Tony Macke1c8c382013-03-26 13:37:59 -0400156 def delete(self, *args, **kwargs):
157 # delete quantum network
158 os_shell = OpenStackShell()
159 os_shell.quantum.delete_network(self.quantum_id)
160
161 super(Network, self).delete(*args, **kwargs)
162
Tony Mackd7cb7712013-03-26 13:21:36 -0400163class SubNet(PlCoreBase):
164 network = models.ForeignKey(Network, related_name='network')
165 name = models.CharField(max_length=200, unique=True)
166 quantum_id = models.CharField(max_length=200, unique=True)
167 cidr = models.CharField(max_length=20)
168 ip_version = models.IntegerField()
169 start = models.IPAddressField()
170 end = models.IPAddressField()
171
172 def __unicode__(self): return u'%s' % (self.name)
173
174 def save(self, *args, **kwargs):
175 os_shell = OpenStackShell()
176 subnet_fields = {'network_id': self.network.quantum_id,
177 'name' : self.name,
178 'ip_version': self.ip_version,
179 'cidr': self.cidr,
Tony Mack560788f2013-03-28 20:46:58 -0400180 'allocation_pools': {'start': self.start,
181 'end': self.end}
Tony Mackd7cb7712013-03-26 13:21:36 -0400182 }
183 if not self.id:
184 subnets = os_shell.quantum.list_subnets(name=self.name)
185 if not subnets:
186 subnet = os_shell.quantum.create_subnet(**subnet_fields)
187 else:
188 subnet = subnets[0]
189 self.quantum_id = subnet.id
190 super(SubNet, self).save(*args, **kwargs)
Tony Macke1c8c382013-03-26 13:37:59 -0400191
192
193 def delete(self, *args, **kwargs):
194 # delete quantum network
195 os_shell = OpenStackShell()
196 os_shell.quantum.delete_subnet(self.quantum_id)
197
198 super(SubNet, self).delete(*args, **kwargs)
Tony Mackd7cb7712013-03-26 13:21:36 -0400199
200
201