
    Vh                         d dl mZmZmZ eZdZdZdZd dl	Z	d dl
mZ d dlmZ d dlmZ d d	lmZ d d
lmZ 	 d dlZdZ e       ZdZd Z G d de      Zy# e$ r dZY !w xY w)    )absolute_importdivisionprint_functiona	  
author:
  - Eric Belhomme (@eric-belhomme) <ebelhomme@fr.scc.com>
version_added: '0.2.0'
name: etcd3
short_description: Get key values from etcd3 server
description:
  - Retrieves key values and/or key prefixes from etcd3 server using its native gRPC API.
  - Try to reuse M(community.general.etcd3) options for connection parameters, but add support for some E(ETCDCTL_*) environment
    variables.
  - See U(https://github.com/etcd-io/etcd/tree/master/Documentation/op-guide) for etcd overview.
options:
  _terms:
    description:
      - The list of keys (or key prefixes) to look up on the etcd3 server.
    type: list
    elements: str
    required: true
  prefix:
    description:
      - Look for key or prefix key.
    type: bool
    default: false
  endpoints:
    description:
      - Counterpart of E(ETCDCTL_ENDPOINTS) environment variable. Specify the etcd3 connection with an URL form, for example
        V(https://hostname:2379), or V(<host>:<port>) form.
      - The V(host) part is overwritten by O(host) option, if defined.
      - The V(port) part is overwritten by O(port) option, if defined.
    env:
      - name: ETCDCTL_ENDPOINTS
    default: '127.0.0.1:2379'
    type: str
  host:
    description:
      - Etcd3 listening client host.
      - Takes precedence over O(endpoints).
    type: str
  port:
    description:
      - Etcd3 listening client port.
      - Takes precedence over O(endpoints).
    type: int
  ca_cert:
    description:
      - Etcd3 CA authority.
    env:
      - name: ETCDCTL_CACERT
    type: str
  cert_cert:
    description:
      - Etcd3 client certificate.
    env:
      - name: ETCDCTL_CERT
    type: str
  cert_key:
    description:
      - Etcd3 client private key.
    env:
      - name: ETCDCTL_KEY
    type: str
  timeout:
    description:
      - Client timeout.
    default: 60
    env:
      - name: ETCDCTL_DIAL_TIMEOUT
    type: int
  user:
    description:
      - Authenticated user name.
    env:
      - name: ETCDCTL_USER
    type: str
  password:
    description:
      - Authenticated user password.
    env:
      - name: ETCDCTL_PASSWORD
    type: str

notes:
  - O(host) and O(port) options take precedence over (endpoints) option.
  - The recommended way to connect to etcd3 server is using E(ETCDCTL_ENDPOINT) environment variable and keep O(endpoints),
    O(host), and O(port) unused.
seealso:
  - module: community.general.etcd3
  - plugin: community.general.etcd
    plugin_type: lookup

requirements:
  - "etcd3 >= 0.10"
a  
- name: "a value from a locally running etcd"
  ansible.builtin.debug:
    msg: "{{ lookup('community.general.etcd3', 'foo/bar') }}"

- name: "values from multiple folders on a locally running etcd"
  ansible.builtin.debug:
    msg: "{{ lookup('community.general.etcd3', 'foo', 'bar', 'baz') }}"

- name: "look for a key prefix"
  ansible.builtin.debug:
    msg: "{{ lookup('community.general.etcd3', '/foo/bar', prefix=True) }}"

- name: "connect to etcd3 with a client certificate"
  ansible.builtin.debug:
    msg: "{{ lookup('community.general.etcd3', 'foo/bar', cert_cert='/etc/ssl/etcd/client.pem', cert_key='/etc/ssl/etcd/client.key') }}"
z
_raw:
  description:
    - List of keys and associated values.
  type: list
  elements: dict
  contains:
    key:
      description: The element's key.
      type: str
    value:
      description: The element's value.
      type: str
N)AnsibleLookupError)missing_required_lib)	to_native)
LookupBase)DisplayTF)hostportca_certcert_key	cert_certtimeoutuserpasswordc                     	 t        j                  di | }|j                          |S # t        $ r}t	        d|       d }~ww xY w)Nz Cannot connect to etcd cluster:  )etcd3clientstatus	Exceptionr   )client_paramsetcdexps      j/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/lookup/etcd3.pyetcd3_clientr      sP    K||,m, K  K #CC5!IJJKs   %) 	AA  Ac                       e Zd Zd Zy)LookupModulec                 j   | j                  ||       t        st        j                  t	        d             y i }t        j                  d      j                  | j                  d            }|rJ|j                  d      r|j                  d      |d<   |j                  d      r|j                  d      |d<   t        D ](  }| j                  |      s| j                  |      ||<   * t        |      }d|v rd|d<   t        j                  d	|        t        |      }g }	|D ]  }
| j                  d
      rQ	 |j                  |
      D ];  \  }}|s	|s|	j                  t!        |j"                        t!        |      d       = e	 |j)                  |
      \  }}|r2|r0|	j                  t!        |j"                        t!        |      d        |	S # t$        $ r"}t        j'                  d|        Y d }~d }~ww xY w# t$        $ r#}t        j'                  d|        Y d }~d }~ww xY w)N)var_optionsdirectr   za^(https?://)?(?P<host>(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})|([-_\d\w\.]+))(:(?P<port>\d{1,5}))?/?$	endpointsr   r   r   z
<redacted>zetcd3 connection parameters: prefix)keyvaluez'Caught except during etcd3.get_prefix: z Caught except during etcd3.get: )set_optionsHAS_ETCDdisplayerrorr   recompilematch
get_optiongroupetcd3_cnx_optsdictverboser   
get_prefixappendr   r%   r   warningget)selfterms	variableskwargsr   r-   optcnx_logr   rettermvalmetar   s                 r   runzLookupModule.run   s   Yv>MM.w78 
 

p

%,
- 	 {{6"(-F(;f%{{6"(-F(;f%! 	:Cs#%)__S%9c"	: }% ".GJ7yAB M* 	NDx(U%)__T%: ^	T4JJy/BYWZ^'\]^N $ICt

9TXX+>SV#XY	N 
 ! UOO&McU$STTU ! NOO&Fse$LMMNs=   ;GG2GAH	H!G>>H	H2H--H2N)__name__
__module____qualname__rA   r       r   r   r      s    4rE   r   )
__future__r   r   r   type__metaclass__DOCUMENTATIONEXAMPLESRETURNr+   ansible.errorsr   ansible.module_utils.basicr   +ansible.module_utils.common.text.convertersr   ansible.plugins.lookupr	   ansible.utils.displayr
   r   r(   ImportErrorr)   r0   r   r   r   rE   r   <module>rR      s    A @\|$
 
 - ; A - )H )
6: 67  Hs   A AA