Skip to content

Commit

Permalink
fix bug lp:1019230,update rpc from openstack-common.
Browse files Browse the repository at this point in the history
Change-Id: I498e578affef4fa9df6c50cd7284ebd7e3f9a64e
  • Loading branch information
yaguangtang committed Jul 10, 2012
1 parent bab54aa commit 6f99f4f
Show file tree
Hide file tree
Showing 20 changed files with 4,006 additions and 9 deletions.
2 changes: 1 addition & 1 deletion openstack-common.conf
@@ -1,7 +1,7 @@
[DEFAULT]

# The list of modules to copy from openstack-common
modules=cfg,exception,importutils,iniparser,jsonutils,policy,setup
modules=cfg,context,exception,excutils,gettextutils,importutils,iniparser,jsonutils,local,policy,rpc,setup

# The base module to hold the copy of openstack.common
base=quantum
81 changes: 81 additions & 0 deletions quantum/openstack/common/context.py
@@ -0,0 +1,81 @@
# vim: tabstop=4 shiftwidth=4 softtabstop=4

# Copyright 2011 OpenStack LLC.
# All Rights Reserved.
#
# 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.

"""
Simple class that stores security context information in the web request.
Projects should subclass this class if they wish to enhance the request
context or provide additional information in their specific WSGI pipeline.
"""

import itertools
import uuid


def generate_request_id():
return 'req-' + str(uuid.uuid4())


class RequestContext(object):

"""
Stores information about the security context under which the user
accesses the system, as well as additional request information.
"""

def __init__(self, auth_tok=None, user=None, tenant=None, is_admin=False,
read_only=False, show_deleted=False, request_id=None):
self.auth_tok = auth_tok
self.user = user
self.tenant = tenant
self.is_admin = is_admin
self.read_only = read_only
self.show_deleted = show_deleted
if not request_id:
request_id = generate_request_id()
self.request_id = request_id

def to_dict(self):
return {'user': self.user,
'tenant': self.tenant,
'is_admin': self.is_admin,
'read_only': self.read_only,
'show_deleted': self.show_deleted,
'auth_token': self.auth_tok,
'request_id': self.request_id}


def get_admin_context(show_deleted="no"):
context = RequestContext(None,
tenant=None,
is_admin=True,
show_deleted=show_deleted)
return context


def get_context_from_function_and_args(function, args, kwargs):
"""Find an arg of type RequestContext 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, RequestContext):
return arg

return None
1 change: 1 addition & 0 deletions quantum/openstack/common/exception.py
Expand Up @@ -19,6 +19,7 @@
Exceptions common to OpenStack projects
"""

import itertools
import logging


Expand Down
49 changes: 49 additions & 0 deletions quantum/openstack/common/excutils.py
@@ -0,0 +1,49 @@
# vim: tabstop=4 shiftwidth=4 softtabstop=4

# Copyright 2011 OpenStack LLC.
# Copyright 2012, Red Hat, Inc.
#
# 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.

"""
Exception related utilities.
"""

import contextlib
import logging
import sys
import traceback


@contextlib.contextmanager
def save_and_reraise_exception():
"""Save current exception, run some code and then re-raise.
In some cases the exception context can be cleared, resulting in None
being attempted to be reraised after an exception handler is run. This
can happen when eventlet switches greenthreads or when running an
exception handler, code raises and catches an exception. In both
cases the exception context will be cleared.
To work around this, we save the exception state, run handler code, and
then re-raise the original exception. If another exception occurs, the
saved exception is logged and the new exception is reraised.
"""
type_, value, tb = sys.exc_info()
try:
yield
except Exception:
logging.error('Original exception being dropped: %s' %
(traceback.format_exception(type_, value, tb)))
raise
raise type_, value, tb
33 changes: 33 additions & 0 deletions quantum/openstack/common/gettextutils.py
@@ -0,0 +1,33 @@
# vim: tabstop=4 shiftwidth=4 softtabstop=4

# Copyright 2012 Red Hat, Inc.
# All Rights Reserved.
#
# 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.

"""
gettext for openstack-common modules.
Usual usage in an openstack.common module:
from openstack.common.gettextutils import _
"""

import gettext


t = gettext.translation('openstack-common', 'locale', fallback=True)


def _(msg):
return t.ugettext(msg)
15 changes: 14 additions & 1 deletion quantum/openstack/common/importutils.py
Expand Up @@ -30,14 +30,27 @@ def import_class(import_str):
return getattr(sys.modules[mod_str], class_str)
except (ImportError, ValueError, AttributeError), exc:
raise ImportError('Class %s cannot be found (%s)' %
(class_str, str(exc)))
(class_str, str(exc)))


def import_object(import_str, *args, **kwargs):
"""Import a class and return an instance of it."""
return import_class(import_str)(*args, **kwargs)


def import_object_ns(name_space, import_str, *args, **kwargs):
"""
Import a class and return an instance of it, first by trying
to find the class in a default namespace, then failing back to
a full path if not found in the default namespace.
"""
import_value = "%s.%s" % (name_space, import_str)
try:
return import_class(import_value)(*args, **kwargs)
except ImportError:
return import_class(import_str)(*args, **kwargs)


def import_module(import_str):
"""Import a module."""
__import__(import_str)
Expand Down
5 changes: 4 additions & 1 deletion quantum/openstack/common/iniparser.py
Expand Up @@ -52,7 +52,10 @@ def _split_key_value(self, line):
else:
key, value = line[:colon], line[colon + 1:]

return key.strip(), [value.strip()]
value = value.strip()
if value[0] == value[-1] and value[0] == "\"" or value[0] == "'":
value = value[1:-1]
return key.strip(), [value]

def parse(self, lineiter):
key = None
Expand Down
11 changes: 9 additions & 2 deletions quantum/openstack/common/jsonutils.py
Expand Up @@ -37,6 +37,7 @@
import inspect
import itertools
import json
import xmlrpclib


def to_primitive(value, convert_instances=False, level=0):
Expand Down Expand Up @@ -81,6 +82,12 @@ def to_primitive(value, convert_instances=False, level=0):
# The try block may not be necessary after the class check above,
# but just in case ...
try:
# It's not clear why xmlrpclib created their own DateTime type, but
# for our purposes, make it a datetime type which is explicitly
# handled
if isinstance(value, xmlrpclib.DateTime):
value = datetime.datetime(*tuple(value.timetuple())[:6])

if isinstance(value, (list, tuple)):
o = []
for v in value:
Expand Down Expand Up @@ -115,8 +122,8 @@ def to_primitive(value, convert_instances=False, level=0):
return unicode(value)


def dumps(value):
return json.dumps(value, default=to_primitive)
def dumps(value, default=to_primitive, **kwargs):
return json.dumps(value, default=default, **kwargs)


def loads(s):
Expand Down
37 changes: 37 additions & 0 deletions quantum/openstack/common/local.py
@@ -0,0 +1,37 @@
# vim: tabstop=4 shiftwidth=4 softtabstop=4

# Copyright 2011 OpenStack LLC.
# All Rights Reserved.
#
# 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.

"""Greenthread local storage of variables using weak references"""

import weakref

from eventlet import corolocal


class WeakLocal(corolocal.local):
def __getattribute__(self, attr):
rval = corolocal.local.__getattribute__(self, attr)
if rval:
rval = rval()
return rval

def __setattr__(self, attr, value):
value = weakref.ref(value)
return corolocal.local.__setattr__(self, attr, value)


store = WeakLocal()
9 changes: 5 additions & 4 deletions quantum/openstack/common/policy.py
Expand Up @@ -17,11 +17,12 @@

"""Common Policy Engine Implementation"""

import json
import logging
import urllib
import urllib2

from quantum.openstack.common import jsonutils


LOG = logging.getLogger(__name__)

Expand Down Expand Up @@ -132,7 +133,7 @@ class Brain(object):
@classmethod
def load_json(cls, data, default_rule=None):
"""Init a brain using json instead of a rules dictionary."""
rules_dict = json.loads(data)
rules_dict = jsonutils.loads(data)
return cls(rules=rules_dict, default_rule=default_rule)

def __init__(self, rules=None, default_rule=None):
Expand Down Expand Up @@ -231,8 +232,8 @@ def _check_http(self, match, target_dict, cred_dict):
"""
url = match % target_dict
data = {'target': json.dumps(target_dict),
'credentials': json.dumps(cred_dict)}
data = {'target': jsonutils.dumps(target_dict),
'credentials': jsonutils.dumps(cred_dict)}
post_data = urllib.urlencode(data)
f = urllib2.urlopen(url, post_data)
return f.read() == "True"

0 comments on commit 6f99f4f

Please sign in to comment.