RoleAdmin and Role model work when openstack is disable
diff --git a/config/plstackapi_config b/config/plstackapi_config
index cf02ee7..1d7b761 100644
--- a/config/plstackapi_config
+++ b/config/plstackapi_config
@@ -17,6 +17,7 @@
ratelimit_enabled=0
omf_enabled=0
mail_support_address=support@localhost
+nova_enabled=True
[nova]
admin_user=admin@domain.com
diff --git a/plstackapi/core/admin.py b/plstackapi/core/admin.py
index a814c55..904b566 100644
--- a/plstackapi/core/admin.py
+++ b/plstackapi/core/admin.py
@@ -1,5 +1,6 @@
from plstackapi.core.models import Site
from plstackapi.core.models import *
+from plstackapi.openstack.manager import OpenStackManager
from plstackapi.openstack.driver import OpenStackDriver
from plstackapi.openstack.client import OpenStackClient
@@ -76,12 +77,24 @@
obj.caller = request.user
obj.delete()
-class RoleAdmin(OSModelAdmin):
+class RoleAdmin(PlanetStackBaseAdmin):
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(
queryset=Site.objects.all(),
diff --git a/plstackapi/core/models/role.py b/plstackapi/core/models/role.py
index b9f3e2f..41bfd62 100644
--- a/plstackapi/core/models/role.py
+++ b/plstackapi/core/models/role.py
@@ -13,13 +13,10 @@
def save(self, *args, **kwds):
- if not self.role_id:
- keystone_role = self.driver.create_role(name=self.role_type)
- self.role_id = keystone_role.id
+ self.os_manager.save_role(self)
super(Role, self).save(*args, **kwds)
def delete(self, *args, **kwds):
- if self.role_id:
- self.driver.delete_role({'id': self.role_id})
+ self.os_manager.delete_role(self)
super(Role, self).delete(*args, **kwds)
diff --git a/plstackapi/openstack/manager.py b/plstackapi/openstack/manager.py
index 68d4dbb..b4ad8d2 100644
--- a/plstackapi/openstack/manager.py
+++ b/plstackapi/openstack/manager.py
@@ -2,19 +2,43 @@
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 *
+
+def require_enabled(callable):
+ enabled = Config().api_nova_enabled
+ def wrapper(*args, **kwds):
+ if enabled:
+ return callable(*args, **kwds)
+ else:
+ return None
+ return wrapper
-class Manager:
+class OpenStackManager:
- def __init__(self):
+ def __init__(self, auth={}, caller=None):
+ self.client = None
+ if auth:
+ self.client = OpenStackClient(**auth)
- self.client = OpenStackClient()
+ self.driver = OpenStackDriver(client=self.client)
+ self.caller=None
+ @require_enabled
+ def save_role(self, role):
+ if not role.role_id:
+ keystone_role = self.driver.create_role(role.role_type)
+ role.role_id = keystone_role.id
+
+ @require_enabled
+ def delete_role(self, role):
+ if role.role_id:
+ self.driver.delete_role({'id': role.role_id})
+
def refresh_nodes(self):
# collect local nodes
- from plstackapi.core.models import Node
- from plstackapi.core.models import DeploymentNetwork
- from plstackapi.core.models import Site
nodes = Node.objects.all()
nodes_dict = {}
for node in nodes:
@@ -51,7 +75,6 @@
def refresh_images(self):
# collect local images
- from plstackapi.core.models import Image
images = Image.objects.all()
images_dict = {}
for image in images:
@@ -75,3 +98,5 @@
# remove old images
old_image_names = set(images_dict.keys()).difference(glance_images_dict.keys())
Image.objects.filter(name__in=old_image_names).delete()
+
+