update Key admin
diff --git a/plstackapi/core/admin.py b/plstackapi/core/admin.py
index 13c636b..c195a37 100644
--- a/plstackapi/core/admin.py
+++ b/plstackapi/core/admin.py
@@ -89,13 +89,20 @@
widgets = {
'password': forms.PasswordInput(),
}
+
class UserAdmin(admin.ModelAdmin):
form = UserForm
fieldsets = [
('User', {'fields': ['firstname', 'lastname', 'email', 'password', 'phone', 'user_url', 'is_admin', 'site']})
]
list_display = ['firstname', 'lastname', 'email', 'password', 'phone', 'user_url', 'is_admin', 'site']
- search_fields = ['email']
+ search_fields = ['email']
+
+class KeyAdmin(admin.ModelAdmin):
+ fieldsets = [
+ ('Key', {'fields': ['name', 'key', 'type', 'blacklisted', 'user']})
+ ]
+ list_display = ['name', 'key', 'type', 'blacklisted', 'user']
class SliceAdmin(PlanetStackBaseAdmin):
fields = ['name', 'site', 'instantiation', 'description', 'slice_url']
@@ -112,7 +119,6 @@
list_display = ('name', 'site', 'deploymentNetwork')
list_filter = ('deploymentNetwork',)
-
class RoleAdmin(admin.ModelAdmin):
fieldsets = [
('Role', {'fields': ['role_type']})
@@ -128,7 +134,7 @@
admin.site.register(Node, NodeAdmin)
admin.site.register(Sliver)
admin.site.register(Flavor)
-admin.site.register(Key)
+admin.site.register(Key, KeyAdmin)
admin.site.register(Role, RoleAdmin)
admin.site.register(User, UserAdmin)
admin.site.register(DeploymentNetwork, DeploymentNetworkAdmin)
diff --git a/plstackapi/core/api/keys.py b/plstackapi/core/api/keys.py
index 42ffe46..ad54041 100644
--- a/plstackapi/core/api/keys.py
+++ b/plstackapi/core/api/keys.py
@@ -27,6 +27,7 @@
nova_fields = {'name': key.name,
'key': key.key}
nova_key = driver.create_keypair(**nova_fields)
+ key.key_id = nova_key.id
key.save()
return key
@@ -37,7 +38,7 @@
driver = OpenStackDriver(client = auth_check(auth))
keys = _get_keys(filter)
for key in keys:
- driver.delete_keypair(name=key.name)
+ driver.delete_keypair(id=key.key_id)
key.delete()
return 1
diff --git a/plstackapi/core/models/key.py b/plstackapi/core/models/key.py
index d0a0a07..080f211 100644
--- a/plstackapi/core/models/key.py
+++ b/plstackapi/core/models/key.py
@@ -2,14 +2,33 @@
from django.db import models
from plstackapi.core.models import PlCoreBase
from plstackapi.core.models import User
+from plstackapi.openstack.driver import OpenStackDriver
# Create your models here.
class Key(PlCoreBase):
name = models.CharField(max_length=256, unique=True)
+ key_id = models.CharField(max_length=256, unique=True, blank=True)
key = models.CharField(max_length=512)
type = models.CharField(max_length=256)
blacklisted = models.BooleanField(default=False)
user = models.ForeignKey(User, related_name='keys')
def __unicode__(self): return u'%s' % (self.name)
+
+ def save(self, *args, **kwds):
+ driver = OpenStackDriver()
+ if not self.key_id:
+ key_fields = {'name': self.name,
+ 'key': self.key}
+ nova_key = driver.create_keypair(**key_fields)
+ print nova_key.id
+ self.key_id = nova_key.id
+ super(Key, self).save(*args, **kwds)
+
+ def delete(self, *args, **kwds):
+ driver = OpenStackDriver()
+ if self.key_id:
+ driver.delete_keypair(self.key_id)
+ super(Key, self).delete(*args, **kwds)
+
diff --git a/plstackapi/openstack/driver.py b/plstackapi/openstack/driver.py
index 21f3422..c7be4ad 100644
--- a/plstackapi/openstack/driver.py
+++ b/plstackapi/openstack/driver.py
@@ -176,8 +176,9 @@
key = self.shell.nova.keypairs.create(name=name, public_key=key)
return key
- def delete_keypair(self, name):
- keys = self.shell.nova.keypairs.findall(name=name)
+ def delete_keypair(self, id):
+ keys = self.shell.nova.keypairs.findall(id=id)
+ print keys
for key in keys:
self.shell.nova.keypairs.delete(key)
return 1