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 |
| 4 | from plstackapi.core.models import User |
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 | 759b57a | 2013-04-14 21:03:31 -0400 | [diff] [blame^] | 11 | key_id = models.CharField(max_length=256, unique=True, blank=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) |
| 15 | user = models.ForeignKey(User, related_name='keys') |
| 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) |
| 25 | print nova_key.id |
| 26 | self.key_id = nova_key.id |
| 27 | super(Key, self).save(*args, **kwds) |
| 28 | |
| 29 | def delete(self, *args, **kwds): |
| 30 | driver = OpenStackDriver() |
| 31 | if self.key_id: |
| 32 | driver.delete_keypair(self.key_id) |
| 33 | super(Key, self).delete(*args, **kwds) |
| 34 | |