Merge pull request #41522 from jettero/mongodb-keys-are-stupid

Sadly, you can't have '.'s and '$'s in dict keys in a mongodb doc.
This commit is contained in:
Mike Place 2017-05-31 10:55:23 -05:00 committed by GitHub
commit 2e7e84b8f2

View File

@ -200,16 +200,54 @@ def returner(ret):
mdb.saltReturns.insert(sdata.copy()) mdb.saltReturns.insert(sdata.copy())
def _safe_copy(dat):
''' mongodb doesn't allow '.' in keys, but does allow unicode equivs.
Apparently the docs suggest using escaped unicode full-width
encodings. *sigh*
\\ --> \\\\
$ --> \\\\u0024
. --> \\\\u002e
Personally, I prefer URL encodings,
\\ --> %5c
$ --> %24
. --> %2e
Which means also escaping '%':
% -> %25
'''
if isinstance(dat, dict):
ret = {}
for k in dat:
r = k.replace('%', '%25').replace('\\', '%5c').replace('$', '%24').replace('.', '%2e')
if r != k:
log.debug('converting dict key from {0} to {1} for mongodb'.format(k, r))
ret[r] = _safe_copy(dat[k])
return ret
if isinstance(dat, (list, tuple)):
return [_safe_copy(i) for i in dat]
return dat
def save_load(jid, load, minions=None): def save_load(jid, load, minions=None):
''' '''
Save the load for a given job id Save the load for a given job id
''' '''
conn, mdb = _get_conn(ret=None) conn, mdb = _get_conn(ret=None)
to_save = _safe_copy(load)
if float(version) > 2.3: if float(version) > 2.3:
#using .copy() to ensure original data for load is unchanged #using .copy() to ensure original data for load is unchanged
mdb.jobs.insert_one(load.copy()) mdb.jobs.insert_one(to_save)
else: else:
mdb.jobs.insert(load.copy()) mdb.jobs.insert(to_save)
def save_minions(jid, minions, syndic_id=None): # pylint: disable=unused-argument def save_minions(jid, minions, syndic_id=None): # pylint: disable=unused-argument