mirror of
https://github.com/saltstack/salt.git
synced 2025-04-17 10:10:20 +00:00
make LDAP attr defaults string types on py2
This commit is contained in:
parent
0ca04ffbeb
commit
8c641c66fb
3 changed files with 37 additions and 8 deletions
|
@ -144,9 +144,9 @@ def search(filter, # pylint: disable=C0103
|
|||
attrs = _config('attrs')
|
||||
_ldap = _connect(**kwargs)
|
||||
start = time.time()
|
||||
log.debug(
|
||||
log.warn(
|
||||
'Running LDAP search with filter:%s, dn:%s, scope:%s, '
|
||||
'attrs:%s', filter, dn, scope, attrs
|
||||
'attrs:%r', filter, dn, scope, attrs
|
||||
)
|
||||
results = _ldap.search_s(dn, int(scope), filter, attrs)
|
||||
elapsed = (time.time() - start)
|
||||
|
|
|
@ -163,15 +163,17 @@ def _render_template(config_file):
|
|||
return template.render(__grains__)
|
||||
|
||||
|
||||
def _config(name, conf):
|
||||
def _config(name, conf, default=None):
|
||||
'''
|
||||
Return a value for 'name' from the config file options.
|
||||
Return a value for 'name' from the config file options. If the 'name' is
|
||||
not in the config, the 'default' value is returned. This method converts
|
||||
unicode values to str type under python 2.
|
||||
'''
|
||||
try:
|
||||
value = salt.utils.data.decode(conf[name], to_str=True)
|
||||
value = conf[name]
|
||||
except KeyError:
|
||||
value = None
|
||||
return value
|
||||
value = default
|
||||
return salt.utils.data.decode(value, to_str=True)
|
||||
|
||||
|
||||
def _result_to_dict(data, result, conf, source):
|
||||
|
@ -285,7 +287,7 @@ def _do_search(conf):
|
|||
scope = _config('scope', conf)
|
||||
_lists = _config('lists', conf) or []
|
||||
_attrs = _config('attrs', conf) or []
|
||||
_dict_key_attr = _config('dict_key_attr', conf) or 'dn'
|
||||
_dict_key_attr = _config('dict_key_attr', conf, 'dn')
|
||||
attrs = _lists + _attrs + [_dict_key_attr]
|
||||
if not attrs:
|
||||
attrs = None
|
||||
|
|
27
tests/unit/pillar/test_pillar_ldap.py
Normal file
27
tests/unit/pillar/test_pillar_ldap.py
Normal file
|
@ -0,0 +1,27 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
|
||||
# Import python libs
|
||||
from __future__ import absolute_import, print_function, unicode_literals
|
||||
|
||||
from tests.support.unit import TestCase
|
||||
import salt.utils.stringutils
|
||||
|
||||
|
||||
from salt.pillar.pillar_ldap import _config
|
||||
|
||||
|
||||
|
||||
class LdapPillarTestCase(TestCase):
|
||||
|
||||
def test__config_returns_str(self):
|
||||
conf = {'foo': 'bar'}
|
||||
assert _config('foo', conf) == salt.utils.stringutils.to_str('bar')
|
||||
|
||||
def test__conf_defaults_to_none(self):
|
||||
conf = {'foo': 'bar'}
|
||||
assert _config('bang', conf) == None
|
||||
|
||||
def test__conf_returns_str_from_unicode_default(self):
|
||||
conf = {'foo': 'bar'}
|
||||
default = salt.utils.stringutils.to_unicode('bam')
|
||||
assert _config('bang', conf, default) == salt.utils.stringutils.to_str('bam')
|
Loading…
Add table
Reference in a new issue