Add ability to remove subsections from /info
Change-Id: Ic881065962cf5f69f7a5b64f6e38d9e6e1f8fd18
This commit is contained in:
parent
08dba08e76
commit
9cbf8a3f5b
|
@ -13,11 +13,13 @@
|
|||
# which will disable admin calls to /info.
|
||||
# admin_key = secret_admin_key
|
||||
#
|
||||
# Allows the ability to withhold sections from showing up in the public
|
||||
# calls to /info. The following would cause the sections 'container_quotas'
|
||||
# and 'tempurl' to not be listed. Default is empty, allowing all registered
|
||||
# fetures to be listed via HTTP GET /info.
|
||||
# disallowed_sections = container_quotas, tempurl
|
||||
# Allows the ability to withhold sections from showing up in the public calls
|
||||
# to /info. You can withhold subsections by separating the dict level with a
|
||||
# ".". The following would cause the sections 'container_quotas' and 'tempurl'
|
||||
# to not be listed, and the key max_failed_deletes would be removed from
|
||||
# bulk_delete. Default is empty, allowing all registered fetures to be listed
|
||||
# via HTTP GET /info.
|
||||
# disallowed_sections = container_quotas, tempurl, bulk_delete.max_failed_deletes
|
||||
|
||||
# Use an integer to override the number of pre-forked processes that will
|
||||
# accept connections. Should default to the number of effective cpu
|
||||
|
|
|
@ -163,11 +163,19 @@ def get_swift_info(admin=False, disallowed_sections=None):
|
|||
:returns: dictionary of information about the swift cluster.
|
||||
"""
|
||||
disallowed_sections = disallowed_sections or []
|
||||
info = {}
|
||||
for section in _swift_info:
|
||||
if section in disallowed_sections:
|
||||
continue
|
||||
info[section] = dict(_swift_info[section].items())
|
||||
info = dict(_swift_info)
|
||||
for section in disallowed_sections:
|
||||
key_to_pop = None
|
||||
sub_section_dict = info
|
||||
for sub_section in section.split('.'):
|
||||
if key_to_pop:
|
||||
sub_section_dict = sub_section_dict.get(key_to_pop, {})
|
||||
if not isinstance(sub_section_dict, dict):
|
||||
sub_section_dict = {}
|
||||
break
|
||||
key_to_pop = sub_section
|
||||
sub_section_dict.pop(key_to_pop, None)
|
||||
|
||||
if admin:
|
||||
info['admin'] = dict(_swift_admin_info)
|
||||
info['admin']['disallowed_sections'] = list(disallowed_sections)
|
||||
|
@ -179,12 +187,16 @@ def register_swift_info(name='swift', admin=False, **kwargs):
|
|||
Registers information about the swift cluster to be retrieved with calls
|
||||
to get_swift_info.
|
||||
|
||||
NOTE: Do not use "." in the param: name or any keys in kwargs. "." is used
|
||||
in the disallowed_sections to remove unwanted keys from /info.
|
||||
|
||||
:param name: string, the section name to place the information under.
|
||||
:param admin: boolean, if True, information will be registered to an
|
||||
admin section which can optionally be withheld when
|
||||
requesting the information.
|
||||
:param kwargs: key value arguments representing the information to be
|
||||
added.
|
||||
:raises ValueError: if name or any of the keys in kwargs has "." in it
|
||||
"""
|
||||
if name == 'admin' or name == 'disallowed_sections':
|
||||
raise ValueError('\'{0}\' is reserved name.'.format(name))
|
||||
|
@ -194,8 +206,12 @@ def register_swift_info(name='swift', admin=False, **kwargs):
|
|||
else:
|
||||
dict_to_use = _swift_info
|
||||
if name not in dict_to_use:
|
||||
if "." in name:
|
||||
raise ValueError('Cannot use "." in a swift_info key: %s' % name)
|
||||
dict_to_use[name] = {}
|
||||
for key, val in kwargs.iteritems():
|
||||
if "." in key:
|
||||
raise ValueError('Cannot use "." in a swift_info key: %s' % key)
|
||||
dict_to_use[name][key] = val
|
||||
|
||||
|
||||
|
|
|
@ -1974,6 +1974,13 @@ class TestSwiftInfo(unittest.TestCase):
|
|||
utils.register_swift_info, 'disallowed_sections',
|
||||
disallowed_sections=None)
|
||||
|
||||
utils.register_swift_info('goodkey', foo='5.6')
|
||||
self.assertRaises(ValueError,
|
||||
utils.register_swift_info, 'bad.key', foo='5.6')
|
||||
data = {'bad.key': '5.6'}
|
||||
self.assertRaises(ValueError,
|
||||
utils.register_swift_info, 'goodkey', **data)
|
||||
|
||||
def test_get_swift_info(self):
|
||||
utils._swift_info = {'swift': {'foo': 'bar'},
|
||||
'cap1': {'cap1_foo': 'cap1_bar'}}
|
||||
|
@ -2090,6 +2097,36 @@ class TestSwiftInfo(unittest.TestCase):
|
|||
|
||||
self.assertTrue('cap3' not in info)
|
||||
|
||||
def test_get_swift_admin_info_with_disallowed_sub_sections(self):
|
||||
utils._swift_info = {'swift': {'foo': 'bar'},
|
||||
'cap1': {'cap1_foo': 'cap1_bar',
|
||||
'cap1_moo': 'cap1_baa'},
|
||||
'cap2': {'cap2_foo': 'cap2_bar'},
|
||||
'cap3': {'cap2_foo': 'cap2_bar'},
|
||||
'cap4': {'a': {'b': {'c': 'c'},
|
||||
'b.c': 'b.c'}}}
|
||||
utils._swift_admin_info = {'admin_cap1': {'ac1_foo': 'ac1_bar'}}
|
||||
|
||||
info = utils.get_swift_info(
|
||||
admin=True, disallowed_sections=['cap1.cap1_foo', 'cap3',
|
||||
'cap4.a.b.c'])
|
||||
self.assertTrue('cap3' not in info)
|
||||
self.assertEquals(info['cap1']['cap1_moo'], 'cap1_baa')
|
||||
self.assertTrue('cap1_foo' not in info['cap1'])
|
||||
self.assertTrue('c' not in info['cap4']['a']['b'])
|
||||
self.assertEqual(info['cap4']['a']['b.c'], 'b.c')
|
||||
|
||||
def test_get_swift_info_with_unmatched_disallowed_sections(self):
|
||||
cap1 = {'cap1_foo': 'cap1_bar',
|
||||
'cap1_moo': 'cap1_baa'}
|
||||
utils._swift_info = {'swift': {'foo': 'bar'},
|
||||
'cap1': cap1}
|
||||
# expect no exceptions
|
||||
info = utils.get_swift_info(disallowed_sections=
|
||||
['cap2.cap1_foo', 'cap1.no_match',
|
||||
'cap1.cap1_foo.no_match.no_match'])
|
||||
self.assertEquals(info['cap1'], cap1)
|
||||
|
||||
|
||||
class TestFileLikeIter(unittest.TestCase):
|
||||
|
||||
|
|
Loading…
Reference in New Issue