create OSModelAdmin. Update some Admin definitions to subclass OSModelAdmin
diff --git a/plstackapi/core/admin.py b/plstackapi/core/admin.py
index be9d76b..81736ac 100644
--- a/plstackapi/core/admin.py
+++ b/plstackapi/core/admin.py
@@ -45,6 +45,19 @@
class PlanetStackBaseAdmin(admin.ModelAdmin):
save_on_top = False
+class OSModelAdmin(PlanetStackBaseAdmin):
+ """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.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.delete()
+
+
class DeploymentNetworkAdminForm(forms.ModelForm):
sites = forms.ModelMultipleChoiceField(
queryset=Site.objects.all(),
@@ -77,7 +90,7 @@
form = DeploymentNetworkAdminForm
inlines = [NodeInline,]
-class SiteAdmin(admin.ModelAdmin):
+class SiteAdmin(OSModelAdmin):
fieldsets = [
(None, {'fields': ['name', 'site_url', 'enabled', 'is_public', 'login_base']}),
('Location', {'fields': ['latitude', 'longitude']}),
@@ -88,40 +101,36 @@
inlines = [NodeInline,]
search_fields = ['name']
-class KeyAdmin(admin.ModelAdmin):
+class KeyAdmin(OSModelAdmin):
fieldsets = [
('Key', {'fields': ['name', 'key', 'type', 'blacklisted', 'user']})
]
list_display = ['name', 'key', 'type', 'blacklisted', 'user']
- def save_model(self, request, obj, form, change):
- # attach the caller's openstack clien connection to the object
- client = OpenStackClient(tenant=request.user.site.login_base, **request.session.get('auth', {}))
- obj.driver = OpenStackDriver(client=client)
- obj.save()
-
- def delete_model(self, request, obj):
- # attach the caller's openstack clien connection to the object
- client = OpenStackClient(tenant=request.user.site.login_base, **request.session.get('auth', {}))
- obj.driver = OpenStackDriver(client=client)
- obj.delete()
-
def get_queryset(self, request):
# get keys user is allowed to see
qs = super(KeyAdmin, self).get_queryset(request)
if request.user.is_superuser:
return qs
+ # users can only see their own keys
return qs.filter(user=request.user)
-class SliceAdmin(PlanetStackBaseAdmin):
+class SliceAdmin(OSModelAdmin):
fields = ['name', 'site', 'instantiation', 'description', 'slice_url']
list_display = ('name', 'site','slice_url', 'instantiation')
inlines = [SliverInline]
-class SubnetAdmin(admin.ModelAdmin):
+ def get_queryset(self, request):
+ qs = super(SliceAdmin, self).get_queryset(request)
+ if request.user.is_superuser:
+ return qs
+ # users can only see slices at their site
+ return qs.filter(site=request.user.site)
+
+class SubnetAdmin(OSModelAdmin):
fields = ['cidr', 'ip_version', 'start', 'end', 'slice']
- list_display = ('slice','cidr', 'start', 'end', 'ip_version' )
+ list_display = ('slice','cidr', 'start', 'end', 'ip_version')
class ImageAdmin(admin.ModelAdmin):
fields = ['image_id', 'name', 'disk_format', 'container_format']
@@ -130,7 +139,7 @@
list_display = ('name', 'site', 'deploymentNetwork')
list_filter = ('deploymentNetwork',)
-class RoleAdmin(admin.ModelAdmin):
+class RoleAdmin(OSModelAdmin):
fieldsets = [
('Role', {'fields': ['role_type']})
]
@@ -148,12 +157,13 @@
'ip': PlainTextWidget(),
}
-class SliverAdmin(admin.ModelAdmin):
+class SliverAdmin(OSModelAdmin):
form = SliverForm
fieldsets = [
('Sliver', {'fields': ['ip', 'name', 'slice', 'flavor', 'image', 'key', 'node', 'deploymentNetwork']})
]
list_display = ['ip', 'name', 'slice', 'flavor', 'image', 'key', 'node', 'deploymentNetwork']
+
class UserCreationForm(forms.ModelForm):
"""A form for creating new users. Includes all the required
diff --git a/plstackapi/core/models/role.py b/plstackapi/core/models/role.py
index 4b4b2e4..b9f3e2f 100644
--- a/plstackapi/core/models/role.py
+++ b/plstackapi/core/models/role.py
@@ -2,9 +2,6 @@
import datetime
from django.db import models
from plstackapi.core.models import PlCoreBase
-from plstackapi.openstack.driver import OpenStackDriver
-
-# Create your models here.
class Role(PlCoreBase):
@@ -16,17 +13,13 @@
def save(self, *args, **kwds):
- driver = OpenStackDriver()
if not self.role_id:
- keystone_role = driver.create_role(name=self.role_type)
+ keystone_role = self.driver.create_role(name=self.role_type)
self.role_id = keystone_role.id
-
super(Role, self).save(*args, **kwds)
def delete(self, *args, **kwds):
- driver = OpenStackDriver()
if self.role_id:
- driver.delete_role({'id': self.role_id})
-
+ self.driver.delete_role({'id': self.role_id})
super(Role, self).delete(*args, **kwds)
diff --git a/plstackapi/core/models/site.py b/plstackapi/core/models/site.py
index 1e1cbe4..67b3e52 100644
--- a/plstackapi/core/models/site.py
+++ b/plstackapi/core/models/site.py
@@ -2,9 +2,6 @@
from django.db import models
from plstackapi.core.models import PlCoreBase
from plstackapi.core.models import DeploymentNetwork
-from plstackapi.openstack.driver import OpenStackDriver
-
-# Create your models here.
class Site(PlCoreBase):
@@ -24,26 +21,24 @@
def __unicode__(self): return u'%s' % (self.name)
def save(self, *args, **kwds):
- driver = OpenStackDriver()
if not self.tenant_id:
- tenant = driver.create_tenant(tenant_name=self.login_base,
- description=self.name,
- enabled=self.enabled)
+ tenant = self.driver.create_tenant(tenant_name=self.login_base,
+ description=self.name,
+ enabled=self.enabled)
self.tenant_id = tenant.id
# update the record
if self.id:
- driver.update_tenant(self.tenant_id,
- name=self.login_base,
- description=self.name,
- enabled=self.enabled)
+ self.driver.update_tenant(self.tenant_id,
+ name=self.login_base,
+ description=self.name,
+ enabled=self.enabled)
super(Site, self).save(*args, **kwds)
def delete(self, *args, **kwds):
- driver = OpenStackDriver()
if self.tenant_id:
- driver.delete_tenant(self.tenant_id)
+ self.driver.delete_tenant(self.tenant_id)
super(Site, self).delete(*args, **kwds)
diff --git a/plstackapi/core/models/slice.py b/plstackapi/core/models/slice.py
index 27184dc..d174b2e 100644
--- a/plstackapi/core/models/slice.py
+++ b/plstackapi/core/models/slice.py
@@ -5,7 +5,6 @@
from plstackapi.core.models import PLUser
from plstackapi.core.models import Role
from plstackapi.core.models import DeploymentNetwork
-from plstackapi.openstack.driver import OpenStackDriver
# Create your models here.
@@ -25,31 +24,28 @@
def __unicode__(self): return u'%s' % (self.name)
def save(self, *args, **kwds):
-
- driver = OpenStackDriver()
if not self.tenant_id:
nova_fields = {'tenant_name': self.name,
'description': self.description,
'enabled': self.enabled}
- tenant = driver.create_tenant(**nova_fields)
+ tenant = self.driver.create_tenant(**nova_fields)
self.tenant_id = tenant.id
# create network
- network = driver.create_network(self.name)
+ network = self.driver.create_network(self.name)
self.network_id = network['id']
# create router
- router = driver.create_router(self.name)
+ router = self.driver.create_router(self.name)
self.router_id = router['id']
super(Slice, self).save(*args, **kwds)
def delete(self, *args, **kwds):
- driver = OpenStackDriver()
if self.tenant_id:
- driver.delete_router(self.router_id)
- driver.delete_network(self.network_id)
- driver.delete_tenant(self.tenant_id)
+ self.driver.delete_router(self.router_id)
+ self.driver.delete_network(self.network_id)
+ self.driver.delete_tenant(self.tenant_id)
super(Slice, self).delete(*args, **kwds)
diff --git a/plstackapi/core/models/sliver.py b/plstackapi/core/models/sliver.py
index 27b9ab1..13f94b2 100644
--- a/plstackapi/core/models/sliver.py
+++ b/plstackapi/core/models/sliver.py
@@ -9,7 +9,6 @@
from plstackapi.core.models import Node
from plstackapi.core.models import Site
from plstackapi.core.models import DeploymentNetwork
-from plstackapi.openstack.driver import OpenStackDriver
# Create your models here.
class Sliver(PlCoreBase):
@@ -30,8 +29,7 @@
raise exceptions.ValidationError, "Slice %s has no subnet" % self.slice.name
if not self.instance_id:
- driver = OpenStackDriver()
- instance = driver.spawn_instance(name=self.name,
+ instance = self.driver.spawn_instance(name=self.name,
key_name = self.key.name,
flavor_id = self.flavor.flavor_id,
image_id = self.image.image_id,
@@ -41,8 +39,7 @@
super(Sliver, self).save(*args, **kwds)
def delete(self, *args, **kwds):
- driver = OpenStackDriver()
if self.instance_id:
- driver.destroy_instance(self.instance_id)
+ self.driver.destroy_instance(self.instance_id)
super(Sliver, self).delete(*args, **kwds)
diff --git a/plstackapi/core/models/subnet.py b/plstackapi/core/models/subnet.py
index 2e10ae1..c9ea1ef 100644
--- a/plstackapi/core/models/subnet.py
+++ b/plstackapi/core/models/subnet.py
@@ -3,7 +3,6 @@
from django.db import models
from plstackapi.core.models import PlCoreBase
from plstackapi.core.models import Slice
-from plstackapi.openstack.driver import OpenStackDriver
# Create your models here.
@@ -18,10 +17,8 @@
def __unicode__(self): return u'%s' % (self.slice.name)
def save(self, *args, **kwds):
-
- driver = OpenStackDriver()
if not self.subnet_id:
- quantum_subnet = driver.create_subnet(name= self.slice.name,
+ quantum_subnet = self.driver.create_subnet(name= self.slice.name,
network_id=self.slice.network_id,
cidr_ip = self.cidr,
ip_version=self.ip_version,
@@ -29,17 +26,16 @@
end = self.end)
self.subnet_id = quantum_subnet['id']
# add subnet as interface to slice's router
- driver.add_router_interface(self.slice.router_id, self.subnet_id)
+ self.driver.add_router_interface(self.slice.router_id, self.subnet_id)
#add_route = 'route add -net %s dev br-ex gw 10.100.0.5' % self.cidr
#commands.getstatusoutput(add_route)
super(Subnet, self).save(*args, **kwds)
def delete(self, *args, **kwds):
- driver = OpenStackDriver()
if self.subnet_id:
- driver.delete_router_interface(self.slice.router_id, self.subnet_id)
- driver.delete_subnet(self.subnet_id)
+ self.driver.delete_router_interface(self.slice.router_id, self.subnet_id)
+ self.driver.delete_subnet(self.subnet_id)
#del_route = 'route del -net %s' % self.cidr
#commands.getstatusoutput(del_route)
super(Subnet, self).delete(*args, **kwds)