blob: 3e4f56f82d12980509ed8c3b30a4de70c5c92006 [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
Siobhan Tully4bc09f22013-04-10 21:15:21 -04005
6# Create your models here.
7
8class Key(PlCoreBase):
9 name = models.CharField(max_length=256, unique=True)
Tony Mackcdec0902013-04-15 00:38:49 -040010 key_id = models.CharField(max_length=256, unique=True)
Siobhan Tully4bc09f22013-04-10 21:15:21 -040011 key = models.CharField(max_length=512)
12 type = models.CharField(max_length=256)
13 blacklisted = models.BooleanField(default=False)
Siobhan Tully53437282013-04-26 19:30:27 -040014 user = models.ForeignKey(PLUser, related_name='keys')
Siobhan Tully4bc09f22013-04-10 21:15:21 -040015
16 def __unicode__(self): return u'%s' % (self.name)
Tony Mack759b57a2013-04-14 21:03:31 -040017
18 def save(self, *args, **kwds):
Tony Mack759b57a2013-04-14 21:03:31 -040019 if not self.key_id:
20 key_fields = {'name': self.name,
21 'key': self.key}
Tony Macke59a7c82013-04-27 11:08:10 -040022 nova_key = self.driver.create_keypair(**key_fields)
Tony Mack759b57a2013-04-14 21:03:31 -040023 self.key_id = nova_key.id
24 super(Key, self).save(*args, **kwds)
25
26 def delete(self, *args, **kwds):
Tony Mack759b57a2013-04-14 21:03:31 -040027 if self.key_id:
Tony Macke59a7c82013-04-27 11:08:10 -040028 self.driver.delete_keypair(self.key_id)
Tony Mack759b57a2013-04-14 21:03:31 -040029 super(Key, self).delete(*args, **kwds)
30