Merge pick up, tweaked role/site/plcore_base or os_manager path when OpenStack not...
Siobhan Tully [Fri, 10 May 2013 14:50:08 +0000 (10:50 -0400)]
25 files changed:
1  2 
planetstack/core/admin.py
planetstack/core/api/deployment_networks.py
planetstack/core/api/images.py
planetstack/core/api/keys.py
planetstack/core/api/nodes.py
planetstack/core/api/roles.py
planetstack/core/api/site_privileges.py
planetstack/core/api/sites.py
planetstack/core/api/slice_memberships.py
planetstack/core/api/slices.py
planetstack/core/api/slivers.py
planetstack/core/api/users.py
planetstack/core/models/key.py
planetstack/core/models/plcorebase.py
planetstack/core/models/role.py
planetstack/core/models/site.py
planetstack/core/models/slice.py
planetstack/core/models/sliver.py
planetstack/core/models/subnet.py
planetstack/core/models/user.py
planetstack/core/serializers.py
planetstack/importer/plclassic/slice_importer.py
planetstack/openstack/driver.py
planetstack/openstack/manager.py
planetstack/planetstack/urls.py

@@@ -125,7 -129,7 +133,8 @@@ class DeploymentNetworkAdmin(PlanetStac
                  continue
              # give inline object access to driver and caller
              auth = request.session.get('auth', {})
--            auth['tenant'] = request.user.site.login_base
++            if request.user.site:
++                auth['tenant'] = request.user.site.login_base
              inline.model.os_manager = OpenStackManager(auth=auth, caller=request.user)
              yield inline.get_formset(request, obj)
  
@@@ -137,9 -141,20 +146,20 @@@ class SiteAdmin(OSModelAdmin)
      ]
      list_display = ('name', 'login_base','site_url', 'enabled')
      filter_horizontal = ('deployments',)
 -    inlines = [NodeInline,]
 +    inlines = [NodeInline, UserInline]
      search_fields = ['name']
  
+     def queryset(self, request):
+         # admins can see all keys. Users can only see sites they belong to.
+         qs = super(SiteAdmin, self).queryset(request)
+         if not request.user.is_admin:
+             valid_sites = [request.user.site.login_base]
+             roles = request.user.get_roles()
+             for tenant_list in roles.values():
+                 valid_sites.extend(tenant_list)
+             qs = qs.filter(login_base__in=valid_sites)
+         return qs
      def get_formsets(self, request, obj=None):
          for inline in self.get_inline_instances(request, obj):
              # hide MyInline in the add view
@@@ -173,19 -202,18 +207,18 @@@ class SitePrivilegeAdmin(PlanetStackBas
  
  class KeyAdmin(OSModelAdmin):
      fieldsets = [
-         ('Key', {'fields': ['name', 'key', 'type', 'blacklisted']})
 -        ('Key', {'fields': ['key', 'type', 'blacklisted', 'user']})
++        ('Key', {'fields': ['key', 'type', 'blacklisted']})
      ]
-     list_display = ['name', 'key', 'type', 'blacklisted']
-     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)  
 -    list_display = ['key', 'type', 'blacklisted', 'user']
++    list_display = ['key', 'type', 'blacklisted']
 -    def queryset(self, request):
++    #def queryset(self, request):
+         # admins can see all keys. Users can only see their own key.
 -        if request.user.is_admin:
 -            qs = super(KeyAdmin, self).queryset(request) 
 -        else:
 -            qs = Key.objects.filter(user=request.user)
 -        return qs 
++        #if request.user.is_admin:
++        #    qs = super(KeyAdmin, self).queryset(request) 
++        #else:
++        #    qs = Key.objects.filter(user=request.user)
++        #return qs 
          
  class SliceAdmin(OSModelAdmin):
      fields = ['name', 'site', 'serviceClass', 'description', 'slice_url']
      list_display = ('name', 'site','serviceClass', 'slice_url')
@@@ -347,13 -413,13 +418,13 @@@ class UserAdmin(UserAdmin, OSModelAdmin
      list_filter = ('site',)
      fieldsets = (
          (None, {'fields': ('email', 'password')}),
-         ('Personal info', {'fields': ('firstname','lastname','phone','site', 'key')}),
 -        ('Personal info', {'fields': ('firstname','lastname','phone', 'is_admin', 'site')}),
++        ('Personal info', {'fields': ('firstname','lastname','phone', 'is_admin', 'site', 'key')}),
          #('Important dates', {'fields': ('last_login',)}),
      )
      add_fieldsets = (
          (None, {
              'classes': ('wide',),
-             'fields': ('email', 'firstname', 'lastname', 'phone', 'site', 'password1', 'password2', 'key')}
 -            'fields': ('email', 'firstname', 'lastname', 'phone', 'site', 'is_admin', 'password1', 'password2')}
++            'fields': ('email', 'firstname', 'lastname', 'phone', 'site', 'is_admin', 'key','password1', 'password2')}
          ),
      )
      search_fields = ('email',)
@@@ -374,14 -440,12 +445,14 @@@ admin.site.register(User, UserAdmin
  admin.site.unregister(Group)
  
  admin.site.register(Site, SiteAdmin)
 -admin.site.register(SitePrivilege, SitePrivilegeAdmin)
 +#admin.site.register(SitePrivilege, SitePrivilegeAdmin)
  admin.site.register(Slice, SliceAdmin)
 -admin.site.register(SliceMembership, SliceMembershipAdmin)
 -admin.site.register(Node, NodeAdmin)
 +#admin.site.register(SliceMembership, SliceMembershipAdmin)
- admin.site.register(Subnet, SubnetAdmin)
- #admin.site.register(Image, ImageAdmin)
++#admin.site.register(Subnet, SubnetAdmin)
++admin.site.register(Image, ImageAdmin)
 +#admin.site.register(Node, NodeAdmin)
  admin.site.register(Sliver, SliverAdmin)
  admin.site.register(Key, KeyAdmin)
 -admin.site.register(Role, RoleAdmin)
 +#admin.site.register(Role, RoleAdmin)
  admin.site.register(DeploymentNetwork, DeploymentNetworkAdmin)
  
@@@ -1,8 -1,6 +1,6 @@@
  from types import StringTypes
- from openstack.client import OpenStackClient
- from openstack.driver import OpenStackDriver
- from core.api.auth import auth_check
 +from core.models import DeploymentNetwork
+ from django.contrib.auth import authenticate
 -from plstackapi.core.models import DeploymentNetwork
  
  def _get_deployment_networks(filter):
      if isinstance(filter, StringTypes) and filter.isdigit():
@@@ -1,8 -1,6 +1,6 @@@
  from types import StringTypes
- from openstack.client import OpenStackClient
- from openstack.driver import OpenStackDriver
- from core.api.auth import auth_check
+ from django.contrib.auth import authenticate
 -from plstackapi.core.models import Image
 +from core.models import Image
   
  def _get_images(filter):
      if isinstance(filter, StringTypes) and filter.isdigit():
@@@ -1,9 -1,8 +1,8 @@@
  from types import StringTypes
- from openstack.client import OpenStackClient
- from openstack.driver import OpenStackDriver
+ from django.contrib.auth import authenticate
 -from plstackapi.openstack.manager import OpenStackManager
 -from plstackapi.core.models import Key
 -from plstackapi.core.api.users import _get_users
++from openstack.manager import OpenStackManager
 +from core.models import Key
- from core.api.auth import auth_check
 +from core.api.users import _get_users
  
  
  def _get_keys(filter):
@@@ -1,8 -1,6 +1,6 @@@
  from types import StringTypes
- from openstack.client import OpenStackClient
- from openstack.driver import OpenStackDriver
- from core.api.auth import auth_check
+ from django.contrib.auth import authenticate
 -from plstackapi.core.models import Node
 +from core.models import Node
   
  def _get_nodes(filter):
      if isinstance(filter, StringTypes) and filter.isdigit():
@@@ -1,8 -1,7 +1,7 @@@
  from types import StringTypes
- from openstack.client import OpenStackClient
- from openstack.driver import OpenStackDriver
- from core.api.auth import auth_check
+ from django.contrib.auth import authenticate
 -from plstackapi.openstack.manager import OpenStackManager
 -from plstackapi.core.models import Role
++from openstack.manager import OpenStackManager
 +from core.models import Role
   
  
  def _get_roles(filter):
@@@ -1,12 -1,11 +1,11 @@@
  from types import StringTypes
  import re
- from openstack.client import OpenStackClient
- from openstack.driver import OpenStackDriver
- from core.api.auth import auth_check
+ from django.contrib.auth import authenticate
 -from plstackapi.openstack.manager import OpenStackManager
 -from plstackapi.core.models import SitePrivilege
 -from plstackapi.core.api.users import _get_users
 -from plstackapi.core.api.sites import _get_sites
 -from plstackapi.core.api.roles import _get_roles
++from openstack.manager import OpenStackManager
 +from core.models import SitePrivilege
 +from core.api.users import _get_users
 +from core.api.sites import _get_sites
 +from core.api.roles import _get_roles
  
  
  def _get_site_privileges(filter):
@@@ -1,10 -1,9 +1,8 @@@
  from types import StringTypes
- from openstack.client import OpenStackClient
- from openstack.driver import OpenStackDriver
- from core.api.auth import auth_check
+ from django.contrib.auth import authenticate
 -from plstackapi.openstack.manager import OpenStackManager    
 -from plstackapi.core.models import Site
 -
++from openstack.manager import OpenStackManager    
 +from core.models import Site
  
  def _get_sites(filter):
      if isinstance(filter, StringTypes) and filter.isdigit():
          filter = int(filter)
@@@ -1,11 -1,10 +1,10 @@@
  from types import StringTypes
- from openstack.client import OpenStackClient
- from openstack.driver import OpenStackDriver
- from core.api.auth import auth_check
+ from django.contrib.auth import authenticate
 -from plstackapi.openstack.manager import OpenStackManager
 -from plstackapi.core.models import SliceMembership
 -from plstackapi.core.api.users import _get_users
 -from plstackapi.core.api.slices import _get_slices
 -from plstackapi.core.api.roles import _get_roles
++from openstack.manager import OpenStackManager
 +from core.models import SliceMembership
 +from core.api.users import _get_users
 +from core.api.slices import _get_slices
 +from core.api.roles import _get_roles
  
  def _get_slice_memberships(filter):
      if isinstance(filter, StringTypes) and filter.isdigit():
@@@ -1,11 -1,9 +1,9 @@@
  import re
  from types import StringTypes
  from django.contrib.auth import authenticate
- from openstack.client import OpenStackClient
- from openstack.driver import OpenStackDriver
- from core.api.auth import auth_check
 -from plstackapi.openstack.manager import OpenStackManager
 -from plstackapi.core.models import Slice
 -from plstackapi.core.api.sites import _get_sites
++from openstack.manager import OpenStackManager
 +from core.models import Slice
 +from core.api.sites import _get_sites
  
  def _get_slices(filter):
      if isinstance(filter, StringTypes) and filter.isdigit():
@@@ -1,15 -1,13 +1,12 @@@
  from types import StringTypes
  from django.contrib.auth import authenticate
- from openstack.client import OpenStackClient
- from openstack.driver import OpenStackDriver
- from core.api.auth import auth_check
 -from plstackapi.openstack.manager import OpenStackManager
 -from plstackapi.core.models import Sliver, Slice
 -from plstackapi.core.api.images import _get_images
 -from plstackapi.core.api.keys import _get_keys
 -from plstackapi.core.api.slices import _get_slices
 -from plstackapi.core.api.deployment_networks import _get_deployment_networks
 -from plstackapi.core.api.nodes import _get_nodes
 - 
++from openstack.manager import OpenStackManager
 +from core.models import Sliver, Slice
 +from core.api.images import _get_images
 +from core.api.keys import _get_keys
 +from core.api.slices import _get_slices
 +from core.api.deployment_networks import _get_deployment_networks
 +from core.api.nodes import _get_nodes
-  
  
  def _get_slivers(filter):
      if isinstance(filter, StringTypes) and filter.isdigit():
@@@ -1,9 -1,8 +1,8 @@@
  from types import StringTypes
- from openstack.client import OpenStackClient
- from openstack.driver import OpenStackDriver
- from core.api.auth import auth_check
+ from django.contrib.auth import authenticate
 -from plstackapi.openstack.manager import OpenStackManager    
 -from plstackapi.core.models import PLUser, Site
 -from plstackapi.core.api.sites import _get_sites
++from openstack.manager import OpenStackManager    
 +from core.models import User, Site
 +from core.api.sites import _get_sites
  
  def _get_users(filter):
      if isinstance(filter, StringTypes) and filter.isdigit():
      return users 
  
  def add_user(auth, fields):
-     driver = OpenStackDriver(client = auth_check(auth))
+     user = authenticate(username=auth.get('username'),
+                         password=auth.get('password'))
+     auth['tenant'] = user.site.login_base
      sites = _get_sites(fields.get('site')) 
      if sites: fields['site'] = sites[0]     
 -    user = PLUser(**fields)
 +    user = User(**fields)
-     nova_fields = {'name': user.email[:user.email.find('@')],
-                    'email': user.email, 
-                    'password': fields.get('password'),
-                    'enabled': user.enabled}    
-     nova_user = driver.create_user(**nova_fields)
-     #driver.add_user_user(user.id, user.site.tenant_id, 'user')
-     user.kuser_id=nova_user.id
+     user.os_manager = OpenStackManager(auth=auth, caller = user)
      user.save()
      return user
  
  def update_user(auth, id, **fields):
-     driver = OpenStackDriver(client = auth_check(auth))
+     user = authenticate(username=auth.get('username'),
+                         password=auth.get('password'))
+     auth['tenant'] = user.site.login_base
 -    users = PLUser.objects.filter(id=id)
 +    users = User.objects.filter(id=id)
      if not users:
          return
  
          nova_fields['password'] = fields['password']
      if 'enabled' in fields:
          nova_fields['enabled'] = fields['enabled']
-     driver.update_user(user.kuser_id, **nova_fields)
 -
 -    
      sites = _get_sites(fields.get('site'))
      if sites: fields['site'] = sites[0]
-     user.update(**fields)
+     user.os_manager = OpenStackManager(auth=auth, caller = user)
+     for (k,v) in fields.items():
+         setattr(user, k, v)    
+     user.save()
      return user 
  
  def delete_user(auth, filter={}):
index 98cfb9b,0000000..7a8b322
mode 100644,000000..100644
--- /dev/null
@@@ -1,23 -1,0 +1,29 @@@
 +import os
 +from django.db import models
 +from core.models import PlCoreBase
++from openstack.manager import OpenStackManager
++
 +
 +# Create your models here.
 +
 +class Key(PlCoreBase):
-     name = models.CharField(max_length=256, unique=True)
-     nkey_id = models.CharField(max_length=256, unique=True)
++    name = models.CharField(max_length=256)
++    nkey_id = models.CharField(null=True, blank=True, max_length=256, unique=True)
 +    key = models.CharField(max_length=512)
 +    type = models.CharField(max_length=256)
 +    blacklisted = models.BooleanField(default=False)
 +
-     def __unicode__(self):  return u'%s' % (self.name)
++    def __unicode__(self):  return u'%s' % (self.key)
 +
 +    def save(self, *args, **kwds):
-         self.os_manager.save_key(self)
++        if not hasattr(self, 'os_manager'):
++            setattr(self, 'os_manager', OpenStackManager())
++            self.os_manager.save_key(self)
 +        super(Key, self).save(*args, **kwds)
 +
 +    def delete(self, *args, **kwds):
-         self.os_manager.delete_key(self)
++        if not hasattr(self, 'os_manager'):
++            setattr(self, 'os_manager', OpenStackManager())
++            self.os_manager.delete_key(self)
 +        super(Key, self).delete(*args, **kwds) 
 +    
@@@ -10,5 -11,36 +11,36 @@@ class PlCoreBase(models.Model)
          abstract = True
          app_label = "core"
  
+     def __init__(self, *args, **kwargs):
+         super(PlCoreBase, self).__init__(*args, **kwargs)
+         self.__initial = self._dict
+     @property
 -    def diff(self):\r
 -        d1 = self.__initial\r
 -        d2 = self._dict\r
 -        diffs = [(k, (v, d2[k])) for k, v in d1.items() if v != d2[k]]\r
 -        return dict(diffs)\r
 -\r
 -    @property\r
 -    def has_changed(self):\r
 -        return bool(self.diff)\r
 -\r
 -    @property\r
 -    def changed_fields(self):\r
 -        return self.diff.keys()\r
 -\r
 -    def get_field_diff(self, field_name):\r
 -        return self.diff.get(field_name, None)\r
 -\r
 -    def save(self, *args, **kwargs):\r
 -        super(PlCoreBase, self).save(*args, **kwargs)\r
++    def diff(self):
++        d1 = self.__initial
++        d2 = self._dict
++        diffs = [(k, (v, d2[k])) for k, v in d1.items() if v != d2[k]]
++        return dict(diffs)
++
++    @property
++    def has_changed(self):
++        return bool(self.diff)
++
++    @property
++    def changed_fields(self):
++        return self.diff.keys()
++
++    def get_field_diff(self, field_name):
++        return self.diff.get(field_name, None)
++
++    def save(self, *args, **kwargs):
++        super(PlCoreBase, self).save(*args, **kwargs)
+         self.__initial = self._dict
+     @property
 -    def _dict(self):\r
 -        return model_to_dict(self, fields=[field.name for field in\r
++    def _dict(self):
++        return model_to_dict(self, fields=[field.name for field in
+                              self._meta.fields])
  
  
@@@ -1,7 -1,7 +1,8 @@@
  import os
  import datetime
  from django.db import models
 -from plstackapi.core.models import PlCoreBase
 +from core.models import PlCoreBase
++from openstack.manager import OpenStackManager
  
  class Role(PlCoreBase):
  
  
  
      def save(self, *args, **kwds):
--        self.os_manager.save_role(self)
++        if not hasattr(self, 'os_manager'):
++            setattr(self, 'os_manager', OpenStackManager())
++            self.os_manager.save_role(self)
          super(Role, self).save(*args, **kwds)
      
      def delete(self, *args, **kwds):
--        self.os_manager.delete_role(self)   
++        if not hasattr(self, 'os_manager'):
++            setattr(self, 'os_manager', OpenStackManager())
++            self.os_manager.delete_role(self)   
          super(Role, self).delete(*args, **kwds)
              
@@@ -1,8 -1,8 +1,8 @@@
  import os
  from django.db import models
 -from plstackapi.core.models import PlCoreBase
 -from plstackapi.core.models import DeploymentNetwork
 -
 +from core.models import PlCoreBase
 +from core.models import DeploymentNetwork
++from openstack.manager import OpenStackManager
  
  class Site(PlCoreBase):
  
      def __unicode__(self):  return u'%s' % (self.name)
  
      def save(self, *args, **kwds):
--        self.os_manager.save_site(self)
++        if not hasattr(self, 'os_manager'):
++            setattr(self, 'os_manager', OpenStackManager())
++            self.os_manager.save_site(self)
          super(Site, self).save(*args, **kwds)               
  
  
      def delete(self, *args, **kwds):
--        self.os_manager.delete_site(self)
++        if not hasattr(self, 'os_manager'):
++            setattr(self, 'os_manager', OpenStackManager())
++            self.os_manager.delete_site(self)
          super(Site, self).delete(*args, **kwds)         
          
  
@@@ -39,11 -39,11 +43,15 @@@ class SitePrivilege(PlCoreBase)
      def __unicode__(self):  return u'%s %s %s' % (self.site, self.user, self.role)
  
      def save(self, *args, **kwds):
-         self.os_manager.driver.add_user_role(self.user.kuser_id, self.site.tenant_id, self.role.role_type)
 -        self.os_manager.driver.add_user_role(self.user.user_id, self.site.tenant_id, self.role.role_type)
++        if not hasattr(self, 'os_manager'):
++            setattr(self, 'os_manager', OpenStackManager())
++            self.os_manager.driver.add_user_role(self.user.kuser_id, self.site.tenant_id, self.role.role_type)
          super(SitePrivilege, self).save(*args, **kwds)
  
      def delete(self, *args, **kwds):
-         self.os_manager.driver.delete_user_role(self.user.kuser_id, self.site.tenant_id, self.role.role_type)
 -        self.os_manager.driver.delete_user_role(self.user.user_id, self.site.tenant_id, self.role.role_type)
++        if not hasattr(self, 'os_manager'):
++            setattr(self, 'os_manager', OpenStackManager())
++            self.os_manager.driver.delete_user_role(self.user.kuser_id, self.site.tenant_id, self.role.role_type)
          super(SitePrivilege, self).delete(*args, **kwds)
  
  
@@@ -1,11 -1,10 +1,12 @@@
  import os
  from django.db import models
 -from plstackapi.core.models import PlCoreBase
 -from plstackapi.core.models import Site
 -from plstackapi.core.models import PLUser
 -from plstackapi.core.models import Role
 -from plstackapi.core.models import DeploymentNetwork
 +from core.models import PlCoreBase
 +from core.models import Site
 +from core.models import User
 +from core.models import Role
 +from core.models import DeploymentNetwork
++from openstack.manager import OpenStackManager
 +
  # Create your models here.
  
  class Slice(PlCoreBase):
      def __unicode__(self):  return u'%s' % (self.name)
  
      def save(self, *args, **kwds):
--        self.os_manager.save_slice(self)
++        if not hasattr(self, 'os_manager'):
++            setattr(self, 'os_manager', OpenStackManager())
++            self.os_manager.save_slice(self)
          super(Slice, self).save(*args, **kwds)
  
      def delete(self, *args, **kwds):
--        self.os_manager.delete_slice(self)
++        if not hasattr(self, 'os_manager'):
++            setattr(self, 'os_manager', OpenStackManager())
++            self.os_manager.delete_slice(self)
          super(Slice, self).delete(*args, **kwds)    
  
  class SliceMembership(PlCoreBase):
      def __unicode__(self):  return u'%s %s %s' % (self.slice, self.user, self.role)
  
      def save(self, *args, **kwds):
-         self.os_manager.driver.add_user_role(self.user.kuser_id, self.slice.tenant_id, self.role.role_type)
 -        self.os_manager.driver.add_user_role(self.user.user_id, self.slice.tenant_id, self.role.role_type)
++        if not hasattr(self, 'os_manager'):
++            setattr(self, 'os_manager', OpenStackManager())
++            self.os_manager.driver.add_user_role(self.user.kuser_id, self.slice.tenant_id, self.role.role_type)
          super(SliceMembership, self).save(*args, **kwds)
  
      def delete(self, *args, **kwds):
-         self.os_manager.driver.delete_user_role(self.user.kuser_id, self.slice.tenant_id, self.role.role_type)
 -        self.os_manager.driver.delete_user_role(self.user.user_id, self.slice.tenant_id, self.role.role_type)
++        if not hasattr(self, 'os_manager'):
++            setattr(self, 'os_manager', OpenStackManager())
++            self.os_manager.driver.delete_user_role(self.user.kuser_id, self.slice.tenant_id, self.role.role_type)
          super(SliceMembership, self).delete(*args, **kwds)
@@@ -1,13 -1,13 +1,14 @@@
  import os
  from django.db import models
  from django.core import exceptions
 -from plstackapi.core.models import PlCoreBase
 -from plstackapi.core.models import Image
 -from plstackapi.core.models import Key
 -from plstackapi.core.models import Slice
 -from plstackapi.core.models import Node
 -from plstackapi.core.models import Site
 -from plstackapi.core.models import DeploymentNetwork
 +from core.models import PlCoreBase
 +from core.models import Image
 +from core.models import Key
 +from core.models import Slice
 +from core.models import Node
 +from core.models import Site
 +from core.models import DeploymentNetwork
++from openstack.manager import OpenStackManager
  
  # Create your models here.
  class Sliver(PlCoreBase):
      slice = models.ForeignKey(Slice, related_name='slivers')
      node = models.ForeignKey(Node, related_name='slivers')
      deploymentNetwork = models.ForeignKey(DeploymentNetwork, verbose_name='deployment', related_name='sliver_deploymentNetwork')
-     numberCores = models.IntegerField(verbose_name="Number of Cores", help_text="Number of cores for sliver", default=2)
+     numberCores = models.IntegerField(verbose_name="Number of Cores", help_text="Number of cores for sliver", default=0)
  
  
 -    def __unicode__(self):  return u'%s::%s::%s' % (self.deploymentNetwork, self.slice, self.node.name)
 +    def __unicode__(self):  return u'%s' % (self.instance_name)
  
      def save(self, *args, **kwds):
-         if not self.slice.subnet.exists():
-             raise exceptions.ValidationError, "Slice %s has no subnet" % self.slice.name
-         self.os_manager.save_sliver(self)
+         if not self.name:
+             self.name = self.slice.name
 -        self.os_manager.save_sliver(self)
++        if not hasattr(self, 'os_manager'):
++            setattr(self, 'os_manager', OpenStackManager())
++            self.os_manager.save_sliver(self)
          super(Sliver, self).save(*args, **kwds)
  
      def delete(self, *args, **kwds):
--        self.os_manager.delete_sliver(self)
++        if not hasattr(self, 'os_manager'):
++            setattr(self, 'os_manager', OpenStackManager())
++            self.os_manager.delete_sliver(self)
          super(Sliver, self).delete(*args, **kwds)
index cad9fea,0000000..37175ec
mode 100644,000000..100644
--- /dev/null
@@@ -1,25 -1,0 +1,30 @@@
 +import os
 +import commands    
 +from django.db import models
 +from core.models import PlCoreBase
 +from core.models import Slice
++from openstack.manager import OpenStackManager
 +
 +# Create your models here.
 +
 +class Subnet(PlCoreBase):
 +    subnet_id = models.CharField(max_length=256, unique=True)
 +    cidr = models.CharField(max_length=20)
 +    ip_version = models.IntegerField()
 +    start = models.IPAddressField()
 +    end = models.IPAddressField()
 +    slice = models.ForeignKey(Slice, related_name='subnet')
 +
 +    def __unicode__(self):  return u'%s' % (self.slice.name)
 +
 +    def save(self, *args, **kwds):
-         self.os_manager.save_subnet(self)
++        if not hasattr(self, 'os_manager'):
++            setattr(self, 'os_manager', OpenStackManager())
++            self.os_manager.save_subnet(self)
 +        super(Subnet, self).save(*args, **kwds)
 +
 +    def delete(self, *args, **kwds):
-         self.os_manager.delete_subnet(self)
++        if not hasattr(self, 'os_manager'):
++            setattr(self, 'os_manager', OpenStackManager())
++            self.os_manager.delete_subnet(self)
 +        super(Subnet, self).delete(*args, **kwds)
@@@ -1,16 -1,15 +1,15 @@@
  import os
  import datetime
+ from collections import defaultdict
  from django.db import models
 -from plstackapi.core.models import PlCoreBase
 -from plstackapi.core.models import Site
 -from plstackapi.openstack.manager import OpenStackManager
 -from django.contrib.auth.models import User, AbstractBaseUser, UserManager, BaseUserManager
 +from core.models import PlCoreBase
 +from core.models import Site
 +from core.models import Key
 +from openstack.manager import OpenStackManager
 +from django.contrib.auth.models import AbstractBaseUser, BaseUserManager
  
  # Create your models here.
- has_openstack = False
--
 -class PLUserManager(BaseUserManager):
 +class UserManager(BaseUserManager):
      def create_user(self, email, firstname, lastname, password=None):
          """
          Creates and saves a User with the given email, date of
@@@ -57,7 -56,7 +56,7 @@@ class User(AbstractBaseUser)
          db_index=True,
      )
  
-     kuser_id = models.CharField(help_text="keystone user id", max_length=200) 
 -    user_id = models.CharField(null=True, blank=True, help_text="keystone user id", max_length=200) 
++    kuser_id = models.CharField(null=True, blank=True, help_text="keystone user id", max_length=200) 
      firstname = models.CharField(help_text="person's given name", max_length=200)
      lastname = models.CharField(help_text="person's surname", max_length=200)
  
          # Simplest possible answer: Yes, always
          return True
  
-     @property
-     def is_staff(self):
-         "Is the user a member of staff?"
-         # Simplest possible answer: All admins are staff
-         return self.is_admin
+     def get_roles(self):
+         from plstackapi.core.models.site import SitePrivilege
+         from plstackapi.core.models.slice import SliceMembership
  
+         site_privileges = SitePrivilege.objects.filter(user=self)
+         slice_memberships = SliceMembership.objects.filter(user=self)
+         roles = defaultdict(list)
+         for site_privilege in site_privileges:
+             roles[site_privilege.role.role_type].append(site_privilege.site.login_base)
+         for slice_membership in slice_memberships:
+             roles[slice_membership.role.role_type].append(slice_membership.slice.name)
+         return roles   
  
      def save(self, *args, **kwds):
-         if has_openstack:
-             if not hasattr(self, 'os_manager'):
-                 setattr(self, 'os_manager', OpenStackManager())
+         if not hasattr(self, 'os_manager'):
+             setattr(self, 'os_manager', OpenStackManager())
 +            self.os_manager.save_user(self)
 -        self.os_manager.save_user(self)
          if not self.id:
              self.set_password(self.password)    
 -        super(PLUser, self).save(*args, **kwds)   
 +        super(User, self).save(*args, **kwds)   
  
      def delete(self, *args, **kwds):
-         if has_openstack:
-             if not hasattr(self, 'os_manager'):
-                 setattr(self, 'os_manager', OpenStackManager())
+         if not hasattr(self, 'os_manager'):
+             setattr(self, 'os_manager', OpenStackManager())
 +            self.os_manager.delete_user(self)
 -        self.os_manager.delete_user(self)
 -        super(PLUser, self).delete(*args, **kwds)    
 +        super(User, self).delete(*args, **kwds)    
Simple merge
@@@ -1,7 -1,7 +1,8 @@@
+ import commands
 -from plstackapi.planetstack.config import Config
 -from plstackapi.openstack.client import OpenStackClient
 +from planetstack.config import Config
 +from openstack.client import OpenStackClient
  
 +has_openstack = False
  class OpenStackDriver:
  
      def __init__(self, config = None, client=None): 
@@@ -1,14 -1,12 +1,11 @@@
+ from netaddr import IPAddress, IPNetwork
 -from plstackapi.planetstack import settings
 +from planetstack import settings
- #from django.core import management
- #management.setup_environ(settings)
- import os
- os.environ.setdefault("DJANGO_SETTINGS_MODULE", "planetstack.settings")
+ from django.core import management
 -management.setup_environ(settings)
 -from plstackapi.planetstack.config import Config
++from planetstack.config import Config
  try:
 -    from plstackapi.openstack.client import OpenStackClient
 -    from plstackapi.openstack.driver import OpenStackDriver
 -    from plstackapi.core.models import * 
 +    from openstack.client import OpenStackClient
 +    from openstack.driver import OpenStackDriver
-     from planetstack.config import Config
 +    from core.models import * 
      has_openstack = True
  except:
      has_openstack = False
@@@ -28,17 -25,39 +25,39 @@@ def require_enabled(callable)
  class OpenStackManager:
  
      def __init__(self, auth={}, caller=None):
-         if auth:
-             self.client = OpenStackClient(**auth)
-         else:
-             self.client = OpenStackClient()   
+         self.client = None
+         self.driver = None
+         self.caller = None
          self.has_openstack = has_openstack       
-         self.enabled = manager_enabled 
-         self.driver = OpenStackDriver(client=self.client) 
-         self.caller=caller
-         if not self.caller:
-             self.caller = self.driver.admin_user
-             self.caller.kuser_id = self.caller.id 
+         self.enabled = manager_enabled
+         if has_openstack and manager_enabled:
+             if auth:
+                 try:
+                     self.init_user(auth, caller)
+                 except:
+                     # if this fails then it meanse the caller doesn't have a
+                     # role at the slice's tenant. if the caller is an admin
+                     # just use the admin client/manager.
+                     if caller and caller.is_admin: 
+                         self.init_admin()
+                     else: raise
+             else:
+                 self.init_admin()
+     @require_enabled 
+     def init_user(self, auth, caller):
+         self.client = OpenStackClient(**auth)
+         self.driver = OpenStackDriver(client=self.client)
+         self.caller = caller                 
+     
+     @require_enabled
+     def init_admin(self):
+         # use the admin credentials 
+         self.client = OpenStackClient()
+         self.driver = OpenStackDriver(client=self.client)
+         self.caller = self.driver.admin_user
 -        self.caller.user_id = self.caller.id 
++        self.caller.kuser_id = self.caller.id 
  
      @require_enabled
      def save_role(self, role):
                             'password': user.password,
                             'enabled': True}
              keystone_user = self.driver.create_user(**user_fields)
 -            user.user_id = keystone_user.id
 +            user.kuser_id = keystone_user.id
-     
+         if user.site:
 -            self.driver.add_user_role(user.user_id, user.site.tenant_id, 'user')
++            self.driver.add_user_role(user.kuser_id, user.site.tenant_id, 'user')
+             if user.is_admin:
 -                self.driver.add_user_role(user.user_id, user.site.tenant_id, 'admin')
++                self.driver.add_user_role(user.kuser_id, user.site.tenant_id, 'admin')
+             else:
+                 # may have admin role so attempt to remove it
 -                self.driver.delete_user_role(user.user_id, user.site.tenant_id, 'admin')
++                self.driver.delete_user_role(user.kuser_id, user.site.tenant_id, 'admin')
+   
      @require_enabled
      def delete_user(self, user):
 -        if user.user_id:
 -            self.driver.delete_user(user.user_id)        
 +        if user.kuser_id:
 +            self.driver.delete_user(user.kuser_id)        
      
-     
      @require_enabled
      def save_site(self, site, add_role=True):
          if not site.tenant_id:
@@@ -2,20 -2,19 +2,19 @@@ from django.conf.urls import patterns, 
  
  # Uncomment the next two lines to enable the admin:
  from django.contrib import admin
 -from plstackapi.core.views.roles import RoleListCreate, RoleRetrieveUpdateDestroy
 -from plstackapi.core.views.sites import SiteListCreate, SiteRetrieveUpdateDestroy
 -from plstackapi.core.views.site_privileges import SitePrivilegeListCreate, SitePrivilegeRetrieveUpdateDestroy
 -from plstackapi.core.views.users import UserListCreate, UserRetrieveUpdateDestroy
 -from plstackapi.core.views.slices import SliceListCreate, SliceRetrieveUpdateDestroy
 -from plstackapi.core.views.slice_memberships import SliceMembershipListCreate, SliceMembershipRetrieveUpdateDestroy
 -from plstackapi.core.views.slivers import SliverListCreate, SliverRetrieveUpdateDestroy
 -from plstackapi.core.views.keys import KeyListCreate, KeyRetrieveUpdateDestroy
 -from plstackapi.core.views.deployment_networks import DeploymentNetworkListCreate, DeploymentNetworkRetrieveUpdateDestroy
 -from plstackapi.core.views.images import ImageListCreate, ImageRetrieveUpdateDestroy
 -from plstackapi.core.views.nodes import NodeListCreate, NodeRetrieveUpdateDestroy
 -from plstackapi.core.models import Site
 -from plstackapi.core.api_root import api_root
 +from core.views.roles import RoleListCreate, RoleRetrieveUpdateDestroy
 +from core.views.sites import SiteListCreate, SiteRetrieveUpdateDestroy
 +from core.views.site_privileges import SitePrivilegeListCreate, SitePrivilegeRetrieveUpdateDestroy
 +from core.views.users import UserListCreate, UserRetrieveUpdateDestroy
 +from core.views.slices import SliceListCreate, SliceRetrieveUpdateDestroy
 +from core.views.slice_memberships import SliceMembershipListCreate, SliceMembershipRetrieveUpdateDestroy
- from core.views.subnets import SubnetListCreate, SubnetRetrieveUpdateDestroy
 +from core.views.slivers import SliverListCreate, SliverRetrieveUpdateDestroy
 +from core.views.keys import KeyListCreate, KeyRetrieveUpdateDestroy
 +from core.views.deployment_networks import DeploymentNetworkListCreate, DeploymentNetworkRetrieveUpdateDestroy
 +from core.views.images import ImageListCreate, ImageRetrieveUpdateDestroy
 +from core.views.nodes import NodeListCreate, NodeRetrieveUpdateDestroy
 +from core.models import Site
 +from core.api_root import api_root
  from rest_framework import generics
  
  admin.autodiscover()