
    Vhx                         d dl mZmZmZ eZdZdZdZd dl	m
Z
 d dlmZmZ d dlmZ d dlmZ 	 d d	lZd
Z G d de      Zy	# e$ rZdZY d	Z[d	Z[ww xY w)    )absolute_importdivisionprint_functionah	  
author: Unknown (!UNKNOWN)
name: consul_kv
short_description: Fetch metadata from a Consul key value store
description:
  - Lookup metadata for a playbook from the key value store in a Consul cluster. Values can be easily set in the kv store
    with simple rest commands.
  - C(curl -X PUT -d 'some-value' http://localhost:8500/v1/kv/ansible/somedata).
requirements:
  - 'python-consul python library U(https://python-consul.readthedocs.io/en/latest/#installation)'
options:
  _raw:
    description: List of key(s) to retrieve.
    type: list
    elements: string
  recurse:
    type: boolean
    description: If true, will retrieve all the values that have the given key as prefix.
    default: false
  index:
    description:
      - If the key has a value with the specified index then this is returned allowing access to historical values.
    type: int
  datacenter:
    description:
      - Retrieve the key from a consul datacenter other than the default for the consul host.
    type: str
  token:
    description: The acl token to allow access to restricted values.
    type: str
  host:
    default: localhost
    type: str
    description:
      - The target to connect to, must be a resolvable address.
      - Will be determined from E(ANSIBLE_CONSUL_URL) if that is set.
    ini:
      - section: lookup_consul
        key: host
  port:
    description:
      - The port of the target host to connect to.
      - If you use E(ANSIBLE_CONSUL_URL) this value will be used from there.
    type: int
    default: 8500
  scheme:
    default: http
    type: str
    description:
      - Whether to use http or https.
      - If you use E(ANSIBLE_CONSUL_URL) this value will be used from there.
  validate_certs:
    default: true
    description: Whether to verify the TLS connection or not.
    type: bool
    env:
      - name: ANSIBLE_CONSUL_VALIDATE_CERTS
    ini:
      - section: lookup_consul
        key: validate_certs
  client_cert:
    description: The client cert to verify the TLS connection.
    type: str
    env:
      - name: ANSIBLE_CONSUL_CLIENT_CERT
    ini:
      - section: lookup_consul
        key: client_cert
  url:
    description:
      - The target to connect to.
      - 'Should look like this: V(https://my.consul.server:8500).'
    type: str
    version_added: 1.0.0
    env:
      - name: ANSIBLE_CONSUL_URL
    ini:
      - section: lookup_consul
        key: url
a2  
- ansible.builtin.debug:
    msg: 'key contains {{item}}'
  with_community.general.consul_kv:
    - 'key/to/retrieve'

- name: Parameters can be provided after the key be more specific about what to retrieve
  ansible.builtin.debug:
    msg: 'key contains {{item}}'
  with_community.general.consul_kv:
    - 'key/to recurse=true token=E6C060A9-26FB-407A-B83E-12DDAFCB4D98'

- name: retrieving a KV from a remote cluster on non default port
  ansible.builtin.debug:
    msg: "{{ lookup('community.general.consul_kv', 'my/key', host='10.10.10.10', port=2000) }}"
zD
_raw:
  description:
    - Value(s) stored in consul.
  type: dict
)urlparse)AnsibleErrorAnsibleAssertionError)
LookupBase)to_textNTFc                       e Zd ZddZd Zy)LookupModuleNc           	      z   t         st        d      | j                  |       | j                  d      }| j                  d      }| j                  d      }| j                  d      }|Gt	        |      }|j
                  r|j
                  }|j                  }|j                  |j                  }| j                  d      }	| j                  d      }
g }	 |D ]  }| j                  |      }t        j                  ||||	|
	      }|j                  j                  |d
   |d   |d   |d   |d         }|d   sct        |d   t              r(|d   D ]  }|j                  t!        |d                ! |j                  t!        |d   d                 	 |S # t"        $ r}t        d d|       d }~ww xY w)Nzppython-consul is required for consul_kv lookup. see http://python-consul.readthedocs.org/en/latest/#installation)directschemehostporturlvalidate_certsclient_cert)r   r   r   verifycertkeytokenindexrecurse
datacenter)r   r   r   dc   ValuezError locating 'z' in kv store. Error was )
HAS_CONSULr   set_options
get_optionr   r   hostnamer   parse_paramsconsulConsulkvget
isinstancelistappendr
   	Exception)selfterms	variableskwargsr   r   r   r   ur   r   valuestermparams
consul_apiresultsres                     n/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/lookup/consul_kv.pyrunzLookupModule.run   s    CD D 	'*v&v&ooe$?Axx::Dvv!vv)9:oom4	G D**40#]]4Welwx
$--++F5M28/28/4:94E/5l/C	 , E
 1:!'!*d3!( ?A"MM'!G**=>? ggaj.A&BCD( 	  	G"4&(A!EG G	Gs    A$F ;AF 	F:$F55F:c                    |j                  d      }|d   | j                  d      | j                  d      | j                  d      | j                  d      d}	 |dd  D ]?  }|st        |      dkD  s|j                  d	      \  }}||vrt        | d
      |||<   A 	 |S # t        t
        f$ r}t        |      d }~ww xY w)N r   r   r   r   r   )r   r   r   r   r   r   =z$ not a valid consul lookup parameter)splitr!   lenr   
ValueErrorAssertionErrorr   )r,   r2   r3   	paramvalsparamnamevaluer7   s           r8   r#   zLookupModule.parse_params   s    C !9__W-y1__W-//,7
		" ,SZ!^"'++c"2KD%9,3tf<`4abb&+IdO,  N+ 	"q/!	"s$   
B$ %B$ 4-B$ $C3B>>C)N)__name__
__module____qualname__r9   r#        r8   r   r      s    .`rI   r   )
__future__r   r   r   type__metaclass__DOCUMENTATIONEXAMPLESRETURN+ansible.module_utils.six.moves.urllib.parser   ansible.errorsr   r   ansible.plugins.lookupr	   +ansible.module_utils.common.text.convertersr
   r$   r   ImportErrorr7   r   rH   rI   r8   <module>rU      sk    C BOb"
 A > - ?J
H: H	  Js   A   AAA