mirror of
https://github.com/saltstack/salt.git
synced 2025-04-16 09:40:20 +00:00
feat(http_pillar): added kwargs to handle extra option for http.query
This commit is contained in:
parent
8006ca52bb
commit
07b3c033a9
3 changed files with 39 additions and 6 deletions
1
changelog/36138.fixed
Normal file
1
changelog/36138.fixed
Normal file
|
@ -0,0 +1 @@
|
||||||
|
Add kwargs to handle extra parameters for http.query
|
|
@ -12,9 +12,15 @@ Set the following Salt config to setup http json result as external pillar sourc
|
||||||
ext_pillar:
|
ext_pillar:
|
||||||
- http_json:
|
- http_json:
|
||||||
url: http://example.com/api/minion_id
|
url: http://example.com/api/minion_id
|
||||||
::TODO::
|
|
||||||
username: username
|
username: username
|
||||||
password: password
|
password: password
|
||||||
|
header_dict: None
|
||||||
|
auth: None
|
||||||
|
|
||||||
|
You can pass additional parameters, they will be added to the http.query call
|
||||||
|
:py:func:`utils.http.query function <salt.utils.http.query>`:
|
||||||
|
|
||||||
|
.. autofunction:: salt.utils.http.query
|
||||||
|
|
||||||
If the with_grains parameter is set, grain keys wrapped in can be provided (wrapped
|
If the with_grains parameter is set, grain keys wrapped in can be provided (wrapped
|
||||||
in <> brackets) in the url in order to populate pillar data based on the grain value.
|
in <> brackets) in the url in order to populate pillar data based on the grain value.
|
||||||
|
@ -52,16 +58,26 @@ def __virtual__():
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
def ext_pillar(minion_id, pillar, url, with_grains=False): # pylint: disable=W0613
|
def ext_pillar(
|
||||||
|
minion_id, pillar, url, with_grains=False, **kwargs
|
||||||
|
): # pylint: disable=W0613
|
||||||
"""
|
"""
|
||||||
Read pillar data from HTTP response.
|
Read pillar data from HTTP response.
|
||||||
|
|
||||||
:param str url: Url to request.
|
:param str url: Url to request.
|
||||||
:param bool with_grains: Whether to substitute strings in the url with their grain values.
|
:param bool with_grains: Whether to substitute strings in the url with their grain values.
|
||||||
|
:param dict header_dict: Extra headers to send
|
||||||
|
:param str username: username for auth
|
||||||
|
:param str pasword: password for auth
|
||||||
|
:param auth: special auth if needed
|
||||||
|
|
||||||
:return: A dictionary of the pillar data to add.
|
:return: A dictionary of the pillar data to add.
|
||||||
:rtype: dict
|
:rtype: dict
|
||||||
"""
|
"""
|
||||||
|
# As we are dealing with kwargs, clean args that are hardcoded in this function
|
||||||
|
for arg in ["url", "decode", "decode_type"]:
|
||||||
|
if arg in kwargs:
|
||||||
|
del kwargs[arg]
|
||||||
|
|
||||||
url = url.replace("%s", urllib.parse.quote(minion_id))
|
url = url.replace("%s", urllib.parse.quote(minion_id))
|
||||||
|
|
||||||
|
@ -82,7 +98,7 @@ def ext_pillar(minion_id, pillar, url, with_grains=False): # pylint: disable=W0
|
||||||
url = re.sub("<{}>".format(grain_name), grain_value, url)
|
url = re.sub("<{}>".format(grain_name), grain_value, url)
|
||||||
|
|
||||||
log.debug("Getting url: %s", url)
|
log.debug("Getting url: %s", url)
|
||||||
data = __salt__["http.query"](url=url, decode=True, decode_type="json")
|
data = __salt__["http.query"](url=url, decode=True, decode_type="json", **kwargs)
|
||||||
|
|
||||||
if "dict" in data:
|
if "dict" in data:
|
||||||
return data["dict"]
|
return data["dict"]
|
||||||
|
|
|
@ -12,9 +12,15 @@ Set the following Salt config to setup an http endpoint as the external pillar s
|
||||||
ext_pillar:
|
ext_pillar:
|
||||||
- http_yaml:
|
- http_yaml:
|
||||||
url: http://example.com/api/minion_id
|
url: http://example.com/api/minion_id
|
||||||
::TODO::
|
|
||||||
username: username
|
username: username
|
||||||
password: password
|
password: password
|
||||||
|
header_dict: None
|
||||||
|
auth: None
|
||||||
|
|
||||||
|
You can pass additional parameters, they will be added to the http.query call
|
||||||
|
:py:func:`utils.http.query function <salt.utils.http.query>`:
|
||||||
|
|
||||||
|
.. autofunction:: salt.utils.http.query
|
||||||
|
|
||||||
If the with_grains parameter is set, grain keys wrapped in can be provided (wrapped
|
If the with_grains parameter is set, grain keys wrapped in can be provided (wrapped
|
||||||
in <> brackets) in the url in order to populate pillar data based on the grain value.
|
in <> brackets) in the url in order to populate pillar data based on the grain value.
|
||||||
|
@ -50,16 +56,26 @@ def __virtual__():
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
def ext_pillar(minion_id, pillar, url, with_grains=False): # pylint: disable=W0613
|
def ext_pillar(
|
||||||
|
minion_id, pillar, url, with_grains=False, **kwargs
|
||||||
|
): # pylint: disable=W0613
|
||||||
"""
|
"""
|
||||||
Read pillar data from HTTP response.
|
Read pillar data from HTTP response.
|
||||||
|
|
||||||
:param str url: Url to request.
|
:param str url: Url to request.
|
||||||
:param bool with_grains: Whether to substitute strings in the url with their grain values.
|
:param bool with_grains: Whether to substitute strings in the url with their grain values.
|
||||||
|
:param dict header_dict: Extra headers to send
|
||||||
|
:param str username: username for auth
|
||||||
|
:param str pasword: password for auth
|
||||||
|
:param auth: special auth if needed
|
||||||
|
|
||||||
:return: A dictionary of the pillar data to add.
|
:return: A dictionary of the pillar data to add.
|
||||||
:rtype: dict
|
:rtype: dict
|
||||||
"""
|
"""
|
||||||
|
# As we are dealing with kwargs, clean args that are hardcoded in this function
|
||||||
|
for arg in ["url", "decode", "decode_type"]:
|
||||||
|
if arg in kwargs:
|
||||||
|
del kwargs[arg]
|
||||||
|
|
||||||
url = url.replace("%s", urllib.parse.quote(minion_id))
|
url = url.replace("%s", urllib.parse.quote(minion_id))
|
||||||
|
|
||||||
|
@ -80,7 +96,7 @@ def ext_pillar(minion_id, pillar, url, with_grains=False): # pylint: disable=W0
|
||||||
url = re.sub("<{}>".format(grain_name), grain_value, url)
|
url = re.sub("<{}>".format(grain_name), grain_value, url)
|
||||||
|
|
||||||
log.debug("Getting url: %s", url)
|
log.debug("Getting url: %s", url)
|
||||||
data = __salt__["http.query"](url=url, decode=True, decode_type="yaml")
|
data = __salt__["http.query"](url=url, decode=True, decode_type="yaml", **kwargs)
|
||||||
|
|
||||||
if "dict" in data:
|
if "dict" in data:
|
||||||
return data["dict"]
|
return data["dict"]
|
||||||
|
|
Loading…
Add table
Reference in a new issue