
    VhB                         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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  G d de      Zy)    )absolute_importdivisionprint_functiona  
author: Ansible Networking Team (@ansible-network)
name: nxos
short_description: Use NX-OS cliconf to run commands on Cisco NX-OS platform
description:
- This nxos plugin provides low level abstraction apis for sending and receiving CLI
  commands from Cisco NX-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_nxos_config_commands
N)AnsibleConnectionFailure)to_bytesto_text)Mapping)ConnectionError)NetworkConfigdumps)to_list)CliconfBasec                        e Zd Z fdZd Zd Zd ZddZ	 	 	 	 	 	 ddZddZ		 	 	 	 	 	 ddZ
	 	 	 	 	 	 dd	Zdd
Zd Zd Z fdZddZd Zd Z xZS )Cliconfc                 H    i | _         i | _        t        t        |   |i | y N)_module_context_device_infosuperr   __init__)selfargskwargs	__class__s      c/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/cisco/nxos/plugins/cliconf/nxos.pyr   zCliconf.__init__?   s'    !gt%t6v6    c                 X    | j                   j                  |      r| j                   |   S y r   )r   get)r   
module_keys     r   read_module_contextzCliconf.read_module_contextD   s*    ##J/''
33r   c                 "    || j                   |<   y r   )r   )r   r   module_contexts      r   save_module_contextzCliconf.save_module_contextJ   s    +9Z(r   c                    | j                   s[i }d|d<   | j                  d      }| j                  d      }t        j                  d|t        j                        }|r|j                  d      |d<   n;t        j                  d|t        j                        }|r|j                  d      |d<   d|vr;t        j                  d	|t        j                        }|r|j                  d      |d<   t        j                  d
|t        j                        }|r"|j                  d      j                         |d<   t        j                  d|t        j                        }|r|j                  d      |d<   t        j                  d|t        j                        }|r|j                  d      |d<   n;t        j                  d|t        j                        }	|	r|	j                  d      |d<   d|vr;t        j                  d|t        j                        }|r|j                  d      |d<   t        j                  d|t        j                        }
|
r|
j                  d      |d<   || _         | j                   S )Nnxos
network_oszshow versionzshow inventoryz\s+system:\s+version\s*(\S+)   network_os_versionz\s+kickstart:\s+version\s*(\S+)z\s+NXOS:\s+version\s*(\S+)zHardware\n\s+cisco(.+)$network_os_modelz\s+Device name:\s*(\S+)network_os_hostnamez \s+system image file is:\s*(\S+)network_os_imagez#\s+kickstart image file is:\s*(\S+)z\s+NXOS image file is:\s*(\S+)z)NAME: "Chassis",\s*DESCR:.*\nPID:\s*(\S+)network_os_platform)r   r   researchMgroupstrip)r   device_inforeplyplatform_replymatch_sys_vermatch_kick_vermatch_chassis_idmatch_host_namematch_isan_file_namematch_kick_file_namematch_os_platforms              r   get_device_infozCliconf.get_device_infoO   s5     K(.K%HH^,E!XX&67NII&EubddSM4A4G4G4J01!#+MuVXVZVZ![!8F8L8LQ8OK 45#;6 "		*GPRPTPT U 8E8K8KA8NK 45!yy)CUBDDQ2B2H2H2K2Q2Q2S./ ii(BE244PO5D5J5J15M12#%99-PRWY[Y]Y]#^ #2F2L2LQ2O./')yy:DD($
 (6J6P6PQR6SK 23!4')yy1RTY[][_[_'`$'6J6P6PQR6SK 23 "		<!
 !5F5L5LQ5O12 +D   r   c                 N    |st        d      d| | d}| j                  |      S )Nz)'file_name' value is required for restorezconfigure replace z best-effort)
ValueErrorsend_command)r   filenamepathcmds       r   restorezCliconf.restore   s4    HII"4&
,?  %%r   c           	         i }| j                         }| j                         }	||d   rt        d      ||	d   vr#t        d|ddj                  |	d               ||	d   vr#t        d|ddj                  |	d               t	        d	
      }
|
j                  |       |r.|dk7  r)|dk7  r$t	        d	||      }|
j                  ||||      }n|
j                  }|rt        |d      nd|d<   |S )Nsupports_generate_diffz4candidate configuration is required to generate diff
diff_matchz'match' value z in invalid, valid values are z, diff_replacez'replace' value    )indentnoneconfig)rI   contentsignore_lines)rA   matchreplacecommands config_diff)	get_device_operationsget_option_valuesr>   joinr   load
differenceitemsr   )r   	candidaterunningrF   diff_ignore_linesrA   rG   diffdevice_operationsoption_valuescandidate_objrunning_objconfigdiffobjss                r   get_diffzCliconf.get_diff   s,     668..0!23K!LSTT]<88tyy|)DEG 
 }^<<=+H!IK  &Q/9%zV+0H'q7QbcK*55 $	 6 N +00NCQeNJ?WY]r   c           	      j   | j                         }||d   vr#t        d|ddj                  |d               ddd}||vrt        d|z        d	j                  ||         }|r|d
k7  r|d|z  z  }|r|dj                  t	        |            z  }|j                         }| j                  |      S )Nformatz'format' value  is invalid. Valid values are ,zrunning-configzstartup-config)rZ   startupz/fetching configuration from %s is not supportedz	show {0} textz| %s  )rT   r>   rU   rd   r   r1   r?   )r   sourceflagsrd   options_valueslookuprB   s          r   
get_configzCliconf.get_config   s    //111388N8$<=? 
 .:JKNQWWXX  0f&7V##C388GEN++Ciik  %%r   c                    |r| j                   j                  dd       i }| j                         }| j                  |||||       g }	g }
|rW|D cg c]   }t	        j
                  t        |            " }}| j                   j                  d      }|j                  |       |rd| }	 |r| j                  d       t        |      D ]S  }t        |t              sd|i}|d   }|dk7  s"|	j                   | j                  di |       |
j                  |       U | j                  d       nt        d	      |
|d
<   |	|d<   ||r|D ]  }j                  |        S S c c}w # |r|D ]  }j                  |        w w xY w)NwarningzFsetting `diff=True` in edit_config() no effect for platform cisco.nxosmessageterminal_stderr_rezconfig replace zconfigure terminalcommandendzcheck mode is not supportedrequestresponse )_connectionqueue_messagerS   check_edit_config_capabilityr-   compiler   _get_terminal_std_reextendr?   r   
isinstancer	   appendr>   remove)r   rY   commitrO   r\   commenterr_responsesresp
operationsresultsrequestserr_recurrent_stderr_relinerB   xs                   r   edit_configzCliconf.edit_config   s    **` + 
 //1
))	
 HUVfRZZ(89VMV $ 0 0 E E$! $$]3 *'3I	0!!"67#I. -D%dG4 )40y/Ce|'8t'8'8'@4'@A ,- !!%( !>??&DO&D & 0A%,,Q/0 E WD & 0A%,,Q/0 s   %E$,?E) ,AE) )Fc                 X    |r| j                  ||      }| j                  ||||||      S )N)rt   promptanswersendonlynewline	check_all)_get_command_with_outputr?   )r   rt   r   r   r   r   outputr   s           r   r   zCliconf.get  sC     33GVDG   ! 
 	
r   c           
      D   |t        d      t               }t        |      D ]  }t        |t              sd|i}|j                  dd       }|r| j                  |d   |      |d<   	  | j                  di |}|Y	 t        |d      j                         }	 t        j                  |      }|j!                  |        |S # t        $ r}|du r t        |d|      }Y d }~jd }~ww xY w# t        $ r t        d|d	t        |      
      w xY w# t         $ r Y pw xY w)Nz'commands' value is requiredrt   r   Terrsurrogate_or_stricterrorszFailed to decode output from z: rq   rx   )r>   listr   r   r	   popr   r?   r   getattrr   r1   UnicodeErrorr
   jsonloadsr   )r   rP   check_rc	responsesrB   r   outes           r   run_commandszCliconf.run_commands-  s>   ;<<F	8$ 	&Cc7+ #&WWXt,F!%!>!>s9~v!VI+'d''.#. !#.CDJJLC**S/C   %9	&: ' , +t#a*+ $ )ILgVYl [  " s6   'C=C+D	C(C##C(+%D	DDc                     ddddddddddddS )NTF)supports_diff_replacesupports_commitsupports_rollbacksupports_defaultssupports_onbox_diffsupports_commit_commentsupports_multiline_delimitersupports_diff_matchsupports_diff_ignore_linesrE   supports_replacerx   r   s    r   rS   zCliconf.get_device_operationsQ  s-    %)$!&!%#(',,1#'*.&* $
 	
r   c                      ddgg dg dg ddS )Nrh   r   )r   strictexactrJ   )r   blockrK   )rh   r   json-pretty)rd   rF   rG   r   rx   r   s    r   rT   zCliconf.get_option_values`  s    v&=75	
 	
r   c                     t         t        |          }|dxx   ddgz  cc<   | j                         |d<   |j	                  | j                                t        j                  |      S )Nrpcrb   r   r]   )r   r   get_capabilitiesrS   updaterT   r   r   )r   resultr   s     r   r   zCliconf.get_capabilitiesh  s]    w68u*n55&*&@&@&B"#d,,./zz&!!r   c                    t        j                  d      t        j                  d      t        j                  d      t        j                  d      t        j                  d      t        j                  d      t        j                  d      t        j                  d      t        j                  d	      t        j                  d
      g
}| j                  j                  d      }|j	                  |       t        j                  d      t        j                  d      t        j                  d      g}| j                  j                  d      }|j	                  |       d}d}	 |s|dk  r|dz  }| j                  |d      }	|d   j                  t        |	            r| j                  dd      }	|d   j                  t        |	            r| j                  dd      }	|d   j                  t        |	            r| j                  |d      }	d|	v rd}|s|dk  r||D ]  }
|j                  |
        |D ]  }
|j                  |
        S # |D ]  }
|j                  |
        |D ]  }
|j                  |
        w xY w)Ns	   timed outs   (?i)No space.*#s   (?i)Permission denied.*#s   (?i)No such file.*#s/   Compaction is not supported on this platform.*#s   Compact of.*failed.*#s   (?i)Could not resolve hostnames$   (?i)Too many authentication failuress   Access Denieds6   (?i)Copying to\/from this server name is not permittedrs   s   file existing with this names$   sure you want to continue connectings   (?i)Password:.*terminal_stdout_rer'   F   )rt   strip_promptr   yyesrH   zCopy completeT)	r-   r|   ry   r}   r~   r?   r.   r   r   )r   rt   remotepasswordpossible_errors_rer   possible_prompts_recurrent_stdout_reretryfile_pulledr   r   s              r   	pull_filezCliconf.pull_filep  sc   JJ}%JJ*+JJ34JJ./JJJKJJ01JJ9:JJ?@JJ()JJQR
 !,,AABVW  !34 JJ78JJ?@JJ*+
 !,,AABVW  !45	,!eqj
**7*O&q)00&1AB!..s.OF&q)00&1AB!..u5.QF&q)00&1AB!..~TY.ZF"f,"&K "eqj  ) ,!((+,' ,!((+, ) ,!((+,' ,!((+,s   &B<I #I 2Jc                 (   | j                   j                  r| j                   j                         }|#t        d| j                   j                  z        t        j                  d      }|j                  t        |d      j                               r}| j                   j                  dd       | j                   j                  d	       | j                   j                         }|j                  t        |d      j                               r|yyy)
zW
        Make sure we are in the operational cli context
        :return: None
        NzGcli prompt is not identified from the last received response window: %srq   z^.*\((?!maint-mode).*\)#$surrogate_then_replacer   vvvvz%wrong context, sending exit to deviceexit)ry   	connected
get_promptr   _last_recv_windowr-   r|   rN   r   r1   rz   r?   )r   r   config_prompts      r   set_cli_prompt_contextzCliconf.set_cli_prompt_context  s    
 %%""--/C{.+-1-=-=-O-OP 
 JJ'CDM%%gc:R&S&Y&Y&[\  ..v7^_  --f5&&113  %%gc:R&S&Y&Y&[\ &r   c           	         d}| j                         }||d   vr#t        d|ddj                  |d               |dv rot        j                  ||      sY| j                         }|j                  dd      }|j                  d	d      }|j                  d
      rd|v rd|z  }|S |d|}|S |dk(  r-t        j                  ||      r|j                  dd      d   }|S |}|S )Nz.+\|\s*json(?:-pretty)?$r   z'output' value re   rf   )r   r   r)   rQ   r,   zDS-MDSz%s | json nativez | rh   |r'   r   )	rT   r>   rU   r-   r.   r<   r   
startswithrsplit)	r   rt   r   	output_rerl   r2   modelplatformrB   s	            r   r   z Cliconf._get_command_with_output  s   /	//111388N8$<=? 
 ,,RYYy'5R..0KOO$6;E"'<bAH""5)eun(72 
 $+F3
 
	 v"))Iw"?..a(+C 
 C
r   )NrQ   )NNr   NNr   )rZ   Nrh   )NTNFNN)NNFTNF)NTr   )__name__
__module____qualname__r   r    r#   r<   rC   rb   rn   r   r   r   rS   rT   r   r   r   r   __classcell__)r   s   @r   r   r   >   s    7

8!t& .`&0 A0L 
*"H

"6,p4(r   r   )
__future__r   r   r   type__metaclass__DOCUMENTATIONr   r-   ansible.errorsr   ansible.module_utils._textr   r   /ansible.module_utils.common._collections_compatr	   ansible.module_utils.connectionr
   Pansible_collections.ansible.netcommon.plugins.module_utils.network.common.configr   r   Oansible_collections.ansible.netcommon.plugins.module_utils.network.common.utilsr   Gansible_collections.ansible.netcommon.plugins.plugin_utils.cliconf_baser   r   rx   r   r   <module>r      sL   & A @ .  	 3 8 C ; d _Sk Sr   