
    Vh5                         d dl mZmZmZ eZd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mZ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mZ d dlm Z  d dl!m"Z"  G d de       Z#y)    )absolute_importdivisionprint_functiona  
author:
 - Ansible Networking Team (@ansible-network)
name: httpapi
short_description: Use httpapi to run command on network appliances
description:
- This connection plugin provides a connection to remote devices over a HTTP(S)-based
  api.
version_added: 1.0.0
extends_documentation_fragment:
- ansible.netcommon.connection_persistent
options:
  host:
    description:
    - Specifies the remote device FQDN or IP address to establish the HTTP(S) connection
      to.
    default: inventory_hostname
    type: string
    vars:
    - name: inventory_hostname
    - name: ansible_host
  port:
    type: int
    description:
    - Specifies the port on the remote device that listens for connections when establishing
      the HTTP(S) connection.
    - When unspecified, will pick 80 or 443 based on the value of use_ssl.
    ini:
    - section: defaults
      key: remote_port
    env:
    - name: ANSIBLE_REMOTE_PORT
    vars:
    - name: ansible_httpapi_port
  network_os:
    description:
    - Configures the device platform network operating system.  This value is used
      to load the correct httpapi plugin to communicate with the remote device
    type: string
    vars:
    - name: ansible_network_os
  remote_user:
    description:
    - The username used to authenticate to the remote device when the API connection
      is first established.  If the remote_user is not specified, the connection will
      use the username of the logged in user.
    - Can be configured from the CLI via the C(--user) or C(-u) options.
    type: string
    ini:
    - section: defaults
      key: remote_user
    env:
    - name: ANSIBLE_REMOTE_USER
    vars:
    - name: ansible_user
  password:
    description:
    - Configures the user password used to authenticate to the remote device when
      needed for the device API.
    type: string
    vars:
    - name: ansible_password
    - name: ansible_httpapi_pass
    - name: ansible_httpapi_password
  session_key:
    type: dict
    description:
    - Configures the session key to be used to authenticate to the remote device when
      needed for the device API.
    - This should contain a dictionary representing the key name and value for the
      token.
    - When specified, I(password) is ignored.
    vars:
    - name: ansible_httpapi_session_key
  ca_path:
    description:
      - Path to CA cert bundle to use.
    type: path
    version_added: 5.2.0
    vars:
      - name: ansible_httpapi_ca_path
  client_cert:
    description:
      - PEM formatted certificate chain file to be used for SSL client
        authentication. This file can also include the key as well, and if the key
        is included, I(client_key) is not required
    version_added: 5.2.0
    vars:
      - name: ansible_httpapi_client_cert
  client_key:
    description:
      - PEM formatted file that contains the private key to be used for SSL client
        authentication. If I(client_cert) contains both the certificate and key,
        this option is not required.
    version_added: 5.2.0
    vars:
      - name: ansible_httpapi_client_key
  http_agent:
    description: User-Agent to use in the request.
    version_added: 5.2.0
    vars:
      - name: ansible_httpapi_http_agent
  use_ssl:
    type: boolean
    description:
    - Whether to connect using SSL (HTTPS) or not (HTTP).
    default: false
    vars:
    - name: ansible_httpapi_use_ssl
  validate_certs:
    type: boolean
    description:
    - Whether to validate SSL certificates
    default: true
    vars:
    - name: ansible_httpapi_validate_certs
  use_proxy:
    type: boolean
    description:
    - Whether to use https_proxy for requests.
    default: true
    vars:
    - name: ansible_httpapi_use_proxy
  ciphers:
    description:
      - SSL/TLS Ciphers to use for requests
      - 'When a list is provided, all ciphers are joined in order with C(:)'
      - See the L(OpenSSL Cipher List Format,https://www.openssl.org/docs/manmaster/man1/openssl-ciphers.html#CIPHER-LIST-FORMAT)
        for more details.
      - The available ciphers is dependent on the Python and OpenSSL/LibreSSL versions.
      - This option will have no effect on ansible-core<2.14 but a warning will be emitted.
    version_added: 5.0.0
    type: list
    elements: string
    vars:
    - name: ansible_httpapi_ciphers
  become:
    type: boolean
    description:
    - The become option will instruct the CLI session to attempt privilege escalation
      on platforms that support it.  Normally this means transitioning from user mode
      to C(enable) mode in the CLI session. If become is set to True and the remote
      device does not support privilege escalation or the privilege has already been
      elevated, then this option is silently ignored.
    - Can be configured from the CLI via the C(--become) or C(-b) options.
    default: false
    ini:
    - section: privilege_escalation
      key: become
    env:
    - name: ANSIBLE_BECOME
    vars:
    - name: ansible_become
  become_method:
    description:
    - This option allows the become method to be specified in for handling privilege
      escalation.  Typically the become_method value is set to C(enable) but could
      be defined as other values.
    default: sudo
    type: string
    ini:
    - section: privilege_escalation
      key: become_method
    env:
    - name: ANSIBLE_BECOME_METHOD
    vars:
    - name: ansible_become_method
  platform_type:
    description:
    - Set type of platform.
    type: string
    env:
    - name: ANSIBLE_PLATFORM_TYPE
    vars:
    - name: ansible_platform_type
)BytesIO)AnsibleConnectionFailure)to_bytes)PY3)cPickle)	HTTPErrorURLError)open_url)PlayContext)ensure_connect)httpapi_loader)__version__)NetworkConnectionBase)Versionc                   p     e Zd ZdZdZdZ fdZddZed        Z	d Z
d Z fd	Zedd
       Zd Z xZS )
ConnectionzNetwork API connectionzansible.netcommon.httpapiTc                     t        t        | 
  ||g|i | d | _        | j                  r| j                  | j                         y y N)superr   __init___auth_network_osload_platform_plugins)selfplay_context	new_stdinargskwargs	__class__s        p/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/ansible/netcommon/plugins/connection/httpapi.pyr   zConnection.__init__   sG    j$(yR4R6R
&&t'7'78     c           	         |xs | j                  d      }|rt        j                  ||       | _        | j                  rmd| j                  j                  | j                  d| _        | j                  dd| j                  j                  d| j                  j                  d|       nt        d|z        t        d	      | j                  d
d|z         y )Nplatform_typehttpapi)typenameobjvvvvzloaded API plugin z from path z for platform type z.unable to load API plugin for platform type %szqUnable to automatically determine host platform type. Please manually configure platform_type value for this hostlogzplatform_type is set to %s)	
get_optionr   getr'   
_load_name_sub_pluginqueue_message_original_pathr   )r   r&   s     r#   r   z Connection.load_platform_plugins   s    %I)I)--mTBDL||% LL33<<$ 
 "" //33%	 /D}T 
 +G  	5">"NOr$   c                     | j                  d      rdnd}| j                  d      }| j                  d      xs	 |dk(  rdnd}|d|d	|S )
Nuse_sslhttpshttphostporti  P   z://:)r-   )r   protocolr7   r8   s       r#   _urlzConnection._url   sK    "ooi87fv&v&N(g2E32't44r$   c                    t        |      }t        rt        j                  |d      }nt        j                  |      }t	               }|j                  |       | j                  dd       | j                  j                  |j                  z  rK| j                  |       |j                  du r| j                  dd       || _        y| j                  dd       || _        y)	z7Updates the play context information for the connectionbytes)encodingr+   z$updating play_context for connectionTzauthorizing connectionzdeauthorizing connectionN)
r   r	   r
   loadsr   deserializer1   _play_contextbecome
set_become)r   pc_datar   s      r#   update_play_contextzConnection.update_play_context  s    7#mmGg>GmmG,G"}  )6#IJ$$|':'::OOL)""d*""6+CD * ""6+EF)r$   c                    | j                   s| j                  dd| j                  j                  d| j                         | j
                  j                  | j                         d| _        | j                  d      r| j                  d      | _	        y | j
                  j                  | j                  d      | j                  d             y y )Nvvvz#ESTABLISH HTTP(S) CONNECTFOR USER: z TO Tsession_keyremote_userpassword)	connectedr1   rB   rJ   r<   r'   rD   
_connectedr-   r   login)r   s    r#   _connectzConnection._connect  s    ~~%%11499>
 LL##D$6$67"DO}-!__];
""4??=#A4??S]C^_ r$   c                     | j                   r"| j                  dd       | j                          t        t        |           y)z8
        Close the active session to the device
        r+   z$closing http(s) connection to deviceN)rM   r1   logoutr   r   close)r   r"   s    r#   rR   zConnection.close%  s3    
 ??v'MNKKMj$%'r$   c                 N   t        i | j                  d      | j                  d      | j                  d      | j                  d      | j                  d      | j                  d      | j                  d            }|j                  |       | j                  d	      }|r3t        t              t        d
      k\  r||d	<   n| j                  dd       | j                  r<t        |j                  di             }|j                  | j                         ||d<   n-d|d<   | j                  d      |d<   | j                  d      |d<   	 | j                  |z   }| j                  d|d|d|d       t        |fd|i|}	t%               }|	j'                         }| j                  d|z         |j)                  |       | j+                  |	|      xs | j                  | _        |j-                  d       |	|fS # t        $ rb}
| j                  |
      }|du r<| | j                  ||fi |cY d}
~
S |r | j                  ||fd|dz
  i|cY d}
~
S  |du r |}	Y d}
~
d}
~
wt        $ r7}
t        dj!                  | j                  |z   |
j"                              d}
~
ww xY w) z:
        Sends the command to the device over api
        	use_proxypersistent_command_timeoutvalidate_certs
http_agentclient_cert
client_keyca_path)headersrT   timeoutrV   rW   rX   rY   rZ   ciphersz2.14.0warningzD'ansible_httpapi_ciphers' option is unavailable on ansible-core<2.14r[   Tforce_basic_authrJ   url_usernamerK   url_passwordz
send url 'z' with data 'z' and kwargs ''dataNretries   FzCould not connect to {0}: {1}zreceived response: '%s'r   )dictr-   updater   ANSIBLE_CORE_VERSIONr1   r   r.   r<   _log_messagesr   r   handle_httperrorsendr   r   formatreasonr   readwriteupdate_authseek)r   pathrc   rd   r!   
url_kwargsr]   r[   urlresponseexc
is_handledresponse_buffer	resp_datas                 r#   rk   zConnection.send0  s   
 ook2OO$@A??+;<|46|4OOI.	

 	&!//),+,0AA(/
9% ""Z
 ::6::i45GNN4::&$+Jy!-1J)*)-)GJ~&)-)DJ~&	))d"CBEtZX  =$=*=H$ ")MMO	4y@Ai( %%h@NDJJ
Q((7  	"..s3JT!?$499T4:6::$499T4O1OOOU"!H 	*/66tyy47GT 	s<   
8G; ;	J$*I!.J$4I!J$I!!J$-2JJ$c                 2    t        | j                  |       y)a  This method enables wait_for_connection to work.

        The sole purpose of this method is to raise an exception if the API's URL
        cannot be reached. As such, it does not do anything except attempt to
        request the root URL with no error handling.
        )r\   N)r   r<   )r   connect_timeouts     r#   transport_testzConnection.transport_testz  s     	O4r$   r   )__name__
__module____qualname____doc__	transporthas_pipeliningr   r   propertyr<   rF   rO   rR   r   rk   r|   __classcell__)r"   s   @r#   r   r      s\     +IN9P@ 5 5*(`	( G) G)R5r$   r   N)$
__future__r   r   r   r(   __metaclass__DOCUMENTATIONior   ansible.errorsr   ansible.module_utils._textr   ansible.module_utils.sixr	   ansible.module_utils.six.movesr
   +ansible.module_utils.six.moves.urllib.errorr   r   ansible.module_utils.urlsr   ansible.playbook.play_contextr   ansible.plugins.connectionr   ansible.plugins.loaderr   ansible.releaser   rh   Jansible_collections.ansible.netcommon.plugins.plugin_utils.connection_baser   Bansible_collections.ansible.netcommon.plugins.plugin_utils.versionr   r    r$   r#   <module>r      s[   
 A @ ob  3 / ( 2 K . 5 5 1 ? Wt5& t5r$   