
    Vh0                         d dl mZmZmZ eZdZdZdZd dl	Z	d dl
Z
d dlmZ d dlmZmZ d Zd	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zedk(  r e        yy)    )absolute_importdivisionprint_functiona  
module: nxos_snapshot
extends_documentation_fragment:
- cisco.nxos.nxos
short_description: Manage snapshots of the running states of selected features.
description:
- Create snapshots of the running states of selected features, add new show commands
  for snapshot creation, delete and compare existing snapshots.
version_added: 1.0.0
author:
- Gabriele Gerbino (@GGabriele)
notes:
- Tested against NXOSv 7.3.(0)D1(1) on VIRL
- Unsupported for Cisco MDS
- C(transport=cli) may cause timeout errors.
- The C(element_key1) and C(element_key2) parameter specify the tags used to distinguish
  among row entries. In most cases, only the element_key1 parameter needs to specified
  to be able to distinguish among row entries.
- C(action=compare) will always store a comparison report on a local file.
options:
  action:
    description:
    - Define what snapshot action the module would perform.
    required: true
    choices:
    - add
    - compare
    - create
    - delete
    - delete_all
    type: str
  snapshot_name:
    description:
    - Snapshot name, to be used when C(action=create) or C(action=delete).
    type: str
  description:
    description:
    - Snapshot description to be used when C(action=create).
    type: str
  snapshot1:
    description:
    - First snapshot to be used when C(action=compare).
    type: str
  snapshot2:
    description:
    - Second snapshot to be used when C(action=compare).
    type: str
  comparison_results_file:
    description:
    - Name of the file where snapshots comparison will be stored when C(action=compare).
    type: str
  compare_option:
    description:
    - Snapshot options to be used when C(action=compare).
    choices:
    - summary
    - ipv4routes
    - ipv6routes
    type: str
  section:
    description:
    - Used to name the show command output, to be used when C(action=add).
    type: str
  show_command:
    description:
    - Specify a new show command, to be used when C(action=add).
    type: str
  row_id:
    description:
    - Specifies the tag of each row entry of the show command's XML output, to be
      used when C(action=add).
    type: str
  element_key1:
    description:
    - Specify the tags used to distinguish among row entries, to be used when C(action=add).
    type: str
  element_key2:
    description:
    - Specify the tags used to distinguish among row entries, to be used when C(action=add).
    type: str
  save_snapshot_locally:
    description:
    - Specify to locally store a new created snapshot, to be used when C(action=create).
    type: bool
    default: false
  path:
    description:
    - Specify the path of the file where new created snapshot or snapshots comparison
      will be stored, to be used when C(action=create) and C(save_snapshot_locally=true)
      or C(action=compare).
    default: ./
    type: str
a>  
# Create a snapshot and store it locally
- cisco.nxos.nxos_snapshot:
    action: create
    snapshot_name: test_snapshot
    description: Done with Ansible
    save_snapshot_locally: true
    path: /home/user/snapshots/

# Delete a snapshot
- cisco.nxos.nxos_snapshot:
    action: delete
    snapshot_name: test_snapshot

# Delete all existing snapshots
- cisco.nxos.nxos_snapshot:
    action: delete_all

# Add a show command for snapshots creation
- cisco.nxos.nxos_snapshot:
    section: myshow
    show_command: show ip interface brief
    row_id: ROW_intf
    element_key1: intf-name

# Compare two snapshots
- cisco.nxos.nxos_snapshot:
    action: compare
    snapshot1: pre_snapshot
    snapshot2: post_snapshot
    comparison_results_file: compare_snapshots.txt
    compare_option: summary
    path: ../snapshot_reports/
z
commands:
    description: commands sent to the device
    returned: verbose mode
    type: list
    sample: ["snapshot create post_snapshot Post-snapshot"]
N)AnsibleModule)load_configrun_commandsc                 &    | ddg} t        ||       S )Ntext)commandoutput)r   )r   modules     l/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/cisco/nxos/plugins/modules/nxos_snapshot.pyexecute_show_commandr      s    "f56G((    c                 B   g }d}t        ||       d   }|rz|j                         }d}|D ]c  }i }	 t        j                  ||t        j                        }|j                         }	|	d   |d<   |	d   |d<   |	d   |d<   |j                  |       e |S # t        $ r Y sw xY w)Nzshow snapshotsr   zS(?P<name>\S+)\s+(?P<date>\w+\s+\w+\s+\d+\s+\d+:\d+:\d+\s+\d+)\s+(?P<description>.*)namedatedescription)r   
splitlinesrematchDOTALL	groupdictappendAttributeError)
r   existingr   body
split_bodysnapshot_regexsnapshottempmatch_snapshotsnapshot_groups
             r   get_existingr$      s    HG03D__&
5 	 # 
	HD!#.(BII!N!/!9!9!;-f5V-f5V&4]&C]#%
	 O " s   AB	BBc                     t               }d}|D ]  }| j                  d   |d   k(  sd} |du r;|j                  dj                  | j                  d   | j                  d                |S )NFsnapshot_namer   Tzsnapshot create {0} {1}r   listparamsr   formatr   existing_snapshotscommandsexistr    s        r   action_creater/      sz    vHE& ==)Xf-==E ~%,,o.m,	
 Or   c           
         t               }d}g }t        ||       d   }|rd}|j                  d      }|D ]  }i }	|j                         D ]  }
	 t	        j
                  ||t        j                        }|j                         d   |	d<   d|
v r|
j                  d      d   |	d	<   [d
|
v r|
j                  d      d   |	d<   wd|
v r|
j                  d      d   |	d<   d|
v s|
j                  d      d   |	d<    |	s|j                  |	        | j                  d   | j                  d	   | j                  d   | j                  d   | j                  d   xs dd}||vr| j                  d   rg|j                  dj                  | j                  d   | j                  d	   | j                  d   | j                  d   | j                  d                |S |j                  dj                  | j                  d   | j                  d	   | j                  d   | j                  d                |S # t        t        f$ r Y w xY w)Nzshow snapshot sectionsr   z.*\[(?P<section>\S+)\].*z

sectionzshow commandzshow command:    show_commandzrow idzrow id: row_idkey1zkey1: element_key1key2zkey2: element_key2-)r1   r3   r4   r6   r8   z*snapshot section add {0} "{1}" {2} {3} {4}z&snapshot section add {0} "{1}" {2} {3})r(   r   splitr   r   r   r   r   r   KeyErrorr   r)   r*   )r   r,   r-   r   sectionsr   section_regexr   r1   r!   linematch_sectionproposeds                r   
action_addrA      sU   vH&GH03D3ZZ'
! 	&GD**, C$&HH]GRYY$OM&3&=&=&?	&JDO "T)+/::6F+G+JD(%%)ZZ
%;A%>DNt^+/::h+?+BD(t^+/::h+?+BD(C  %'	&, ==+n5--)n5n5<H x==(OO<CCMM),MM.1MM(+MM.1MM.1& O OO8??MM),MM.1MM(+MM.1	 OY '1 s   ;H11IIc                     dj                  | j                  d   | j                  d         }| j                  d   r!|dj                  | j                  d         z  }t        ||       d   }|S )Nshow snapshot compare {0} {1}	snapshot1	snapshot2compare_option {0}r   )r*   r)   r   )r   r,   r   r   s       r   action_comparerH     sl    -44k"k"G
 }}%&6==/?!@AA03DKr   c                     t               }d}|D ]  }| j                  d   |d   k(  sd} |r-|j                  dj                  | j                  d                |S )NFr&   r   Tzsnapshot delete {0}r'   r+   s        r   action_deleterJ     sc    vHE& ==)Xf-==E -44V]]?5STUOr   c                 @    t               }|r|j                  d       |S )Nzsnapshot delete all)r(   r   )r   r,   r-   s      r   action_delete_allrL   *  s    vH-.Or   c                 J    t               j                  |       }|r ||i |S y )N)globalsget)r   argskwargsfuncs       r   invokerS   1  s*    9==DT$V$$ r   c                    |j                   d   }|d   dk7  r|dz  }dj                  ||      }	 t        |d      }|j                  |        |j	                          |S # t
        $ r |j                  d       Y |S w xY w)Npath/z{0}{1}wzError while writing on file.msg)r)   r*   openwriteclose	Exception	fail_json)contentfilenamer   rU   filepathreports         r   write_on_filerd   7  s    == DBx3tX.H=h$W O  =;<O=s   -A A>=A>c                  Z   t        t        dg d      t        d      t        d      t        d      t        d      t        g d      t        d      t        d      t        d      t        d      t        dd	      t        dd	      t        d
d      t        dd            } ddg dfddddgfddg dfdddgfg}t        | |d      }|j                  d   }|j                  d   }t        j                  j                  |j                  d         s.|j                  dj                  |j                  d                t        d|      }t        d|z  ||      }dg d}|j                  s|dk(  rg |d<   |j                  d   r|r|j                  d    }|j                  d!   }	|j                  d"   }
d#j                  ||	      }|
r|d$j                  |
      z  }t        ||      d%   }|rt        |||       n|rt        ||       ||d<   d|d&<   |dk(  rp|j                  d   ra|j                  d'   rRd(j                  |j                  d         }t        ||      d%   }|r#t        t        |      |j                  d   |        |j                  d)i | y )*NT)createaddcomparedelete
delete_all)requiredchoicesstr)type)summary
ipv4routes
ipv6routes)rl   F)rn   no_logbool)rn   defaultz./)actionr&   r   rD   rE   rF   comparison_results_filer1   r3   r4   r6   r8   save_snapshot_locallyrU   ru   rh   )rD   rE   rv   rf   r&   r   rg   )r1   r3   r4   r6   ri   )argument_specrequired_ifsupports_check_moderv   rU   z"{0} is not a valid directory name.rY   r$   z	action_%s)changedr-   r-   rD   rE   rF   rC   rG   r   r{   rw   zshow snapshot dump {0} | json )dictr   r)   osrU   isdirr_   r*   rS   
check_moder   rd   r   rm   	exit_json)rx   ry   r   ru   rv   r,   action_resultsresultrD   rE   rF   r   r`   s                r   mainr   F  s   H
 &e$E"E"$KL $% 0% u%uU3uU3">ud+#M, A	

 
8o}=>A	

 
8o./K # F ]]8$F$mm,EF77==v./AHHW]I^_`7K&0&:LMNB/FY!#F:}}V$)@"MM+6	"MM+6	!'/?!@9@@IV!v}}^<<G.w?B!'+BFKFN3%3z"$(y! ("MM&)MM"9:9@@A_`.w?B!#g,o0NPVWFvr   __main__)
__future__r   r   r   rn   __metaclass__DOCUMENTATIONEXAMPLESRETURNr~   r   ansible.module_utils.basicr   Eansible_collections.cisco.nxos.plugins.module_utils.network.nxos.nxosr   r   r   r$   r/   rA   rH   rJ   rL   rS   rd   r   __name__r|   r   r   <module>r      s    A @ 
\|!F
 
 	 4)4$;|
%Sl zF r   