Merge "Uses oslo's deprecated decorator; removes ours"

This commit is contained in:
Jenkins 2013-12-17 05:31:01 +00:00 committed by Gerrit Code Review
commit 12ef6153aa
5 changed files with 42 additions and 290 deletions

View File

@ -19,19 +19,20 @@ import functools
import uuid
from keystone.common import dependency
from keystone.common import utils
from keystone.common import wsgi
from keystone import config
from keystone import exception
from keystone.openstack.common import log as logging
from keystone.openstack.common import versionutils
LOG = logging.getLogger(__name__)
CONF = config.CONF
DEFAULT_DOMAIN_ID = CONF.identity.default_domain_id
v2_deprecated = utils.deprecated(what='v2 API',
as_of=utils.deprecated.ICEHOUSE,
in_favor_of='v3 API')
v2_deprecated = versionutils.deprecated(what='v2 API',
as_of=versionutils.deprecated.ICEHOUSE,
in_favor_of='v3 API')
def _build_policy_check_credentials(self, action, context, kwargs):

View File

@ -19,7 +19,6 @@
# under the License.
import calendar
import functools
import grp
import hashlib
import json
@ -515,101 +514,3 @@ def make_dirs(path, mode=None, user=None, group=None, log=None):
raise EnvironmentError("makedirs('%s'): %s" % (path, exc.strerror))
set_permissions(path, mode, user, group, log)
class deprecated(object):
"""A decorator to mark callables as deprecated.
deprecated logs a deprecation message when the callable it decorates
is used. The message will include the release where the callable was
deprecated, the release where is may be removed and possibly an
optional replacement.
Examples:
1. Specifing the required deprecated release
>>> @deprecated(as_of=deprecated.ICEHOUSE)
... def a(): pass
2. Specifing a replacement:
>>> @deprecated(as_of=deprecated.ICEHOUSE, in_favor_of='f()')
... def b(): pass
3. Specifying the release where the functionality may be removed:
>>> @deprecated(as_of=deprecated.ICEHOUSE, remove_in=+1)
... def c(): pass
"""
FOLSOM = 'F'
GRIZZLY = 'G'
HAVANA = 'H'
ICEHOUSE = 'I'
_RELEASES = {
'F': 'Folsom',
'G': 'Grizzly',
'H': 'Havana',
'I': 'Icehouse',
}
_deprecated_msg_with_alternative = _(
'%(what)s is deprecated as of %(as_of)s in favor of '
'%(in_favor_of)s and may be removed in %(remove_in)s.')
_deprecated_msg_no_alternative = _(
'%(what)s is deprecated as of %(as_of)s and may be '
'removed in %(remove_in)s. It will not be superseded.')
def __init__(self, as_of, in_favor_of=None, remove_in=2, what=None):
"""Initialize decorator
:param as_of: the release deprecating the callable. Constants
are define in this class for convenience.
:param in_favor_of: the replacement for the callable (optional)
:param remove_in: an integer specifying how many releases to wait
before removing (default: 2)
:param what: name of the thing being deprecated (default: the
callable's name)
"""
self.as_of = as_of
self.in_favor_of = in_favor_of
self.remove_in = remove_in
self.what = what
def __call__(self, func):
if not self.what:
self.what = func.__name__ + '()'
@functools.wraps(func)
def wrapped(*args, **kwargs):
msg, details = self._build_message()
LOG.deprecated(msg, details)
return func(*args, **kwargs)
return wrapped
def _get_safe_to_remove_release(self, release):
# TODO(dstanek): this method will have to be reimplemented once
# when we get to the X release because once we get to the Y
# release, what is Y+2?
new_release = chr(ord(release) + self.remove_in)
if new_release in self._RELEASES:
return self._RELEASES[new_release]
else:
return new_release
def _build_message(self):
details = dict(what=self.what,
as_of=self._RELEASES[self.as_of],
remove_in=self._get_safe_to_remove_release(self.as_of))
if self.in_favor_of:
details['in_favor_of'] = self.in_favor_of
msg = self._deprecated_msg_with_alternative
else:
msg = self._deprecated_msg_no_alternative
return msg, details

View File

@ -27,12 +27,12 @@ from keystone import clean
from keystone.common import controller
from keystone.common import dependency
from keystone.common import manager
from keystone.common import utils
from keystone import config
from keystone import exception
from keystone import notifications
from keystone.openstack.common import importutils
from keystone.openstack.common import log as logging
from keystone.openstack.common import versionutils
CONF = config.CONF
@ -40,6 +40,15 @@ CONF = config.CONF
LOG = logging.getLogger(__name__)
def moved_to_assignment(f):
name = f.__name__
deprecated = versionutils.deprecated(versionutils.deprecated.ICEHOUSE,
what="identity_api." + name,
in_favor_of="assignment_api." + name,
remove_in=+1)
return deprecated(f)
def filter_user(user_ref):
"""Filter out private items in a user dict.
@ -457,124 +466,97 @@ class Manager(manager.Manager):
# TODO(morganfainberg): Remove the following deprecated methods once
# Icehouse is released. Maintain identity -> assignment proxy for 1
# release.
@utils.deprecated('I', in_favor_of='assignment_api.get_domain_by_name',
remove_in=1, what='identity_api.get_domain_by_name')
@moved_to_assignment
def get_domain_by_name(self, domain_name):
return self.assignment_api.get_domain_by_name(domain_name)
@utils.deprecated('I', in_favor_of='assignment_api.get_domain',
remove_in=1, what='identity_api.get_domain')
@moved_to_assignment
def get_domain(self, domain_id):
return self.assignment_api.get_domain(domain_id)
@utils.deprecated('I', in_favor_of='assignment_api.update_domain',
remove_in=1, what='identity_api.update_domain')
@moved_to_assignment
def update_domain(self, domain_id, domain):
return self.assignment_api.update_domain(domain_id, domain)
@utils.deprecated('I', in_favor_of='assignment_api.list_domains',
remove_in=1, what='identity_api.list_domains')
@moved_to_assignment
def list_domains(self):
return self.assignment_api.list_domains()
@utils.deprecated('I', in_favor_of='assignment_api.delete_domain',
remove_in=1, what='identity_api.delete_domain')
@moved_to_assignment
def delete_domain(self, domain_id):
return self.assignment_api.delete_domain(domain_id)
@utils.deprecated('I', in_favor_of='assignment_api.create_domain',
remove_in=1, what='identity_api.create_domain')
@moved_to_assignment
def create_domain(self, domain_id, domain):
return self.assignment_api.create_domain(domain_id, domain)
@utils.deprecated('I', in_favor_of='assignment_api.list_projects_for_user',
remove_in=1, what='identity_api.list_projects_for_user')
@moved_to_assignment
def list_projects_for_user(self, user_id):
return self.assignment_api.list_projects_for_user(user_id)
@utils.deprecated('I', in_favor_of='assignment_api.add_user_to_project',
remove_in=1, what='identity_api.add_user_to_project')
@moved_to_assignment
def add_user_to_project(self, tenant_id, user_id):
return self.assignment_api.add_user_to_project(tenant_id, user_id)
@utils.deprecated('I',
in_favor_of='assignment_api.remove_user_from_project',
remove_in=1,
what='identity_api.remove_user_from_project')
@moved_to_assignment
def remove_user_from_project(self, tenant_id, user_id):
return self.assignment_api.remove_user_from_project(tenant_id, user_id)
@utils.deprecated('I', in_favor_of='assignment_api.get_project',
remove_in=1, what='identity_api.get_project')
@moved_to_assignment
def get_project(self, tenant_id):
return self.assignment_api.get_project(tenant_id)
@utils.deprecated('I', in_favor_of='assignment_api.list_projects',
remove_in=1, what='identity_api.list_projects')
@moved_to_assignment
def list_projects(self, domain_id=None):
return self.assignment_api.list_projects(domain_id)
@utils.deprecated('I', in_favor_of='assignment_api.get_role',
remove_in=1, what='identity_api.get_role')
@moved_to_assignment
def get_role(self, role_id):
return self.assignment_api.get_role(role_id)
@utils.deprecated('I', in_favor_of='assignment_api.list_roles',
remove_in=1, what='identity_api.list_roles')
@moved_to_assignment
def list_roles(self):
return self.assignment_api.list_roles()
@utils.deprecated('I', in_favor_of='assignment_api.get_project_users',
remove_in=1, what='identity_api.get_project_users')
@moved_to_assignment
def get_project_users(self, tenant_id):
return self.assignment_api.get_project_users(tenant_id)
@utils.deprecated('I', in_favor_of='assignment_api.list_projects_for_user',
remove_in=1, what='identity_api.list_projects_for_user')
@moved_to_assignment
def get_roles_for_user_and_project(self, user_id, tenant_id):
return self.assignment_api.get_roles_for_user_and_project(
user_id, tenant_id)
@utils.deprecated(
'I', in_favor_of='assignment_api.get_roles_for_user_and_domain',
remove_in=1, what='identity_api.get_roles_for_user_and_domain')
@moved_to_assignment
def get_roles_for_user_and_domain(self, user_id, domain_id):
return (self.assignment_api.get_roles_for_user_and_domain
(user_id, domain_id))
@utils.deprecated(
'I', in_favor_of='assignment_api.add_role_to_user_and_project',
remove_in=1, what='identity_api.add_role_to_user_and_project')
@moved_to_assignment
def add_role_to_user_and_project(self, user_id,
tenant_id, role_id):
return (self.assignment_api.add_role_to_user_and_project
(user_id, tenant_id, role_id))
@utils.deprecated('I', in_favor_of='assignment_api.create_role',
remove_in=1, what='identity_api.create_role')
@moved_to_assignment
def create_role(self, role_id, role):
return self.assignment_api.create_role(role_id, role)
@utils.deprecated('I', in_favor_of='assignment_api.delete_role',
remove_in=1, what='identity_api.delete_role')
@moved_to_assignment
def delete_role(self, role_id):
return self.assignment_api.delete_role(role_id)
@utils.deprecated(
'I', in_favor_of='assignment_api.remove_role_from_user_and_project',
remove_in=1, what='identity_api.remove_role_from_user_and_project')
@moved_to_assignment
def remove_role_from_user_and_project(self, user_id,
tenant_id, role_id):
return (self.assignment_api.remove_role_from_user_and_project
(user_id, tenant_id, role_id))
@utils.deprecated('I', in_favor_of='assignment_api.update_role',
remove_in=1, what='identity_api.update_role')
@moved_to_assignment
def update_role(self, role_id, role):
return self.assignment_api.update_role(role_id, role)
@utils.deprecated('I', in_favor_of='assignment_api.create_grant',
remove_in=1, what='identity_api.create_grant')
@moved_to_assignment
def create_grant(self, role_id, user_id=None, group_id=None,
domain_id=None, project_id=None,
inherited_to_projects=False):
@ -582,8 +564,7 @@ class Manager(manager.Manager):
(role_id, user_id, group_id, domain_id, project_id,
inherited_to_projects))
@utils.deprecated('I', in_favor_of='assignment_api.list_grants',
remove_in=1, what='identity_api.list_grants')
@moved_to_assignment
def list_grants(self, user_id=None, group_id=None,
domain_id=None, project_id=None,
inherited_to_projects=False):
@ -591,8 +572,7 @@ class Manager(manager.Manager):
(user_id, group_id, domain_id, project_id,
inherited_to_projects))
@utils.deprecated('I', in_favor_of='assignment_api.get_grant',
remove_in=1, what='identity_api.get_grant')
@moved_to_assignment
def get_grant(self, role_id, user_id=None, group_id=None,
domain_id=None, project_id=None,
inherited_to_projects=False):
@ -600,8 +580,7 @@ class Manager(manager.Manager):
(role_id, user_id, group_id, domain_id, project_id,
inherited_to_projects))
@utils.deprecated('I', in_favor_of='assignment_api.delete_grant',
remove_in=1, what='identity_api.delete_grant')
@moved_to_assignment
def delete_grant(self, role_id, user_id=None, group_id=None,
domain_id=None, project_id=None,
inherited_to_projects=False):

View File

@ -31,13 +31,8 @@
import datetime
import functools
import logging
import os
import time
import uuid
from six import moves
from testtools import matchers
from keystone.common import utils
from keystone import tests
@ -176,127 +171,3 @@ class LimitingReaderTests(tests.TestCase):
self.assertEqual(len(data.read_args), 1)
self.assertEqual(len(data.read_kwargs), 0)
self.assertEqual(data.read_args[0], 10)
class TestDeprecated(tests.TestCase):
def setUp(self):
super(TestDeprecated, self).setUp()
self.deprecated_message = moves.cStringIO()
self.handler = logging.StreamHandler(self.deprecated_message)
self.logger = logging.getLogger('keystone.common.utils')
self.logger.addHandler(self.handler)
def tearDown(self):
super(TestDeprecated, self).tearDown()
self.logger.removeHandler(self.handler)
def test_deprecating_a_function_returns_correct_value(self):
@utils.deprecated(as_of=utils.deprecated.ICEHOUSE)
def do_outdated_stuff(data):
return data
expected_rv = uuid.uuid4().hex
retval = do_outdated_stuff(expected_rv)
self.assertThat(retval, matchers.Equals(expected_rv))
def test_deprecating_a_method_returns_correct_value(self):
class C(object):
@utils.deprecated(as_of=utils.deprecated.ICEHOUSE)
def outdated_method(self, *args):
return args
retval = C().outdated_method(1, 'of anything')
self.assertThat(retval, matchers.Equals((1, 'of anything')))
def test_deprecated_with_unknown_future_release(self):
@utils.deprecated(as_of=utils.deprecated.ICEHOUSE,
in_favor_of='different_stuff()')
def do_outdated_stuff():
return
do_outdated_stuff()
expected = ('do_outdated_stuff() is deprecated as of Icehouse '
'in favor of different_stuff() and may be removed in K.')
self.assertThat(self.deprecated_message.getvalue(),
matchers.Contains(expected))
def test_deprecated_with_known_future_release(self):
@utils.deprecated(as_of=utils.deprecated.GRIZZLY,
in_favor_of='different_stuff()')
def do_outdated_stuff():
return
do_outdated_stuff()
expected = ('do_outdated_stuff() is deprecated as of Grizzly '
'in favor of different_stuff() and may be removed in '
'Icehouse.')
self.assertThat(self.deprecated_message.getvalue(),
matchers.Contains(expected))
def test_deprecated_without_replacement(self):
@utils.deprecated(as_of=utils.deprecated.GRIZZLY)
def do_outdated_stuff():
return
do_outdated_stuff()
expected = ('do_outdated_stuff() is deprecated as of Grizzly '
'and may be removed in Icehouse. It will not be '
'superseded.')
self.assertThat(self.deprecated_message.getvalue(),
matchers.Contains(expected))
def test_deprecated_with_custom_what(self):
@utils.deprecated(as_of=utils.deprecated.GRIZZLY,
what='v2.0 API',
in_favor_of='v3 API')
def do_outdated_stuff():
return
do_outdated_stuff()
expected = ('v2.0 API is deprecated as of Grizzly in favor of '
'v3 API and may be removed in Icehouse.')
self.assertThat(self.deprecated_message.getvalue(),
matchers.Contains(expected))
def test_deprecated_with_removed_next_release(self):
@utils.deprecated(as_of=utils.deprecated.GRIZZLY,
remove_in=1)
def do_outdated_stuff():
return
do_outdated_stuff()
expected = ('do_outdated_stuff() is deprecated as of Grizzly '
'and may be removed in Havana. It will not be '
'superseded.')
self.assertThat(self.deprecated_message.getvalue(),
matchers.Contains(expected))
def test_deprecated_with_removed_plus_3(self):
@utils.deprecated(as_of=utils.deprecated.GRIZZLY,
remove_in=+3)
def do_outdated_stuff():
return
do_outdated_stuff()
expected = ('do_outdated_stuff() is deprecated as of Grizzly '
'and may be removed in J. It will not '
'be superseded.')
self.assertThat(self.deprecated_message.getvalue(),
matchers.Contains(expected))

View File

@ -26,11 +26,11 @@ from keystone.common import cache
from keystone.common import cms
from keystone.common import dependency
from keystone.common import manager
from keystone.common import utils
from keystone import config
from keystone import exception
from keystone.openstack.common import log as logging
from keystone.openstack.common import timeutils
from keystone.openstack.common import versionutils
CONF = config.CONF
@ -194,7 +194,7 @@ class Manager(manager.Manager):
self._get_token.invalidate(self, token_id)
self.token_provider_api.invalidate_individual_token_cache(token_id)
@utils.deprecated(utils.deprecated.ICEHOUSE, remove_in=+1)
@versionutils.deprecated(versionutils.deprecated.ICEHOUSE, remove_in=+1)
def list_tokens(self, user_id, tenant_id=None, trust_id=None,
consumer_id=None):
"""Returns a list of current token_id's for a user