mirror of
https://github.com/saltstack/salt.git
synced 2025-04-17 10:10:20 +00:00
Added strcit argument to sdb.get
This commit is contained in:
parent
961ca8930d
commit
885e8763ec
4 changed files with 59 additions and 11 deletions
1
changelog/39163.fixed
Normal file
1
changelog/39163.fixed
Normal file
|
@ -0,0 +1 @@
|
|||
Added an bool "strict" argument to sdb.get module to force module to fail if the sdb uri is not correct.
|
|
@ -13,18 +13,18 @@ __func_alias__ = {
|
|||
}
|
||||
|
||||
|
||||
def get(uri):
|
||||
def get(uri, strict=False):
|
||||
"""
|
||||
Get a value from a db, using a uri in the form of sdb://<profile>/<key>. If
|
||||
the uri provided does not start with sdb://, then it will be returned as-is.
|
||||
Get a value from a db, using a uri in the form of ``sdb://<profile>/<key>``. If
|
||||
the uri provided is not valid, then it will be returned as-is, unless ``strict=True`` was passed.
|
||||
|
||||
CLI Example:
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
salt '*' sdb.get sdb://mymemcached/foo
|
||||
salt '*' sdb.get sdb://mymemcached/foo strict=True
|
||||
"""
|
||||
return salt.utils.sdb.sdb_get(uri, __opts__, __utils__)
|
||||
return salt.utils.sdb.sdb_get(uri, __opts__, __utils__, strict)
|
||||
|
||||
|
||||
def set_(uri, value):
|
||||
|
|
|
@ -12,17 +12,21 @@ import random
|
|||
|
||||
# Import salt libs
|
||||
import salt.loader
|
||||
from salt.exceptions import SaltInvocationError
|
||||
from salt.ext.six import string_types
|
||||
from salt.ext.six.moves import range
|
||||
|
||||
|
||||
def sdb_get(uri, opts, utils=None):
|
||||
def sdb_get(uri, opts, utils=None, strict=False):
|
||||
"""
|
||||
Get a value from a db, using a uri in the form of ``sdb://<profile>/<key>``. If
|
||||
the uri provided does not start with ``sdb://``, then it will be returned as-is.
|
||||
the uri provided is not valid, then it will be returned as-is, unless ``strict=True`` was passed.
|
||||
"""
|
||||
if not isinstance(uri, string_types) or not uri.startswith("sdb://"):
|
||||
return uri
|
||||
if strict:
|
||||
raise SaltInvocationError('SDB uri must start with "sdb://"')
|
||||
else:
|
||||
return uri
|
||||
|
||||
if utils is None:
|
||||
utils = salt.loader.utils(opts)
|
||||
|
@ -31,13 +35,25 @@ def sdb_get(uri, opts, utils=None):
|
|||
indx = uri.find("/", sdlen)
|
||||
|
||||
if (indx == -1) or not uri[(indx + 1) :]:
|
||||
return uri
|
||||
if strict:
|
||||
raise SaltInvocationError(
|
||||
"SDB uri must have a profile name as a first part of the uri before the /"
|
||||
)
|
||||
else:
|
||||
return uri
|
||||
|
||||
profile = opts.get(uri[sdlen:indx], {})
|
||||
if not profile:
|
||||
profile = opts.get("pillar", {}).get(uri[sdlen:indx], {})
|
||||
if "driver" not in profile:
|
||||
return uri
|
||||
if strict:
|
||||
raise SaltInvocationError(
|
||||
'SDB profile "{}" wasnt found in the minion configuration'.format(
|
||||
uri[sdlen:indx]
|
||||
)
|
||||
)
|
||||
else:
|
||||
return uri
|
||||
|
||||
fun = "{0}.get".format(profile["driver"])
|
||||
query = uri[indx + 1 :]
|
||||
|
|
|
@ -8,6 +8,7 @@ from __future__ import absolute_import, print_function, unicode_literals
|
|||
|
||||
# Import Salt Libs
|
||||
import salt.modules.sdb as sdb
|
||||
from salt.exceptions import SaltInvocationError
|
||||
|
||||
# Import Salt Testing Libs
|
||||
from tests.support.mixins import LoaderModuleMockMixin
|
||||
|
@ -22,7 +23,7 @@ class SdbTestCase(TestCase, LoaderModuleMockMixin):
|
|||
def setup_loader_modules(self):
|
||||
return {sdb: {}}
|
||||
|
||||
# 'get' function tests: 1
|
||||
# 'get' function tests: 4
|
||||
|
||||
def test_get(self):
|
||||
"""
|
||||
|
@ -31,6 +32,36 @@ class SdbTestCase(TestCase, LoaderModuleMockMixin):
|
|||
"""
|
||||
self.assertEqual(sdb.get("sdb://salt/foo"), "sdb://salt/foo")
|
||||
|
||||
def test_get_strict_no_sdb_in_uri(self):
|
||||
"""
|
||||
Test if SaltInvocationError exception will be raised if we
|
||||
don't start uri with sdb://
|
||||
"""
|
||||
|
||||
msg = 'SDB uri must start with "sdb://"'
|
||||
with self.assertRaisesRegex(SaltInvocationError, msg) as cm:
|
||||
sdb.get("://salt/foo", strict=True)
|
||||
|
||||
def test_get_strict_no_profile(self):
|
||||
"""
|
||||
Test if SaltInvocationError exception will be raised if we
|
||||
don't have a valid profile in the uri
|
||||
"""
|
||||
|
||||
msg = "SDB uri must have a profile name as a first part of the uri before the /"
|
||||
with self.assertRaisesRegex(SaltInvocationError, msg) as cm:
|
||||
sdb.get("sdb://salt", strict=True)
|
||||
|
||||
def test_get_strict_no_profile_in_config(self):
|
||||
"""
|
||||
Test if SaltInvocationError exception will be raised if we
|
||||
don't have expected profile in the minion config
|
||||
"""
|
||||
|
||||
msg = 'SDB profile "salt" wasnt found in the minion configuration'
|
||||
with self.assertRaisesRegex(SaltInvocationError, msg) as cm:
|
||||
sdb.get("sdb://salt/foo", strict=True)
|
||||
|
||||
# 'set_' function tests: 1
|
||||
|
||||
def test_set(self):
|
||||
|
|
Loading…
Add table
Reference in a new issue