blob: e224831a1d8d48f7ec9b3c66280a88d689d6a25a [file] [log] [blame]
Siobhan Tully4bc09f22013-04-10 21:15:21 -04001import os
2from django.db import models
3from plstackapi.core.models import PlCoreBase
4from plstackapi.core.models import User
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)
15 user = models.ForeignKey(User, related_name='keys')
16
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