Subclass OS-Common's Context class

This provides tenant_id and user_id fields for consistency.

Change-Id: Ia9c161a3802d59361a5d2a8788c987e25916a6ac
This commit is contained in:
Kiall Mac Innes
2012-11-13 13:26:15 +00:00
parent 9709bc99f8
commit bddab4fef3
5 changed files with 73 additions and 11 deletions

View File

@@ -1,6 +1,6 @@
{
"admin": "role:admin or is_admin:True",
"admin_or_owner": "rule:admin or tenant:%(tenant)s",
"admin_or_owner": "rule:admin or tenant_id:%(tenant_id)s",
"default": "@"
}

View File

@@ -17,10 +17,10 @@ import os
import subprocess
from moniker.openstack.common import cfg
from moniker.openstack.common import log as logging
from moniker.openstack.common.context import get_admin_context
from moniker import utils
from moniker.backend import base
from moniker.central import api as central_api
from moniker.context import MonikerContext
LOG = logging.getLogger(__name__)
@@ -41,7 +41,7 @@ class Bind9Backend(base.Backend):
super(Bind9Backend, self).start()
# TODO: This is a hack to ensure the data dir is 100% up to date
admin_context = get_admin_context()
admin_context = MonikerContext.get_admin_context()
domains = central_api.get_domains(admin_context)
@@ -80,7 +80,7 @@ class Bind9Backend(base.Backend):
# TODO: Rewrite this entire thing ASAP
LOG.debug('Synchronising domains')
admin_context = get_admin_context()
admin_context = MonikerContext.get_admin_context()
domains = central_api.get_domains(admin_context)
@@ -105,7 +105,7 @@ class Bind9Backend(base.Backend):
# TODO: Rewrite this entire thing ASAP
LOG.debug('Synchronising Domain: %s' % domain['id'])
admin_context = get_admin_context()
admin_context = MonikerContext.get_admin_context()
servers = central_api.get_servers(admin_context)

62
moniker/context.py Normal file
View File

@@ -0,0 +1,62 @@
# Copyright 2012 Managed I.T.
#
# Author: Kiall Mac Innes <kiall@managedit.ie>
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import itertools
from moniker.openstack.common import context
class MonikerContext(context.RequestContext):
def __init__(self, auth_tok=None, user=None, tenant=None, is_admin=False,
read_only=False, show_deleted=False, request_id=None):
super(MonikerContext, self).__init__(
auth_tok=auth_tok,
user=user,
tenant=tenant,
is_admin=is_admin,
read_only=read_only,
show_deleted=show_deleted,
request_id=request_id)
self.user_id = user
self.tenant_id = tenant
def to_dict(self):
d = super(MonikerContext, self).to_dict()
d.update({
'user_id': self.user_id,
'tenant_id': self.tenant_id,
})
return d
@classmethod
def get_admin_context(cls):
return cls(None, tenant=None, is_admin=True)
@classmethod
def get_context_from_function_and_args(cls, function, args, kwargs):
"""
Find an arg of type MonikerContext and return it.
This is useful in a couple of decorators where we don't
know much about the function we're wrapping.
"""
for arg in itertools.chain(kwargs.values(), args):
if isinstance(arg, cls):
return arg
return None

View File

@@ -15,8 +15,8 @@
# under the License.
from moniker.openstack.common import cfg
from moniker.openstack.common import log as logging
from moniker.openstack.common.context import get_admin_context
from moniker import exceptions
from moniker.context import MonikerContext
from moniker.notification_handler.base import Handler
LOG = logging.getLogger(__name__)
@@ -83,7 +83,7 @@ class NovaHandler(Handler):
raise ValueError('NovaHandler recieved an invalid event type')
def handle_instance_create(self, payload):
context = get_admin_context()
context = MonikerContext.get_admin_context()
# Fetch the FixedIP Domain
fixed_ip_domain = self.central_service.get_domain(context,
@@ -110,7 +110,7 @@ class NovaHandler(Handler):
record_values)
def handle_instance_delete(self, payload):
context = get_admin_context()
context = MonikerContext.get_admin_context()
# Fetch the instances managed records
criterion = {

View File

@@ -17,7 +17,7 @@ import unittest2
import mox
from moniker.openstack.common import cfg
from moniker.openstack.common import log as logging
from moniker.openstack.common.context import RequestContext, get_admin_context
from moniker.context import MonikerContext
from moniker import storage
from moniker.central import service as central_service
@@ -58,7 +58,7 @@ class TestCase(unittest2.TestCase):
return central_service.Service()
def get_context(self, **kwargs):
return RequestContext(**kwargs)
return MonikerContext(**kwargs)
def get_admin_context(self):
return get_admin_context()
return MonikerContext.get_admin_context()