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 |
Tony Mack | 759b57a | 2013-04-14 21:03:31 -0400 | [diff] [blame] | 5 | from plstackapi.openstack.driver import OpenStackDriver |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 6 | |
| 7 | # Create your models here. |
| 8 | |
| 9 | class Key(PlCoreBase): |
| 10 | name = models.CharField(max_length=256, unique=True) |
Tony Mack | cdec090 | 2013-04-15 00:38:49 -0400 | [diff] [blame] | 11 | key_id = models.CharField(max_length=256, unique=True) |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 12 | key = models.CharField(max_length=512) |
| 13 | type = models.CharField(max_length=256) |
| 14 | blacklisted = models.BooleanField(default=False) |
Siobhan Tully | 5343728 | 2013-04-26 19:30:27 -0400 | [diff] [blame^] | 15 | user = models.ForeignKey(PLUser, related_name='keys') |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 16 | |
| 17 | def __unicode__(self): return u'%s' % (self.name) |
Tony Mack | 759b57a | 2013-04-14 21:03:31 -0400 | [diff] [blame] | 18 | |
| 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 Mack | 759b57a | 2013-04-14 21:03:31 -0400 | [diff] [blame] | 25 | 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 | |