"Fossies" - the Fresh Open Source Software Archive

Member "salt-3002.2/salt/modules/consul.py" (18 Nov 2020, 70640 Bytes) of package /linux/misc/salt-3002.2.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "consul.py" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 3002.1_vs_3002.2.

    1 """
    2 Interact with Consul
    3 
    4 https://www.consul.io
    5 
    6 """
    7 
    8 
    9 import base64
   10 import http.client
   11 import logging
   12 import urllib
   13 
   14 import salt.utils.http
   15 import salt.utils.json
   16 from salt.exceptions import SaltInvocationError
   17 
   18 log = logging.getLogger(__name__)
   19 
   20 
   21 # Don't shadow built-ins.
   22 __func_alias__ = {"list_": "list"}
   23 
   24 __virtualname__ = "consul"
   25 
   26 
   27 def _get_config():
   28     """
   29     Retrieve Consul configuration
   30     """
   31     return __salt__["config.get"]("consul.url") or __salt__["config.get"]("consul:url")
   32 
   33 
   34 def _get_token():
   35     """
   36     Retrieve Consul configuration
   37     """
   38     return __salt__["config.get"]("consul.token") or __salt__["config.get"](
   39         "consul:token"
   40     )
   41 
   42 
   43 def _query(
   44     function,
   45     consul_url,
   46     token=None,
   47     method="GET",
   48     api_version="v1",
   49     data=None,
   50     query_params=None,
   51 ):
   52     """
   53     Consul object method function to construct and execute on the API URL.
   54 
   55     :param api_url:     The Consul api url.
   56     :param api_version  The Consul api version
   57     :param function:    The Consul api function to perform.
   58     :param method:      The HTTP method, e.g. GET or POST.
   59     :param data:        The data to be sent for POST method. This param is ignored for GET requests.
   60     :return:            The json response from the API call or False.
   61     """
   62 
   63     if not query_params:
   64         query_params = {}
   65 
   66     ret = {"data": "", "res": True}
   67 
   68     if not token:
   69         token = _get_token()
   70 
   71     headers = {"X-Consul-Token": token, "Content-Type": "application/json"}
   72     base_url = urllib.parse.urljoin(consul_url, "{}/".format(api_version))
   73     url = urllib.parse.urljoin(base_url, function, False)
   74 
   75     if method == "GET":
   76         data = None
   77     else:
   78         if data is None:
   79             data = {}
   80         data = salt.utils.json.dumps(data)
   81 
   82     result = salt.utils.http.query(
   83         url,
   84         method=method,
   85         params=query_params,
   86         data=data,
   87         decode=True,
   88         status=True,
   89         header_dict=headers,
   90         opts=__opts__,
   91     )
   92 
   93     if result.get("status", None) == http.client.OK:
   94         ret["data"] = result.get("dict", result)
   95         ret["res"] = True
   96     elif result.get("status", None) == http.client.NO_CONTENT:
   97         ret["res"] = False
   98     elif result.get("status", None) == http.client.NOT_FOUND:
   99         ret["data"] = "Key not found."
  100         ret["res"] = False
  101     else:
  102         if result:
  103             ret["data"] = result
  104             ret["res"] = True
  105         else:
  106             ret["res"] = False
  107     return ret
  108 
  109 
  110 def list_(consul_url=None, token=None, key=None, **kwargs):
  111     """
  112     List keys in Consul
  113 
  114     :param consul_url: The Consul server URL.
  115     :param key: The key to use as the starting point for the list.
  116     :return: The list of keys.
  117 
  118     CLI Example:
  119 
  120     .. code-block:: bash
  121 
  122         salt '*' consul.list
  123         salt '*' consul.list key='web'
  124 
  125     """
  126     ret = {}
  127 
  128     if not consul_url:
  129         consul_url = _get_config()
  130         if not consul_url:
  131             log.error("No Consul URL found.")
  132             ret["message"] = "No Consul URL found."
  133             ret["res"] = False
  134             return ret
  135 
  136     query_params = {}
  137 
  138     if "recurse" in kwargs:
  139         query_params["recurse"] = "True"
  140 
  141     # No key so recurse and show all values
  142     if not key:
  143         query_params["recurse"] = "True"
  144         function = "kv/"
  145     else:
  146         function = "kv/{}".format(key)
  147 
  148     query_params["keys"] = "True"
  149     query_params["separator"] = "/"
  150     ret = _query(
  151         consul_url=consul_url, function=function, token=token, query_params=query_params
  152     )
  153     return ret
  154 
  155 
  156 def get(consul_url=None, key=None, token=None, recurse=False, decode=False, raw=False):
  157     """
  158     Get key from Consul
  159 
  160     :param consul_url: The Consul server URL.
  161     :param key: The key to use as the starting point for the list.
  162     :param recurse: Return values recursively beginning at the value of key.
  163     :param decode: By default values are stored as Base64 encoded values,
  164                    decode will return the whole key with the value decoded.
  165     :param raw: Simply return the decoded value of the key.
  166     :return: The keys in Consul.
  167 
  168     CLI Example:
  169 
  170     .. code-block:: bash
  171 
  172         salt '*' consul.get key='web/key1'
  173         salt '*' consul.get key='web' recurse=True
  174         salt '*' consul.get key='web' recurse=True decode=True
  175 
  176     By default values stored in Consul are base64 encoded, passing the
  177     decode option will show them as the decoded values.
  178 
  179     .. code-block:: bash
  180 
  181         salt '*' consul.get key='web' recurse=True decode=True raw=True
  182 
  183     By default Consult will return other information about the key, the raw
  184     option will return only the raw value.
  185 
  186     """
  187     ret = {}
  188     if not consul_url:
  189         consul_url = _get_config()
  190         if not consul_url:
  191             log.error("No Consul URL found.")
  192             ret["message"] = "No Consul URL found."
  193             ret["res"] = False
  194             return ret
  195 
  196     if not key:
  197         raise SaltInvocationError('Required argument "key" is missing.')
  198 
  199     query_params = {}
  200     function = "kv/{}".format(key)
  201     if recurse:
  202         query_params["recurse"] = "True"
  203     if raw:
  204         query_params["raw"] = True
  205     ret = _query(
  206         consul_url=consul_url, function=function, token=token, query_params=query_params
  207     )
  208 
  209     if ret["res"]:
  210         if decode:
  211             for item in ret["data"]:
  212                 if item["Value"] is not None:
  213                     item["Value"] = base64.b64decode(item["Value"])
  214                 else:
  215                     item["Value"] = ""
  216     return ret
  217 
  218 
  219 def put(consul_url=None, token=None, key=None, value=None, **kwargs):
  220     """
  221     Put values into Consul
  222 
  223     :param consul_url: The Consul server URL.
  224     :param key: The key to use as the starting point for the list.
  225     :param value: The value to set the key to.
  226     :param flags: This can be used to specify an unsigned value
  227                   between 0 and 2^64-1. Clients can choose to use
  228                   this however makes sense for their application.
  229     :param cas: This flag is used to turn the PUT into a
  230                 Check-And-Set operation.
  231     :param acquire: This flag is used to turn the PUT into a
  232                     lock acquisition operation.
  233     :param release: This flag is used to turn the PUT into a
  234                     lock release operation.
  235     :return: Boolean & message of success or failure.
  236 
  237     CLI Example:
  238 
  239     .. code-block:: bash
  240 
  241         salt '*' consul.put key='web/key1' value="Hello there"
  242 
  243         salt '*' consul.put key='web/key1' value="Hello there" acquire='d5d371f4-c380-5280-12fd-8810be175592'
  244 
  245         salt '*' consul.put key='web/key1' value="Hello there" release='d5d371f4-c380-5280-12fd-8810be175592'
  246 
  247     """
  248     ret = {}
  249     if not consul_url:
  250         consul_url = _get_config()
  251         if not consul_url:
  252             log.error("No Consul URL found.")
  253             ret["message"] = "No Consul URL found."
  254             ret["res"] = False
  255             return ret
  256 
  257     if not key:
  258         raise SaltInvocationError('Required argument "key" is missing.')
  259 
  260     # Invalid to specified these together
  261     conflicting_args = ["cas", "release", "acquire"]
  262     for _l1 in conflicting_args:
  263         for _l2 in conflicting_args:
  264             if _l1 in kwargs and _l2 in kwargs and _l1 != _l2:
  265                 raise SaltInvocationError(
  266                     "Using arguments `{}` and `{}`"
  267                     " together is invalid.".format(_l1, _l2)
  268                 )
  269 
  270     query_params = {}
  271 
  272     available_sessions = session_list(consul_url=consul_url, return_list=True)
  273     _current = get(consul_url=consul_url, key=key)
  274 
  275     if "flags" in kwargs:
  276         if kwargs["flags"] >= 0 and kwargs["flags"] <= 2 ** 64:
  277             query_params["flags"] = kwargs["flags"]
  278 
  279     if "cas" in kwargs:
  280         if _current["res"]:
  281             if kwargs["cas"] == 0:
  282                 ret["message"] = "Key {} exists, index " "must be non-zero.".format(key)
  283                 ret["res"] = False
  284                 return ret
  285 
  286             if kwargs["cas"] != _current["data"]["ModifyIndex"]:
  287                 ret["message"] = "Key {} exists, but indexes " "do not match.".format(
  288                     key
  289                 )
  290                 ret["res"] = False
  291                 return ret
  292             query_params["cas"] = kwargs["cas"]
  293         else:
  294             ret[
  295                 "message"
  296             ] = "Key {} does not exists, " "CAS argument can not be used.".format(key)
  297             ret["res"] = False
  298             return ret
  299 
  300     if "acquire" in kwargs:
  301         if kwargs["acquire"] not in available_sessions:
  302             ret["message"] = "{} is not a valid session.".format(kwargs["acquire"])
  303             ret["res"] = False
  304             return ret
  305 
  306         query_params["acquire"] = kwargs["acquire"]
  307 
  308     if "release" in kwargs:
  309         if _current["res"]:
  310             if "Session" in _current["data"]:
  311                 if _current["data"]["Session"] == kwargs["release"]:
  312                     query_params["release"] = kwargs["release"]
  313                 else:
  314                     ret["message"] = "{} locked by another session.".format(key)
  315                     ret["res"] = False
  316                     return ret
  317 
  318             else:
  319                 ret["message"] = "{} is not a valid session.".format(kwargs["acquire"])
  320                 ret["res"] = False
  321         else:
  322             log.error("Key {0} does not exist. Skipping release.")
  323 
  324     data = value
  325     function = "kv/{}".format(key)
  326     method = "PUT"
  327     ret = _query(
  328         consul_url=consul_url,
  329         token=token,
  330         function=function,
  331         method=method,
  332         data=data,
  333         query_params=query_params,
  334     )
  335 
  336     if ret["res"]:
  337         ret["res"] = True
  338         ret["data"] = "Added key {} with value {}.".format(key, value)
  339     else:
  340         ret["res"] = False
  341         ret["data"] = "Unable to add key {} with value {}.".format(key, value)
  342     return ret
  343 
  344 
  345 def delete(consul_url=None, token=None, key=None, **kwargs):
  346     """
  347     Delete values from Consul
  348 
  349     :param consul_url: The Consul server URL.
  350     :param key: The key to use as the starting point for the list.
  351     :param recurse: Delete values recursively beginning at the value of key.
  352     :param cas: This flag is used to turn the DELETE into
  353                 a Check-And-Set operation.
  354     :return: Boolean & message of success or failure.
  355 
  356     CLI Example:
  357 
  358     .. code-block:: bash
  359 
  360         salt '*' consul.delete key='web'
  361         salt '*' consul.delete key='web' recurse='True'
  362 
  363     """
  364     ret = {}
  365     if not consul_url:
  366         consul_url = _get_config()
  367         if not consul_url:
  368             log.error("No Consul URL found.")
  369             ret["message"] = "No Consul URL found."
  370             ret["res"] = False
  371             return ret
  372 
  373     if not key:
  374         raise SaltInvocationError('Required argument "key" is missing.')
  375 
  376     query_params = {}
  377 
  378     if "recurse" in kwargs:
  379         query_params["recurse"] = True
  380 
  381     if "cas" in kwargs:
  382         if kwargs["cas"] > 0:
  383             query_params["cas"] = kwargs["cas"]
  384         else:
  385             ret["message"] = (
  386                 "Check and Set Operation ",
  387                 "value must be greater than 0.",
  388             )
  389             ret["res"] = False
  390             return ret
  391 
  392     function = "kv/{}".format(key)
  393     ret = _query(
  394         consul_url=consul_url,
  395         token=token,
  396         function=function,
  397         method="DELETE",
  398         query_params=query_params,
  399     )
  400 
  401     if ret["res"]:
  402         ret["res"] = True
  403         ret["message"] = "Deleted key {}.".format(key)
  404     else:
  405         ret["res"] = False
  406         ret["message"] = "Unable to delete key {}.".format(key)
  407     return ret
  408 
  409 
  410 def agent_checks(consul_url=None, token=None):
  411     """
  412     Returns the checks the local agent is managing
  413 
  414     :param consul_url: The Consul server URL.
  415     :return: Returns the checks the local agent is managing
  416 
  417     CLI Example:
  418 
  419     .. code-block:: bash
  420 
  421         salt '*' consul.agent_checks
  422 
  423     """
  424     ret = {}
  425     if not consul_url:
  426         consul_url = _get_config()
  427         if not consul_url:
  428             log.error("No Consul URL found.")
  429             ret["message"] = "No Consul URL found."
  430             ret["res"] = False
  431             return ret
  432 
  433     function = "agent/checks"
  434     ret = _query(consul_url=consul_url, function=function, token=token, method="GET")
  435     return ret
  436 
  437 
  438 def agent_services(consul_url=None, token=None):
  439     """
  440     Returns the services the local agent is managing
  441 
  442     :param consul_url: The Consul server URL.
  443     :return: Returns the services the local agent is managing
  444 
  445     CLI Example:
  446 
  447     .. code-block:: bash
  448 
  449         salt '*' consul.agent_services
  450 
  451     """
  452     ret = {}
  453     if not consul_url:
  454         consul_url = _get_config()
  455         if not consul_url:
  456             log.error("No Consul URL found.")
  457             ret["message"] = "No Consul URL found."
  458             ret["res"] = False
  459             return ret
  460 
  461     function = "agent/services"
  462     ret = _query(consul_url=consul_url, function=function, token=token, method="GET")
  463     return ret
  464 
  465 
  466 def agent_members(consul_url=None, token=None, **kwargs):
  467     """
  468     Returns the members as seen by the local serf agent
  469 
  470     :param consul_url: The Consul server URL.
  471     :return: Returns the members as seen by the local serf agent
  472 
  473     CLI Example:
  474 
  475     .. code-block:: bash
  476 
  477         salt '*' consul.agent_members
  478 
  479     """
  480     ret = {}
  481     query_params = {}
  482     if not consul_url:
  483         consul_url = _get_config()
  484         if not consul_url:
  485             log.error("No Consul URL found.")
  486             ret["message"] = "No Consul URL found."
  487             ret["res"] = False
  488             return ret
  489 
  490     if "wan" in kwargs:
  491         query_params["wan"] = kwargs["wan"]
  492 
  493     function = "agent/members"
  494     ret = _query(
  495         consul_url=consul_url,
  496         function=function,
  497         token=token,
  498         method="GET",
  499         query_params=query_params,
  500     )
  501     return ret
  502 
  503 
  504 def agent_self(consul_url=None, token=None):
  505     """
  506     Returns the local node configuration
  507 
  508     :param consul_url: The Consul server URL.
  509     :return: Returns the local node configuration
  510 
  511     CLI Example:
  512 
  513     .. code-block:: bash
  514 
  515         salt '*' consul.agent_self
  516 
  517     """
  518     ret = {}
  519     query_params = {}
  520     if not consul_url:
  521         consul_url = _get_config()
  522         if not consul_url:
  523             log.error("No Consul URL found.")
  524             ret["message"] = "No Consul URL found."
  525             ret["res"] = False
  526             return ret
  527 
  528     function = "agent/self"
  529     ret = _query(
  530         consul_url=consul_url,
  531         function=function,
  532         token=token,
  533         method="GET",
  534         query_params=query_params,
  535     )
  536     return ret
  537 
  538 
  539 def agent_maintenance(consul_url=None, token=None, **kwargs):
  540     """
  541     Manages node maintenance mode
  542 
  543     :param consul_url: The Consul server URL.
  544     :param enable: The enable flag is required.
  545                    Acceptable values are either true
  546                    (to enter maintenance mode) or
  547                    false (to resume normal operation).
  548     :param reason: If provided, its value should be a
  549                    text string explaining the reason for
  550                    placing the node into maintenance mode.
  551     :return: Boolean and message indicating success or failure.
  552 
  553     CLI Example:
  554 
  555     .. code-block:: bash
  556 
  557         salt '*' consul.agent_maintenance enable='False' reason='Upgrade in progress'
  558 
  559     """
  560     ret = {}
  561     query_params = {}
  562     if not consul_url:
  563         consul_url = _get_config()
  564         if not consul_url:
  565             log.error("No Consul URL found.")
  566             ret["message"] = "No Consul URL found."
  567             ret["res"] = False
  568             return ret
  569 
  570     if "enable" in kwargs:
  571         query_params["enable"] = kwargs["enable"]
  572     else:
  573         ret["message"] = 'Required parameter "enable" is missing.'
  574         ret["res"] = False
  575         return ret
  576 
  577     if "reason" in kwargs:
  578         query_params["reason"] = kwargs["reason"]
  579 
  580     function = "agent/maintenance"
  581     res = _query(
  582         consul_url=consul_url,
  583         function=function,
  584         token=token,
  585         method="PUT",
  586         query_params=query_params,
  587     )
  588     if res["res"]:
  589         ret["res"] = True
  590         ret["message"] = "Agent maintenance mode " "{}ed.".format(kwargs["enable"])
  591     else:
  592         ret["res"] = True
  593         ret["message"] = "Unable to change maintenance mode for agent."
  594     return ret
  595 
  596 
  597 def agent_join(consul_url=None, token=None, address=None, **kwargs):
  598     """
  599     Triggers the local agent to join a node
  600 
  601     :param consul_url: The Consul server URL.
  602     :param address: The address for the agent to connect to.
  603     :param wan: Causes the agent to attempt to join using the WAN pool.
  604     :return: Boolean and message indicating success or failure.
  605 
  606     CLI Example:
  607 
  608     .. code-block:: bash
  609 
  610         salt '*' consul.agent_join address='192.168.1.1'
  611 
  612     """
  613     ret = {}
  614     query_params = {}
  615     if not consul_url:
  616         consul_url = _get_config()
  617         if not consul_url:
  618             log.error("No Consul URL found.")
  619             ret["message"] = "No Consul URL found."
  620             ret["res"] = False
  621             return ret
  622 
  623     if not address:
  624         raise SaltInvocationError('Required argument "address" is missing.')
  625 
  626     if "wan" in kwargs:
  627         query_params["wan"] = kwargs["wan"]
  628 
  629     function = "agent/join/{}".format(address)
  630     res = _query(
  631         consul_url=consul_url,
  632         function=function,
  633         token=token,
  634         method="GET",
  635         query_params=query_params,
  636     )
  637     if res["res"]:
  638         ret["res"] = True
  639         ret["message"] = "Agent joined the cluster"
  640     else:
  641         ret["res"] = False
  642         ret["message"] = "Unable to join the cluster."
  643     return ret
  644 
  645 
  646 def agent_leave(consul_url=None, token=None, node=None):
  647     """
  648     Used to instruct the agent to force a node into the left state.
  649 
  650     :param consul_url: The Consul server URL.
  651     :param node: The node the agent will force into left state
  652     :return: Boolean and message indicating success or failure.
  653 
  654     CLI Example:
  655 
  656     .. code-block:: bash
  657 
  658         salt '*' consul.agent_leave node='web1.example.com'
  659 
  660     """
  661     ret = {}
  662     query_params = {}
  663     if not consul_url:
  664         consul_url = _get_config()
  665         if not consul_url:
  666             log.error("No Consul URL found.")
  667             ret["message"] = "No Consul URL found."
  668             ret["res"] = False
  669             return ret
  670 
  671     if not node:
  672         raise SaltInvocationError('Required argument "node" is missing.')
  673 
  674     function = "agent/force-leave/{}".format(node)
  675     res = _query(
  676         consul_url=consul_url,
  677         function=function,
  678         token=token,
  679         method="GET",
  680         query_params=query_params,
  681     )
  682     if res["res"]:
  683         ret["res"] = True
  684         ret["message"] = "Node {} put in leave state.".format(node)
  685     else:
  686         ret["res"] = False
  687         ret["message"] = "Unable to change state for {}.".format(node)
  688     return ret
  689 
  690 
  691 def agent_check_register(consul_url=None, token=None, **kwargs):
  692     """
  693     The register endpoint is used to add a new check to the local agent.
  694 
  695     :param consul_url: The Consul server URL.
  696     :param name: The description of what the check is for.
  697     :param id: The unique name to use for the check, if not
  698                provided 'name' is used.
  699     :param notes: Human readable description of the check.
  700     :param script: If script is provided, the check type is
  701                    a script, and Consul will evaluate that script
  702                    based on the interval parameter.
  703     :param http: Check will perform an HTTP GET request against
  704                  the value of HTTP (expected to be a URL) based
  705                  on the interval parameter.
  706     :param ttl: If a TTL type is used, then the TTL update endpoint
  707                 must be used periodically to update the state of the check.
  708     :param interval: Interval at which the check should run.
  709     :return: Boolean and message indicating success or failure.
  710 
  711     CLI Example:
  712 
  713     .. code-block:: bash
  714 
  715         salt '*' consul.agent_check_register name='Memory Utilization' script='/usr/local/bin/check_mem.py' interval='15s'
  716 
  717     """
  718     ret = {}
  719     data = {}
  720     if not consul_url:
  721         consul_url = _get_config()
  722         if not consul_url:
  723             log.error("No Consul URL found.")
  724             ret["message"] = "No Consul URL found."
  725             ret["res"] = False
  726             return ret
  727 
  728     if "name" in kwargs:
  729         data["Name"] = kwargs["name"]
  730     else:
  731         raise SaltInvocationError('Required argument "name" is missing.')
  732 
  733     if True not in [True for item in ("script", "http", "ttl") if item in kwargs]:
  734         ret["message"] = 'Required parameter "script" or "http" is missing.'
  735         ret["res"] = False
  736         return ret
  737 
  738     if "id" in kwargs:
  739         data["ID"] = kwargs["id"]
  740 
  741     if "notes" in kwargs:
  742         data["Notes"] = kwargs["notes"]
  743 
  744     if "script" in kwargs:
  745         if "interval" not in kwargs:
  746             ret["message"] = 'Required parameter "interval" is missing.'
  747             ret["res"] = False
  748             return ret
  749         data["Script"] = kwargs["script"]
  750         data["Interval"] = kwargs["interval"]
  751 
  752     if "http" in kwargs:
  753         if "interval" not in kwargs:
  754             ret["message"] = 'Required parameter "interval" is missing.'
  755             ret["res"] = False
  756             return ret
  757         data["HTTP"] = kwargs["http"]
  758         data["Interval"] = kwargs["interval"]
  759 
  760     if "ttl" in kwargs:
  761         data["TTL"] = kwargs["ttl"]
  762 
  763     function = "agent/check/register"
  764     res = _query(
  765         consul_url=consul_url, function=function, token=token, method="PUT", data=data
  766     )
  767 
  768     if res["res"]:
  769         ret["res"] = True
  770         ret["message"] = "Check {} added to agent.".format(kwargs["name"])
  771     else:
  772         ret["res"] = False
  773         ret["message"] = "Unable to add check to agent."
  774     return ret
  775 
  776 
  777 def agent_check_deregister(consul_url=None, token=None, checkid=None):
  778     """
  779     The agent will take care of deregistering the check from the Catalog.
  780 
  781     :param consul_url: The Consul server URL.
  782     :param checkid: The ID of the check to deregister from Consul.
  783     :return: Boolean and message indicating success or failure.
  784 
  785     CLI Example:
  786 
  787     .. code-block:: bash
  788 
  789         salt '*' consul.agent_check_deregister checkid='Memory Utilization'
  790 
  791     """
  792     ret = {}
  793     if not consul_url:
  794         consul_url = _get_config()
  795         if not consul_url:
  796             log.error("No Consul URL found.")
  797             ret["message"] = "No Consul URL found."
  798             ret["res"] = False
  799             return ret
  800 
  801     if not checkid:
  802         raise SaltInvocationError('Required argument "checkid" is missing.')
  803 
  804     function = "agent/check/deregister/{}".format(checkid)
  805     res = _query(consul_url=consul_url, function=function, token=token, method="GET")
  806     if res["res"]:
  807         ret["res"] = True
  808         ret["message"] = "Check {} removed from agent.".format(checkid)
  809     else:
  810         ret["res"] = False
  811         ret["message"] = "Unable to remove check from agent."
  812     return ret
  813 
  814 
  815 def agent_check_pass(consul_url=None, token=None, checkid=None, **kwargs):
  816     """
  817     This endpoint is used with a check that is of the TTL type. When this
  818     is called, the status of the check is set to passing and the TTL
  819     clock is reset.
  820 
  821     :param consul_url: The Consul server URL.
  822     :param checkid: The ID of the check to mark as passing.
  823     :param note: A human-readable message with the status of the check.
  824     :return: Boolean and message indicating success or failure.
  825 
  826     CLI Example:
  827 
  828     .. code-block:: bash
  829 
  830         salt '*' consul.agent_check_pass checkid='redis_check1' note='Forcing check into passing state.'
  831 
  832     """
  833     ret = {}
  834     query_params = {}
  835     if not consul_url:
  836         consul_url = _get_config()
  837         if not consul_url:
  838             log.error("No Consul URL found.")
  839             ret["message"] = "No Consul URL found."
  840             ret["res"] = False
  841             return ret
  842 
  843     if not checkid:
  844         raise SaltInvocationError('Required argument "checkid" is missing.')
  845 
  846     if "note" in kwargs:
  847         query_params["note"] = kwargs["note"]
  848 
  849     function = "agent/check/pass/{}".format(checkid)
  850     res = _query(
  851         consul_url=consul_url,
  852         function=function,
  853         token=token,
  854         query_params=query_params,
  855         method="GET",
  856     )
  857     if res["res"]:
  858         ret["res"] = True
  859         ret["message"] = "Check {} marked as passing.".format(checkid)
  860     else:
  861         ret["res"] = False
  862         ret["message"] = "Unable to update check {}.".format(checkid)
  863     return ret
  864 
  865 
  866 def agent_check_warn(consul_url=None, token=None, checkid=None, **kwargs):
  867     """
  868     This endpoint is used with a check that is of the TTL type. When this
  869     is called, the status of the check is set to warning and the TTL
  870     clock is reset.
  871 
  872     :param consul_url: The Consul server URL.
  873     :param checkid: The ID of the check to deregister from Consul.
  874     :param note: A human-readable message with the status of the check.
  875     :return: Boolean and message indicating success or failure.
  876 
  877     CLI Example:
  878 
  879     .. code-block:: bash
  880 
  881         salt '*' consul.agent_check_warn checkid='redis_check1' note='Forcing check into warning state.'
  882 
  883     """
  884     ret = {}
  885     query_params = {}
  886     if not consul_url:
  887         consul_url = _get_config()
  888         if not consul_url:
  889             log.error("No Consul URL found.")
  890             ret["message"] = "No Consul URL found."
  891             ret["res"] = False
  892             return ret
  893 
  894     if not checkid:
  895         raise SaltInvocationError('Required argument "checkid" is missing.')
  896 
  897     if "note" in kwargs:
  898         query_params["note"] = kwargs["note"]
  899 
  900     function = "agent/check/warn/{}".format(checkid)
  901     res = _query(
  902         consul_url=consul_url,
  903         function=function,
  904         token=token,
  905         query_params=query_params,
  906         method="GET",
  907     )
  908     if res["res"]:
  909         ret["res"] = True
  910         ret["message"] = "Check {} marked as warning.".format(checkid)
  911     else:
  912         ret["res"] = False
  913         ret["message"] = "Unable to update check {}.".format(checkid)
  914     return ret
  915 
  916 
  917 def agent_check_fail(consul_url=None, token=None, checkid=None, **kwargs):
  918     """
  919     This endpoint is used with a check that is of the TTL type. When this
  920     is called, the status of the check is set to critical and the
  921     TTL clock is reset.
  922 
  923     :param consul_url: The Consul server URL.
  924     :param checkid: The ID of the check to deregister from Consul.
  925     :param note: A human-readable message with the status of the check.
  926     :return: Boolean and message indicating success or failure.
  927 
  928     CLI Example:
  929 
  930     .. code-block:: bash
  931 
  932         salt '*' consul.agent_check_fail checkid='redis_check1' note='Forcing check into critical state.'
  933 
  934     """
  935     ret = {}
  936     query_params = {}
  937     if not consul_url:
  938         consul_url = _get_config()
  939         if not consul_url:
  940             log.error("No Consul URL found.")
  941             ret["message"] = "No Consul URL found."
  942             ret["res"] = False
  943             return ret
  944 
  945     if not checkid:
  946         raise SaltInvocationError('Required argument "checkid" is missing.')
  947 
  948     if "note" in kwargs:
  949         query_params["note"] = kwargs["note"]
  950 
  951     function = "agent/check/fail/{}".format(checkid)
  952     res = _query(
  953         consul_url=consul_url,
  954         function=function,
  955         token=token,
  956         query_params=query_params,
  957         method="GET",
  958     )
  959     if res["res"]:
  960         ret["res"] = True
  961         ret["message"] = "Check {} marked as critical.".format(checkid)
  962     else:
  963         ret["res"] = False
  964         ret["message"] = "Unable to update check {}.".format(checkid)
  965     return ret
  966 
  967 
  968 def agent_service_register(consul_url=None, token=None, **kwargs):
  969     """
  970     The used to add a new service, with an optional
  971     health check, to the local agent.
  972 
  973     :param consul_url: The Consul server URL.
  974     :param name: A name describing the service.
  975     :param address: The address used by the service, defaults
  976                     to the address of the agent.
  977     :param port: The port used by the service.
  978     :param id: Unique ID to identify the service, if not
  979                provided the value of the name parameter is used.
  980     :param tags: Identifying tags for service, string or list.
  981     :param script: If script is provided, the check type is
  982                    a script, and Consul will evaluate that script
  983                    based on the interval parameter.
  984     :param http: Check will perform an HTTP GET request against
  985                  the value of HTTP (expected to be a URL) based
  986                  on the interval parameter.
  987     :param check_ttl: If a TTL type is used, then the TTL update
  988                       endpoint must be used periodically to update
  989                       the state of the check.
  990     :param check_interval: Interval at which the check should run.
  991     :return: Boolean and message indicating success or failure.
  992 
  993     CLI Example:
  994 
  995     .. code-block:: bash
  996 
  997         salt '*' consul.agent_service_register name='redis' tags='["master", "v1"]' address="127.0.0.1" port="8080" check_script="/usr/local/bin/check_redis.py" interval="10s"
  998 
  999     """
 1000     ret = {}
 1001     data = {}
 1002     if not consul_url:
 1003         consul_url = _get_config()
 1004         if not consul_url:
 1005             log.error("No Consul URL found.")
 1006             ret["message"] = "No Consul URL found."
 1007             ret["res"] = False
 1008             return ret
 1009 
 1010     lc_kwargs = dict()
 1011     for k, v in kwargs.items():
 1012         lc_kwargs[k.lower()] = v
 1013 
 1014     if "name" in lc_kwargs:
 1015         data["Name"] = lc_kwargs["name"]
 1016     else:
 1017         raise SaltInvocationError('Required argument "name" is missing.')
 1018 
 1019     if "address" in lc_kwargs:
 1020         data["Address"] = lc_kwargs["address"]
 1021 
 1022     if "port" in lc_kwargs:
 1023         data["Port"] = lc_kwargs["port"]
 1024 
 1025     if "id" in lc_kwargs:
 1026         data["ID"] = lc_kwargs["id"]
 1027 
 1028     if "tags" in lc_kwargs:
 1029         _tags = lc_kwargs["tags"]
 1030         if not isinstance(_tags, list):
 1031             _tags = [_tags]
 1032         data["Tags"] = _tags
 1033 
 1034     if "enabletagoverride" in lc_kwargs:
 1035         data["EnableTagOverride"] = lc_kwargs["enabletagoverride"]
 1036 
 1037     if "check" in lc_kwargs:
 1038         dd = dict()
 1039         for k, v in lc_kwargs["check"].items():
 1040             dd[k.lower()] = v
 1041         interval_required = False
 1042         check_dd = dict()
 1043 
 1044         if "script" in dd:
 1045             interval_required = True
 1046             check_dd["Script"] = dd["script"]
 1047         if "http" in dd:
 1048             interval_required = True
 1049             check_dd["HTTP"] = dd["http"]
 1050         if "ttl" in dd:
 1051             check_dd["TTL"] = dd["ttl"]
 1052         if "interval" in dd:
 1053             check_dd["Interval"] = dd["interval"]
 1054 
 1055         if interval_required:
 1056             if "Interval" not in check_dd:
 1057                 ret["message"] = 'Required parameter "interval" is missing.'
 1058                 ret["res"] = False
 1059                 return ret
 1060         else:
 1061             if "Interval" in check_dd:
 1062                 del check_dd["Interval"]  # not required, so ignore it
 1063 
 1064         if check_dd:
 1065             data["Check"] = check_dd  # if empty, ignore it
 1066 
 1067     function = "agent/service/register"
 1068     res = _query(
 1069         consul_url=consul_url, function=function, token=token, method="PUT", data=data
 1070     )
 1071     if res["res"]:
 1072         ret["res"] = True
 1073         ret["message"] = "Service {} registered on agent.".format(kwargs["name"])
 1074     else:
 1075         ret["res"] = False
 1076         ret["message"] = "Unable to register service {}.".format(kwargs["name"])
 1077     return ret
 1078 
 1079 
 1080 def agent_service_deregister(consul_url=None, token=None, serviceid=None):
 1081     """
 1082     Used to remove a service.
 1083 
 1084     :param consul_url: The Consul server URL.
 1085     :param serviceid: A serviceid describing the service.
 1086     :return: Boolean and message indicating success or failure.
 1087 
 1088     CLI Example:
 1089 
 1090     .. code-block:: bash
 1091 
 1092         salt '*' consul.agent_service_deregister serviceid='redis'
 1093 
 1094     """
 1095     ret = {}
 1096     data = {}
 1097     if not consul_url:
 1098         consul_url = _get_config()
 1099         if not consul_url:
 1100             log.error("No Consul URL found.")
 1101             ret["message"] = "No Consul URL found."
 1102             ret["res"] = False
 1103             return ret
 1104 
 1105     if not serviceid:
 1106         raise SaltInvocationError('Required argument "serviceid" is missing.')
 1107 
 1108     function = "agent/service/deregister/{}".format(serviceid)
 1109     res = _query(
 1110         consul_url=consul_url, function=function, token=token, method="PUT", data=data
 1111     )
 1112     if res["res"]:
 1113         ret["res"] = True
 1114         ret["message"] = "Service {} removed from agent.".format(serviceid)
 1115     else:
 1116         ret["res"] = False
 1117         ret["message"] = "Unable to remove service {}.".format(serviceid)
 1118     return ret
 1119 
 1120 
 1121 def agent_service_maintenance(consul_url=None, token=None, serviceid=None, **kwargs):
 1122     """
 1123     Used to place a service into maintenance mode.
 1124 
 1125     :param consul_url: The Consul server URL.
 1126     :param serviceid: A name of the service.
 1127     :param enable: Whether the service should be enabled or disabled.
 1128     :param reason: A human readable message of why the service was
 1129                    enabled or disabled.
 1130     :return: Boolean and message indicating success or failure.
 1131 
 1132     CLI Example:
 1133 
 1134     .. code-block:: bash
 1135 
 1136         salt '*' consul.agent_service_deregister serviceid='redis' enable='True' reason='Down for upgrade'
 1137 
 1138     """
 1139     ret = {}
 1140     query_params = {}
 1141     if not consul_url:
 1142         consul_url = _get_config()
 1143         if not consul_url:
 1144             log.error("No Consul URL found.")
 1145             ret["message"] = "No Consul URL found."
 1146             ret["res"] = False
 1147             return ret
 1148 
 1149     if not serviceid:
 1150         raise SaltInvocationError('Required argument "serviceid" is missing.')
 1151 
 1152     if "enable" in kwargs:
 1153         query_params["enable"] = kwargs["enable"]
 1154     else:
 1155         ret["message"] = 'Required parameter "enable" is missing.'
 1156         ret["res"] = False
 1157         return ret
 1158 
 1159     if "reason" in kwargs:
 1160         query_params["reason"] = kwargs["reason"]
 1161 
 1162     function = "agent/service/maintenance/{}".format(serviceid)
 1163     res = _query(
 1164         consul_url=consul_url, token=token, function=function, query_params=query_params
 1165     )
 1166 
 1167     if res["res"]:
 1168         ret["res"] = True
 1169         ret["message"] = "Service {} set in " "maintenance mode.".format(serviceid)
 1170     else:
 1171         ret["res"] = False
 1172         ret["message"] = "Unable to set service " "{} to maintenance mode.".format(
 1173             serviceid
 1174         )
 1175     return ret
 1176 
 1177 
 1178 def session_create(consul_url=None, token=None, **kwargs):
 1179     """
 1180     Used to create a session.
 1181 
 1182     :param consul_url: The Consul server URL.
 1183     :param lockdelay: Duration string using a "s" suffix for seconds.
 1184                       The default is 15s.
 1185     :param node: Must refer to a node that is already registered,
 1186                  if specified. By default, the agent's own node
 1187                  name is used.
 1188     :param name: A human-readable name for the session
 1189     :param checks: A list of associated health checks. It is highly
 1190                    recommended that, if you override this list, you
 1191                    include the default "serfHealth".
 1192     :param behavior: Can be set to either release or delete. This controls
 1193                      the behavior when a session is invalidated. By default,
 1194                      this is release, causing any locks that are held to be
 1195                      released. Changing this to delete causes any locks that
 1196                      are held to be deleted. delete is useful for creating
 1197                      ephemeral key/value entries.
 1198     :param ttl: Session is invalidated if it is not renewed before
 1199                 the TTL expires
 1200     :return: Boolean and message indicating success or failure.
 1201 
 1202     CLI Example:
 1203 
 1204     .. code-block:: bash
 1205 
 1206         salt '*' consul.session_create node='node1' name='my-session' behavior='delete' ttl='3600s'
 1207 
 1208     """
 1209     ret = {}
 1210     if not consul_url:
 1211         consul_url = _get_config()
 1212         if not consul_url:
 1213             log.error("No Consul URL found.")
 1214             ret["message"] = "No Consul URL found."
 1215             ret["res"] = False
 1216             return ret
 1217     data = {}
 1218 
 1219     if "lockdelay" in kwargs:
 1220         data["LockDelay"] = kwargs["lockdelay"]
 1221 
 1222     if "node" in kwargs:
 1223         data["Node"] = kwargs["node"]
 1224 
 1225     if "name" in kwargs:
 1226         data["Name"] = kwargs["name"]
 1227     else:
 1228         raise SaltInvocationError('Required argument "name" is missing.')
 1229 
 1230     if "checks" in kwargs:
 1231         data["Touch"] = kwargs["touch"]
 1232 
 1233     if "behavior" in kwargs:
 1234         if not kwargs["behavior"] in ("delete", "release"):
 1235             ret["message"] = ("Behavior must be ", "either delete or release.")
 1236             ret["res"] = False
 1237             return ret
 1238         data["Behavior"] = kwargs["behavior"]
 1239 
 1240     if "ttl" in kwargs:
 1241         _ttl = kwargs["ttl"]
 1242         if str(_ttl).endswith("s"):
 1243             _ttl = _ttl[:-1]
 1244 
 1245         if int(_ttl) < 0 or int(_ttl) > 3600:
 1246             ret["message"] = ("TTL must be ", "between 0 and 3600.")
 1247             ret["res"] = False
 1248             return ret
 1249         data["TTL"] = "{}s".format(_ttl)
 1250 
 1251     function = "session/create"
 1252     res = _query(
 1253         consul_url=consul_url, function=function, token=token, method="PUT", data=data
 1254     )
 1255 
 1256     if res["res"]:
 1257         ret["res"] = True
 1258         ret["message"] = "Created session {}.".format(kwargs["name"])
 1259     else:
 1260         ret["res"] = False
 1261         ret["message"] = "Unable to create session {}.".format(kwargs["name"])
 1262     return ret
 1263 
 1264 
 1265 def session_list(consul_url=None, token=None, return_list=False, **kwargs):
 1266     """
 1267     Used to list sessions.
 1268 
 1269     :param consul_url: The Consul server URL.
 1270     :param dc: By default, the datacenter of the agent is queried;
 1271                however, the dc can be provided using the "dc" parameter.
 1272     :param return_list: By default, all information about the sessions is
 1273                         returned, using the return_list parameter will return
 1274                         a list of session IDs.
 1275     :return: A list of all available sessions.
 1276 
 1277     CLI Example:
 1278 
 1279     .. code-block:: bash
 1280 
 1281         salt '*' consul.session_list
 1282 
 1283     """
 1284     ret = {}
 1285     if not consul_url:
 1286         consul_url = _get_config()
 1287         if not consul_url:
 1288             log.error("No Consul URL found.")
 1289             ret["message"] = "No Consul URL found."
 1290             ret["res"] = False
 1291             return ret
 1292 
 1293     query_params = {}
 1294 
 1295     if "dc" in kwargs:
 1296         query_params["dc"] = kwargs["dc"]
 1297 
 1298     function = "session/list"
 1299     ret = _query(
 1300         consul_url=consul_url, function=function, token=token, query_params=query_params
 1301     )
 1302 
 1303     if return_list:
 1304         _list = []
 1305         for item in ret["data"]:
 1306             _list.append(item["ID"])
 1307         return _list
 1308     return ret
 1309 
 1310 
 1311 def session_destroy(consul_url=None, token=None, session=None, **kwargs):
 1312     """
 1313     Destroy session
 1314 
 1315     :param consul_url: The Consul server URL.
 1316     :param session: The ID of the session to destroy.
 1317     :param dc: By default, the datacenter of the agent is queried;
 1318                however, the dc can be provided using the "dc" parameter.
 1319     :return: Boolean & message of success or failure.
 1320 
 1321     CLI Example:
 1322 
 1323     .. code-block:: bash
 1324 
 1325         salt '*' consul.session_destroy session='c1c4d223-91cb-3d1f-1ee8-f2af9e7b6716'
 1326 
 1327     """
 1328     ret = {}
 1329     if not consul_url:
 1330         consul_url = _get_config()
 1331         if not consul_url:
 1332             log.error("No Consul URL found.")
 1333             ret["message"] = "No Consul URL found."
 1334             ret["res"] = False
 1335             return ret
 1336 
 1337     if not session:
 1338         raise SaltInvocationError('Required argument "session" is missing.')
 1339 
 1340     query_params = {}
 1341 
 1342     if "dc" in kwargs:
 1343         query_params["dc"] = kwargs["dc"]
 1344 
 1345     function = "session/destroy/{}".format(session)
 1346     res = _query(
 1347         consul_url=consul_url, function=function, token=token, query_params=query_params
 1348     )
 1349     if res["res"]:
 1350         ret["res"] = True
 1351         ret["message"] = "Created Service {}.".format(kwargs["name"])
 1352     else:
 1353         ret["res"] = False
 1354         ret["message"] = "Unable to create service {}.".format(kwargs["name"])
 1355     return ret
 1356 
 1357 
 1358 def session_info(consul_url=None, token=None, session=None, **kwargs):
 1359     """
 1360     Information about a session
 1361 
 1362     :param consul_url: The Consul server URL.
 1363     :param session: The ID of the session to return information about.
 1364     :param dc: By default, the datacenter of the agent is queried;
 1365                however, the dc can be provided using the "dc" parameter.
 1366     :return: Boolean & message of success or failure.
 1367 
 1368     CLI Example:
 1369 
 1370     .. code-block:: bash
 1371 
 1372         salt '*' consul.session_info session='c1c4d223-91cb-3d1f-1ee8-f2af9e7b6716'
 1373 
 1374     """
 1375     ret = {}
 1376     if not consul_url:
 1377         consul_url = _get_config()
 1378         if not consul_url:
 1379             log.error("No Consul URL found.")
 1380             ret["message"] = "No Consul URL found."
 1381             ret["res"] = False
 1382             return ret
 1383 
 1384     if not session:
 1385         raise SaltInvocationError('Required argument "session" is missing.')
 1386 
 1387     query_params = {}
 1388 
 1389     if "dc" in kwargs:
 1390         query_params["dc"] = kwargs["dc"]
 1391 
 1392     function = "session/info/{}".format(session)
 1393     ret = _query(
 1394         consul_url=consul_url, function=function, token=token, query_params=query_params
 1395     )
 1396     return ret
 1397 
 1398 
 1399 def catalog_register(consul_url=None, token=None, **kwargs):
 1400     """
 1401     Registers a new node, service, or check
 1402 
 1403     :param consul_url: The Consul server URL.
 1404     :param dc: By default, the datacenter of the agent is queried;
 1405                however, the dc can be provided using the "dc" parameter.
 1406     :param node: The node to register.
 1407     :param address: The address of the node.
 1408     :param service: The service that will be registered.
 1409     :param service_address: The address that the service listens on.
 1410     :param service_port: The port for the service.
 1411     :param service_id: A unique identifier for the service, if this is not
 1412                        provided "name" will be used.
 1413     :param service_tags: Any tags associated with the service.
 1414     :param check: The name of the health check to register
 1415     :param check_status: The initial status of the check,
 1416                          must be one of unknown, passing, warning, or critical.
 1417     :param check_service: The service that the check is performed against.
 1418     :param check_id: Unique identifier for the service.
 1419     :param check_notes: An opaque field that is meant to hold human-readable text.
 1420     :return: Boolean & message of success or failure.
 1421 
 1422     CLI Example:
 1423 
 1424     .. code-block:: bash
 1425 
 1426         salt '*' consul.catalog_register node='node1' address='192.168.1.1' service='redis' service_address='127.0.0.1' service_port='8080' service_id='redis_server1'
 1427 
 1428     """
 1429     ret = {}
 1430     data = {}
 1431     data["NodeMeta"] = {}
 1432     if not consul_url:
 1433         consul_url = _get_config()
 1434         if not consul_url:
 1435             log.error("No Consul URL found.")
 1436             ret["message"] = "No Consul URL found."
 1437             ret["res"] = False
 1438             return ret
 1439 
 1440     if "datacenter" in kwargs:
 1441         data["Datacenter"] = kwargs["datacenter"]
 1442 
 1443     if "node" in kwargs:
 1444         data["Node"] = kwargs["node"]
 1445     else:
 1446         ret["message"] = "Required argument node argument is missing."
 1447         ret["res"] = False
 1448         return ret
 1449 
 1450     if "address" in kwargs:
 1451         if isinstance(kwargs["address"], list):
 1452             _address = kwargs["address"][0]
 1453         else:
 1454             _address = kwargs["address"]
 1455         data["Address"] = _address
 1456     else:
 1457         ret["message"] = "Required argument address argument is missing."
 1458         ret["res"] = False
 1459         return ret
 1460 
 1461     if "ip_interfaces" in kwargs:
 1462         data["TaggedAddresses"] = {}
 1463         for k in kwargs["ip_interfaces"]:
 1464             if kwargs["ip_interfaces"].get(k):
 1465                 data["TaggedAddresses"][k] = kwargs["ip_interfaces"][k][0]
 1466 
 1467     if "service" in kwargs:
 1468         data["Service"] = {}
 1469         data["Service"]["Service"] = kwargs["service"]
 1470 
 1471         if "service_address" in kwargs:
 1472             data["Service"]["Address"] = kwargs["service_address"]
 1473 
 1474         if "service_port" in kwargs:
 1475             data["Service"]["Port"] = kwargs["service_port"]
 1476 
 1477         if "service_id" in kwargs:
 1478             data["Service"]["ID"] = kwargs["service_id"]
 1479 
 1480         if "service_tags" in kwargs:
 1481             _tags = kwargs["service_tags"]
 1482             if not isinstance(_tags, list):
 1483                 _tags = [_tags]
 1484             data["Service"]["Tags"] = _tags
 1485 
 1486     if "cpu" in kwargs:
 1487         data["NodeMeta"]["Cpu"] = kwargs["cpu"]
 1488 
 1489     if "num_cpus" in kwargs:
 1490         data["NodeMeta"]["Cpu_num"] = kwargs["num_cpus"]
 1491 
 1492     if "mem" in kwargs:
 1493         data["NodeMeta"]["Memory"] = kwargs["mem"]
 1494 
 1495     if "oscode" in kwargs:
 1496         data["NodeMeta"]["Os"] = kwargs["oscode"]
 1497 
 1498     if "osarch" in kwargs:
 1499         data["NodeMeta"]["Osarch"] = kwargs["osarch"]
 1500 
 1501     if "kernel" in kwargs:
 1502         data["NodeMeta"]["Kernel"] = kwargs["kernel"]
 1503 
 1504     if "kernelrelease" in kwargs:
 1505         data["NodeMeta"]["Kernelrelease"] = kwargs["kernelrelease"]
 1506 
 1507     if "localhost" in kwargs:
 1508         data["NodeMeta"]["localhost"] = kwargs["localhost"]
 1509 
 1510     if "nodename" in kwargs:
 1511         data["NodeMeta"]["nodename"] = kwargs["nodename"]
 1512 
 1513     if "os_family" in kwargs:
 1514         data["NodeMeta"]["os_family"] = kwargs["os_family"]
 1515 
 1516     if "lsb_distrib_description" in kwargs:
 1517         data["NodeMeta"]["lsb_distrib_description"] = kwargs["lsb_distrib_description"]
 1518 
 1519     if "master" in kwargs:
 1520         data["NodeMeta"]["master"] = kwargs["master"]
 1521 
 1522     if "check" in kwargs:
 1523         data["Check"] = {}
 1524         data["Check"]["Name"] = kwargs["check"]
 1525 
 1526         if "check_status" in kwargs:
 1527             if kwargs["check_status"] not in (
 1528                 "unknown",
 1529                 "passing",
 1530                 "warning",
 1531                 "critical",
 1532             ):
 1533                 ret[
 1534                     "message"
 1535                 ] = "Check status must be unknown, passing, warning, or critical."
 1536                 ret["res"] = False
 1537                 return ret
 1538             data["Check"]["Status"] = kwargs["check_status"]
 1539 
 1540         if "check_service" in kwargs:
 1541             data["Check"]["ServiceID"] = kwargs["check_service"]
 1542 
 1543         if "check_id" in kwargs:
 1544             data["Check"]["CheckID"] = kwargs["check_id"]
 1545 
 1546         if "check_notes" in kwargs:
 1547             data["Check"]["Notes"] = kwargs["check_notes"]
 1548 
 1549     function = "catalog/register"
 1550     res = _query(
 1551         consul_url=consul_url, function=function, token=token, method="PUT", data=data
 1552     )
 1553     if res["res"]:
 1554         ret["res"] = True
 1555         ret["message"] = "Catalog registration " "for {} successful.".format(
 1556             kwargs["node"]
 1557         )
 1558     else:
 1559         ret["res"] = False
 1560         ret["message"] = "Catalog registration " "for {} failed.".format(kwargs["node"])
 1561     ret["data"] = data
 1562     return ret
 1563 
 1564 
 1565 def catalog_deregister(consul_url=None, token=None, **kwargs):
 1566     """
 1567     Deregisters a node, service, or check
 1568 
 1569     :param consul_url: The Consul server URL.
 1570     :param node: The node to deregister.
 1571     :param datacenter: By default, the datacenter of the agent is queried;
 1572                however, the dc can be provided using the "dc" parameter.
 1573     :param checkid: The ID of the health check to deregister.
 1574     :param serviceid: The ID of the service to deregister.
 1575     :return: Boolean & message of success or failure.
 1576 
 1577     CLI Example:
 1578 
 1579     .. code-block:: bash
 1580 
 1581         salt '*' consul.catalog_register node='node1' serviceid='redis_server1' checkid='redis_check1'
 1582 
 1583     """
 1584     ret = {}
 1585     data = {}
 1586     if not consul_url:
 1587         consul_url = _get_config()
 1588         if not consul_url:
 1589             log.error("No Consul URL found.")
 1590             ret["message"] = "No Consul URL found."
 1591             ret["res"] = False
 1592             return ret
 1593 
 1594     if "datacenter" in kwargs:
 1595         data["Datacenter"] = kwargs["datacenter"]
 1596 
 1597     if "node" in kwargs:
 1598         data["Node"] = kwargs["node"]
 1599     else:
 1600         ret["message"] = "Node argument required."
 1601         ret["res"] = False
 1602         return ret
 1603 
 1604     if "checkid" in kwargs:
 1605         data["CheckID"] = kwargs["checkid"]
 1606 
 1607     if "serviceid" in kwargs:
 1608         data["ServiceID"] = kwargs["serviceid"]
 1609 
 1610     function = "catalog/deregister"
 1611     res = _query(
 1612         consul_url=consul_url, function=function, token=token, method="PUT", data=data
 1613     )
 1614 
 1615     if res["res"]:
 1616         ret["res"] = True
 1617         ret["message"] = "Catalog item {} removed.".format(kwargs["node"])
 1618     else:
 1619         ret["res"] = False
 1620         ret["message"] = "Removing Catalog " "item {} failed.".format(kwargs["node"])
 1621     return ret
 1622 
 1623 
 1624 def catalog_datacenters(consul_url=None, token=None):
 1625     """
 1626     Return list of available datacenters from catalog.
 1627 
 1628     :param consul_url: The Consul server URL.
 1629     :return: The list of available datacenters.
 1630 
 1631     CLI Example:
 1632 
 1633     .. code-block:: bash
 1634 
 1635         salt '*' consul.catalog_datacenters
 1636 
 1637     """
 1638     ret = {}
 1639     if not consul_url:
 1640         consul_url = _get_config()
 1641         if not consul_url:
 1642             log.error("No Consul URL found.")
 1643             ret["message"] = "No Consul URL found."
 1644             ret["res"] = False
 1645             return ret
 1646 
 1647     function = "catalog/datacenters"
 1648     ret = _query(consul_url=consul_url, function=function, token=token)
 1649     return ret
 1650 
 1651 
 1652 def catalog_nodes(consul_url=None, token=None, **kwargs):
 1653     """
 1654     Return list of available nodes from catalog.
 1655 
 1656     :param consul_url: The Consul server URL.
 1657     :param dc: By default, the datacenter of the agent is queried;
 1658                however, the dc can be provided using the "dc" parameter.
 1659     :return: The list of available nodes.
 1660 
 1661     CLI Example:
 1662 
 1663     .. code-block:: bash
 1664 
 1665         salt '*' consul.catalog_nodes
 1666 
 1667     """
 1668     ret = {}
 1669     query_params = {}
 1670     if not consul_url:
 1671         consul_url = _get_config()
 1672         if not consul_url:
 1673             log.error("No Consul URL found.")
 1674             ret["message"] = "No Consul URL found."
 1675             ret["res"] = False
 1676             return ret
 1677 
 1678     if "dc" in kwargs:
 1679         query_params["dc"] = kwargs["dc"]
 1680 
 1681     function = "catalog/nodes"
 1682     ret = _query(
 1683         consul_url=consul_url, function=function, token=token, query_params=query_params
 1684     )
 1685     return ret
 1686 
 1687 
 1688 def catalog_services(consul_url=None, token=None, **kwargs):
 1689     """
 1690     Return list of available services rom catalog.
 1691 
 1692     :param consul_url: The Consul server URL.
 1693     :param dc: By default, the datacenter of the agent is queried;
 1694                however, the dc can be provided using the "dc" parameter.
 1695     :return: The list of available services.
 1696 
 1697     CLI Example:
 1698 
 1699     .. code-block:: bash
 1700 
 1701         salt '*' consul.catalog_services
 1702 
 1703     """
 1704     ret = {}
 1705     query_params = {}
 1706     if not consul_url:
 1707         consul_url = _get_config()
 1708         if not consul_url:
 1709             log.error("No Consul URL found.")
 1710             ret["message"] = "No Consul URL found."
 1711             ret["res"] = False
 1712             return ret
 1713 
 1714     if "dc" in kwargs:
 1715         query_params["dc"] = kwargs["dc"]
 1716 
 1717     function = "catalog/services"
 1718     ret = _query(
 1719         consul_url=consul_url, function=function, token=token, query_params=query_params
 1720     )
 1721     return ret
 1722 
 1723 
 1724 def catalog_service(consul_url=None, token=None, service=None, **kwargs):
 1725     """
 1726     Information about the registered service.
 1727 
 1728     :param consul_url: The Consul server URL.
 1729     :param dc: By default, the datacenter of the agent is queried;
 1730                however, the dc can be provided using the "dc" parameter.
 1731     :param tag: Filter returned services with tag parameter.
 1732     :return: Information about the requested service.
 1733 
 1734     CLI Example:
 1735 
 1736     .. code-block:: bash
 1737 
 1738         salt '*' consul.catalog_service service='redis'
 1739 
 1740     """
 1741     ret = {}
 1742     query_params = {}
 1743     if not consul_url:
 1744         consul_url = _get_config()
 1745         if not consul_url:
 1746             log.error("No Consul URL found.")
 1747             ret["message"] = "No Consul URL found."
 1748             ret["res"] = False
 1749             return ret
 1750 
 1751     if not service:
 1752         raise SaltInvocationError('Required argument "service" is missing.')
 1753 
 1754     if "dc" in kwargs:
 1755         query_params["dc"] = kwargs["dc"]
 1756 
 1757     if "tag" in kwargs:
 1758         query_params["tag"] = kwargs["tag"]
 1759 
 1760     function = "catalog/service/{}".format(service)
 1761     ret = _query(
 1762         consul_url=consul_url, function=function, token=token, query_params=query_params
 1763     )
 1764     return ret
 1765 
 1766 
 1767 def catalog_node(consul_url=None, token=None, node=None, **kwargs):
 1768     """
 1769     Information about the registered node.
 1770 
 1771     :param consul_url: The Consul server URL.
 1772     :param node: The node to request information about.
 1773     :param dc: By default, the datacenter of the agent is queried;
 1774                however, the dc can be provided using the "dc" parameter.
 1775     :return: Information about the requested node.
 1776 
 1777     CLI Example:
 1778 
 1779     .. code-block:: bash
 1780 
 1781         salt '*' consul.catalog_service service='redis'
 1782 
 1783     """
 1784     ret = {}
 1785     query_params = {}
 1786     if not consul_url:
 1787         consul_url = _get_config()
 1788         if not consul_url:
 1789             log.error("No Consul URL found.")
 1790             ret["message"] = "No Consul URL found."
 1791             ret["res"] = False
 1792             return ret
 1793 
 1794     if not node:
 1795         raise SaltInvocationError('Required argument "node" is missing.')
 1796 
 1797     if "dc" in kwargs:
 1798         query_params["dc"] = kwargs["dc"]
 1799 
 1800     function = "catalog/node/{}".format(node)
 1801     ret = _query(
 1802         consul_url=consul_url, function=function, token=token, query_params=query_params
 1803     )
 1804     return ret
 1805 
 1806 
 1807 def health_node(consul_url=None, token=None, node=None, **kwargs):
 1808     """
 1809     Health information about the registered node.
 1810 
 1811     :param consul_url: The Consul server URL.
 1812     :param node: The node to request health information about.
 1813     :param dc: By default, the datacenter of the agent is queried;
 1814                however, the dc can be provided using the "dc" parameter.
 1815     :return: Health information about the requested node.
 1816 
 1817     CLI Example:
 1818 
 1819     .. code-block:: bash
 1820 
 1821         salt '*' consul.health_node node='node1'
 1822 
 1823     """
 1824     ret = {}
 1825     query_params = {}
 1826     if not consul_url:
 1827         consul_url = _get_config()
 1828         if not consul_url:
 1829             log.error("No Consul URL found.")
 1830             ret["message"] = "No Consul URL found."
 1831             ret["res"] = False
 1832             return ret
 1833 
 1834     if not node:
 1835         raise SaltInvocationError('Required argument "node" is missing.')
 1836 
 1837     if "dc" in kwargs:
 1838         query_params["dc"] = kwargs["dc"]
 1839 
 1840     function = "health/node/{}".format(node)
 1841     ret = _query(
 1842         consul_url=consul_url, function=function, token=token, query_params=query_params
 1843     )
 1844     return ret
 1845 
 1846 
 1847 def health_checks(consul_url=None, token=None, service=None, **kwargs):
 1848     """
 1849     Health information about the registered service.
 1850 
 1851     :param consul_url: The Consul server URL.
 1852     :param service: The service to request health information about.
 1853     :param dc: By default, the datacenter of the agent is queried;
 1854                however, the dc can be provided using the "dc" parameter.
 1855     :return: Health information about the requested node.
 1856 
 1857     CLI Example:
 1858 
 1859     .. code-block:: bash
 1860 
 1861         salt '*' consul.health_checks service='redis1'
 1862 
 1863     """
 1864     ret = {}
 1865     query_params = {}
 1866     if not consul_url:
 1867         consul_url = _get_config()
 1868         if not consul_url:
 1869             log.error("No Consul URL found.")
 1870             ret["message"] = "No Consul URL found."
 1871             ret["res"] = False
 1872             return ret
 1873 
 1874     if not service:
 1875         raise SaltInvocationError('Required argument "service" is missing.')
 1876 
 1877     if "dc" in kwargs:
 1878         query_params["dc"] = kwargs["dc"]
 1879 
 1880     function = "health/checks/{}".format(service)
 1881     ret = _query(
 1882         consul_url=consul_url, function=function, token=token, query_params=query_params
 1883     )
 1884     return ret
 1885 
 1886 
 1887 def health_service(consul_url=None, token=None, service=None, **kwargs):
 1888     """
 1889     Health information about the registered service.
 1890 
 1891     :param consul_url: The Consul server URL.
 1892     :param service: The service to request health information about.
 1893     :param dc: By default, the datacenter of the agent is queried;
 1894                however, the dc can be provided using the "dc" parameter.
 1895     :param tag: Filter returned services with tag parameter.
 1896     :param passing: Filter results to only nodes with all
 1897                     checks in the passing state.
 1898     :return: Health information about the requested node.
 1899 
 1900     CLI Example:
 1901 
 1902     .. code-block:: bash
 1903 
 1904         salt '*' consul.health_service service='redis1'
 1905 
 1906         salt '*' consul.health_service service='redis1' passing='True'
 1907 
 1908     """
 1909     ret = {}
 1910     query_params = {}
 1911     if not consul_url:
 1912         consul_url = _get_config()
 1913         if not consul_url:
 1914             log.error("No Consul URL found.")
 1915             ret["message"] = "No Consul URL found."
 1916             ret["res"] = False
 1917             return ret
 1918 
 1919     if not service:
 1920         raise SaltInvocationError('Required argument "service" is missing.')
 1921 
 1922     if "dc" in kwargs:
 1923         query_params["dc"] = kwargs["dc"]
 1924 
 1925     if "tag" in kwargs:
 1926         query_params["tag"] = kwargs["tag"]
 1927 
 1928     if "passing" in kwargs:
 1929         query_params["passing"] = kwargs["passing"]
 1930 
 1931     function = "health/service/{}".format(service)
 1932     ret = _query(
 1933         consul_url=consul_url, function=function, token=token, query_params=query_params
 1934     )
 1935     return ret
 1936 
 1937 
 1938 def health_state(consul_url=None, token=None, state=None, **kwargs):
 1939     """
 1940     Returns the checks in the state provided on the path.
 1941 
 1942     :param consul_url: The Consul server URL.
 1943     :param state: The state to show checks for. The supported states
 1944                   are any, unknown, passing, warning, or critical.
 1945                   The any state is a wildcard that can be used to
 1946                   return all checks.
 1947     :param dc: By default, the datacenter of the agent is queried;
 1948                however, the dc can be provided using the "dc" parameter.
 1949     :return: The checks in the provided state.
 1950 
 1951     CLI Example:
 1952 
 1953     .. code-block:: bash
 1954 
 1955         salt '*' consul.health_state state='redis1'
 1956 
 1957         salt '*' consul.health_state service='redis1' passing='True'
 1958 
 1959     """
 1960     ret = {}
 1961     query_params = {}
 1962     if not consul_url:
 1963         consul_url = _get_config()
 1964         if not consul_url:
 1965             log.error("No Consul URL found.")
 1966             ret["message"] = "No Consul URL found."
 1967             ret["res"] = False
 1968             return ret
 1969 
 1970     if not state:
 1971         raise SaltInvocationError('Required argument "state" is missing.')
 1972 
 1973     if "dc" in kwargs:
 1974         query_params["dc"] = kwargs["dc"]
 1975 
 1976     if state not in ("any", "unknown", "passing", "warning", "critical"):
 1977         ret["message"] = "State must be any, unknown, passing, warning, or critical."
 1978         ret["res"] = False
 1979         return ret
 1980 
 1981     function = "health/state/{}".format(state)
 1982     ret = _query(
 1983         consul_url=consul_url, function=function, token=token, query_params=query_params
 1984     )
 1985     return ret
 1986 
 1987 
 1988 def status_leader(consul_url=None, token=None):
 1989     """
 1990     Returns the current Raft leader
 1991 
 1992     :param consul_url: The Consul server URL.
 1993     :return: The address of the Raft leader.
 1994 
 1995     CLI Example:
 1996 
 1997     .. code-block:: bash
 1998 
 1999         salt '*' consul.status_leader
 2000 
 2001     """
 2002     ret = {}
 2003     if not consul_url:
 2004         consul_url = _get_config()
 2005         if not consul_url:
 2006             log.error("No Consul URL found.")
 2007             ret["message"] = "No Consul URL found."
 2008             ret["res"] = False
 2009             return ret
 2010 
 2011     function = "status/leader"
 2012     ret = _query(consul_url=consul_url, function=function, token=token)
 2013     return ret
 2014 
 2015 
 2016 def status_peers(consul_url, token=None):
 2017     """
 2018     Returns the current Raft peer set
 2019 
 2020     :param consul_url: The Consul server URL.
 2021     :return: Retrieves the Raft peers for the
 2022              datacenter in which the agent is running.
 2023 
 2024     CLI Example:
 2025 
 2026     .. code-block:: bash
 2027 
 2028         salt '*' consul.status_peers
 2029 
 2030     """
 2031     ret = {}
 2032     if not consul_url:
 2033         consul_url = _get_config()
 2034         if not consul_url:
 2035             log.error("No Consul URL found.")
 2036             ret["message"] = "No Consul URL found."
 2037             ret["res"] = False
 2038             return ret
 2039 
 2040     function = "status/peers"
 2041     ret = _query(consul_url=consul_url, function=function, token=token)
 2042     return ret
 2043 
 2044 
 2045 def acl_create(consul_url=None, token=None, **kwargs):
 2046     """
 2047     Create a new ACL token.
 2048 
 2049     :param consul_url: The Consul server URL.
 2050     :param name: Meaningful indicator of the ACL's purpose.
 2051     :param type: Type is either client or management. A management
 2052                  token is comparable to a root user and has the
 2053                  ability to perform any action including creating,
 2054                  modifying, and deleting ACLs.
 2055     :param rules: The Consul server URL.
 2056     :return: Boolean & message of success or failure.
 2057 
 2058     CLI Example:
 2059 
 2060     .. code-block:: bash
 2061 
 2062         salt '*' consul.acl_create
 2063 
 2064     """
 2065     ret = {}
 2066     data = {}
 2067     if not consul_url:
 2068         consul_url = _get_config()
 2069         if not consul_url:
 2070             log.error("No Consul URL found.")
 2071             ret["message"] = "No Consul URL found."
 2072             ret["res"] = False
 2073             return ret
 2074 
 2075     if "name" in kwargs:
 2076         data["Name"] = kwargs["name"]
 2077     else:
 2078         raise SaltInvocationError('Required argument "name" is missing.')
 2079 
 2080     if "type" in kwargs:
 2081         data["Type"] = kwargs["type"]
 2082 
 2083     if "rules" in kwargs:
 2084         data["Rules"] = kwargs["rules"]
 2085 
 2086     function = "acl/create"
 2087     res = _query(
 2088         consul_url=consul_url, token=token, data=data, method="PUT", function=function
 2089     )
 2090 
 2091     if res["res"]:
 2092         ret["res"] = True
 2093         ret["message"] = "ACL {} created.".format(kwargs["name"])
 2094     else:
 2095         ret["res"] = False
 2096         ret["message"] = "Removing Catalog " "item {} failed.".format(kwargs["name"])
 2097     return ret
 2098 
 2099 
 2100 def acl_update(consul_url=None, token=None, **kwargs):
 2101     """
 2102     Update an ACL token.
 2103 
 2104     :param consul_url: The Consul server URL.
 2105     :param name: Meaningful indicator of the ACL's purpose.
 2106     :param id: Unique identifier for the ACL to update.
 2107     :param type: Type is either client or management. A management
 2108                  token is comparable to a root user and has the
 2109                  ability to perform any action including creating,
 2110                  modifying, and deleting ACLs.
 2111     :param rules: The Consul server URL.
 2112     :return: Boolean & message of success or failure.
 2113 
 2114     CLI Example:
 2115 
 2116     .. code-block:: bash
 2117 
 2118         salt '*' consul.acl_update
 2119 
 2120     """
 2121     ret = {}
 2122     data = {}
 2123     if not consul_url:
 2124         consul_url = _get_config()
 2125         if not consul_url:
 2126             log.error("No Consul URL found.")
 2127             ret["message"] = "No Consul URL found."
 2128             ret["res"] = False
 2129             return ret
 2130 
 2131     if "id" in kwargs:
 2132         data["ID"] = kwargs["id"]
 2133     else:
 2134         ret["message"] = 'Required parameter "id" is missing.'
 2135         ret["res"] = False
 2136         return ret
 2137 
 2138     if "name" in kwargs:
 2139         data["Name"] = kwargs["name"]
 2140     else:
 2141         raise SaltInvocationError('Required argument "name" is missing.')
 2142 
 2143     if "type" in kwargs:
 2144         data["Type"] = kwargs["type"]
 2145 
 2146     if "rules" in kwargs:
 2147         data["Rules"] = kwargs["rules"]
 2148 
 2149     function = "acl/update"
 2150     res = _query(
 2151         consul_url=consul_url, token=token, data=data, method="PUT", function=function
 2152     )
 2153 
 2154     if res["res"]:
 2155         ret["res"] = True
 2156         ret["message"] = "ACL {} created.".format(kwargs["name"])
 2157     else:
 2158         ret["res"] = False
 2159         ret["message"] = "Adding ACL " "{} failed.".format(kwargs["name"])
 2160 
 2161     return ret
 2162 
 2163 
 2164 def acl_delete(consul_url=None, token=None, **kwargs):
 2165     """
 2166     Delete an ACL token.
 2167 
 2168     :param consul_url: The Consul server URL.
 2169     :param id: Unique identifier for the ACL to update.
 2170     :return: Boolean & message of success or failure.
 2171 
 2172     CLI Example:
 2173 
 2174     .. code-block:: bash
 2175 
 2176         salt '*' consul.acl_delete id='c1c4d223-91cb-3d1f-1ee8-f2af9e7b6716'
 2177 
 2178     """
 2179     ret = {}
 2180     data = {}
 2181     if not consul_url:
 2182         consul_url = _get_config()
 2183         if not consul_url:
 2184             log.error("No Consul URL found.")
 2185             ret["message"] = "No Consul URL found."
 2186             ret["res"] = False
 2187             return ret
 2188 
 2189     if "id" not in kwargs:
 2190         ret["message"] = 'Required parameter "id" is missing.'
 2191         ret["res"] = False
 2192         return ret
 2193 
 2194     function = "acl/delete/{}".format(kwargs["id"])
 2195     res = _query(
 2196         consul_url=consul_url, token=token, data=data, method="PUT", function=function
 2197     )
 2198 
 2199     if res["res"]:
 2200         ret["res"] = True
 2201         ret["message"] = "ACL {} deleted.".format(kwargs["id"])
 2202     else:
 2203         ret["res"] = False
 2204         ret["message"] = "Removing ACL " "{} failed.".format(kwargs["id"])
 2205 
 2206     return ret
 2207 
 2208 
 2209 def acl_info(consul_url=None, **kwargs):
 2210     """
 2211     Information about an ACL token.
 2212 
 2213     :param consul_url: The Consul server URL.
 2214     :param id: Unique identifier for the ACL to update.
 2215     :return: Information about the ACL requested.
 2216 
 2217     CLI Example:
 2218 
 2219     .. code-block:: bash
 2220 
 2221         salt '*' consul.acl_info id='c1c4d223-91cb-3d1f-1ee8-f2af9e7b6716'
 2222 
 2223     """
 2224     ret = {}
 2225     data = {}
 2226     if not consul_url:
 2227         consul_url = _get_config()
 2228         if not consul_url:
 2229             log.error("No Consul URL found.")
 2230             ret["message"] = "No Consul URL found."
 2231             ret["res"] = False
 2232             return ret
 2233 
 2234     if "id" not in kwargs:
 2235         ret["message"] = 'Required parameter "id" is missing.'
 2236         ret["res"] = False
 2237         return ret
 2238 
 2239     function = "acl/info/{}".format(kwargs["id"])
 2240     ret = _query(consul_url=consul_url, data=data, method="GET", function=function)
 2241     return ret
 2242 
 2243 
 2244 def acl_clone(consul_url=None, token=None, **kwargs):
 2245     """
 2246     Information about an ACL token.
 2247 
 2248     :param consul_url: The Consul server URL.
 2249     :param id: Unique identifier for the ACL to update.
 2250     :return: Boolean, message of success or
 2251              failure, and new ID of cloned ACL.
 2252 
 2253     CLI Example:
 2254 
 2255     .. code-block:: bash
 2256 
 2257         salt '*' consul.acl_info id='c1c4d223-91cb-3d1f-1ee8-f2af9e7b6716'
 2258 
 2259     """
 2260     ret = {}
 2261     data = {}
 2262     if not consul_url:
 2263         consul_url = _get_config()
 2264         if not consul_url:
 2265             log.error("No Consul URL found.")
 2266             ret["message"] = "No Consul URL found."
 2267             ret["res"] = False
 2268             return ret
 2269 
 2270     if "id" not in kwargs:
 2271         ret["message"] = 'Required parameter "id" is missing.'
 2272         ret["res"] = False
 2273         return ret
 2274 
 2275     function = "acl/clone/{}".format(kwargs["id"])
 2276     res = _query(
 2277         consul_url=consul_url, token=token, data=data, method="PUT", function=function
 2278     )
 2279     if res["res"]:
 2280         ret["res"] = True
 2281         ret["message"] = "ACL {} cloned.".format(kwargs["name"])
 2282         ret["ID"] = ret["data"]
 2283     else:
 2284         ret["res"] = False
 2285         ret["message"] = "Cloning ACL" "item {} failed.".format(kwargs["name"])
 2286     return ret
 2287 
 2288 
 2289 def acl_list(consul_url=None, token=None, **kwargs):
 2290     """
 2291     List the ACL tokens.
 2292 
 2293     :param consul_url: The Consul server URL.
 2294     :return: List of ACLs
 2295 
 2296     CLI Example:
 2297 
 2298     .. code-block:: bash
 2299 
 2300         salt '*' consul.acl_list
 2301 
 2302     """
 2303     ret = {}
 2304     data = {}
 2305     if not consul_url:
 2306         consul_url = _get_config()
 2307         if not consul_url:
 2308             log.error("No Consul URL found.")
 2309             ret["message"] = "No Consul URL found."
 2310             ret["res"] = False
 2311             return ret
 2312 
 2313     if "id" not in kwargs:
 2314         ret["message"] = 'Required parameter "id" is missing.'
 2315         ret["res"] = False
 2316         return ret
 2317 
 2318     function = "acl/list"
 2319     ret = _query(
 2320         consul_url=consul_url, token=token, data=data, method="PUT", function=function
 2321     )
 2322     return ret
 2323 
 2324 
 2325 def event_fire(consul_url=None, token=None, name=None, **kwargs):
 2326     """
 2327     List the ACL tokens.
 2328 
 2329     :param consul_url: The Consul server URL.
 2330     :param name: The name of the event to fire.
 2331     :param dc: By default, the datacenter of the agent is queried;
 2332                however, the dc can be provided using the "dc" parameter.
 2333     :param node: Filter by node name.
 2334     :param service: Filter by service name.
 2335     :param tag: Filter by tag name.
 2336     :return: List of ACLs
 2337 
 2338     CLI Example:
 2339 
 2340     .. code-block:: bash
 2341 
 2342         salt '*' consul.event_fire name='deploy'
 2343 
 2344     """
 2345     ret = {}
 2346     query_params = {}
 2347     if not consul_url:
 2348         consul_url = _get_config()
 2349         if not consul_url:
 2350             log.error("No Consul URL found.")
 2351             ret["message"] = "No Consul URL found."
 2352             ret["res"] = False
 2353             return ret
 2354 
 2355     if not name:
 2356         raise SaltInvocationError('Required argument "name" is missing.')
 2357 
 2358     if "dc" in kwargs:
 2359         query_params = kwargs["dc"]
 2360 
 2361     if "node" in kwargs:
 2362         query_params = kwargs["node"]
 2363 
 2364     if "service" in kwargs:
 2365         query_params = kwargs["service"]
 2366 
 2367     if "tag" in kwargs:
 2368         query_params = kwargs["tag"]
 2369 
 2370     function = "event/fire/{}".format(name)
 2371     res = _query(
 2372         consul_url=consul_url,
 2373         token=token,
 2374         query_params=query_params,
 2375         method="PUT",
 2376         function=function,
 2377     )
 2378 
 2379     if res["res"]:
 2380         ret["res"] = True
 2381         ret["message"] = "Event {} fired.".format(name)
 2382         ret["data"] = ret["data"]
 2383     else:
 2384         ret["res"] = False
 2385         ret["message"] = "Cloning ACL" "item {} failed.".format(kwargs["name"])
 2386     return ret
 2387 
 2388 
 2389 def event_list(consul_url=None, token=None, **kwargs):
 2390     """
 2391     List the recent events.
 2392 
 2393     :param consul_url: The Consul server URL.
 2394     :param name: The name of the event to fire.
 2395     :return: List of ACLs
 2396 
 2397     CLI Example:
 2398 
 2399     .. code-block:: bash
 2400 
 2401         salt '*' consul.event_list
 2402 
 2403     """
 2404     ret = {}
 2405     query_params = {}
 2406     if not consul_url:
 2407         consul_url = _get_config()
 2408         if not consul_url:
 2409             log.error("No Consul URL found.")
 2410             ret["message"] = "No Consul URL found."
 2411             ret["res"] = False
 2412             return ret
 2413 
 2414     if "name" in kwargs:
 2415         query_params = kwargs["name"]
 2416     else:
 2417         raise SaltInvocationError('Required argument "name" is missing.')
 2418 
 2419     function = "event/list/"
 2420     ret = _query(
 2421         consul_url=consul_url, token=token, query_params=query_params, function=function
 2422     )
 2423     return ret