
    Vh*                         d dl mZmZmZ eZdZd dl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mZ d d
lmZ d Z G d de      Zy)    )absolute_importdivisionprint_functiona  
author: Ansible Networking Team (@ansible-network)
name: junos
short_description: Use junos cliconf to run command on Juniper Junos OS platform
description:
- This junos plugin provides low level abstraction apis for sending and receiving
  CLI commands from Juniper Junos OS network devices.
version_added: 1.0.0
options:
  config_commands:
    description:
    - Specifies a list of commands that can make configuration changes
      to the target device.
    - When `ansible_network_single_user_mode` is enabled, if a command sent
      to the device is present in this list, the existing cache is invalidated.
    version_added: 2.0.0
    type: list
    elements: str
    default: []
    vars:
    - name: ansible_junos_config_commands
Nwraps)chain)AnsibleConnectionFailure)to_text)Mapping)to_list)CliconfBasec                 .     t                fd       }|S )Nc                     | j                   j                         }t        |d      j                         j	                  d      s| j                  d        | g|i |S )Nsurrogate_or_stricterrors#	configure)_connection
get_promptr
   stripendswithsend_command)selfargskwargspromptfuncs       o/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/junipernetworks/junos/plugins/cliconf/junos.pywrappedzconfigure.<locals>.wrapped=   sY    !!,,.v&;<BBDMMcRk*D*4*6**    r   )r   r    s   ` r   r   r   <   s     
4[+ + Nr!   c                        e Zd Z fdZd Zd ZddZe	 	 	 	 dd       Z	 	 	 	 	 	 ddZ	e	 	 	 	 dd       Z
ed        Zed	        Zedd
       Zedd       Zedd       ZddZd Zd Z fdZd Zd Z xZS )Cliconfc                 :    i | _         t        t        |   |i | y N)_device_infosuperr#   __init__)r   r   r   	__class__s      r   r(   zCliconf.__init__H   s    gt%t6v6r!   c                     	 t        |j                  |      j                  d      j                         S # t        $ r Y y w xY w)Nsurrogate_then_replacer   )r
   findtextr   AttributeError)r   eletags      r   get_textzCliconf.get_textL   sE    	""/ eg  		s   36 	AAc                    | j                   si }d|d<   | j                  d      }t        |d      j                         }t	        j
                  d|      }|r|j                  d      |d	<   t	        j
                  d
|t        j                        }|r|j                  d      |d<   t	        j
                  d|t        j                        }|r|j                  d      |d<   || _         | j                   S )Njunos
network_oszshow version)commandr   r   zJunos: (\S+)   network_os_versionzModel: (\S+)network_os_modelzHostname: (\S+)network_os_hostname)r&   getr
   r   researchgroupM)r   device_inforeplydatamatchs        r   get_device_infozCliconf.get_device_infoU   s      K(/K%HH^H4E5)>?EEGDIIot4E49KKN01IIotRTT:E27++a../II0$=E5:[[^12 +D   r!   c           	      2   |dk7  rt        d|z        | j                         }||d   vr#t        d|ddj                  |d               |dk(  rd}nd	|z  }|d
j                  t        |            z  }|j	                         }| j                  |      S )Nrunningz/fetching configuration from %s is not supportedformatz'format' value  is invalid. Valid values are ,r-   zshow configurationzshow configuration | display %s )
ValueErrorget_option_valuesjoinr   r   r   )r   sourceflagsrF   options_valuescmds         r   
get_configzCliconf.get_configm   s    YAFJ  //111388N8$<=? 
 V&C3f<Csxx''iik  %%r!   c                 j   | j                         }| j                  |||||       i }g }g }|rdj                  |      }t        |      D ]N  }	t	        |	t
              sd|	i}	|	d   }
	 |j                   | j                  d	i |	       |j                  |
       P | j                         }|r+||d<   |r| j                  |       n2| j                          n!| j                  d       | j                          ||d<   ||d<   |S # t        $ r$}d|j                  v r| j                           d }~ww xY w)
Nzload override {0}r5   zerror: commit faileddiff)commenttoprequestresponse )get_device_operationscheck_edit_config_capabilityrF   r   
isinstancer   appendr   r	   messagediscard_changescompare_configurationcommit)r   	candidater`   replacerT   
operationsrespresultsrequestslinerP   excrS   s                r   edit_configzCliconf.edit_config   sN    //1
))	
 +227;II& 
	!DdG,!4(y/C0t008489
 OOC 
	! ))+DLG,$$& e$  ""Y"Z+ , )S[[8((*s   '!D	D2D--D2c                 X    |r| j                  ||      }| j                  ||||||      S )N)r5   r   answersendonlynewline	check_all)_get_command_with_outputr   )r   r5   r   rk   rl   rm   outputrn   s           r   r:   zCliconf.get   sC     33GVDG   ! 
 	
r!   c                     d}|r|dj                  |      z  }|r|dz  }|r|dj                  |      z  }|r|dz  }|dz  }	 | j                  |      }|S # t        $ r | j                           w xY w)a  
        Execute commit command on remote device.
        :param comment: Comment to be associated with commit
        :param confirmed: Boolean flag to indicate if the previous commit should confirmed
        :param at_time: Time at which to activate configuration changes
        :param synchronize: Boolean flag to indicate if commit should synchronize on remote peers
        :return: Command response received from device
        r`   z comment {0}z
 confirmedz {0}z peers-synchronizez	 and-quit)rF   r   r	   r^   )r   rT   	confirmedat_timesynchronizer5   rW   s          r   r`   zCliconf.commit   s      ~,,W55G|#Gv}}W--G++G;	((1H
 	 ( 	  "	s   A A1c                 `    d}t        t        |      dg      D ]  }| j                  |        y )Nz
rollback 0exit)r   r   r   )r   r5   rP   s      r   r^   zCliconf.discard_changes   s2    )F84 	#Cc"	#r!   c                 $    | j                  d      S )Nzcommit check)r   r   s    r   validatezCliconf.validate   s      00r!   c                     d}||dt        |      z  z  }| j                  |      }|j                         }t        |      dk(  rd|d   v s"t        |      dk(  r|d   j	                  d      rd}|S )	Nzshow | comparez rollback %sr6   z[edit]r      z	- version )intr   
splitlineslen
startswith)r   rollback_idr5   rd   rs        r   r_   zCliconf.compare_configuration   sv    ""~K(888G  )OOq6Q;8qt+s1v{qt{?[Dr!   c                     i }| j                  dt        |      z         | j                         |d<   |r| j                          |S | j	                          |S )Nzrollback %srS   )r   r}   r_   r`   r^   )r   r   r`   rd   s       r   rollbackzCliconf.rollback  sW    -#k*::;113VKKM    "r!   c                 p    |st        d      d| | }| j                  |      }| j                          |S )Nz)'file_name' value is required for restorezload override )rJ   r   r`   )r   filenamepathrP   rd   s        r   restorezCliconf.restore  s?    HIItfXJ/  %r!   c                 @    dd i}| j                  |      }|r||d<   |S )Nconfig_diff)r   )r_   )r   r   rS   rW   s       r   get_diffzCliconf.get_diff  s1    t$--+-F"*Dr!   c                     ddddddddddddS )NFT)supports_diff_replacesupports_commitsupports_rollbacksupports_defaultssupports_onbox_diffsupports_commit_commentsupports_multiline_delimitersupports_diff_matchsupports_diff_ignore_linessupports_generate_diffsupports_replacerX   rx   s    r   rY   zCliconf.get_device_operations#  s-    %*#!%!&#''+,1#(*/&+ $
 	
r!   c                     g dg g g ddS )N)r-   setxmljson)rF   
diff_matchdiff_replacerp   rX   rx   s    r   rK   zCliconf.get_option_values2  s    44	
 	
r!   c                     t         t        |          }|dxx   g dz  cc<   | j                         |d<   |j	                  | j                                t        j                  |      S )Nrpc)r`   r^   run_commandsr_   ry   r   device_operations)r'   r#   get_capabilitiesrY   updaterK   r   dumps)r   resultr)   s     r   r   zCliconf.get_capabilities:  s_    w68u 
 	
 '+&@&@&B"#d,,./zz&!!r!   c                 V    | j                   j                  r| j                  d       yy)zT
        Make sure we are in the operational cli mode
        :return: None
        r   )config_contextN)r   	connected_update_cli_prompt_contextrx   s    r   set_cli_prompt_contextzCliconf.set_cli_prompt_contextH  s)    
 %%++3+? &r!   c           	      n   | j                         }||d   vr#t        d|ddj                  |d               |dk(  r|j                  d      sd|z  }|S |dk(  r|j                  d	      sd
|z  }|S |dk(  r9|j                  d      s|j                  d	      r|j	                  dd      d   }|S |}|S )Nrp   z'output' value rG   rH   r   z| display jsonz%s | display jsonr   z| display xmlz%s | display xmlr-   |r6   r   )rK   rJ   rL   r   rsplit)r   r5   rp   rO   rP   s        r   ro   z Cliconf._get_command_with_outputP  s    //111388N8$<=? 
 VG$4$45E$F%/C 
 u_W%5%5o%F$w.C 
 v-.'2B2B?2S..a(+C 
 C
r!   )rE   Nr-   )NTNN)NNFTNF)NFNFr%   )T)Nr|   )__name__
__module____qualname__r(   r1   rC   rQ   r   ri   r:   r`   r^   ry   r_   r   r   r   rY   rK   r   r   ro   __classcell__)r)   s   @r   r#   r#   G   s    7!0&,  2 2n 
*  ! !F # #
 1 1 
 
    

"@r!   r#   )
__future__r   r   r   type__metaclass__DOCUMENTATIONr   r;   	functoolsr   	itertoolsr   ansible.errorsr	   ansible.module_utils._textr
   /ansible.module_utils.common._collections_compatr   Oansible_collections.ansible.netcommon.plugins.module_utils.network.common.utilsr   Gansible_collections.ansible.netcommon.plugins.plugin_utils.cliconf_baser   r   r#   rX   r!   r   <module>r      sL   & A @ .  	   3 . C c _[k [r!   