Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 1 | import os |
| 2 | from django.db import models |
| 3 | from plstackapi.core.models import PlCoreBase |
Siobhan Tully | 5343728 | 2013-04-26 19:30:27 -0400 | [diff] [blame] | 4 | from plstackapi.core.models import PLUser |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 5 | |
| 6 | # Create your models here. |
| 7 | |
| 8 | class Key(PlCoreBase): |
| 9 | name = models.CharField(max_length=256, unique=True) |
Tony Mack | cdec090 | 2013-04-15 00:38:49 -0400 | [diff] [blame] | 10 | key_id = models.CharField(max_length=256, unique=True) |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 11 | key = models.CharField(max_length=512) |
| 12 | type = models.CharField(max_length=256) |
| 13 | blacklisted = models.BooleanField(default=False) |
Siobhan Tully | 5343728 | 2013-04-26 19:30:27 -0400 | [diff] [blame] | 14 | user = models.ForeignKey(PLUser, related_name='keys') |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 15 | |
| 16 | def __unicode__(self): return u'%s' % (self.name) |
Tony Mack | 759b57a | 2013-04-14 21:03:31 -0400 | [diff] [blame] | 17 | |
| 18 | def save(self, *args, **kwds): |
Tony Mack | 759b57a | 2013-04-14 21:03:31 -0400 | [diff] [blame] | 19 | if not self.key_id: |
| 20 | key_fields = {'name': self.name, |
| 21 | 'key': self.key} |
Tony Mack | e59a7c8 | 2013-04-27 11:08:10 -0400 | [diff] [blame] | 22 | nova_key = self.driver.create_keypair(**key_fields) |
Tony Mack | 759b57a | 2013-04-14 21:03:31 -0400 | [diff] [blame] | 23 | self.key_id = nova_key.id |
| 24 | super(Key, self).save(*args, **kwds) |
| 25 | |
| 26 | def delete(self, *args, **kwds): |
Tony Mack | 759b57a | 2013-04-14 21:03:31 -0400 | [diff] [blame] | 27 | if self.key_id: |
Tony Mack | e59a7c8 | 2013-04-27 11:08:10 -0400 | [diff] [blame] | 28 | self.driver.delete_keypair(self.key_id) |
Tony Mack | 759b57a | 2013-04-14 21:03:31 -0400 | [diff] [blame] | 29 | super(Key, self).delete(*args, **kwds) |
| 30 | |