B
    `1                 @   s   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 dlmZ d	d
 Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zedkre  dS )    )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
- 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: no
  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_commands)nxos_argument_specc             C   s   | ddg} t || S )Ntext)commandoutput)r   )r
   module r   k/home/dcms/DCMS/lib/python3.7/site-packages/ansible_collections/cisco/nxos/plugins/modules/nxos_snapshot.pyexecute_show_command   s    r   c       
   	   C   s   g }d}t || d }|r| }d}xp|D ]h}i }yJt||tj}| }	|	d |d< |	d |d< |	d |d< || W q, tk
r   Y q,X q,W |S )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_bodyZsnapshot_regexsnapshottempZmatch_snapshotZsnapshot_groupr   r   r   get_existing   s$    

r   c             C   sX   t  }d}x"|D ]}| jd |d krd}qW |dkrT|d| jd | jd  |S )NFsnapshot_namer   Tzsnapshot create {0} {1}r   )listparamsr   format)r   existing_snapshotscommandsexistr   r   r   r   action_create   s    
r'   c             C   s  t  }d}g }t|| d }|rd}|d}x|D ]}i }	x| D ]}
y$t||tj}| d |	d< W n tt	fk
r   Y nX d|
kr|
dd |	d	< qHd
|
kr|
dd |	d< qHd|
kr|
dd |	d< qHd|
krH|
dd |	d< qHW |	r6|
|	 q6W | jd | jd	 | jd | jd | jd p<dd}||kr| jd r|
d| jd | jd	 | jd | jd | jd  n.|
d| jd | jd	 | jd | jd  |S )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-)r(   r*   r+   r-   r/   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   Zsection_regexr   r(   r   lineZmatch_sectionZproposedr   r   r   
action_add   s\    


r5   c             C   sH   d | jd | jd }| jd r6|d | jd 7 }t|| d }|S )Nzshow snapshot compare {0} {1}	snapshot1	snapshot2compare_optionz {0}r   )r#   r"   r   )r   r$   r
   r   r   r   r   action_compare  s    
r9   c             C   sL   t  }d}x"|D ]}| jd |d krd}qW |rH|d| jd  |S )NFr    r   Tzsnapshot delete {0})r!   r"   r   r#   )r   r$   r%   r&   r   r   r   r   action_delete  s    
r:   c             C   s   t  }|r|d |S )Nzsnapshot delete all)r!   r   )r   r$   r%   r   r   r   action_delete_all*  s    
r;   c             O   s   t  | }|r|||S d S )N)globalsget)r   argskwargsfuncr   r   r   invoke1  s    rA   c             C   sp   |j d }|d dkr|d7 }d||}y t|d}||  |  W n  tk
rj   |jdd Y nX |S )Npath/z{0}{1}wzError while writing on file.)msg)r"   r#   openwriteclose	Exception	fail_json)contentfilenamer   rB   filepathreportr   r   r   write_on_file7  s    


rP   c              C   sZ  t t ddddddgdt dd	t dd	t dd	t dd	t d
ddgdt dd	t dd	t dd	t dd	t dd	t dd	t dddt dddd} | t dddddgfddddgfddddddgfdddgfg}t| |dd}|jd }|jd }tj|jd s|jd	|jd d  t
d!|}t
d"| ||}dg d#}|jsJ|dkrg |d$< |jd rJ|rJ|jd }|jd }	|jd% }
d&	||	}|
r|d'	|
7 }t||d( }|rJt||| n||rt|| ||d$< d|d)< |dkrJ|jd rJ|jd* rJd+	|jd }t||d( }|rJtt||jd | |jf | d S ),NTcreateaddcomparedeleteZ
delete_all)requiredchoicesstr)typesummaryZ
ipv4routesZ
ipv6routes)rV   boolF)rX   defaultz./)actionr    r   r6   r7   r8   comparison_results_filer(   r*   r+   r-   r/   save_snapshot_locallyrB   r\   r6   r7   r]   r    r   r(   r*   r+   r-   )argument_specrequired_ifZsupports_check_moderB   z"{0} is not a valid directory name.)rF   r   z	action_%s)changedr%   r%   r8   zshow snapshot compare {0} {1}z {0}r   ra   r^   zshow snapshot dump {0} | json)dictupdater   r   r"   osrB   isdirrK   r#   rA   Z
check_moder   rP   r   rW   Z	exit_json)r_   r`   r   r\   r]   r$   Zaction_resultsresultr6   r7   r8   r
   rL   r   r   r   mainF  s    












rg   __main__)
__future__r   r   r   rX   __metaclass__ZDOCUMENTATIONZEXAMPLESZRETURNrd   r   Zansible.module_utils.basicr   ZEansible_collections.cisco.nxos.plugins.module_utils.network.nxos.nxosr   r   r   r   r   r'   r5   r9   r:   r;   rA   rP   rg   __name__r   r   r   r   <module>   s*   `#>b