keys and users work when openstack is disabled or unavailable
diff --git a/plstackapi/core/admin.py b/plstackapi/core/admin.py
index 4bb9a98..58f5f58 100644
--- a/plstackapi/core/admin.py
+++ b/plstackapi/core/admin.py
@@ -63,37 +63,26 @@
save_on_top = False
class OSModelAdmin(PlanetStackBaseAdmin):
- """Attach client connection to openstack on delete() and save()"""
+ """Attach client connection to openstack on delete() and save()"""
def save_model(self, request, obj, form, change):
- client = OpenStackClient(tenant=request.user.site.login_base, **request.session.get('auth', {}))
- obj.driver = OpenStackDriver(client=client)
- obj.caller = request.user
+ auth = request.session.get('auth', {})
+ auth['tenant'] = request.user.site.login_base
+ obj.os_manager = OpenStackManager(auth=auth, caller=request.user)
obj.save()
def delete_model(self, request, obj):
- client = OpenStackClient(tenant=request.user.site.login_base, **request.session.get('auth', {}))
- obj.driver = OpenStackDriver(client=client)
- obj.caller = request.user
- obj.delete()
+ auth = request.session.get('auth', {})
+ auth['tenant'] = request.user.site.login_base
+ obj.os_manager = OpenStackManager(auth=auth, caller=request.user)
+ obj.delete()
-class RoleAdmin(PlanetStackBaseAdmin):
+class RoleAdmin(OSModelAdmin):
fieldsets = [
('Role', {'fields': ['role_type']})
]
list_display = ('role_type',)
- def save_model(self, request, obj, form, change):
- auth = request.session.get('auth', {})
- auth['tenant'] = request.user.site.login_base
- obj.os_manager = OpenStackManager(auth=auth, caller=request.user)
- obj.save()
-
- def delete_model(self, request, obj):
- auth = request.session.get('auth', {})
- auth['tenant'] = request.user.site.login_base
- obj.os_manager = OpenStackManager(auth=auth, caller=request.user)
- obj.delete()
class DeploymentNetworkAdminForm(forms.ModelForm):
sites = forms.ModelMultipleChoiceField(
diff --git a/plstackapi/core/models/key.py b/plstackapi/core/models/key.py
index 3e4f56f..afbebff 100644
--- a/plstackapi/core/models/key.py
+++ b/plstackapi/core/models/key.py
@@ -16,15 +16,10 @@
def __unicode__(self): return u'%s' % (self.name)
def save(self, *args, **kwds):
- if not self.key_id:
- key_fields = {'name': self.name,
- 'key': self.key}
- nova_key = self.driver.create_keypair(**key_fields)
- self.key_id = nova_key.id
+ self.os_manager.save_key(self)
super(Key, self).save(*args, **kwds)
def delete(self, *args, **kwds):
- if self.key_id:
- self.driver.delete_keypair(self.key_id)
+ self.os_manager.delete_key(self)
super(Key, self).delete(*args, **kwds)
diff --git a/plstackapi/core/models/pluser.py b/plstackapi/core/models/pluser.py
index f914662..474f044 100644
--- a/plstackapi/core/models/pluser.py
+++ b/plstackapi/core/models/pluser.py
@@ -3,7 +3,7 @@
from django.db import models
from plstackapi.core.models import PlCoreBase
from plstackapi.core.models import Site
-from plstackapi.openstack.driver import OpenStackDriver
+from plstackapi.openstack.manager import OpenStackManager
from django.contrib.auth.models import User, AbstractBaseUser, UserManager, BaseUserManager
# Create your models here.
@@ -101,22 +101,16 @@
def save(self, *args, **kwds):
- if not self.user_id:
- if not self.driver:
- setattr(self, 'driver', OpenStackDriver())
- name = self.email[:self.email.find('@')]
- user_fields = {'name': name,
- 'email': self.email,
- 'password': self.password,
- 'enabled': True}
- keystone_user = self.driver.create_user(**user_fields)
- self.user_id = keystone_user.id
+ if not hasattr(self, 'os_manager'):
+ setattr(self, 'os_manager', OpenStackManager())
+
+ self.os_manager.save_user(self)
self.set_password(self.password)
super(PLUser, self).save(*args, **kwds)
def delete(self, *args, **kwds):
- if self.user_id:
- if not getattr(self, 'driver'):
- setattr(self, 'driver', OpenStackDriver())
- self.driver.delete_user(self.user_id)
+ if not hasattr(self, 'os_manager'):
+ setattr(self, 'os_manager', OpenStackManager())
+
+ self.os_manager.delete_user(self)
super(PLUser, self).delete(*args, **kwds)
diff --git a/plstackapi/openstack/manager.py b/plstackapi/openstack/manager.py
index b4ad8d2..79403a8 100644
--- a/plstackapi/openstack/manager.py
+++ b/plstackapi/openstack/manager.py
@@ -1,15 +1,19 @@
from plstackapi.planetstack import settings
from django.core import management
management.setup_environ(settings)
-from plstackapi.openstack.client import OpenStackClient
-from plstackapi.openstack.driver import OpenStackDriver
-from plstackapi.planetstack.config import Config
-from plstackapi.core.models import *
+try:
+ from plstackapi.openstack.client import OpenStackClient
+ from plstackapi.openstack.driver import OpenStackDriver
+ from plstackapi.planetstack.config import Config
+ from plstackapi.core.models import *
+ has_openstack = True
+except:
+ has_openstack = False
def require_enabled(callable):
enabled = Config().api_nova_enabled
def wrapper(*args, **kwds):
- if enabled:
+ if enabled and has_openstack:
return callable(*args, **kwds)
else:
return None
@@ -35,8 +39,38 @@
@require_enabled
def delete_role(self, role):
if role.role_id:
- self.driver.delete_role({'id': role.role_id})
-
+ self.driver.delete_role({'id': role.role_id})
+
+ @require_enabled
+ def save_key(self, key):
+ if not key.key_id:
+ key_fields = {'name': key.name,
+ 'key': key.key}
+ nova_key = self.driver.create_keypair(**key_fields)
+ key.key_id = nova_key.id
+
+ @require_enabled
+ def delete_key(self, key):
+ if key.key_id:
+ self.driver.delete_keypair(key.key_id)
+
+ @require_enabled
+ def save_user(self, user):
+ if not user.user_id:
+ name = user.email[:user.email.find('@')]
+ user_fields = {'name': name,
+ 'email': user.email,
+ 'password': user.password,
+ 'enabled': True}
+ keystone_user = self.driver.create_user(**user_fields)
+ user.user_id = keystone_user.id
+
+ @require_enabled
+ def delete_user(self, user):
+ if user.user_id:
+ self.driver.delete_user(user.user_id)
+
+
def refresh_nodes(self):
# collect local nodes
nodes = Node.objects.all()