blob: 9d4b139cafed12eb80dc1d64231fcc12d092ac10 [file] [log] [blame]
Siobhan Tully4bc09f22013-04-10 21:15:21 -04001import os
2from django.db import models
3from plstackapi.core.models import PlCoreBase
Siobhan Tully53437282013-04-26 19:30:27 -04004from plstackapi.core.models import PLUser
Tony Mack759b57a2013-04-14 21:03:31 -04005from plstackapi.openstack.driver import OpenStackDriver
Siobhan Tully4bc09f22013-04-10 21:15:21 -04006
7# Create your models here.
8
9class Key(PlCoreBase):
10 name = models.CharField(max_length=256, unique=True)
Tony Mackcdec0902013-04-15 00:38:49 -040011 key_id = models.CharField(max_length=256, unique=True)
Siobhan Tully4bc09f22013-04-10 21:15:21 -040012 key = models.CharField(max_length=512)
13 type = models.CharField(max_length=256)
14 blacklisted = models.BooleanField(default=False)
Siobhan Tully53437282013-04-26 19:30:27 -040015 user = models.ForeignKey(PLUser, related_name='keys')
Siobhan Tully4bc09f22013-04-10 21:15:21 -040016
17 def __unicode__(self): return u'%s' % (self.name)
Tony Mack759b57a2013-04-14 21:03:31 -040018
19 def save(self, *args, **kwds):
20 driver = OpenStackDriver()
21 if not self.key_id:
22 key_fields = {'name': self.name,
23 'key': self.key}
24 nova_key = driver.create_keypair(**key_fields)
Tony Mack759b57a2013-04-14 21:03:31 -040025 self.key_id = nova_key.id
26 super(Key, self).save(*args, **kwds)
27
28 def delete(self, *args, **kwds):
29 driver = OpenStackDriver()
30 if self.key_id:
31 driver.delete_keypair(self.key_id)
32 super(Key, self).delete(*args, **kwds)
33