blob: 6e7dcd4ac5bd98c0b7f6568999addf6f4c035165 [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 Mack67f60ce2013-04-02 10:46:07 -040017 name = models.CharField(max_length=200, help_text="Name for this Site")
Tony Mackc689d502013-04-02 11:32:04 -040018 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 -040019 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 Mack39444e42013-04-02 10:22:24 -040031 tenant = driver.create_tenant(tenant_name=self.login_base,
Tony Mack7823b892013-03-29 20:35:26 -040032 description=self.name,
33 enabled=self.enabled)
Tony Mackd8c390f2013-04-02 10:36:19 -040034 self.tenant_id = tenant.id
Tony Mackd7cb7712013-03-26 13:21:36 -040035 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)
Tony Macka3a40cf2013-04-02 11:21:21 -040050 enabled = models.BooleanField(default=True, help_text="Status for this Slice")
Tony Mack7130ac32013-03-22 21:58:00 -040051 SLICE_CHOICES = (('plc', 'PLC'), ('delegated', 'Delegated'), ('controller','Controller'), ('none','None'))
52 instantiation = models.CharField(help_text="The instantiation type of the slice", max_length=80, choices=SLICE_CHOICES)
53 omf_friendly = models.BooleanField()
54 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 -040055 slice_url = models.URLField(blank=True, max_length=512)
Tony Mack7130ac32013-03-22 21:58:00 -040056 site = models.ForeignKey(Site, related_name='slices', help_text="The Site this Node belongs too")
57
58 def __unicode__(self): return u'%s' % (self.name)
59
Tony Mackd7cb7712013-03-26 13:21:36 -040060 def save(self, *args, **kwds):
Tony Macke1c8c382013-03-26 13:37:59 -040061 # sync keystone tenant
Tony Mack7823b892013-03-29 20:35:26 -040062 driver = OpenStackDriver()
63
Tony Mackd7cb7712013-03-26 13:21:36 -040064 if not self.id:
Tony Mack39444e42013-04-02 10:22:24 -040065 tenant = driver.create_tenant(tenant_name=self.name,
Tony Mack7823b892013-03-29 20:35:26 -040066 description=self.description,
67 enabled=self.enabled)
Tony Mackd8c390f2013-04-02 10:36:19 -040068 self.tenant_id = tenant.id
Tony Mackd7cb7712013-03-26 13:21:36 -040069 else:
70 # update record
Tony Mack7823b892013-03-29 20:35:26 -040071 self.driver.update_tenant(self.tenant_id, name=self.name,
72 description=self.description, enabled=self.enabled)
Tony Mack6299cb72013-04-02 11:23:26 -040073 super(Slice, self).save(*args, **kwds)
Tony Mackd7cb7712013-03-26 13:21:36 -040074
Tony Macke1c8c382013-03-26 13:37:59 -040075 def delete(self, *args, **kwds):
76 # delete keystone tenant
Tony Mack7823b892013-03-29 20:35:26 -040077 driver = OpenStackDriver()
78 driver.delete_tenant(self.tenant_id)
Tony Mack6299cb72013-04-02 11:23:26 -040079 super(Slice, self).delete(*args, **kwds)
Tony Macke1c8c382013-03-26 13:37:59 -040080
Tony Mack7130ac32013-03-22 21:58:00 -040081class DeploymentNetwork(PlCoreBase):
82 name = models.CharField(max_length=200, unique=True, help_text="Name of the Deployment Network")
83
84 def __unicode__(self): return u'%s' % (self.name)
85
86class SiteDeploymentNetwork(PlCoreBase):
87 class Meta:
88 unique_together = ['site', 'deploymentNetwork']
89
90 site = models.ForeignKey(Site, related_name='deploymentNetworks')
91 deploymentNetwork = models.ForeignKey(DeploymentNetwork, related_name='sites')
92 name = models.CharField(default="Blah", max_length=100)
93
94
95 def __unicode__(self): return u'%s::%s' % (self.site, self.deploymentNetwork)
96
97
98class Sliver(PlCoreBase):
Tony Mackebb24872013-04-02 08:20:59 -040099 tenant_id = models.CharField(max_length=200, help_text="Keystone tenant id")
100 instance_id = models.CharField(max_length=200, help_text="Nova instance id")
Tony Mackd9ae9da2013-04-02 08:58:22 -0400101 name = models.CharField(max_length=200, help_text="Sliver name")
102 flavor = models.CharField(max_length=200, help_text="OS Flavor")
103 image = models.CharField(max_length=200, help_text="Image Name")
Tony Mack27527b92013-04-02 08:59:35 -0400104 slice = models.ForeignKey(Slice, related_name='sliver_slice')
Tony Mack7130ac32013-03-22 21:58:00 -0400105 siteDeploymentNetwork = models.ForeignKey(SiteDeploymentNetwork)
Tony Mack4ec65352013-04-02 08:09:29 -0400106 #node = models.ForeignKey(Node, related_name='node')
Tony Mack7130ac32013-03-22 21:58:00 -0400107
108 def __unicode__(self): return u'%s::%s' % (self.slice, self.siteDeploymentNetwork)
109
Tony Mack7823b892013-03-29 20:35:26 -0400110 def save(self, *args, **kwds):
111 driver = OpenStackDriver()
112 instance = driver.spawn_instances(name=self.name,
113 keyname=self.name,
114 hostnames=self.node.name,
115 flavor=self.flavor,
116 image=self.image)
117 self.instance_id = instance.id
118 super(Sliver, self).save(*args, **kwds)
119
120 def delete(self, *args, **kwds):
121 driver = OpenStackDriver()
122 driver.destroy_instance(name=self.name, id=self.instance_id)
123 super(Sliver, self).delete(*args, **kwds)
124
125
Tony Mack7130ac32013-03-22 21:58:00 -0400126class Node(PlCoreBase):
127 name = models.CharField(max_length=200, unique=True, help_text="Name of the Node")
128 siteDeploymentNetwork = models.ForeignKey(SiteDeploymentNetwork, help_text="The Site and Deployment Network this Node belongs too.")
129
130 def __unicode__(self): return u'%s' % (self.name)
131
Tony Mackd7cb7712013-03-26 13:21:36 -0400132class Network(PlCoreBase):
Tony Mack27527b92013-04-02 08:59:35 -0400133 slice = models.ForeignKey(Slice, related_name='network_slice')
Tony Mackd7cb7712013-03-26 13:21:36 -0400134 name = models.CharField(max_length=200, unique=True)
135 quantum_id = models.CharField(max_length=200, unique=True)
136
137 def __unicode__(self): return u'%s' % (self.name)
138
139 def save(self, *args, **kwargs):
140 os_shell = OpenStackShell()
141 network_fields = {'name': self.name}
142
143 if not self.id:
144 # check if quantum network record exists
145 networks = os_shell.quantum.list_networks(name=self.name)
146 if not networks:
147 network = os_shell.quantum.create_network(name=self.name,
148 admin_state_up=False)
149 else:
150 network = networks[0]
151 self.quantum_id = network.id
152 super(Network, self).save(*args, **kwargs)
153
Tony Macke1c8c382013-03-26 13:37:59 -0400154 def delete(self, *args, **kwargs):
155 # delete quantum network
156 os_shell = OpenStackShell()
157 os_shell.quantum.delete_network(self.quantum_id)
158
159 super(Network, self).delete(*args, **kwargs)
160
Tony Mackd7cb7712013-03-26 13:21:36 -0400161class SubNet(PlCoreBase):
162 network = models.ForeignKey(Network, related_name='network')
163 name = models.CharField(max_length=200, unique=True)
164 quantum_id = models.CharField(max_length=200, unique=True)
165 cidr = models.CharField(max_length=20)
166 ip_version = models.IntegerField()
167 start = models.IPAddressField()
168 end = models.IPAddressField()
169
170 def __unicode__(self): return u'%s' % (self.name)
171
172 def save(self, *args, **kwargs):
173 os_shell = OpenStackShell()
174 subnet_fields = {'network_id': self.network.quantum_id,
175 'name' : self.name,
176 'ip_version': self.ip_version,
177 'cidr': self.cidr,
Tony Mack560788f2013-03-28 20:46:58 -0400178 'allocation_pools': {'start': self.start,
179 'end': self.end}
Tony Mackd7cb7712013-03-26 13:21:36 -0400180 }
181 if not self.id:
182 subnets = os_shell.quantum.list_subnets(name=self.name)
183 if not subnets:
184 subnet = os_shell.quantum.create_subnet(**subnet_fields)
185 else:
186 subnet = subnets[0]
187 self.quantum_id = subnet.id
188 super(SubNet, self).save(*args, **kwargs)
Tony Macke1c8c382013-03-26 13:37:59 -0400189
190
191 def delete(self, *args, **kwargs):
192 # delete quantum network
193 os_shell = OpenStackShell()
194 os_shell.quantum.delete_subnet(self.quantum_id)
195
196 super(SubNet, self).delete(*args, **kwargs)
Tony Mackd7cb7712013-03-26 13:21:36 -0400197
198
199