
    Vh &                         d dl mZmZmZ e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mZmZ d dlmZ dZ	 d d
lmZ dZ G d de      Zy# e$ r d	ZY w xY w# e$ r d	ZY "w xY w)    )absolute_importdivisionprint_functiona  
author:
  - Ansible Networking Team (@ansible-network)
name: grpc
short_description: Provides a persistent connection using the gRPC protocol
description:
  - This connection plugin provides a connection to remote devices over gRPC and
    is typically used with devices for sending and receiving RPC calls
    over gRPC framework.
  - Note this connection plugin requires the grpcio python library to be installed on the
    local Ansible controller.
version_added: "3.1.0"
requirements:
  - grpcio
  - protobuf
extends_documentation_fragment:
  - ansible.netcommon.connection_persistent
options:
  host:
    description:
      - Specifies the remote device FQDN or IP address to establish the gRPC
        connection to.
    default: inventory_hostname
    type: string
    vars:
      - name: ansible_host
  port:
    type: int
    description:
      - Specifies the port on the remote device that listens for connections
        when establishing the gRPC connection. If None only the C(host) part will
        be used.
    ini:
      - section: defaults
        key: remote_port
    env:
      - name: ANSIBLE_REMOTE_PORT
    vars:
      - name: ansible_port
  network_os:
    description:
      - Configures the device platform network operating system. This value is
        used to load a device specific grpc 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 gRPC
        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 first establishing the gRPC connection.
    type: string
    vars:
      - name: ansible_password
      - name: ansible_ssh_pass
  private_key_file:
    description:
      - The PEM encoded private key file used to authenticate to the
        remote device when first establishing the grpc connection.
    type: string
    ini:
      - section: grpc_connection
        key: private_key_file
    env:
      - name: ANSIBLE_PRIVATE_KEY_FILE
    vars:
      - name: ansible_private_key_file
  root_certificates_file:
    description:
      - The PEM encoded root certificate file used to create a SSL-enabled
        channel, if the value is None it reads the root certificates from
        a default location chosen by gRPC at runtime.
    type: string
    ini:
      - section: grpc_connection
        key: root_certificates_file
    env:
      - name: ANSIBLE_ROOT_CERTIFICATES_FILE
    vars:
      - name: ansible_root_certificates_file
  certificate_chain_file:
    description:
      - The PEM encoded certificate chain file used to create a SSL-enabled
        channel. If the value is None, no certificate chain is used.
    type: string
    ini:
      - section: grpc_connection
        key: certificate_chain_file
    env:
      - name: ANSIBLE_CERTIFICATE_CHAIN_FILE
    vars:
      - name: ansible_certificate_chain_file
  ssl_target_name_override:
    description:
      - The option overrides SSL target name used for SSL host name checking.
        The name used for SSL host name checking will be the target parameter
        (assuming that the secure channel is an SSL channel). If this parameter is
        specified and the underlying is not an SSL channel, it will just be ignored.
    type: string
    ini:
      - section: grpc_connection
        key: ssl_target_name_override
    env:
      - name: ANSIBLE_GPRC_SSL_TARGET_NAME_OVERRIDE
    vars:
      - name: ansible_grpc_ssl_target_name_override
  grpc_type:
    description:
        - This option indicates the grpc type and it can be used
          in place of network_os. (example cisco.iosxr.iosxr)
    default: False
    ini:
      - section: grpc_connection
        key: type
    env:
      - name: ANSIBLE_GRPC_CONNECTION_TYPE
    vars:
      - name: ansible_grpc_connection_type
)import_module)AnsibleConnectionFailureAnsibleError)NetworkConnectionBase)insecure_channelsecure_channelssl_channel_credentials)implementationsTF)protobufc                   :     e Zd ZdZdZdZ fdZd Z fdZ xZ	S )
ConnectionzGRPC connectionszansible.netcommon.grpcFc                 
   t        t        | 
  ||g|i | | j                  xs | j	                  d      }|rt
        st        d      | j                  s|| _        t        t        g d|j                  d                  } dj                  di |}t        t        |      d      } ||       }	|	r6d||	d| _        | j                  d	d
|z         | j                  d	d|z         y t        d|z        t        d      )N	grpc_typezWprotobuf is required to use the grpc connection type. Please run 'pip install protobuf')corgcnameplugin.zDansible_collections.{corg}.{cname}.plugins.sub_plugins.grpc.{plugin}Grpcgrpc)typenameobjlogzloaded gRPC plugin for type %szgrpc type is set to %sz+unable to load API plugin for network_os %szUnable to automatically determine gRPC implementation type. Please manually configure ansible_network_os value or grpc_type configuration for this host )superr   __init___network_os
get_optionHAS_PROTOBUFr   dictzipsplitformatgetattrr   _sub_pluginqueue_messager   )selfplay_context	new_stdinargskwargsr   crefgrpclibgrpcclsgrpc_obj	__class__s             m/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/ansible/netcommon/plugins/connection/grpc.pyr   zConnection.__init__   s    j$(yR4R6R$$D(D	"m  ###, 79MNODc\cc G mG4f=Gt}H"%#$ 
 ""5*JY*VW""5*BY*NO.AIM  +o     c           	         t         st        d      | j                  d      }| j                  j                  }| j
                  r| j                  dd|z         y| j                  d      }||nd||fz  | _        | j                  d      | _        d	| j                  d
      fd| j                  d      fg| _	        | j                  d      }|rd|fg| _
        nd| _
        i }| j                  d      }| j                  d      }| j                  d      }	 |r(t        |d      5 }|j                         |d<   ddd       |r(t        |d      5 }|j                         |d<   ddd       |r(t        |d      5 }|j                         |d<   ddd       |r.t        di |}
t!        | j                  |
| j                        }n!t#        | j                  | j                        }| j                  dd| j                  d
      d|d|       t%        j&                  |      | _        | j                  dd       d| _        y# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   xY w# t        $ r}	t        d|	z        d}	~	ww xY w)zM
        Create GRPC connection to target host
        :return: None
        zSgrpcio is required to use the gRPC connection type. Please run 'pip install grpcio'hostr   z(gRPC connection to host %s already existNportz%s:%dpersistent_command_timeoutusernameremote_userpasswordssl_target_name_overridezgrpc.ssl_target_name_overrideprivate_key_fileroot_certificates_filecertificate_chain_filerbroot_certificatesprivate_keycertificate_chainz#Failed to read certificate keys: %s)optionsvvvz$ESTABLISH GRPC CONNECTION FOR USER: z	 on PORT z TO vvvvz*grpc connection has completed successfullyTr   )HAS_GRPCr   r!   _play_contextremote_addr	connectedr)   _target_timeout_login_credentials_channel_optionsopenread	Exceptionr   r   r   r
   r   Channel_channel
_connected)r*   r7   r8   r=   certsr>   r?   r@   fecredschannels               r4   _connectzConnection._connect   s}   
 e  v&!!-->>u&PSW&WXv&#|tD$<1G(DE7845#
 $(??3M#N #02JK%D! %)D!??+=>!%1I!J!%1I!J	V%0$7 :112E-.:*D1 4Q+,668E-(4%0$7 :112E-.: +4e4E$T\\5$BWBWXG&t||T=R=RSG}-tT;	

 (//86#OP/: :4 4: : 	V*+PST+TUU	Vs`   I* &I:I* I$I* :II* I
I* II* I'#I* *	J3JJc                     | j                   r,| j                  dd       | j                  j                          t        t
        |           y)zN
        Close the active session to the device
        :return: None
        rG   z&closing gRPC connection to target hostN)rU   r)   rT   closer   r   )r*   r3   s    r4   r]   zConnection.close  s:    
 ??v'OPMM!j$%'r5   )
__name__
__module____qualname____doc__	transporthas_pipeliningr   r[   r]   __classcell__)r3   s   @r4   r   r      s&    (IN"H<|( (r5   r   N)
__future__r   r   r   r   __metaclass__DOCUMENTATION	importlibr   ansible.errorsr   r   ansible.plugins.connectionr	   r   r
   r   r   	grpc.betar   rH   ImportErrorgoogler   r"   r   r   r5   r4   <module>rn      s   
 A @ DL $ A <NN)HL
p(& p(  H  Ls"   A A AAA"!A"